Completed
Push — master ( 0db593...5f0535 )
by Jean-Christophe
01:58
created

Controller::getDefaultViewName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of Ubiquity framework
5
 *
6
 */
7
namespace Ubiquity\controllers;
8
9
use Ubiquity\views\View;
10
use Ubiquity\exceptions\RouterException;
11
12
/**
13
 * Base class for controllers
14
 *
15
 * @author jcheron
16
 * @version 1.0.3
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<br>
33
	 * Automatically invokes the isValid () method to check if access is allowed
34
	 */
35
	public function __construct() {
36
		if (! $this->isValid ())
37
			$this->onInvalidControl ();
38
		$this->view = new View ();
39
	}
40
41
	/**
42
	 * Method called before each action
43
	 * Can be override in derived class
44
	 */
45
	public function initialize() {
46
	}
47
48
	/**
49
	 * Method called after each action
50
	 * Can be override in derived class
51
	 */
52
	public function finalize() {
53
	}
54
55
	/**
56
	 * Loads the view $viewName possibly passing the variables $pdata
57
	 *
58
	 * @param string $viewName
59
	 *        	view name to load
60
	 * @param mixed $pData
61
	 *        	Variable or associative array to pass to the view <br> If a variable is passed, it will have the name <b> $ data </ b> in the view, <br>
62
	 *        	If an associative array is passed, the view retrieves variables from the table's key names
63
	 * @param boolean $asString
64
	 *        	If true, the view is not displayed but returned as a string (usable in a variable)
65
	 * @throws \Exception
66
	 * @return string
67
	 */
68
	public function loadView($viewName, $pData = NULL, $asString = false) {
69
		if (isset ( $pData ))
70
			$this->view->setVars ( $pData );
71
		return $this->view->render ( $viewName, $asString );
72
	}
73
	
74
	/**
75
	 * Loads the default view (controllerName/actionName) possibly passing the variables $pdata
76
	 * @param mixed $pData
77
	 *        	Variable or associative array to pass to the view <br> If a variable is passed, it will have the name <b> $ data </ b> in the view, <br>
78
	 *        	If an associative array is passed, the view retrieves variables from the table's key names
79
	 * @param boolean $asString
80
	 *        	If true, the view is not displayed but returned as a string (usable in a variable)
81
	 * @throws \Exception
82
	 * @return string
83
	 */
84
	public function loadDefaultView($pData=NULL,$asString=false){
85
		return $this->loadView($this->getDefaultViewName(),$pData,$asString);
86
	}
87
	
88
	/**
89
	 * Returns the default view name for this controller/action i.e ControllerName/actionName.html for the action actionName in ControllerName
90
	 * @return string the default view name
91
	 */
92
	public function getDefaultViewName(){
93
		return Startup::getControllerSimpleName()."/".Startup::getAction().".".Startup::getViewNameFileExtension();
94
	}
95
96
	/**
97
	 * Returns True if access to the controller is allowed
98
	 * To be override in sub classes
99
	 *
100
	 * @return boolean
101
	 */
102
	public function isValid() {
103
		return true;
104
	}
105
106
	/**
107
	 * Called if isValid () returns false <br>
108
	 * To be override in sub classes
109
	 */
110
	public function onInvalidControl() {
111
		\header ( 'HTTP/1.1 401 Unauthorized', true, 401 );
112
	}
113
114
	/**
115
	 * Loads the controller $controller and calls its $action method by passing the parameters $params
116
	 *
117
	 * @param string $controller
118
	 *        	The Controller
119
	 * @param string $action
120
	 *        	The action to call
121
	 * @param mixed $params
122
	 *        	Parameters passed to the $action method
123
	 * @param boolean $initialize
124
	 *        	If true, the controller's initialize method is called before $action
125
	 * @param boolean $finalize
126
	 *        	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
		return Startup::runAction ( $u, $initialize, $finalize );
137
	}
138
139
	/**
140
	 * Redirect to a route by its name
141
	 *
142
	 * @param string $routeName
143
	 * @param array $parameters
144
	 * @param boolean $initialize
145
	 * @param boolean $finalize
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
				return $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
	 *
164
	 * @return \Ubiquity\views\View
165
	 */
166
	public function getView() {
167
		return $this->view;
168
	}
169
}
170