/
LS_Twig_Extension.php
147 lines (127 loc) · 5.9 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
<?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)
{
if (!YII_DEBUG || Yii::app()->getConfig('use_asset_manager')){
// Publish the css file as an asset and then register it
Yii::app()->getClientScript()->registerCssFile( // 2. Register the CSS file (add the <link rel="stylesheet" type="text/css" href=".../tmp/.../file.css"... to the HTML page)
Yii::app()->getAssetManager()->publish( // 1. Publish the asset (copy the file.css to tmp/.../assets/... directory)
Yii::app()->getConfig('publicstylepath') . // NOTE: assets needs a path, not an url, because the file is first moved to the tmp directory
$sPublicCssFileName
)
);
}else{
// Directly register the CSS file without using the asset manager
Yii::app()->getClientScript()->registerCssFile(
Yii::app()->getConfig('publicstyleurl') . // NOTE: URL can be use, because it will only add a link to that url (add the <link rel="stylesheet" type="text/css" href="url.../file.css")
$sPublicCssFileName
);
}
}
/**
* 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
*/
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-unrelevant';
$aQuestionClass .= ' ls-hidden';
}
if ($lemQuestionInfo['hidden']){ /* Can use aQuestionAttributes too */
$aQuestionClass .= ' ls-hidden-attribute';/* another string ? */
$aQuestionClass .= ' ls-hidden';
}
$aQuestionAttributes = getQuestionAttributeValues($iQid);
//add additional classes
if(isset($aQuestionAttributes['cssclass']) && $aQuestionAttributes['cssclass']!=""){
/* Got to use static expression */
$emCssClass = trim(LimeExpressionManager::ProcessString($aQuestionAttributes['cssclass'], null, array(), false, 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()
{
App()->getController()->widget('CCaptcha',array(
'buttonOptions'=>array('class'=> 'btn btn-xs btn-info'),
'buttonType' => 'button',
'buttonLabel' => gt('Reload image','unescaped')
));
}
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);
}
}