-
Notifications
You must be signed in to change notification settings - Fork 988
/
LS_Twig_Extension.php
279 lines (236 loc) · 9.79 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
<?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)
{
$oTemplate = self::getTemplateForRessource($sTemplateCssFileName);
Yii::app()->getClientScript()->registerCssFile(
$oTemplate->sTemplateUrl .
$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)
*/
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)
*/
public static function registerTemplateScript($sTemplateScriptFileName, $position=null, array $htmlOptions=array())
{
$oTemplate = self::getTemplateForRessource($sTemplateScriptFileName);
$position = self::getPosition($position);
Yii::app()->getClientScript()->registerScriptFile(
$oTemplate->sTemplateUrl .
$sTemplateScriptFileName,
$position,
$htmlOptions
);
}
/**
* Publish a script
* In any twig file, you can register a script doing: {{ registerScript($sId, $sScript) }}
*/
public static function registerScript($id, $script, $position=null, array $htmlOptions=array())
{
$position = self::getPosition($position);
Yii::app()->getClientScript()->registerScript(
$id,
$script,
$position,
$htmlOptions
);
}
public static function getPosition($position){
switch($position) {
case "POS_HEAD":
$position = CClientScript::POS_HEAD;
break;
case "POS_BEGIN":
$position = CClientScript::POS_BEGIN;
break;
case "POS_END":
$position = CClientScript::POS_END;
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
*/
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';
}
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 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
*/
public static function image($sImagePath, $alt='', $htmlOptions=array ( ) )
{
// Reccurence on templates to find the file
$oTemplate = self::getTemplateForRessource($sImagePath);
if($oTemplate){
$sUrlImgAsset = self::assetPublish($oTemplate->path.'/'.$sImagePath);
}else{
// TODO: publish a default image "not found"
}
return CHtml::image($sUrlImgAsset, $alt, $htmlOptions);
}
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);
}
public static function unregisterPackage($name)
{
return Yii::app()->getClientScript()->unregisterPackage($name);
}
public static function listCoreScripts()
{
foreach(Yii::app()->getClientScript()->coreScripts as $key => $package){
echo "<hr>";
echo "$key: <br>";
var_dump($package);
}
}
}