PluginProbe
CSS & JavaScript Toolbox / trunk
CSS & JavaScript Toolbox vtrunk
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 trunk, at framework/mvc/view.inc.php

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