PluginProbe
CSS & JavaScript Toolbox / 8.0.3
CSS & JavaScript Toolbox v8.0.3
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 / view.inc.php

view.inc.php in CSS & JavaScript Toolbox 8.0.3, at framework/mvc/view.inc.php

473 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @version view.inc.php
4 */
5
6 /**
7 * No direct access.
8 */
9 defined('ABSPATH') or die("Access denied");
10
11 /**
12 * CJT view base class.
13 */
14 abstract class CJTView extends CJTHookableClass {
15
16 /**
17 * put your comment there...
18 *
19 * @var mixed
20 */
21 protected $model = null;
22
23 /**
24 * put your comment there...
25 *
26 * @var mixed
27 */
28 protected $oncreated = array(
29 'hookType' => CJTWordpressEvents::HOOK_ACTION,
30 'parameters' => array('info'),
31 );
32
33 /**
34 * put your comment there...
35 *
36 * @var mixed
37 */
38 protected static $oncreateview = array(
39 'parameters' => array('view')
40 );
41
42 /**
43 * put your comment there...
44 *
45 * @var mixed
46 */
47 protected $ongetmodel = array(
48 'parameters' => array('model')
49 );
50
51 /**
52 * put your comment there...
53 *
54 * @var mixed
55 */
56 protected $onimporthelper = array(
57 'parameters' => array('file'),
58 );
59
60 /**
61 * put your comment there...
62 *
63 * @var mixed
64 */
65 protected $onimporttemplate = array(
66 'parameters' => array('file'),
67 );
68
69 /**
70 * put your comment there...
71 *
72 * @var mixed
73 */
74 protected $onloadtemplate = array(
75 'parameters' => array('content', 'file'),
76 );
77
78 /**
79 * put your comment there...
80 *
81 * @var mixed
82 */
83 protected $onsetmodel = array(
84 'parameters' => array('model'),
85 );
86
87 /**
88 * put your comment there...
89 *
90 * @var mixed
91 */
92 protected $ontemplateparameters = array(
93 'parameters' => array('params', 'name', 'dir', 'extension'),
94 );
95
96 /**
97 * put your comment there...
98 *
99 * @var mixed
100 */
101 protected static $onusescripts = array(
102 'parameters' => array('scripts'),
103 );
104
105 /**
106 * put your comment there...
107 *
108 * @var mixed
109 */
110 protected static $onusestyles = array(
111 'parameters' => array('styles'),
112 );
113
114 /**
115 * put your comment there...
116 *
117 * @var mixed
118 */
119 protected $params = null;
120
121 /**
122 * put your comment there...
123 *
124 * @var mixed
125 */
126 protected $request;
127
128 /**
129 * put your comment there...
130 *
131 * @var mixed
132 */
133 private $viewInfo = null;
134
135 /**
136 * put your comment there...
137 *
138 * @var mixed
139 */
140 private $views = array();
141
142 /**
143 * put your comment there...
144 *
145 * @param mixed $info
146 * @param mixed $params
147 * @return CJTView
148 */
149 public function __construct($info, $params = null) {
150 // Initialize vars!
151 $this->viewInfo = $info;
152 $this->params = $params ? $params : array();
153 // Initialize events engine!
154 parent::__construct();
155 // Fire created event!
156 $this->oncreated($info);
157 }
158
159 /**
160 * Create view object.
161 *
162 * @deprecated Use CJTView::getInstance().
163 */
164 public static function create($view) {
165 return self::trigger('CJTView.createview', CJTController::getView($view));
166 }
167
168 /**
169 * put your comment there...
170 *
171 * @param mixed $view
172 * @param mixed $params
173 */
174 public static function getInstance($view, $params = null) {
175 return self::trigger('CJTView.createview', CJTController::getView($view, $params));
176 }
177
178 /**
179 * put your comment there...
180 *
181 * @param mixed $name
182 */
183 public function getModel($name = null) {
184 // Instantiate model if required!
185 if ($name) {
186 $this->model = CJTModel::getInstance($name);
187 }
188 return $this->ongetmodel($this->model);
189 }
190
191 /**
192 * put your comment there...
193 *
194 * @param mixed $name
195 */
196 public function getParam($name) {
197 return isset($this->params[$name]) ? $this->params[$name] : null;
198 }
199
200 /**
201 * put your comment there...
202 *
203 * @param mixed $destination
204 */
205 public function getPath($destination) {
206 return self::getViewPath($this->viewInfo['name'], $destination, $this->viewInfo['viewsPath']);
207 }
208
209 /**
210 * put your comment there...
211 *
212 */
213 public function & getRequest() {
214 return $this->request;
215 }
216
217 /**
218 * put your comment there...
219 *
220 * @param mixed $name
221 * @param mixed $value
222 */
223 public function getRequestParameter($name) {
224 return isset($this->request[$name]) ? $this->request[$name] : null;
225 }
226
227 /**
228 * put your comment there...
229 *
230 * @param mixed $name
231 */
232 public function getTemplate($name, $params = array(), $dir = 'tmpl', $extension = null) {
233 // Initialize defaults.
234 if ($extension === null) {
235 $extension = '.html.tmpl';
236 }
237 // filter parameters.
238 $params = $this->ontemplateparameters($params, $name, $dir, $extension);
239 // Push params into the local scope.
240 extract($params);
241 // Templates collected under the view/tmpl directory.
242 $templateFile = $this->getPath("{$dir}/{$name}{$extension}");
243 // Get template content into variable.
244 ob_start();
245 require $this->onimporttemplate($templateFile);
246 $template = $this->onloadtemplate(ob_get_clean(), $name);
247 return $template;
248 }
249
250 /**
251 * put your comment there...
252 *
253 */
254 public function getURI($destination) {
255 return self::getViewURI($this->viewInfo['name'], $destination, $this->viewInfo['viewsUrl']);
256 }
257
258 /**
259 * put your comment there...
260 *
261 * @param mixed $view
262 * @param mixed $destination
263 */
264 public static function getViewPath($view, $destination, $overrideViewPath = null) {
265 # Use passed view path or use CJT constants if not passed
266 if (!$overrideViewPath) {
267 $overrideViewPath = CJTOOLBOX_VIEWS_PATH;
268 }
269 $viewPath = $overrideViewPath . DIRECTORY_SEPARATOR . $view;
270 $destination = $destination ? "/{$destination}" : '';
271 return "{$viewPath}{$destination}";
272 }
273
274 /**
275 * put your comment there...
276 *
277 * @param mixed $view
278 * @param mixed $destination
279 * @param mixed $overrideViewUrl
280 */
281 public static function getViewURI($view, $destination, $overrideViewUrl = null) {
282 if (!$overrideViewUrl) {
283 $overrideViewUrl = CJTOOLBOX_VIEWS_URL;
284 }
285 $viewURI = "{$overrideViewUrl}/{$view}/public";
286 return "{$viewURI}/{$destination}";
287 }
288
289 /**
290 * put your comment there...
291 *
292 * @param mixed $file
293 * @param mixed $destination
294 */
295 public static function getURIFromViewFile($file, $destination) {
296 $path = dirname($file);
297 $viewPath = str_replace((CJTOOLBOX_VIEWS_PATH . '/'), '', $path);
298 return self::getViewURI($viewPath, $destination);
299 }
300
301 /**
302 * put your comment there...
303 *
304 */
305 public function importHelper($name, $helperDirectory = 'helpers') {
306 $helperPath = "{$this->viewInfo['path']}/{$helperDirectory}/{$name}.inc.php";
307 require_once $this->onimporthelper($helperPath);
308 }
309
310 /**
311 *
312 */
313 public static function import($path) {
314 $viewInfo = CJTController::getViewInfo($path);
315 // Import view.
316 require_once $viewInfo['viewFile'];
317 }
318
319 /**
320 * put your comment there...
321 *
322 * @param mixed $model
323 */
324 public function setModel($model) {
325 $this->model = $this->onsetmodel($model);
326 return $this;
327 }
328
329 /**
330 * put your comment there...
331 *
332 * @param mixed $request
333 */
334 public function setRequest(& $request) {
335 $this->request = $request;
336 return $this;
337 }
338
339 /**
340 * put your comment there...
341 *
342 * @param mixed $name
343 * @param mixed $value
344 */
345 public function setRequestParameter($name, $value) {
346 $this->request[$name] = $value;
347 return $this;
348 }
349
350 /**
351 * put your comment there...
352 *
353 */
354 protected function & suppressPrintScriptsHook() {
355 // Access Global.
356 global $wp_actions;
357 // Mark as triggered.
358 $wp_actions['wp_print_scripts'] = true;
359 // Chain.
360 return $this;
361 }
362 /**
363 * put your comment there...
364 *
365 */
366 protected static function useScripts($className, $scripts = null) {
367 wp_enqueue_script('Just Load Default Scripts, this works great!!!!');
368 // Use current class name is $className is not provided!
369 if (!$className) {
370 $className = __CLASS__;
371 }
372 // Accept variable number of args of script list.
373 $allPassedArgs = func_get_args();
374 $scripts = self::trigger("{$className}.usescripts", (is_array($scripts) ? $scripts : array_slice($allPassedArgs, 1)));
375 $stack =& $GLOBALS['wp_scripts']->registered;
376 if (!$scripts) {
377 throw new Exception('CJTView::useScripts method must has at least on script parameter passed!');
378 }
379 // Script name Reg Exp pattern.
380 $nameExp = '/\:?(\{((\w+)-)\})?([\w\-\.]+)(\(.+\))?(\;(\d))?$/';
381 // For every script, Enqueue and localize, only if localization file found/exists.
382 foreach ($scripts as $script) {
383 // Get script name.
384 preg_match($nameExp, $script, $scriptObject);
385 // [[2]Prefix], [4] name. Prefix may be not presented.
386 $name = "{$scriptObject[2]}{$scriptObject[4]}";
387 $location = isset($scriptObject[7]) ? $scriptObject[7] : null;
388 $scriptParameters = isset($scriptObject[5]) ? $scriptObject[5] : '';
389 if (!isset($stack[$name])) {
390 // Any JS lib file should named the same as the parent folder with the extension added.
391 // Except files with _ at the begning
392 $libPath = ':' . ((strpos($scriptObject[4], '_') === 0) ? substr($scriptObject[4], 1) : "{$scriptObject[4]}:{$scriptObject[4]}");
393 // Pass virtual path to getURI and resolvePath to
394 // get JS file URI and localization file path.
395 $jsFile = cssJSToolbox::getURI(preg_replace($nameExp, "{$libPath}.js", $script));
396 $localizationFile = cssJSToolbox::resolvePath(preg_replace($nameExp, "{$libPath}.localization.php", $script));
397 // Enqueue script file.
398 wp_enqueue_script($name, $jsFile, null, null, $location);
399 // Set script parameters.
400 if (preg_match_all('/(\w+)=(\w+)/', $scriptParameters, $params, PREG_SET_ORDER) ) {
401 // Set parameters.
402 foreach ($params as $param) {
403 $stack[$name]->cjt[$param[1]] = $param[2];
404 }
405 // Initialize CJT for the script data object.
406 // This object caryy other informations so that the other
407 // Plugin parts/components can use it to know how script file work.
408 $stack[$name]->cjt = (object) $stack[$name]->cjt;
409 }
410 // If localization file exists localize JS.
411 if (file_exists($localizationFile)) {
412 // Get localization text array.
413 $localization = require $localizationFile;
414 // Object name is the script name with .'s and -'s stripped.
415 // Capitalize first char after each - or . and append I18N postfix.
416 $objectName = str_replace(' ', '', ucwords(str_replace(array('.', '-'), ' ', "{$name}I18N")));
417 // Ask Wordpress to localize the script file.
418 wp_localize_script($name, $objectName, $localization);
419 }
420 }
421 // Enqueue already registered scripts!
422 else {
423 wp_enqueue_script($name, null, null, null, $location);
424 }
425 }
426 }
427
428 /**
429 * put your comment there...
430 *
431 */
432 public static function useStyles($className, $styles = null) {
433 wp_enqueue_style('Just Load Default Styles, this works great!!!!');
434 // Use current class name is $className is not provided!
435 if (!$className) {
436 $className = __CLASS__;
437 }
438 // Accept variable number of args of script list.
439 $allPassedArgs = func_get_args();
440 $styles = self::trigger("{$className}.usestyles", (is_array($styles) ? $styles : array_slice($allPassedArgs, 1)));
441 if (!$styles) {
442 throw new Exception('CJTView::useStyles method must has at least on script parameter passed!');
443 }
444 // Script name Reg Exp pattern.
445 $nameExp = '/\:?(\{((\w+)-)\})?([\w\-\.]+)$/';
446 // For every script, Enqueue and localize, only if localization file found/exists.
447 foreach ($styles as $style) {
448 // Get script name.
449 preg_match($nameExp, $style, $styleObject);
450 // [[2]Prefix], [4] name. Prefix may be not presented.
451 $name = "{$styleObject[2]}{$styleObject[4]}";
452 if (!isset($GLOBALS['wp_styles']->registered[$name])) {
453 // Make all enqueued styles names unique from enqueued scripts.
454 // This is useful when merging styles & scripts is required.
455 $name = "CSS-{$name}";
456 // Any JS lib file should named the same as the parent folder with the extension added.
457 $libPath = ":{$styleObject[4]}";
458 // Get css file URI.
459 $cssFile = cssJSToolbox::getURI(preg_replace($nameExp, "{$libPath}.css", $style));
460 // Register + Enqueue style.
461 wp_enqueue_style($name, $cssFile);
462 }
463 else {
464 // Enqueue already registered styles.
465 wp_enqueue_style($name);
466 }
467 }
468 }
469
470 } // End class.
471
472 // Initialize CJTView Event!
473 CJTView::define('CJTView', array('hookType' => CJTWordpressEvents::HOOK_FILTER));