PluginProbe
CSS & JavaScript Toolbox / 12
CSS & JavaScript Toolbox v12
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 12, at framework/mvc/controller.inc.php

353 lines 8.8 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 * @param mixed $overrideControllerPath
102 * @param mixed $overrideContollerPrefix
103 * @return CJTController
104 */
105 public function __construct($hasView = null,
106 $request = null,
107 $overrideControllerPath = '',
108 $overrideContollerPrefix = null) {
109 // Initialize hookable!
110 parent::__construct();
111 // Read request parameters.
112 $this->request = array_merge(((array) $_REQUEST), ((array) $request));
113
114 $overrideControllerPath = $overrideControllerPath ? null : '';
115 // Create default model.
116 if (isset($this->controllerInfo['model'])) {
117 // E_ALL complain!
118 if (!isset($this->controllerInfo['model_file'])) {
119 $this->controllerInfo['model_file'] = null;
120 }
121 $this->model = CJTModel::create($this->controllerInfo['model'],
122 $this->request,
123 $this->controllerInfo['model_file'],
124 dirname($overrideControllerPath),
125 $overrideContollerPrefix);
126 }
127 // Create default view.
128 if ($hasView === null) { // Default value for $hasView = true
129 // Request/passed parameters has priority over controller default view!
130 $view = $this->ongetviewname(isset($this->request['view']) ? esc_html($this->request['view']) :
131 (isset($this->controllerInfo['view']) ? $this->controllerInfo['view'] : null)
132 );
133 if ($view) {
134 $this->view = self::getView($view,
135 null,
136 dirname($overrideControllerPath),
137 $overrideContollerPrefix)
138 // Push data into view.
139 ->setModel($this->model)
140 ->setRequest($this->request);
141 }
142 }
143 }
144
145 /**
146 * put your comment there...
147 *
148 */
149 public function _doAction() {
150 // Force use of internal action untless its empty
151 // then look for submitted action or get the default!
152 $action = $this->action ? $this->action :
153 (isset($_GET['action']) ? esc_html($_GET['action']) : $this->defaultAction);
154 // filter action name!
155 $action = $this->ongetactionname($action);
156 if ($action) {
157 $actionHandler = "{$action}Action";
158 // filter callback
159 $callback = $this->oncallback(array($this, $actionHandler), $action);
160 // Callback!
161 call_user_func($callback);
162 }
163 }
164
165 /**
166 * put your comment there...
167 *
168 * @param mixed $name
169 * @param mixed $hasView
170 * @param mixed $request
171 * @param mixed $overrideControllersPath
172 * @param mixed $overrideControllersPrefix
173 */
174 public static function create($name,
175 $hasView = null,
176 $request = null,
177 $overrideControllersPath = null,
178 $overrideControllersPrefix = null) {
179 // Import controller file.
180 $pathToControllers = $overrideControllersPath ? $overrideControllersPath : CJTOOLBOX_CONTROLLERS_PATH;
181 $controllerFile = "{$pathToControllers}/{$name}.php";
182 require_once self::trigger('CJTController.loadcontroller', $controllerFile, $name);
183 // Get controller class name.
184 $class = self::getClassName($name, 'Controller', $overrideControllersPrefix);
185 // Instantiate controller class.
186 return new $class($hasView, $request, $overrideControllersPath, $overrideControllersPrefix);
187 }
188
189 /**
190 * put your comment there...
191 *
192 * @deprecated Use cssJSToolbox::createSecurityToken
193 */
194 public function createSecurityToken() {
195 return wp_create_nonce(self::NONCE_ACTION);
196 }
197
198 /**
199 * put your comment there...
200 *
201 */
202 protected function displayAction() {
203 // Get view layout!
204 $layout = isset($this->request['layout']) ? esc_html($this->request['layout']) : 'default';
205 ob_start();
206 $this->view->display($layout);
207 $content = ob_get_clean();
208 return $content;
209 }
210
211 /**
212 * put your comment there...
213 *
214 * @param mixed $name
215 * @param mixed $hasView
216 * @param mixed $request
217 * @param mixed $overrideControllersPath
218 */
219 public static function getInstance($name,
220 $hasView = null,
221 $request = null,
222 $overrideControllersPath = null,
223 $overrideControllersPrefix = null) {
224 return self::create($name, $hasView, $request, $overrideControllersPath, $overrideControllersPrefix);
225 }
226
227 /**
228 * Use CJTModel::create instead.
229 *
230 * @deprecated No longer used.
231 */
232 public static function getModel($name,
233 $params = array(),
234 $file = null,
235 $overrideModelsPath = null,
236 $overrideModelsPrefix = null) {
237 $model = null;
238 $pathToModels = $overrideModelsPath ? ($overrideModelsPath . DIRECTORY_SEPARATOR . 'models') : CJTOOLBOX_MODELS_PATH;
239 if (!$file) {
240 $file = $name;
241 }
242 // Import model file.
243 $modelFile = "{$pathToModels}/{$file}.php";
244 require_once $modelFile;
245 // Create model object.
246 $modelClass = self::getClassName($name, 'Model', $overrideModelsPrefix);
247 if (!class_exists($modelClass)) {
248 throw new Exception("Model class {$modelClass} doesn't exists!!!");
249 }
250 $model = new $modelClass($params);
251 return $model;
252 }
253
254 /**
255 * @deprecated No longer used.
256 */
257 public static function getClassName($name, $type, $prefix = null) {
258 // Init vars
259 $className = '';
260 // Getting default prefix
261 if (!$prefix) {
262 $prefix = 'CJT';
263 }
264 // Every word start with uppercase character.
265 $sanitizedName = ucfirst(str_replace(array('-', '_'), ' ', "{$name} {$type}"));
266 // Remove spaces.
267 $sanitizedName = str_replace(' ', '', $sanitizedName);
268 // Filter.
269 $className = self::trigger('CJTController.getclassname', "{$prefix}{$sanitizedName}", $name, $type);
270 return $className;
271 }
272
273 /**
274 * put your comment there...
275 *
276 * @param mixed $name
277 */
278 public function getRequestParameter($name) {
279 return isset($this->request[$name]) ? esc_html($this->request[$name]) : null;
280 }
281
282 /**
283 *
284 * Use CJTView:create instrad.
285 *
286 * @deprecated
287 */
288 public static function getView($path,
289 $params = null,
290 $overrideViewsPath = null,
291 $overrideViewsPrefix = null) {
292 $view = null;
293 // Import view file.
294 $viewInfo = self::getViewInfo($path, $overrideViewsPath, $overrideViewsPrefix);
295 require_once $viewInfo['viewFile'];
296 // Create view object.
297 $name = str_replace(' ', '', ucwords(str_replace('/', ' ',$path)));
298 $viewClass = self::getClassName($name, 'view', $overrideViewsPrefix);
299 $view = new $viewClass($viewInfo, $params);
300 return $view;
301 }
302
303 /**
304 * put your comment there...
305 *
306 */
307 public static function getViewInfo($path, $overrideViewsPath = null, $overrideViewsPrefix = null) {
308 // Plugin views Url
309 $viewsUrl = $overrideViewsPath ?
310 WP_PLUGIN_URL . '/' . basename(dirname($overrideViewsPath)) :
311 CJTOOLBOX_VIEWS_URL;
312 // Path to views dir.
313 $pathToViews = $overrideViewsPath ? ($overrideViewsPath . DIRECTORY_SEPARATOR . 'views') : CJTOOLBOX_VIEWS_PATH;
314 // Get view name.
315 $name = basename($path);
316 // View info struct.
317 $viewInfo = array(
318 'name' => $path,
319 'url' => "{$viewsUrl}/{$path}",
320 'path' => "{$pathToViews}/{$path}",
321 'viewsPath' => $pathToViews,
322 'viewsUrl' => $viewsUrl,
323 'viewFile' => "{$pathToViews}/{$path}/view.php",
324 );
325 return $viewInfo;
326 }
327
328 /**
329 * put your comment there...
330 *
331 * @param mixed $action
332 */
333 public function setAction($action) {
334 $this->action = $action;
335 return $this;
336 }
337
338 /**
339 * put your comment there...
340 *
341 * @param mixed $name
342 * @param mixed $value
343 */
344 public function setRequestParameter($name, $value) {
345 $this->request[$name] = $value;
346 return $this;
347 }
348
349 } // End class.
350
351 // Hookable!
352 CJTController::define('CJTController', array('hookType' => CJTWordpressEvents::HOOK_FILTER));
353