first import
[projectpier.git] / environment / classes / controller / Controller.class.php
blob57632b1f889c79dffe1e94d0742a67f7dc466e2e
1 <?php
3 /**
4 * Base controller class
5 *
6 * This class is inherited by all script controllers. All methods of this class
7 * are reserved - there can't be actions with that names (for instance, there
8 * can't be execute actions in real controllers).
10 * @version 1.0
11 * @http://www.projectpier.org/
12 * @abstract
14 class Controller {
16 /**
17 * This controller name
19 * @var string
21 private $controller_name;
23 /**
24 * Action that was (or need to be) executed
26 * @var string
28 private $action;
30 /**
31 * System controller class. System controller class is class withch methods
32 * are reserved (can't be called). Basic system controllers are Controller and
33 * PageController classes.
35 * @var string
37 private $system_controller_class;
39 /**
40 * Contruct controller and set controller name
42 * @access public
43 * @param void
44 * @return null
46 function __construct() {
48 // Get controller name (tableized classname) and controller class
49 $this->setControllerName( Env::getControllerName( get_class($this) ) );
50 $this->setSystemControllerClass('Controller');
52 } // __construct
54 /**
55 * Execute specific controller action
57 * @access public
58 * @param string $action
59 * @return InvalidControllerActionError if action name is not valid or true
61 function execute($action) {
63 // Prepare action name
64 $action = trim(strtolower($action));
66 // If we have valid action execute and done... Else throw exception
67 if($this->validAction($action)) {
68 $this->setAction($action);
69 $this->$action();
70 return true;
71 } else {
72 throw new InvalidControllerActionError($this->getControllerName(), $action);
73 } // if
75 } // execute
77 /**
78 * Forward to specific contrller / action
80 * @access public
81 * @param string $action
82 * @param string $controller. If this value is NULL current controller will be
83 * used
84 * @return null
86 function forward($action, $controller = null) {
87 return empty($controller) ? $this->execute($action) : Env::executeAction($controller, $action);
88 } // forward
90 /**
91 * Check if specific $action is valid controller action (method exists and it is not reserved)
93 * @access public
94 * @param string $action
95 * @return boolean or Error
97 function validAction($action) {
99 // Get reserved names and check action name...
100 $reserved_names = Controller::getReservedActionNames();
101 if(is_array($reserved_names) && in_array($action, $reserved_names)) return false;
103 // Get methods of this class...
104 $methods = get_class_methods(get_class($this));
106 // If we don't have defined action return false
107 if(!in_array($action, $methods)) return false;
109 // All fine...
110 return true;
112 } // validAction
114 // -------------------------------------------------------
115 // Getters and setters
116 // -------------------------------------------------------
119 * Get controller_name
121 * @access public
122 * @param null
123 * @return string
125 function getControllerName() {
126 return $this->controller_name;
127 } // getControllerName
130 * Set controller_name value
132 * @access public
133 * @param string $value
134 * @return null
136 function setControllerName($value) {
137 $this->controller_name = $value;
138 } // setControllerName
141 * Get action
143 * @access public
144 * @param null
145 * @return string
147 function getAction() {
148 return $this->action;
149 } // getAction
152 * Set action value
154 * @access public
155 * @param string $value
156 * @return null
158 function setAction($value) {
159 $this->action = $value;
160 } // setAction
163 * Get system_controller_class
165 * @access public
166 * @param null
167 * @return string
169 function getSystemControllerClass() {
170 return $this->system_controller_class;
171 } // getSystemControllerClass
174 * Set system_controller_class value
176 * @access public
177 * @param string $value
178 * @return null
180 function setSystemControllerClass($value) {
181 $this->system_controller_class = $value;
182 } // setSystemControllerClass
185 * Return reserved action names (methods of controller class)
187 * @access private
188 * @param void
189 * @return arrays
191 function getReservedActionNames() {
192 static $names;
194 // Get and check controller class
195 $controller_class = $this->getSystemControllerClass();
196 if(!class_exists($controller_class)) throw new Error("Controller class '$controller_class' does not exists");
198 // If we don't have names get them
199 if(is_null($names)) {
200 $names = get_class_methods($controller_class);
201 foreach($names as $k => $v) $names[$k] = strtolower($v);
202 } // if
204 // And return...
205 return $names;
207 } // getReservedActionNames
209 } // Controller