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

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