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

465 lines 11.2 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);
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 // Get template content into variable.
240 ob_start();
241 // Push params into the local scope.
242 extract($params);
243 // Templates collected under the view/tmpl directory.
244 $templateFile = $this->getPath("{$dir}/{$name}{$extension}");
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);
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) {
265 $viewPath = CJTOOLBOX_VIEWS_PATH . "/{$view}";
266 $destination = $destination ? "/{$destination}" : '';
267 return "{$viewPath}{$destination}";
268 }
269
270 /**
271 * put your comment there...
272 *
273 * @param mixed $file
274 */
275 public static function getViewURI($view, $destination) {
276 $viewURI = CJTOOLBOX_VIEWS_URL . "/{$view}/public";
277 return "{$viewURI}/{$destination}";
278 }
279
280 /**
281 * put your comment there...
282 *
283 * @param mixed $file
284 * @param mixed $destination
285 */
286 public static function getURIFromViewFile($file, $destination) {
287 $path = dirname($file);
288 $viewPath = str_replace((CJTOOLBOX_VIEWS_PATH . '/'), '', $path);
289 return self::getViewURI($viewPath, $destination);
290 }
291
292 /**
293 * put your comment there...
294 *
295 */
296 public function importHelper($name, $helperDirectory = 'helpers') {
297 $helperPath = "{$this->viewInfo['path']}/{$helperDirectory}/{$name}.inc.php";
298 require_once $this->onimporthelper($helperPath);
299 }
300
301 /**
302 *
303 */
304 public static function import($path) {
305 $viewInfo = CJTController::getViewInfo($path);
306 // Import view.
307 require_once $viewInfo['viewFile'];
308 }
309
310 /**
311 * put your comment there...
312 *
313 * @param mixed $model
314 */
315 public function setModel($model) {
316 $this->model = $this->onsetmodel($model);
317 return $this;
318 }
319
320 /**
321 * put your comment there...
322 *
323 * @param mixed $request
324 */
325 public function setRequest(& $request) {
326 $this->request = $request;
327 return $this;
328 }
329
330 /**
331 * put your comment there...
332 *
333 * @param mixed $name
334 * @param mixed $value
335 */
336 public function setRequestParameter($name, $value) {
337 $this->request[$name] = $value;
338 return $this;
339 }
340
341 /**
342 * put your comment there...
343 *
344 */
345 protected function & suppressPrintScriptsHook() {
346 // Access Global.
347 global $wp_actions;
348 // Mark as triggered.
349 $wp_actions['wp_print_scripts'] = true;
350 // Chain.
351 return $this;
352 }
353 /**
354 * put your comment there...
355 *
356 */
357 protected static function useScripts($className, $scripts = null) {
358 wp_enqueue_script('Just Load Default Scripts, this works great!!!!');
359 // Use current class name is $className is not provided!
360 if (!$className) {
361 $className = __CLASS__;
362 }
363 // Accept variable number of args of script list.
364 $allPassedArgs = func_get_args();
365 $scripts = self::trigger("{$className}.usescripts", (is_array($scripts) ? $scripts : array_slice($allPassedArgs, 1)));
366 $stack =& $GLOBALS['wp_scripts']->registered;
367 if (!$scripts) {
368 throw new Exception('CJTView::useScripts method must has at least on script parameter passed!');
369 }
370 // Script name Reg Exp pattern.
371 $nameExp = '/\:?(\{((\w+)-)\})?([\w\-\.]+)(\(.+\))?(\;(\d))?$/';
372 // For every script, Enqueue and localize, only if localization file found/exists.
373 foreach ($scripts as $script) {
374 // Get script name.
375 preg_match($nameExp, $script, $scriptObject);
376 // [[2]Prefix], [4] name. Prefix may be not presented.
377 $name = "{$scriptObject[2]}{$scriptObject[4]}";
378 $location = isset($scriptObject[7]) ? $scriptObject[7] : null;
379 $scriptParameters = isset($scriptObject[5]) ? $scriptObject[5] : '';
380 if (!isset($stack[$name])) {
381 // Any JS lib file should named the same as the parent folder with the extension added.
382 // Except files with _ at the begning
383 $libPath = ':' . ((strpos($scriptObject[4], '_') === 0) ? substr($scriptObject[4], 1) : "{$scriptObject[4]}:{$scriptObject[4]}");
384 // Pass virtual path to getURI and resolvePath to
385 // get JS file URI and localization file path.
386 $jsFile = cssJSToolbox::getURI(preg_replace($nameExp, "{$libPath}.js", $script));
387 $localizationFile = cssJSToolbox::resolvePath(preg_replace($nameExp, "{$libPath}.localization.php", $script));
388 // Enqueue script file.
389 wp_enqueue_script($name, $jsFile, null, null, $location);
390 // Set script parameters.
391 if (preg_match_all('/(\w+)=(\w+)/', $scriptParameters, $params, PREG_SET_ORDER) ) {
392 // Set parameters.
393 foreach ($params as $param) {
394 $stack[$name]->cjt[$param[1]] = $param[2];
395 }
396 // Initialize CJT for the script data object.
397 // This object caryy other informations so that the other
398 // Plugin parts/components can use it to know how script file work.
399 $stack[$name]->cjt = (object) $stack[$name]->cjt;
400 }
401
402 // If localization file exists localize JS.
403 if (file_exists($localizationFile)) {
404 // Get localization text array.
405 $localization = require $localizationFile;
406 // Object name is the script name with .'s and -'s stripped.
407 // Capitalize first char after each - or . and append I18N postfix.
408 $objectName = str_replace(' ', '', ucwords(str_replace(array('.', '-'), ' ', "{$name}I18N")));
409 // Ask Wordpress to localize the script file.
410 wp_localize_script($name, $objectName, $localization);
411 }
412 }
413 // Enqueue already registered scripts!
414 else {
415 wp_enqueue_script($name, null, null, null, $location);
416 }
417 }
418 }
419
420 /**
421 * put your comment there...
422 *
423 */
424 public static function useStyles($className, $styles = null) {
425 wp_enqueue_style('Just Load Default Styles, this works great!!!!');
426 // Use current class name is $className is not provided!
427 if (!$className) {
428 $className = __CLASS__;
429 }
430 // Accept variable number of args of script list.
431 $allPassedArgs = func_get_args();
432 $styles = self::trigger("{$className}.usestyles", (is_array($styles) ? $styles : array_slice($allPassedArgs, 1)));
433 if (!$styles) {
434 throw new Exception('CJTView::useStyles method must has at least on script parameter passed!');
435 }
436 // Script name Reg Exp pattern.
437 $nameExp = '/\:?(\{((\w+)-)\})?([\w\-\.]+)$/';
438 // For every script, Enqueue and localize, only if localization file found/exists.
439 foreach ($styles as $style) {
440 // Get script name.
441 preg_match($nameExp, $style, $styleObject);
442 // [[2]Prefix], [4] name. Prefix may be not presented.
443 $name = "{$styleObject[2]}{$styleObject[4]}";
444 if (!isset($GLOBALS['wp_styles']->registered[$name])) {
445 // Make all enqueued styles names unique from enqueued scripts.
446 // This is useful when merging styles & scripts is required.
447 $name = "CSS-{$name}";
448 // Any JS lib file should named the same as the parent folder with the extension added.
449 $libPath = ":{$styleObject[4]}";
450 // Get css file URI.
451 $cssFile = cssJSToolbox::getURI(preg_replace($nameExp, "{$libPath}.css", $style));
452 // Register + Enqueue style.
453 wp_enqueue_style($name, $cssFile);
454 }
455 else {
456 // Enqueue already registered styles.
457 wp_enqueue_style($name);
458 }
459 }
460 }
461
462 } // End class.
463
464 // Initialize CJTView Event!
465 CJTView::define('CJTView', array('hookType' => CJTWordpressEvents::HOOK_FILTER));