/
LS_Twig_Extension.php
541 lines (467 loc) · 19.5 KB
/
LS_Twig_Extension.php
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
<?php
/**
* This extension is needed to add complex functions to twig, needing specific process (like accessing config datas).
* Most of the calls to internal functions don't need to be set here, but can be directly added to the internal config file.
* For example, the calls to encode, gT and eT don't need any extra parameters or process, so they are added as filters in the congif/internal.php:
*
* 'filters' => array(
* 'jencode' => 'CJSON::encode',
* 't' => 'eT',
* 'gT' => 'gT',
* ),
*
* So you only add functions here when they need a specific process while called via Twig.
* To add an advanced function to twig:
*
* 1. Add it here as a static public function
* eg:
* static public function foo($bar)
* {
* return procces($bar);
* }
*
* 2. Add it in config/internal.php as a function, and as an allowed function in the sandbox
* eg:
* twigRenderer' => array(
* ...
* 'functions' => array(
* ...
* 'foo' => 'LS_Twig_Extension::foo',
* ...),
* ...
* 'sandboxConfig' => array(
* ...
* 'functions' => array('include', ..., 'foo')
* ),
*
* Now you access this function in any twig file via: {{ foo($bar) }}, it will show the result of process($bar).
* If LS_Twig_Extension::foo() returns some HTML, by default the HTML will be escaped and shows as text.
* To get the pure HTML, just do: {{ foo($bar) | raw }}
*/
class LS_Twig_Extension extends Twig_Extension
{
/**
* Publish a css file from public style directory, using or not the asset manager (depending on configuration)
* In any twig file, you can register a public css file doing: {{ registerPublicCssFile($sPublicCssFileName) }}
* @param string $sPublicCssFileName name of the CSS file to publish in public style directory
*/
public static function registerPublicCssFile($sPublicCssFileName)
{
Yii::app()->getClientScript()->registerCssFile(
Yii::app()->getConfig('publicstyleurl').
$sPublicCssFileName
);
}
/**
* Publish a css file from template directory, using or not the asset manager (depending on configuration)
* In any twig file, you can register a template css file doing: {{ registerTemplateCssFile($sTemplateCssFileName) }}
* @param string $sTemplateCssFileName name of the CSS file to publish in template directory (it should contains the subdirectories)
*/
public static function registerTemplateCssFile($sTemplateCssFileName)
{
/*
CSS added from template could require some files from the template folder file... (eg: background.css)
So, if we want the statements like :
url("../files/myfile.jpg)
to point to an existing file, the css file must be published in the same tmp directory than the template files
in other words, the css file must be added to the template package.
*/
$oTemplate = self::getTemplateForRessource($sTemplateCssFileName);
Yii::app()->getClientScript()->packages[$oTemplate->sPackageName]['css'][] = $sTemplateCssFileName;
}
/**
* Publish a script file from general script directory, using or not the asset manager (depending on configuration)
* In any twig file, you can register a general script file doing: {{ registerGeneralScript($sGeneralScriptFileName) }}
* @param string $sGeneralScriptFileName name of the script file to publish in general script directory (it should contains the subdirectories)
* @param string $position
* @param array $htmlOptions
*/
public static function registerGeneralScript($sGeneralScriptFileName, $position = null, array $htmlOptions = array())
{
$position = self::getPosition($position);
Yii::app()->getClientScript()->registerScriptFile(
App()->getConfig('generalscripts').
$sGeneralScriptFileName,
$position,
$htmlOptions
);
}
/**
* Publish a script file from template directory, using or not the asset manager (depending on configuration)
* In any twig file, you can register a template script file doing: {{ registerTemplateScript($sTemplateScriptFileName) }}
* @param string $sTemplateScriptFileName name of the script file to publish in general script directory (it should contains the subdirectories)
* @param string $position
* @param array $htmlOptions
*/
public static function registerTemplateScript($sTemplateScriptFileName, $position = null, array $htmlOptions = array())
{
$oTemplate = self::getTemplateForRessource($sTemplateScriptFileName);
Yii::app()->getClientScript()->packages[$oTemplate->sPackageName]['js'][] = $sTemplateScriptFileName;
}
/**
* Publish a script
* In any twig file, you can register a script doing: {{ registerScript($sId, $sScript) }}
*
* NOTE: this function is not recursive, so don't use it to register a script located inside a theme folder, or inherited themes will be broken.
* NOTE! to register a script located inside a theme folder, registerTemplateScript()
*
*/
public static function registerScript($id, $script, $position = null, array $htmlOptions = array())
{
$position = self::getPosition($position);
Yii::app()->getClientScript()->registerScript(
$id,
$script,
$position,
$htmlOptions
);
}
/**
* Convert a json object to a PHP array (so no troubles with object method in sandbox)
*/
public static function json_decode($json)
{
return (array) json_decode($json);
}
/**
* @param $position
* @return string
*/
public static function getPosition($position)
{
switch ($position) {
case "POS_HEAD":
$position = LSYii_ClientScript::POS_HEAD;
break;
case "POS_BEGIN":
$position = LSYii_ClientScript::POS_BEGIN;
break;
case "POS_END":
$position = LSYii_ClientScript::POS_END;
break;
case "POS_POSTSCRIPT":
$position = LSYii_ClientScript::POS_POSTSCRIPT;
break;
default:
$position = '';
break;
}
return $position;
}
/**
* Retreive the question classes for a given question id
* Use in survey template question.twig file.
* TODO: we'd rather provide a oQuestion object to the twig view with a method getAllQuestion(). But for now, this public static function respect the old way of doing
*
* @param int $iQid the question id
* @return string the classes
* @deprecated must be removed when allow to broke template. Since it was in 3.0 , it was in API (and question.twig are surely be updated).
*/
public static function getAllQuestionClasses($iQid)
{
$lemQuestionInfo = LimeExpressionManager::GetQuestionStatus($iQid);
$sType = $lemQuestionInfo['info']['type'];
$aSGQA = explode('X', $lemQuestionInfo['sgqa']);
$iSurveyId = $aSGQA[0];
$aQuestionClass = Question::getQuestionClass($sType);
/* Add the relevance class */
if (!$lemQuestionInfo['relevant']) {
$aQuestionClass .= ' ls-irrelevant';
$aQuestionClass .= ' ls-hidden';
}
/* Can use aQuestionAttributes too */
if ($lemQuestionInfo['hidden']) {
$aQuestionClass .= ' ls-hidden-attribute'; /* another string ? */
$aQuestionClass .= ' ls-hidden';
}
$aQuestionAttributes = QuestionAttribute::model()->getQuestionAttributes($iQid);
//add additional classes
if (isset($aQuestionAttributes['cssclass']) && $aQuestionAttributes['cssclass'] != "") {
/* Got to use static expression */
$emCssClass = trim(LimeExpressionManager::ProcessString($aQuestionAttributes['cssclass'], null, array(), 1, 1, false, false, true)); /* static var is the lmast one ...*/
if ($emCssClass != "") {
$aQuestionClass .= " ".Chtml::encode($emCssClass);
}
}
if ($lemQuestionInfo['info']['mandatory'] == 'Y') {
$aQuestionClass .= ' mandatory';
}
if ($lemQuestionInfo['anyUnanswered'] && $_SESSION['survey_'.$iSurveyId]['maxstep'] != $_SESSION['survey_'.$iSurveyId]['step']) {
$aQuestionClass .= ' missing';
}
return $aQuestionClass;
}
public static function renderCaptcha()
{
return App()->getController()->createWidget('LSCaptcha', array(
'captchaAction'=>'captcha',
'buttonOptions'=>array('class'=> 'btn btn-xs btn-info'),
'buttonType' => 'button',
'buttonLabel' => gt('Reload image', 'unescaped')
));
}
public static function createUrl($url, $params = array())
{
return App()->getController()->createUrl($url, $params);
}
/**
* @param string $sRessource
*/
public static function assetPublish($sRessource)
{
return App()->getAssetManager()->publish($sRessource);
}
/**
* @var $sImagePath string the image path relative to the template root
* @var $alt string the alternative text display
* @var $htmlOptions array additional HTML attribute
* @return string
*/
public static function image($sImagePath, $alt = '', $htmlOptions = array( ))
{
// Reccurence on templates to find the file
$oTemplate = self::getTemplateForRessource($sImagePath);
$sUrlImgAsset = '';
if ($oTemplate) {
$sUrlImgAsset = self::assetPublish($oTemplate->path.$sImagePath);
}
if (@is_array(getimagesize(Yii::app()->getConfig('rootdir').'/'.$sImagePath))) {
$sUrlImgAsset = self::assetPublish(Yii::app()->getConfig('rootdir').'/'.$sImagePath);
}
return CHtml::image($sUrlImgAsset, $alt, $htmlOptions);
}
/**
* @var $sImagePath string the image path relative to the template root
* @var $default string an alternative image if the provided one cant be found
* @return string
*/
/* @TODO => implement the default in a secure way */
public static function imageSrc($sImagePath, $default = './files/pattern.png')
{
// Reccurence on templates to find the file
$oTemplate = self::getTemplateForRessource($sImagePath);
$sUrlImgAsset = $sImagePath;
if ($oTemplate) {
$sUrlImgAsset = self::assetPublish($oTemplate->path.$sImagePath);
}
if (@is_array(getimagesize(Yii::app()->getConfig('rootdir').'/'.$sImagePath))) {
$sUrlImgAsset = self::assetPublish(Yii::app()->getConfig('rootdir').'/'.$sImagePath);
}
$myTemplateAsset = $sUrlImgAsset;
return $sUrlImgAsset;
}
/**
* @param string $sRessource
*/
public static function getTemplateForRessource($sRessource)
{
$oRTemplate = Template::model()->getInstance();
while (!file_exists($oRTemplate->path.$sRessource)) {
$oMotherTemplate = $oRTemplate->oMotherTemplate;
if (!($oMotherTemplate instanceof TemplateConfiguration)) {
return false;
break;
}
$oRTemplate = $oMotherTemplate;
}
return $oRTemplate;
}
public static function getPost($sName, $sDefaultValue = null)
{
return Yii::app()->request->getPost($sName, $sDefaultValue);
}
public static function getParam($sName, $sDefaultValue = null)
{
return Yii::app()->request->getParam($sName, $sDefaultValue);
}
public static function getQuery($sName, $sDefaultValue = null)
{
return Yii::app()->request->getQuery($sName, $sDefaultValue);
}
/**
* @param string $name
*/
public static function unregisterPackage($name)
{
return Yii::app()->getClientScript()->unregisterPackage($name);
}
/**
* @param string $name
*/
public static function unregisterScriptFile($name)
{
return Yii::app()->getClientScript()->unregisterScriptFile($name);
}
public static function registerScriptFile($path, $position = null)
{
Yii::app()->getClientScript()->registerScriptFile($path, ($position === null ? LSYii_ClientScript::POS_BEGIN : self::getPosition($position)));
}
public static function registerCssFile($path)
{
Yii::app()->getClientScript()->registerCssFile($path);
}
public static function registerPackage($name)
{
Yii::app()->getClientScript()->registerPackage($name, LSYii_ClientScript::POS_BEGIN);
}
/**
* Unregister all packages/script files for AJAX rendering
*/
public static function unregisterScriptForAjax()
{
$oTemplate = Template::model()->getInstance();
$sTemplatePackageName = 'limesurvey-'.$oTemplate->sTemplateName;
self::unregisterPackage($sTemplatePackageName);
self::unregisterPackage('template-core');
self::unregisterPackage('bootstrap');
self::unregisterPackage('jquery');
self::unregisterPackage('bootstrap-template');
self::unregisterPackage('fontawesome');
self::unregisterPackage('template-default-ltr');
self::unregisterPackage('decimal');
self::unregisterScriptFile('/assets/scripts/survey_runtime.js');
self::unregisterScriptFile('/assets/scripts/admin/expression.js');
self::unregisterScriptFile('/assets/scripts/nojs.js');
self::unregisterScriptFile('/assets/scripts/expressions/em_javascript.js');
}
public static function listCoreScripts()
{
foreach (Yii::app()->getClientScript()->coreScripts as $key => $package) {
echo "<hr>";
echo "$key: <br>";
var_dump($package);
}
}
public static function listScriptFiles()
{
foreach (Yii::app()->getClientScript()->getScriptFiles() as $key => $file) {
echo "<hr>";
echo "$key: <br>";
var_dump($file);
}
}
/**
* Process any string with current page
* @param string to be processed
* @param boolean $static return static string (or not)
* @param integer $numRecursionLevels recursion (max) level to do
* @param array $aReplacement replacement out of EM
* @return string
*/
public static function processString($string,$static=false,$numRecursionLevels=3,$aReplacement = array())
{
if(!is_string($string)) {
/* Add some errors in template editor , see #13532 too */
if(Yii::app()->getController()->getId() == 'admin' && Yii::app()->getController()->getAction()->getId() == 'themes') {
Yii::app()->setFlashMessage(gT("Usage of processString without a string in your template"),'error');
}
return;
}
return LimeExpressionManager::ProcessStepString($string, $aReplacement,$numRecursionLevels, $static);
}
/**
* Get html text and remove whole not clean string
* @param string $string to flatten
* @param boolean $encode html entities
* @return string
*/
public static function flatString($string,$encode=false)
{
// Remove script before removing tag, no tag : no other script (onload, on error etc …
$string = strip_tags(stripJavaScript($string));
// Remove new lines
if (version_compare(substr(PCRE_VERSION, 0, strpos(PCRE_VERSION, ' ')), '7.0') > -1) {
$string = preg_replace(array('~\R~u'), array(' '), $string);
} else {
$string = str_replace(array("\r\n", "\n", "\r"), array(' ', ' ', ' '), $string);
}
// White space to real space
$string = preg_replace('/\s+/', ' ', $string);
if($encode) {
return \CHtml::encode($string);
}
return $string;
}
/**
* get flat and ellipsize string
* @param string $string to ellipsize
* @param integer $maxlength of the final string
* @param float $position of the ellipsis in string (between 0 and 1)
* @param string $ellipsis string to shown in place of removed part
* @return string
*/
public static function ellipsizeString($string, $maxlength, $position = 1, $ellipsis = '…')
{
$string = self::flatString($string,false);
$string = ellipsize($string, $maxlength, $position, $ellipsis);// Use common_helper function
return $string;
}
/**
* flat and ellipsize text, for template compatibility
* @deprecated (4.0)
* @param string $sString :the string
* @param boolean $bFlat : flattenText or not : completely flat (not like flattenText from common_helper)
* @param integer $iAbbreviated : max string text (if true : allways flat), 0 or false : don't abbreviated
* @param string $sEllipsis if abbreviated : the char to put at end (or middle)
* @param integer $fPosition if abbreviated position to split (in % : 0 to 1)
* @return string
*/
public static function flatEllipsizeText($sString, $bFlat = true, $iAbbreviated = 0, $sEllipsis = '...', $fPosition = 1)
{
if (!$bFlat && !$iAbbreviated) {
return $sString;
}
$sString = self::flatString($sString);
if ($iAbbreviated > 0) {
$sString = ellipsize($sString, $iAbbreviated, $fPosition, $sEllipsis);
}
return $sString;
}
public static function darkencss($cssColor, $grade=10, $alpha=1){
$aColors = str_split(substr($cssColor,1), 2);
$return = [];
foreach ($aColors as $color) {
$decColor = hexdec($color);
$decColor = $decColor-$grade;
$decColor = $decColor<0 ? 0 : ($decColor>255 ? 255 : $decColor);
$return[] = $decColor;
}
if($alpha === 1) {
return '#'.join('', array_map(function($val){ return dechex($val);}, $return));
}
return 'rgba('.join(', ', $return).','.$alpha.')';
}
/**
* Check if a needle is in a multidimensional array
* @param mixed $needle The searched value.
* @param array $haystack The array.
* @param bool $strict If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack.
*/
function in_multiarray($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
public static function lightencss($cssColor, $grade=10, $alpha=1)
{
$aColors = str_split(substr($cssColor,1), 2);
$return = [];
foreach ($aColors as $color) {
$decColor = hexdec($color);
$decColor = $decColor+$grade;
$decColor = $decColor<0 ? 0 : ($decColor>255 ? 255 : $decColor);
$return[] = $decColor;
}
if($alpha === 1) {
return '#'.join('', array_map(function($val){ return dechex($val);}, $return));
}
return 'rgba('.join(', ', $return).','.$alpha.')';
}
public static function getConfig($item)
{
return Yii::app()->getConfig($item);
}
}