PluginProbe
CSS & JavaScript Toolbox / 6.0.14
CSS & JavaScript Toolbox v6.0.14
trunk 0.3 0.8 10 10.1 11 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.9.1 12 12.0 12.0.1 12.0.3 12.0.4 12.0.5 12.0.6 12.0.7 6.0 6.0.11 All 60 releases
css-javascript-toolbox / framework / mvc / controller.inc.php

controller.inc.php in CSS & JavaScript Toolbox 6.0.14, at framework/mvc/controller.inc.php

310 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @version controller.inc.php
4 */
5
6 /**
7 * No direct access.
8 */
9 defined('ABSPATH') or die("Access denied");
10
11 /**
12 * CJT controller base class.
13 */
14 abstract class CJTController extends CJTHookableClass {
15
16 /** */
17 const NONCE_ACTION = 'cjtoolbox';
18
19 /**
20 * put your comment there...
21 *
22 * @var mixed
23 */
24 protected $action;
25
26 /**
27 * put your comment there...
28 *
29 * @var mixed
30 */
31 protected $controllerInfo = null;
32
33 /**
34 * put your comment there...
35 *
36 * @var mixed
37 */
38 protected $defaultAction = 'index';
39
40 /**
41 * put your comment there...
42 *
43 * @var mixed
44 */
45 protected $request;
46
47 /**
48 * put your comment there...
49 *
50 * @var mixed
51 */
52 protected $model = null;
53
54 /**
55 * put your comment there...
56 *
57 * @var mixed
58 */
59 protected $oncallback = array('parameters' => array('callback', 'action', 'args'));
60
61 /**
62 * put your comment there...
63 *
64 * @var mixed
65 */
66 protected $ongetactionname = array('parameters' => array('action'));
67
68 /**
69 * put your comment there...
70 *
71 * @var mixed
72 */
73 protected static $ongetclassname = array('parameters' => array('class', 'name', 'type'));
74
75 /**
76 * put your comment there...
77 *
78 * @var mixed
79 */
80 protected $ongetviewname = array('parameters' => array('view'));
81
82 /**
83 * put your comment there...
84 *
85 * @var mixed
86 */
87 protected static $onloadcontroller = array('parameters' => array('file', 'name'));
88
89 /**
90 * put your comment there...
91 *
92 * @var mixed
93 */
94 protected $view = null;
95
96 /**
97 * put your comment there...
98 *
99 * @param mixed $hasView
100 * @param mixed $request
101 * @return CJTController
102 */
103 public function __construct($hasView = null, $request = null) {
104 // Initialize hookable!
105 parent::__construct();
106 // Read request parameters.
107 $this->request = array_merge(((array) $_REQUEST), ((array) $request));
108 // Create default model.
109 if (isset($this->controllerInfo['model'])) {
110 // E_ALL complain!
111 if (!isset($this->controllerInfo['model_file'])) {
112 $this->controllerInfo['model_file'] = null;
113 }
114 $this->model = CJTModel::create($this->controllerInfo['model'], array(), $this->controllerInfo['model_file']);
115 }
116 // Create default view.
117 if ($hasView === null) { // Default value for $hasView = true
118 // Request/passed parameters has priority over controller default view!
119 $view = $this->ongetviewname(isset($this->request['view']) ? $this->request['view'] :
120 (isset($this->controllerInfo['view']) ? $this->controllerInfo['view'] : null)
121 );
122 if ($view) {
123 $this->view = self::getView($view)
124 // Push data into view.
125 ->setModel($this->model)
126 ->setRequest($this->request);
127 }
128 }
129 }
130
131 /**
132 * put your comment there...
133 *
134 */
135 public function _doAction() {
136 // Force use of internal action untless its empty
137 // then look for submitted action or get the default!
138 $action = $this->action ? $this->action :
139 (isset($_GET['action']) ? $_GET['action'] : $this->defaultAction);
140 // filter action name!
141 $action = $this->ongetactionname($action);
142 if ($action) {
143 $actionHandler = "{$action}Action";
144 // filter callback
145 $callback = $this->oncallback(array($this, $actionHandler), $action);
146 // Callback!
147 call_user_func($callback);
148 }
149 }
150
151 /**
152 * put your comment there...
153 *
154 * @deprecated Use CJTController::getInstance() instead!
155 *
156 * @param mixed $name
157 * @param mixed $request
158 */
159 public static function create($name, $hasView = null, $request = null) {
160 // Import controller file.
161 $pathToControllers = CJTOOLBOX_CONTROLLERS_PATH;
162 $controllerFile = "{$pathToControllers}/{$name}.php";
163 require_once self::trigger('CJTController.loadcontroller', $controllerFile, $name);
164 // Get controller class name.
165 $class = self::getClassName($name, 'Controller');
166 // Instantiate controller class.
167 return new $class($hasView, $request);
168 }
169
170 /**
171 * put your comment there...
172 *
173 * @deprecated Use cssJSToolbox::createSecurityToken
174 */
175 public function createSecurityToken() {
176 return wp_create_nonce(self::NONCE_ACTION);
177 }
178
179 /**
180 * put your comment there...
181 *
182 */
183 protected function displayAction() {
184 // Get view layout!
185 $layout = isset($this->request['layout']) ? $this->request['layout'] : 'default';
186 ob_start();
187 $this->view->display($layout);
188 $content = ob_get_clean();
189 return $content;
190 }
191
192 /**
193 * put your comment there...
194 *
195 * @param mixed $name
196 * @param mixed $hasView
197 * @param mixed $request
198 */
199 public static function getInstance($name, $hasView = null, $request = null) {
200 return self::create($name, $hasView, $request);
201 }
202
203 /**
204 * Use CJTModel::create instead.
205 *
206 * @deprecated No longer used.
207 */
208 public static function getModel($name, $params = array(), $file = null) {
209 $model = null;
210 $pathToModels = CJTOOLBOX_MODELS_PATH;
211 if (!$file) {
212 $file = $name;
213 }
214 // Import model file.
215 $modelFile = "{$pathToModels}/{$file}.php";
216 require_once $modelFile;
217 // Create model object.
218 $modelClass = self::getClassName($name, 'Model');
219 if (!class_exists($modelClass)) {
220 throw new Exception("Model class {$modelClass} doesn't exists!!!");
221 }
222 $model = new $modelClass($params);
223 return $model;
224 }
225
226 /**
227 * @deprecated No longer used.
228 */
229 public static function getClassName($name, $type) {
230 $className = '';
231 // Every word start with uppercase character.
232 $sanitizedName = ucfirst(str_replace(array('-', '_'), ' ', "{$name} {$type}"));
233 // Remove spaces.
234 $sanitizedName = str_replace(' ', '', $sanitizedName);
235 // Filter.
236 $className = self::trigger('CJTController.getclassname', "CJT{$sanitizedName}", $name, $type);
237 return $className;
238 }
239
240 /**
241 * put your comment there...
242 *
243 * @param mixed $name
244 */
245 public function getRequestParameter($name) {
246 return $this->request;
247 }
248
249 /**
250 *
251 * Use CJTView:create instrad.
252 *
253 * @deprecated
254 */
255 public static function getView($path) {
256 $view = null;
257 // Import view file.
258 $viewInfo = self::getViewInfo($path);
259 require_once $viewInfo['viewFile'];
260 // Create view object.
261 $name = str_replace(' ', '', ucwords(str_replace('/', ' ',$path)));
262 $viewClass = self::getClassName($name, 'view');
263 $view = new $viewClass($viewInfo);
264 return $view;
265 }
266
267 /**
268 * put your comment there...
269 *
270 */
271 public static function getViewInfo($path) {
272 // Path to views dir.
273 $pathToViews = CJTOOLBOX_VIEWS_PATH;
274 // Get view name.
275 $name = basename($path);
276 // View info struct.
277 $viewInfo = array(
278 'name' => $path,
279 'url' => (CJTOOLBOX_VIEWS_URL . "/{$path}"),
280 'path' => "{$pathToViews}/{$path}",
281 'viewFile' => "{$pathToViews}/{$path}/view.php",
282 );
283 return $viewInfo;
284 }
285
286 /**
287 * put your comment there...
288 *
289 * @param mixed $action
290 */
291 public function setAction($action) {
292 $this->action = $action;
293 return $this;
294 }
295
296 /**
297 * put your comment there...
298 *
299 * @param mixed $name
300 * @param mixed $value
301 */
302 public function setRequestParameter($name, $value) {
303 $this->request[$name] = $value;
304 return $this;
305 }
306
307 } // End class.
308
309 // Hookable!
310 CJTController::define('CJTController', array('hookType' => CJTWordpressEvents::HOOK_FILTER));