Test Failed
Push — master ( c3ea58...eb5801 )
by Jean-Christophe
13:07
created

Controller::renderPhp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 10
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
namespace Ubiquity\controllers;
4
5
use Ubiquity\views\View;
6
use Ubiquity\exceptions\RouterException;
7
use Ubiquity\themes\ThemesManager;
8
9
/**
10
 * Base class for controllers.
11
 * Ubiquity\controllers$Controller
12
 * This class is part of Ubiquity
13
 *
14
 * @author jcheron <[email protected]>
15
 * @version 1.0.4
16
 *
17
 */
18
abstract class Controller {
19
	/**
20
	 * The view
21
	 *
22
	 * @var View
23
	 */
24
	protected $view;
25
26
	/**
27
	 * Default action
28
	 */
29
	abstract public function index();
30
31
	/**
32
	 * Constructor
33
	 * initialize $view variable
34
	 */
35 35
	public function __construct() {
36 35
		$this->view = new View ();
37 35
	}
38
39
	/**
40
	 * Method called before each action
41
	 * Can be override in derived class
42
	 */
43 14
	public function initialize() {
44 14
	}
45
46
	/**
47
	 * Method called after each action
48
	 * Can be override in derived class
49
	 */
50 26
	public function finalize() {
51 26
	}
52
53
	/**
54
	 * Loads the view $viewName possibly passing the variables $pdata
55
	 *
56
	 * @param string $viewName The name of the view to load
57
	 * @param mixed $pData Variable or associative array to pass to the view
58
	 *        If a variable is passed, it will have the name **$data** in the view,
59
	 *        If an associative array is passed, the view retrieves variables from the table's key names
60
	 * @param boolean $asString If true, the view is not displayed but returned as a string (usable in a variable)
61
	 * @throws \Exception
62
	 * @return string null or the view content if **$asString** parameter is true
63
	 */
64 24
	public function loadView($viewName, $pData = NULL, $asString = false) {
65 24
		if (isset ( $pData ))
66 18
			$this->view->setVars ( $pData );
67 24
		return $this->view->render ( $viewName, $asString );
68
	}
69
70
	/**
71
	 * Loads the default view (controllerName/actionName) possibly passing the variables $pdata.
72
	 * (@activeTheme/controllerName/actionName if themes are activated)
73
	 *
74
	 * @param mixed $pData Variable or associative array to pass to the view
75
	 *        If a variable is passed, it will have the name **$data** in the view,
76
	 *        If an associative array is passed, the view retrieves variables from the table's key names
77
	 * @param boolean $asString If true, the view is not displayed but returned as a string (usable in a variable)
78
	 * @throws \Exception
79
	 * @return string null or the view content if **$asString** parameter is true
80
	 */
81
	public function loadDefaultView($pData = NULL, $asString = false) {
82
		return $this->loadView ( $this->getDefaultViewName (), $pData, $asString );
83
	}
84
85
	/**
86
	 * Returns the default view name for this controller/action i.e ControllerName/actionName.html for the action actionName in ControllerName
87
	 * If there is an activeTheme **@activeTheme/ControllerName/actionName.html**
88
	 *
89
	 * @return string the default view name
90
	 */
91
	public function getDefaultViewName() {
92
		$activeTheme = ThemesManager::getActiveTheme ();
93
		if ($activeTheme !== '') {
94
			return '@activeTheme/' . Startup::getControllerSimpleName () . "/" . Startup::getAction () . "." . Startup::getViewNameFileExtension ();
95
		}
96
		return Startup::getControllerSimpleName () . "/" . Startup::getAction () . "." . Startup::getViewNameFileExtension ();
97
	}
98
99
	/**
100
	 * Returns True if access to the controller is allowed
101
	 * To be override in sub classes
102
	 *
103
	 * @param string $action
104
	 * @return boolean
105
	 */
106 37
	public function isValid($action) {
107 37
		return true;
108
	}
109
110
	/**
111
	 * Called if isValid () returns false
112
	 * To be override in sub classes
113
	 */
114 5
	public function onInvalidControl() {
115 5
		if (! headers_sent ())
116 5
			\header ( 'HTTP/1.1 401 Unauthorized', true, 401 );
117 5
	}
118
119
	/**
120
	 * Loads the controller $controller and calls its $action method by passing the parameters $params
121
	 *
122
	 * @param string $controller The Controller
123
	 * @param string $action The action to call
124
	 * @param mixed $params Parameters passed to the **$action** method
125
	 * @param boolean $initialize If true, the controller's initialize method is called before $action
126
	 * @param boolean $finalize If true, the controller's finalize method is called after $action
127
	 * @throws \Exception
128
	 */
129
	public function forward($controller, $action = "index", $params = array(), $initialize = false, $finalize = false) {
130
		$u = array ($controller,$action );
131
		if (\is_array ( $params )) {
132
			$u = \array_merge ( $u, $params );
133
		} else {
134
			$u = \array_merge ( $u, [ $params ] );
135
		}
136
		Startup::runAction ( $u, $initialize, $finalize );
137
	}
138
139
	/**
140
	 * Redirect to a route by its name
141
	 *
142
	 * @param string $routeName The route name
143
	 * @param array $parameters The parameters to pass to the route action
144
	 * @param boolean $initialize Call the **initialize** method if true
145
	 * @param boolean $finalize Call the **finalize** method if true
146
	 * @throws RouterException
147
	 */
148
	public function redirectToRoute($routeName, $parameters = [], $initialize = false, $finalize = false) {
149
		$path = Router::getRouteByName ( $routeName, $parameters );
150
		if ($path !== false) {
151
			$route = Router::getRoute ( $path, false );
152
			if ($route !== false) {
153
				$this->forward ( $route [0], $route [1], \array_slice ( $route, 2 ), $initialize, $finalize );
154
			} else {
155
				throw new RouterException ( "Route {$routeName} not found", 404 );
156
			}
157
		} else {
158
			throw new RouterException ( "Route {$routeName} not found", 404 );
159
		}
160
	}
161
162
	/**
163
	 * Returns the associated view instance
164
	 *
165
	 * @return \Ubiquity\views\View
166
	 */
167 10
	public function getView() {
168 10
		return $this->view;
169
	}
170
}
171