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

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