/
labels.php
452 lines (380 loc) · 15.9 KB
/
labels.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
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
/*
* LimeSurvey
* Copyright (C) 2007-2011 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.
*/
/**
* labels
*
* @package LimeSurvey
* @author
* @copyright 2011
* @access public
*/
class labels extends Survey_Common_Action
{
/**
* routes to the correct subdir
*
* @access public
* @param string $sa
* @return void
*/
public function run($sa=null)
{
if ($sa == 'newlabelset' || $sa == 'editlabelset')
$this->route('index', array('sa', 'lid'));
}
/**
* Function responsible to import label resources from a '.zip' file.
*
* @access public
* @return void
*/
public function importlabelresources()
{
$clang = $this->getController()->lang;
$lid = returnGlobal('lid');
if (!empty($lid))
{
if (Yii::app()->getConfig('demoMode'))
$this->getController()->error($clang->gT("Demo mode only: Uploading files is disabled in this system."), $this->getController()->createUrl("admin/labels/sa/view/lid/{$lid}"));
// Create temporary directory
// If dangerous content is unzipped
// then no one will know the path
$extractdir = $this->_tempdir(Yii::app()->getConfig('tempdir'));
$zipfilename = $_FILES['the_file']['tmp_name'];
$basedestdir = Yii::app()->getConfig('uploaddir') . "/labels";
$destdir = $basedestdir . "/$lid/";
Yii::app()->loadLibrary('admin.pclzip.pclzip');
$zip = new PclZip($zipfilename);
if (!is_writeable($basedestdir))
$this->getController()->error(sprintf($clang->gT("Incorrect permissions in your %s folder."), $basedestdir), $this->getController()->createUrl("admin/labels/sa/view/lid/{$lid}"));
if (!is_dir($destdir))
mkdir($destdir);
$aImportedFilesInfo = array();
$aErrorFilesInfo = array();
if (is_file($zipfilename))
{
if ($zip->extract($extractdir) <= 0)
$this->getController()->error($clang->gT("This file is not a valid ZIP file archive. Import failed. " . $zip->errorInfo(true)), $this->getController()->createUrl("admin/labels/sa/view/lid/{$lid}"));
// now read tempdir and copy authorized files only
$folders = array('flash', 'files', 'images');
foreach ($folders as $folder)
{
list($_aImportedFilesInfo, $_aErrorFilesInfo) = $this->_filterImportedResources($extractdir . "/" . $folder, $destdir . $folder);
$aImportedFilesInfo = array_merge($aImportedFilesInfo, $_aImportedFilesInfo);
$aErrorFilesInfo = array_merge($aErrorFilesInfo, $_aErrorFilesInfo);
}
// Deletes the temp directory
rmdirr($extractdir);
// Delete the temporary file
unlink($zipfilename);
if (is_null($aErrorFilesInfo) && is_null($aImportedFilesInfo))
$this->getController()->error($clang->gT("This ZIP archive contains no valid Resources files. Import failed."), $this->getController()->createUrl("admin/labels/sa/view/lid/{$lid}"));
}
else
$this->getController()->error(sprintf($clang->gT("An error occurred uploading your file. This may be caused by incorrect permissions in your %s folder."), $basedestdir), $this->getController()->createUrl("admin/labels/sa/view/lid/{$lid}"));
$aData = array(
'aErrorFilesInfo' => $aErrorFilesInfo,
'aImportedFilesInfo' => $aImportedFilesInfo,
'lid' => $lid
);
$this->_renderWrappedTemplate('labels', 'importlabelresources_view', $aData);
}
}
/**
* Function to import a label set
*
* @access public
* @return void
*/
public function import()
{
$clang = $this->getController()->lang;
$action = returnGlobal('action');
$aViewUrls = array();
if ($action == 'importlabels')
{
Yii::app()->loadHelper('admin/import');
$sFullFilepath = Yii::app()->getConfig('tempdir') . DIRECTORY_SEPARATOR . randomChars(20);
$aPathInfo = pathinfo($_FILES['the_file']['name']);
$sExtension = !empty($aPathInfo['extension']) ? $aPathInfo['extension'] : '';
if (!@move_uploaded_file($_FILES['the_file']['tmp_name'], $sFullFilepath))
$this->getController()->error(sprintf($clang->gT("An error occurred uploading your file. This may be caused by incorrect permissions in your %s folder."), Yii::app()->getConfig('tempdir')));
$options['checkforduplicates'] = 'off';
if (isset($_POST['checkforduplicates']))
$options['checkforduplicates'] = $_POST['checkforduplicates'];
if (strtolower($sExtension) == 'csv')
$aImportResults = CSVImportLabelset($sFullFilepath, $options);
elseif (strtolower($sExtension) == 'lsl')
$aImportResults = XMLImportLabelsets($sFullFilepath, $options);
else
$this->getController()->error($clang->gT("Uploaded label set file needs to have an .lsl extension."));
unlink($sFullFilepath);
$aViewUrls['import_view'][] = array('aImportResults' => $aImportResults);
}
$this->_renderWrappedTemplate('labels', $aViewUrls);
}
/**
* Function to load new/edit labelset screen.
*
* @access public
* @param mixed $action
* @param integer $lid
* @return
*/
public function index($sa, $lid=0)
{
Yii::app()->loadHelper('surveytranslator');
$clang = $this->getController()->lang;
$lid = sanitize_int($lid);
$aViewUrls = array();
if (hasGlobalPermission('labelsets','read'))
{
if ($sa == "editlabelset" && Permission::model()->hasGlobalPermission('labelsets','update'))
{
$result = LabelSet::model()->findAllByAttributes(array('lid' => $lid));
foreach ($result as $row)
{
$row = $row->attributes;
$lbname = $row['label_name'];
$lblid = $row['lid'];
$langids = $row['languages'];
}
$aData['lbname'] = $lbname;
$aData['lblid'] = $lblid;
}
$aData['action'] = $sa;
$aData['lid'] = $lid;
if ($sa == "newlabelset" && Permission::model()->hasGlobalPermission('labelsets','create'))
{
$langids = Yii::app()->session['adminlang'];
$tabitem = $clang->gT("Create new label set");
}
else
$tabitem = $clang->gT("Edit label set");
$langidsarray = explode(" ", trim($langids)); // Make an array of it
if (isset($row['lid']))
$panecookie = $row['lid'];
else
$panecookie = 'new';
$aData['langids'] = $langids;
$aData['langidsarray'] = $langidsarray;
$aData['panecookie'] = $panecookie;
$aData['tabitem'] = $tabitem;
$aViewUrls['editlabel_view'][] = $aData;
}
$this->_renderWrappedTemplate('labels', $aViewUrls, $aData);
}
/**
* Function to view a labelset.
*
* @access public
* @param int $lid
* @return void
*/
public function view($lid = 0)
{
// Escapes the id variable
if ($lid != false)
$lid = sanitize_int($lid);
Yii::app()->session['FileManagerContext'] = "edit:label:{$lid}";
// Gets the current language
$clang = $this->getController()->lang;
$action = 'labels';
$aViewUrls = array();
$aData = array();
// Includes some javascript files
App()->getClientScript->registerScriptFile(Yii::app()->getConfig('adminscripts') . 'labels.js');
App()->getClientScript->registerPackage('jquery-json');
// Checks if user have the sufficient rights to manage the labels
if (Permission::model()->hasGlobalPermission('labelsets','read'))
{
// Get a result containing labelset with the specified id
$result = LabelSet::model()->findByAttributes(array('lid' => $lid));
// If there is label id in the variable $lid and there are labelset records in the database
$labelset_exists = !empty($result);
if ($lid && $labelset_exists)
{
// Now recieve all labelset information and display it
$aData['lid'] = $lid;
$aData['clang'] = $clang;
$aData['row'] = $result->attributes;
// Display a specific labelbar menu
$aViewUrls['labelbar_view'][] = $aData;
$rwlabelset = $result;
// Make languages array from the current row
$lslanguages = explode(" ", trim($result['languages']));
Yii::app()->loadHelper("admin/htmleditor");
$aViewUrls['output'] = PrepareEditorScript(false, $this->getController());
$criteria = new CDbCriteria;
$criteria->select = 'max(sortorder) as maxsortorder, sortorder';
$criteria->addCondition('lid = :lid');
$criteria->addCondition('language = :language');
$criteria->params = array(':lid' => $lid, ':language' => $lslanguages[0]);
$criteria->group = 'sortorder';
$maxresult = Label::model()->find($criteria);
$maxsortorder = 1;
if (!empty($maxresult))
$maxsortorder = $maxresult->maxsortorder + 1;
$i = 0;
Yii::app()->loadHelper("surveytranslator");
$results = array();
foreach ($lslanguages as $lslanguage)
{
$result = Label::model()->findAllByAttributes(array('lid' => $lid, 'language' => $lslanguage), array('order' => 'sortorder, code'));
$criteria = new CDbCriteria;
$criteria->order = 'sortorder, code';
$criteria->condition = 'lid = :lid AND language = :language';
$criteria->params = array(':lid' => $lid, ':language' => $lslanguage);
$labelcount = Label::model()->count($criteria);
$results[$i] = array();
foreach ($result as $row)
$results[$i][] = $row->attributes;
$i++;
}
$aViewUrls['labelview_view'][] = array(
'results' => $results,
'lslanguages' => $lslanguages,
'clang' => $clang,
'lid' => $lid,
'maxsortorder' => $maxsortorder,
// 'msorow' => $maxresult->sortorder,
'action' => $action,
);
}
}
$this->_renderWrappedTemplate('labels', $aViewUrls, $aData);
}
/**
* Process labels form data depending on $action.
*
* @access public
* @return void
*/
public function process()
{
if ( Permission::model()->hasGlobalPermission('labelsets','update'))
{
if (isset($_POST['method']) && get_magic_quotes_gpc())
$_POST['method'] = stripslashes($_POST['method']);
$action = returnGlobal('action');
Yii::app()->loadHelper('admin/label');
$lid = returnGlobal('lid');
if ($action == "updateset")
{
updateset($lid);
Yii::app()->session['flashmessage'] = Yii::app()->lang->gT("Label set properties sucessfully updated.");
}
if ($action == "insertlabelset")
$lid = insertlabelset();
if (($action == "modlabelsetanswers") || ($action == "ajaxmodlabelsetanswers"))
modlabelsetanswers($lid);
if ($action == "deletelabelset")
if (deletelabelset($lid))
{
Yii::app()->session['flashmessage'] = Yii::app()->lang->gT("Label set sucessfully deleted.");
$lid = 0;
}
if ($lid)
$this->getController()->redirect(array("admin/labels/sa/view/lid/" . $lid));
else
$this->getController()->redirect(array("admin/labels/sa/view"));
}
}
/**
* Multi label export
*
* @access public
* @return void
*/
public function exportmulti()
{
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('adminscripts') . 'labels.js');
$this->_renderWrappedTemplate('labels', 'exportmulti_view');
}
public function getAllSets()
{
$results = LabelSet::model()->findAll();
$output = array();
foreach($results as $row)
{
$output[$row->lid] = $row->getAttribute('label_name');
}
echo ls_json_encode($output);
}
public function ajaxSets()
{
$lid = Yii::app()->getRequest()->getPost('lid');
$answers = Yii::app()->getRequest()->getPost('answers');
$code = Yii::app()->getRequest()->getPost('code');
//Create new label set
$language = "";
foreach ($answers as $lang => $answer) {
$language .= $lang." ";
}
$language = trim($language);
if ($lid == 0)
{
$lset = new LabelSet;
$lset->label_name = sanitize_xss_string(Yii::app()->getRequest()->getPost('laname'));
$lset->languages = sanitize_xss_string($language);
$lset->save();
$lid = getLastInsertID($lset->tableName());
}
else
{
Label::model()->deleteAll('lid = :lid', array(':lid' => $lid));
}
$res = 'ok'; //optimistic
foreach($answers as $lang => $answer) {
foreach ($answer as $key => $ans)
{
$label = new Label;
$label->lid = $lid;
$label->code = $code[$key];
$label->title = $ans;
$label->sortorder = $key;
$label->language = $lang;
if(!$label->save())
$res = 'fail';
}
}
echo ls_json_encode($res);
}
/**
* Renders template(s) wrapped in header and footer
*
* @param string $sAction Current action, the folder to fetch views from
* @param string|array $aViewUrls View url(s)
* @param array $aData Data to be passed on. Optional.
*/
protected function _renderWrappedTemplate($sAction = 'labels', $aViewUrls = array(), $aData = array())
{
if (!isset($aData['display']['menu_bars']['labels']) || $aData['display']['menu_bars']['labels'] != false)
{
if (empty($aData['labelsets']))
{
$aData['labelsets'] = getLabelSets();
}
if (empty($aData['lid']))
{
$aData['lid'] = 0;
}
$aViewUrls = (array) $aViewUrls;
array_unshift($aViewUrls, 'labelsetsbar_view');
}
$aData['display']['menu_bars'] = false;
parent::_renderWrappedTemplate($sAction, $aViewUrls, $aData);
}
}