-
Notifications
You must be signed in to change notification settings - Fork 988
/
viewHelper.php
258 lines (246 loc) · 9.35 KB
/
viewHelper.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
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* LimeSurvey
* Copyright (C) 2007-2013 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*
*/
/**
* General helper class for generating views.
*/
class viewHelper
{
/**
* getImageLink returns HTML needed for a link that consists of only an image with alt text.
*
* Usage: getImageLink('test.png', 'controller/action/params', 'Your description', 'optionalClass', '_blank')
*
* @return string
* @param string $imgName the name of the image to use, adminImageUrl will be added to it
* @param string $linkUrl Url we want to go to, uses CController->createUrl()
* @param string $linkTxt Text to show for the link
* @param string $linkTarget Optional target to use for the link
* @param string $linkclass Optional class to add to the link
* @param array $attribs Optional array of attirbutes to set on the link
*/
public static function getImageLink($imgName, $linkUrl, $linkTxt, $linkTarget = null, $linkClass = 'imagelink', $attribs = array())
{
if (!is_null($linkUrl) && $linkUrl != '#') {
$linkUrl = Yii::app()->getController()->createUrl($linkUrl);
} else {
$linkUrl = "#";
}
$output = '<a href="' . $linkUrl;
if (!empty($linkClass)) {
$output .= '" class="' . $linkClass . '"';
}
if (!empty($linkTarget)) {
$output .= ' target="' . $linkTarget . '"';
}
if (!empty($attribs)) {
foreach($attribs as $attrib => $value) {
$output .= ' ' . $attrib . '="' . str_replace('"', '"', $value) . '"';
}
}
$output .= '><img src="' . Yii::app()->getConfig('adminimageurl') . $imgName . '" alt="' . $linkTxt. '" title="' . $linkTxt. '"></a>';
return $output;
}
/**
* getFieldText returns complete field information text.
*
* Usage: getFieldText($aField, $aOption)
*
* @return string
* @param array $aField the field information from createFieldMap
* @param array $aOption option (see default)
*/
public static function getFieldText($aField, $aOption=array())
{
// Default options
$aDefaultOption=array(
'flat'=>true,
'separator'=>array('(',')'),
'abbreviated'=>false,
'afterquestion'=>" ",
'ellipsis'=>'...',// more for export or option, less for HTML display
);
$aOption=array_merge($aDefaultOption,$aOption);
$sQuestionText="";// Allways return a string
if(isset($aField['fieldname']))
{
$sQuestionText=self::flatEllipsizeText($aField['question'],$aOption['flat'],$aOption['abbreviated'],$aOption['ellipsis']).$aOption['afterquestion'];
// Did this question have sub question, maybe not needed, think only isset is OK
$bHaveSubQuestion=isset($aField['aid']) && $aField['aid']!="";
if(isset($aField['subquestion']) && $bHaveSubQuestion)
{
$sQuestionText.=self::putSeparator(self::flatEllipsizeText($aField['subquestion'],$aOption['flat'],$aOption['abbreviated'],$aOption['ellipsis']),$aOption['separator']);
}
if(isset($aField['subquestion1']) && $bHaveSubQuestion)
{
$sQuestionText.=self::putSeparator(self::flatEllipsizeText($aField['subquestion1'],$aOption['flat'],$aOption['abbreviated'],$aOption['ellipsis']),$aOption['separator']);
}
if(isset($aField['subquestion2']) && $bHaveSubQuestion)
{
$sQuestionText.=self::putSeparator(self::flatEllipsizeText($aField['subquestion2'],$aOption['flat'],$aOption['abbreviated'],$aOption['ellipsis']),$aOption['separator']);
}
if(isset($aField['scale']) && $aField['scale'])
{
$sQuestionText.=self::putSeparator(self::flatEllipsizeText($aField['scale'],$aOption['flat'],$aOption['abbreviated'],$aOption['ellipsis']),$aOption['separator']);;
}
}
return $sQuestionText;
}
/**
* getFieldCode returns complete field information code.
*
* Usage: getFieldCode($aField, $aOption)
*
* @return string
* @param array $aField the field information from createFieldMap
* @param array $aOption option for filtering
*/
public static function getFieldCode($aField, $aOption=array())
{
// Default options
$aDefaultOption=array(
'LEMcompat'=>false,
'separator'=>array('[',']'),
);
$aOption=array_merge($aDefaultOption,$aOption);
if($aOption['LEMcompat']){$aOption['separator']="_";}
$sQuestionCode="";
if(isset($aField['fieldname']))
{
if(isset($aField['title']) && $aField['title'])
{
$sQuestionCode=$aField['title'];
if(isset($aField['aid']) && $aField['aid']!="")
{
$sQuestionCode.=self::putSeparator($aField['aid'],$aOption['separator']);
}
if(isset($aField['scale']) && $aField['scale'])
{
if($aOption['LEMcompat']){
$scalenum=intval($aField['scale_id']);
}else{
$scalenum=intval($aField['scale_id'])+1;
}
$sQuestionCode.=self::putSeparator($scalenum,$aOption['separator']);
}
}
else
{
$sQuestionCode=$aField['fieldname'];
}
}
return $sQuestionCode;
}
/**
* Return a string with the good separator before and after
*
* @param $sString :the string
* @param : string/array : the string to put before of the array (before,after)
*/
public static function putSeparator($sString,$separator){
if(is_array($separator))
{
return $separator[0].$sString.$separator[1];
}
else
{
return $separator.$sString;
}
}
/**
* Return a string fixed according to option
*
* @param $sString :the string
* @param $bFlat : flattenText or not : completely flat (not like flattenText from common_helper)
* @param $iAbbreviated : max string text (if true : allways flat), 0 or false : don't abbreviated
* @param $sEllipsis if abbreviated : the char to put at end (or middle)
* @param $fPosition if abbreviated position to split (in % : 0 to 1)
*
*/
public static function flatEllipsizeText($sString,$bFlat,$iAbbreviated,$sEllipsis='...',$fPosition = 1){
if($bFlat || $iAbbreviated)
{
$sString = flattenText($sString);
}
if($iAbbreviated)
{
$sString = ellipsize($sString, $iAbbreviated, $fPosition, $sEllipsis);
}
return $sString;
}
/**
* disableLogging deactivate default logging in HTML if we don't produce HTML
*
* Usage: disableLogging()
*
* @return void
* @author Menno Dekker
*/
public static function disableHtmlLogging(){
foreach (App()->log->routes as $route)
{
$route->enabled = $route->enabled && !($route instanceOf CWebLogRoute);
}
}
/**
* Deactivate script but show it for debuging
* This only filter script tag
* @todo : filter inline javascript (onclick etc ..., but don't filter EM javascript)
* Maybe doing it directly in LEM->GetLastPrettyPrintExpression();
* @param string : Html to filter
* @return string
* @author Denis Chenu
*/
public static function filterScript($sHtml){
return preg_replace('#<script(.*?)>(.*?)</script>#is', '<pre><script>${2}</script></pre>', $sHtml);
}
/**
* Show purified html
* @param string : Html to purify
* @return string
*/
public static function purified($sHtml){
$oPurifier = new CHtmlPurifier();
return $oPurifier->purify($sHtml);
}
/**
* Show clean string, leaving ONLY tag for Expression
* @param string : Html to clean
* @return string
*/
public static function stripTagsEM($sHtml)
{
$oPurifier = new CHtmlPurifier();
$oPurifier->options = array(
'HTML.Allowed'=>'span[title|class],a[class|title|href]',
'Attr.AllowedClasses'=>array(
'em-expression',
'em-haveerror',
'em-var-string',
'em-function',
'em-var-static',
'em-var-before',
'em-var-after',
'em-var-inpage',
'em-var-error',
'em-assign',
'em-error',
),
'URI.AllowedSchemes'=>array( // Maybe only local ?
'http' => true,
'https' => true,
)
);
return $oPurifier->purify($sHtml);
}
}