Passed
Push — master ( 4d0f3d...b95030 )
by Jean-Christophe
02:31
created

Controller::getDefaultViewName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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