-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMtpl.php
More file actions
505 lines (474 loc) · 12.7 KB
/
Mtpl.php
File metadata and controls
505 lines (474 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
<?php
/**
* M PHP Framework
*
* @package M
* @subpackage Mtpl
* @author Arnaud Sellenet <demental at github>
* @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
* @version 0.1
*/
/**
*
* Bare bones php-based template engine
*
*/
class Mtpl {
protected $_addComments = true;
protected $_assignvars = array();
protected $_config;
protected $_postFilters = array();
protected $_module;
private $_currentCapture;
private $_currentFetch;
protected static $_captures;
protected static $_css = array();
protected static $_js = array();
protected static $_jsgroups = array();
protected static $_cssgroups = array();
protected static $_jsinline = array();
protected static $_meta = array();
function __construct($tpldir,$module=null)
{
if(!is_array($tpldir)) {
$tpldir = array($tpldir);
}
$this->_config['tplfolders'] = $tpldir;
$this->_module=$module;
if('production'==MODE) {
$this->_addComments = false;
}
}
/**
* Adds a string path to the current instance's paths array
* @param $path string new path (relative to PHP's include_paths)
* @param $pos string 'after' or 'before' : paths added before take precedence to other paths, after does not.
**/
public function addPath($path,$pos='before')
{
switch($pos) {
case 'before':
array_unshift($this->_config['tplfolders'],$path);
break;
default:
array_push($this->_config['tplfolders'],$path);
break;
}
}
/**
* Set the instance's paths
* @param $paths array of paths (relative to PHP's include_paths)
**/
public function setPaths($paths)
{
$this->_config['tplfolders'] = $paths;
}
public function getPaths()
{
return $this->_config['tplfolders'];
}
public static function getCSS()
{
return Mtpl::$_css;
}
public static function getJS()
{
return Mtpl::$_js;
}
public static function getJSgroups()
{
return Mtpl::$_jsgroups;
}
public static function getCSSgroups()
{
return Mtpl::$_cssgroups;
}
public static function getJSinline($event='ready')
{
return is_array(Mtpl::$_jsinline[$event])?Mtpl::$_jsinline[$event]:array();
}
public static function addCSS($css,$media='screen,print',$conditional=null)
{
$data = array('name'=>$css,'media'=>$media,'conditional'=>$conditional);
Mtpl::$_css[md5(serialize($data))] = $data;
}
public static function addCSSgroup($group, $media='screen,print')
{
if(is_array($group)) {
Mtpl::$_cssgroups[$media] = array_merge(Mtpl::$_cssgroups[$media],$group);
} else {
Mtpl::$_cssgroups[$media][] = $group;
}
Mtpl::$_cssgroups[$media] = array_unique(Mtpl::$_cssgroups[$media]);
}
public static function addJSgroup($group)
{
if(is_array($group)) {
Mtpl::$_jsgroups = array_merge(Mtpl::$_jsgroups,$group);
} else {
Mtpl::$_jsgroups[] = $group;
}
Mtpl::$_jsgroups = array_unique(Mtpl::$_jsgroups);
}
public static function addJS($js)
{
if(is_array($js)) {
Mtpl::$_js = array_merge(Mtpl::$_js,$js);
} else {
Mtpl::$_js[] = $js;
}
Mtpl::$_js = array_unique(Mtpl::$_js);
}
public static function addJSinline($js,$event='ready')
{
Mtpl::$_jsinline[$event][] = $js;
}
public static function setMeta($name,$content)
{
Mtpl::$_meta[$name]=$content;
}
public static function getMeta($name)
{
return Mtpl::$_meta[$name];
}
public function &instance()
{
return new Mtpl($this->_config['tplfolders'],$module);
}
public function &getVars ()
{
return $this->_assignvars;
}
public function setVars (&$vars)
{
$this->_assignvars = &$vars;
}
public function assign ($var, $val)
{
$this->_assignvars[$var] = $val;
}
public function append($var,$val)
{
$this->_assignvars[$var][]=$val;
}
public function concat($var,$val)
{
$this->_assignvars[$var].=$val;
}
public function assignArray ($arr)
{
$this->_assignvars = array_merge($this->_assignvars, $arr);
}
public function assignRef ($var, &$val)
{
$this->_assignvars[$var] = &$val;
}
public function addPostFilter ( Mtpl_filter $filter )
{
$this->_postFilters[] = &$filter;
}
public function display($tplfile, $cacheId = null)
{
echo $this->fetch($tplfile);
}
public function getTemplatePath()
{
$folders = array_reverse($this->_config['tplfolders']);
Log::error('Searching file '.$this->_tplfile.' in '.print_r($folders,true));
foreach($folders as $folder) {
if(FileUtils::file_exists_incpath($folder.$this->_tplfile.'.php')) {
Log::error('Found file in '.$folder);
return $folder.$this->_tplfile.'.php';
}
}
return false;
}
public function fetch($tplfile)
{
if(!is_array($tplfile)) {
$tplfile = array($tplfile);
}
$buffer = ob_get_contents();
ob_clean();
extract($this->_assignvars);
$included=false;
foreach($tplfile as $file) {
$pluginfile = explode(':',$file);
if(count($pluginfile) == 2) {
PluginRegistry::initPlugin($pluginfile[0]);
$file = $pluginfile[1];
$this->_config['tplfolders'] = array('M/plugins/'.$pluginfile[0].'/templates/', APP_ROOT.'app/plugins/'.$pluginfile[0].'/templates/');
}
$this->_tplfile=$file;
if($tpl = $this->getTemplatePath()) {
ob_start();
$bf = trim($buffer);
if(!empty($bf)) {
echo $this->comment('Start include '.$file);
}
include($tpl);
if(!empty($bf)) {
echo $this->comment('End include '.$file);
}
$included=true;
$ret = ob_get_contents();
ob_clean();
echo $buffer;
foreach($this->_postFilters as $filter) {
$filter->execute($ret);
}
return $ret;
break;
}
}
echo $buffer;
ob_clean();
throw new Exception('Fichier '.print_r($tplfile,true).' introuvable dans dossier(s) '.print_r($this->_config['tplfolders'],true));
}
/**
* Template helpers (for use in the template as $this->methodName)
**/
private function startCapture($capturename) {
$this->_currentFetch=ob_get_contents();
ob_clean();
$this->_currentCapture=$capturename;
ob_start();
}
private function endCapture() {
$res = ob_get_contents();
ob_clean();
ob_start();
echo $this->_currentFetch;
Mtpl::$_captures[$this->_currentCapture]=$res;
}
public function getCapture($name) {
return Mtpl::$_captures[$name];
}
// partial inclusion
private function includetpl($file, $params = array(),$autoglobal = false)
{
return $this->i($file, $params,$autoglobal);
}
public function get_part($file, $params = array(),$autoglobal = false)
{
$tpl = new Mtpl($this->_config['tplfolders']);
if($autoglobal) {
$tpl->setVars($this->getVars());
}
if(is_array($params)) {
foreach($params as $var=>&$value) {
if(is_object($value)) {
$tpl->assignRef($var, $value);
} else {
$tpl->assign($var, $value);
}
}
}
return $tpl->fetch($file);
}
private function i($file, $params = array(),$autoglobal = false)
{
echo $this->get_part($file, $params, $autoglobal);
}
// Render HTML_QuickForm
private function rf(&$form,$type='dynamic') {
if(is_array($form)) {
return $form;
}
$args = func_get_args();
M::Hook(__CLASS__,__FUNCTION__,$args);
if($type=='dynamic') {
$r = new HTML_QuickForm_Renderer_Array(true,true);
if(!is_object($form)) {
throw new Exception('Object is not a form object');
}
$form->accept($r);
$ret = $r->toArray();
if(!count($ret['sections'])) {
$ret['sections'] = array(array('elements'=>$ret['elements']));
}
} else {
$r = new HTML_QuickForm_Renderer_ArrayStatic(true,true);
$form->accept($r);
$ret = $r->toArray();
}
return $ret;
}
// Echo
private function e($var) {
echo $var;
}
/**
* Adds a component to the template
* @param string module name
* @param string module action
* @return string rendered module
**/
private function component($componentId,$action='index',$params=null) {
return $this->c($componentId,$action,$params);
}
public function c($componentId,$action='index',$params=null) {
if(is_object($this->_module)) {
$conf = $this->_module->getConfig('component_'.$componentId,$this->_module->getCurrentAction());
if(!is_array($conf)) {
$conf = $this->_module->getConfig('component_'.$componentId);
}
} else {
$conf=null;
}
if(is_array($conf)) {
$module = $conf[0];
$action = empty($conf[1])?'index':$conf[1];
} elseif($conf=='__none') {
return;
} else {
$module = $componentId;
}
$c = new Component($module, $action,$params);
$c->execute();
return $this->comment('Start component '.$module.'/'.$action.' routed to '.$c->getPage()->getCurrentModule().'/'.$c->getPage()->getCurrentAction())
.$c->display()
.$this->comment('End component '.$module.'/'.$action);
}
public function toArray() {
return $this->_assignvars;
}
public function comment($comment)
{
if(!$this->_addComments) return '';
return '
<!-- '.$comment.' -->
';
}
public function img($filename,$subfolders=null,$mainfolder = null)
{
if(is_null($mainfolder)) {
$mainfolder='images/';
}
if(!is_array($subfolders)) {
return SITE_URL.$mainfolder.$filename;
} else {
foreach($subfolders as $afolder) {
if(file_exists(APP_ROOT.'public/'.$mainfolder.$afolder.'/'.$filename)) {
return SITE_URL.$mainfolder.$afolder.'/'.$filename;
}
}
// No image found, so we return a default path to handle the "no image" in the app.
return SITE_URL.$mainfolder.$defaultfolder.$filename;
}
}
public function localeimg($filename,$mainfolder = 'images/locale/')
{
if (T::getLang() != Config::get('defaultLang')) {
return $this->img($filename,array(T::getLang(),substr(T::getLang(),0,2),Config::get('defaultLang')),$mainfolder);
} else {
return $this->img($filename,array(T::getLang(),substr(T::getLang(),0,2)),$mainfolder);
}
}
public function altlocaleimg($filename, $altfolder = NULL, $mainfolder = 'images/locale/')
{
if (! is_null($altfolder)) {
return $this->img($filename,array($altfolder.'/'.T::getLang(),T::getLang(),substr(T::getLang(),0,2)),$mainfolder);
}
return $this->img($filename,array(T::getLang(),substr(T::getLang(),0,2)),$mainfolder);
}
public static function getCSSblock()
{
$out='';
$groups = Mtpl::getCSSgroups();
if(count($groups)>0) {
foreach($groups as $medias => $subgroups) {
if(count($subgroups) > 0) {
$assetsversion = (int)file_get_contents(APP_ROOT.'app/ASSETSVERSION');
foreach($subgroups as $group) {
$cssfile = '/cache/'.$group.$assetsversion.'.css';
$out .= '<link rel="stylesheet" type="text/css" href="'.$cssfile.'" media="'.$medias.'" />';
}
}
}
}
foreach (Mtpl::getCSS() as $css) {
if(preg_match('`^https*`',$css['name'])) {
$cssfile = $css['name'].'.css';
} else {
$cssfile = '/css/'.$css['name'].'.css';
}
if (is_null($css['conditional'])) {
$out.='
<link rel="stylesheet" type="text/css" href="'.$cssfile.'" media="'.$css['media'].'" />';
} else {
$out.='
<!--[if '.$css['conditional'].']>
<link rel="stylesheet" type="text/css" href="'.$cssfile.'" media="'.$css['media'].'" />
<![endif]-->';
}
}
return $out;
}
public function getJSblock($root = array())
{
if(!is_array($root)) {
$root = array($root);
}
$root[]= '/js';
$out='';
$groups = Mtpl::getJSgroups();
if(count($groups)>0) {
$assetsversion = (int)file_get_contents(APP_ROOT.'app/ASSETSVERSION');
foreach(Mtpl::getJSgroups() as $group) {
$jsfile = '/cache/'.$group.$assetsversion.'.js';
$out.='
<script type="text/javascript" src="'.$jsfile.'"></script>';
}
}
foreach (Mtpl::getJS() as $js) {
if(preg_match('`^https*`',$js)) {
$jsfile = $js;
} else {
$jsfile = self::first_in_assets($root, $js.'.js');
}
if(!$jsfile) throw new Exception('non exist '.$js);
$out.='
<script type="text/javascript" src="'.$jsfile.'"></script>';
}
return $out;
}
public static function first_in_assets($roots, $filename)
{
foreach($roots as $dir) {
$file = APP_ROOT.'public/'.$dir.'/'.$filename;
if(file_exists($file)) {
return $dir.'/'.$filename;
}
}
}
public static function printJS($root = '/js')
{
echo self::getJSblock($root);
}
public static function printCSS()
{
echo self::getCSSblock();
}
public function getJSinlineblock($event='ready')
{
$out='';
foreach(Mtpl::getJSinline($event) as $line) {
$out.=$line."\n";
}
$out = trim($out);
if(!empty($out)) {
$out= '
<script type="text/javascript">
jQuery(function($) {
'.$out.'
});
</script>';
}
return $out;
}
public function printJSinline($event='ready')
{
echo $this->getJSinlineblock($event);
}
}