/
ExpressionValidate.php
286 lines (274 loc) · 12.6 KB
/
ExpressionValidate.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
<?php
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
/*
* Offer some way to validate Expression in survey
*
* @copyright 2014 The LimeSurvey Project Team
* @license http://www.gnu.org/licenses/gpl-3.0.txt
* @todo : Add any expression
* @version : 1.1
*/
class ExpressionValidate extends Survey_Common_Action {
/**
* @var string : Default layout is popup : less header, no footer
*/
public $layout = 'popup';
/**
* @var integer : The survey id to start to fill know vars
*/
private $iSurveyId;
/**
* @var string : The language for the survey
*/
private $sLang;
public function index()
{
throw new CHttpException(400);
}
/**
* Check the Expression in quota
* @param integer $iSurveyId : the survey id : can be sid/surveyid url GET parameters
* @param integer $quota : the quota id
* @param string $lang : the survey language, optional : if not set get all language of survey
*
* @author Denis Chenu
* @version 1.0
*/
public function quota($iSurveyId, $quota, $lang = null)
{
if (!Permission::model()->hasSurveyPermission($iSurveyId, 'quotas', 'read')) {
throw new CHttpException(401, "401 Unauthorized");
}
$iQuotaId = $quota;
if (is_string($lang))
{
$oValidator = new LSYii_Validators;
$aLangs = array($oValidator->languageFilter($lang));
} else
{
$aLangs = Survey::model()->findByPk($iSurveyId)->getAllLanguages();
}
$aExpressions = array();
$this->iSurveyId = $iSurveyId;
foreach ($aLangs as $sLang)
{
$oQuotaLanguageSetting = QuotaLanguageSetting::model()->find("quotals_quota_id =:quota_id and quotals_language=:language", array(':quota_id'=>$iQuotaId, ':language'=>$sLang));
// We don't need to go to step since new feature #8823, maybe need to be fixed ?
if ($oQuotaLanguageSetting)
{
$this->sLang = $sLang;
$aExpressions['name_'.$sLang] = array(
'title'=>sprintf(gT("Quota name (%s)"), $sLang),
'expression'=> $this->getHtmlExpression($oQuotaLanguageSetting->quotals_name, array(), __METHOD__),
);
$aExpressions['message_'.$sLang] = array(
'title'=>sprintf(gT("Quota message (%s)"), $sLang),
'expression'=> $this->getHtmlExpression($oQuotaLanguageSetting->quotals_message, array(), __METHOD__),
);
$aExpressions['url_'.$sLang] = array(
'title'=>sprintf(gT("URL (%s)"), $sLang),
'expression'=> $this->getHtmlExpression($oQuotaLanguageSetting->quotals_url, array(), __METHOD__),
);
$aExpressions['urldescrip_'.$sLang] = array(
'title'=>sprintf(gT("URL description (%s)"), $sLang),
'expression'=> $this->getHtmlExpression($oQuotaLanguageSetting->quotals_urldescrip, array(), __METHOD__),
);
}
}
$aData = array(
'aExpressions'=>$aExpressions,
);
$this->getController()->layout = $this->layout;
$this->getController()->pageTitle = gT("Validate quota");
$this->getController()->render("/admin/expressions/validationList", $aData);
}
/**
* Check the Expression in email
* @param integer $iSurveyId : the survey id : can be sid/surveyid url GET parameters
* @param string $lang : the mail language
*
* @author Denis Chenu
* @version 1.1
*/
public function email($iSurveyId, $lang)
{
if (!Permission::model()->hasSurveyPermission($iSurveyId, 'surveysettings', 'read')) {
throw new CHttpException(401, "401 Unauthorized");
}
$sType = Yii::app()->request->getQuery('type');
$this->sLang = $sLang = $lang;
$this->iSurveyId = $iSurveyId; // This start the survey before Expression : is this allways needed ?
$aTypeAttributes = array(
'invitation'=>array(
'subject'=>array(
'attribute'=>'surveyls_email_invite_subj',
'title'=>gT('Invitation email subject'),
),
'message'=>array(
'attribute'=>'surveyls_email_invite',
'title'=>gT('Invitation email body'),
),
),
'reminder'=>array(
'subject'=>array(
'attribute'=>'surveyls_email_remind_subj',
'title'=>gT('Reminder email subject'),
),
'message'=>array(
'attribute'=>'surveyls_email_remind',
'title'=>gT('Reminder email body'),
),
),
'confirmation'=>array(
'subject'=>array(
'attribute'=>'surveyls_email_confirm_subj',
'title'=>gT('Confirmation email subject'),
),
'message'=>array(
'attribute'=>'surveyls_email_confirm',
'title'=>gT('Confirmation email body'),
),
),
'registration'=>array(
'subject'=>array(
'attribute'=>'surveyls_email_register_subj',
'title'=>gT('Registration email subject'),
),
'message'=>array(
'attribute'=>'surveyls_email_register',
'title'=>gT('Registration email body'),
),
),
'admin_notification'=>array(
'subject'=>array(
'attribute'=>'email_admin_notification_subj',
'title'=>gT('Basic admin notification subject'),
),
'message'=>array(
'attribute'=>'email_admin_notification',
'title'=>gT('Basic admin notification body'),
),
),
'admin_detailed_notification'=>array(
'subject'=>array(
'attribute'=>'email_admin_responses_subj',
'title'=>gT('Detailed admin notification subject'),
),
'message'=>array(
'attribute'=>'email_admin_responses',
'title'=>gT('Detailed admin notification body'),
),
),
);
$aSurveyInfo = getSurveyInfo($iSurveyId, $sLang);
// Replaced before email edit
$aReplacement = array(
'ADMINNAME'=> $aSurveyInfo['admin'],
'ADMINEMAIL'=> $aSurveyInfo['adminemail'],
);
// Not needed : templatereplace do the job : but this can/must be fixed for invitaton/reminder/registration (#9424)
$aReplacement["SURVEYNAME"] = gT("Name of the survey");
$aReplacement["SURVEYDESCRIPTION"] = gT("Description of the survey");
// Replaced when sending email with Survey
$aAttributes = getTokenFieldsAndNames($iSurveyId, true);
$aReplacement["TOKEN"] = gT("Token code for this participant");
$aReplacement["TOKEN:EMAIL"] = gT("Email from the token");
$aReplacement["TOKEN:FIRSTNAME"] = gT("First name from token");
$aReplacement["TOKEN:LASTNAME"] = gT("Last name from token");
$aReplacement["TOKEN:TOKEN"] = gT("Token code for this participant");
$aReplacement["TOKEN:LANGUAGE"] = gT("language of token");
foreach ($aAttributes as $sAttribute=>$aAttribute)
{
$aReplacement['TOKEN:'.strtoupper($sAttribute).''] = sprintf(gT("Token attribute: %s"), $aAttribute['description']);
}
switch ($sType)
{
case 'invitation' :
case 'reminder' :
case 'registration' :
// Replaced when sending email (registration too ?)
$aReplacement["EMAIL"] = gT("Email from the token");
$aReplacement["FIRSTNAME"] = gT("First name from token");
$aReplacement["LASTNAME"] = gT("Last name from token");
$aReplacement["LANGUAGE"] = gT("language of token");
$aReplacement["OPTOUTURL"] = gT("URL for a respondent to opt-out of this survey");
$aReplacement["OPTINURL"] = gT("URL for a respondent to opt-in to this survey");
$aReplacement["SURVEYURL"] = gT("URL of the survey");
foreach ($aAttributes as $sAttribute=>$aAttribute)
{
$aReplacement[''.strtoupper($sAttribute).''] = sprintf(gT("Token attribute: %s"), $aAttribute['description']);
}
break;
case 'confirmation' :
$aReplacement["EMAIL"] = gT("Email from the token");
$aReplacement["FIRSTNAME"] = gT("First name from token");
$aReplacement["LASTNAME"] = gT("Last name from token");
$aReplacement["SURVEYURL"] = gT("URL of the survey");
foreach ($aAttributes as $sAttribute=>$aAttribute)
{
$aReplacement[''.strtoupper($sAttribute).''] = sprintf(gT("Token attribute: %s"), $aAttribute['description']);
}
// $moveResult = LimeExpressionManager::NavigateForwards(); // Seems OK without, nut need $LEM::StartSurvey
break;
case 'admin_notification' :
case 'admin_detailed_notification' :
$aReplacement["RELOADURL"] = gT("Reload URL");
$aReplacement["VIEWRESPONSEURL"] = gT("View response URL");
$aReplacement["EDITRESPONSEURL"] = gT("Edit response URL");
$aReplacement["STATISTICSURL"] = gT("Statistics URL");
$aReplacement["ANSWERTABLE"] = gT("Answers from this response");
// $moveResult = LimeExpressionManager::NavigateForwards(); // Seems OK without, nut need $LEM::StartSurvey
break;
default:
throw new CHttpException(400, gT('Invalid type.'));
break;
}
$aData = array();
//$oSurveyLanguage=SurveyLanguageSetting::model()->find("surveyls_survey_id=:sid and surveyls_language=:language",array(":sid"=>$iSurveyId,":language"=>$sLang));
$aExpressions = array();
foreach ($aTypeAttributes[$sType] as $key=>$aAttribute)
{
$sAttribute = $aAttribute['attribute'];
// Email send do : templatereplace + ReplaceField to the Templatereplace done : we need 2 in one
// $LEM::ProcessString($oSurveyLanguage->$sAttribute,null,$aReplacement,false,1,1,false,false,true); // This way : ProcessString don't replace coreReplacements
$aExpressions[$key] = array(
'title'=>$aAttribute['title'],
'expression'=> $this->getHtmlExpression($aSurveyInfo[$sAttribute], $aReplacement, __METHOD__),
);
}
$aData['aExpressions'] = $aExpressions;
$this->getController()->layout = $this->layout;
$this->getController()->pageTitle = sprintf(gT("Validate expression in email : %s"), $sType);
$this->getController()->render("/admin/expressions/validationList", $aData);
}
/**
* Get the complete HTML from a string
* @param string $sExpression : the string to parse
* @param array $aReplacement : optionnal array of replacemement
* @param string $sDebugSource : optionnal debug source (for templatereplace)
* @uses ExpressionValidate::$iSurveyId
* @uses ExpressionValidate::$sLang
*
* @author Denis Chenu
* @version 1.0
*/
private function getHtmlExpression($sExpression, $aReplacement = array(), $sDebugSource = __CLASS__)
{
$LEM = LimeExpressionManager::singleton();
$LEM::SetDirtyFlag(); // Not sure it's needed
$LEM::SetPreviewMode('logic');
$aReData = array();
if ($this->iSurveyId)
{
$LEM::StartSurvey($this->iSurveyId, 'survey', array('hyperlinkSyntaxHighlighting'=>true)); // replace QCODE
$aReData['thissurvey'] = getSurveyInfo($this->iSurveyId, $this->sLang);
}
// TODO : Find error in class name, style etc ....
// need: templatereplace without any filter and find if there are error but $bHaveError=$LEM->em->HasErrors() is Private
$oFilter = new CHtmlPurifier();
templatereplace($oFilter->purify(viewHelper::filterScript($sExpression)), $aReplacement, $aReData, $sDebugSource, false, null, array(), true);
return $LEM::GetLastPrettyPrintExpression();
}
}