PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/application/helpers/admin/label_helper.php

https://bitbucket.org/machaven/limesurvey
PHP | 255 lines | 160 code | 55 blank | 40 comment | 22 complexity | 4533bb90511b84239ee01db89532aaf7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, GPL-3.0, LGPL-3.0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /*
  3. * LimeSurvey
  4. * Copyright (C) 2007-2011 The LimeSurvey Project Team / Carsten Schmitz
  5. * All rights reserved.
  6. * License: GNU/GPL License v2 or later, see LICENSE.php
  7. * LimeSurvey is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. *
  13. * $Id$
  14. */
  15. //include_once("login_check.php");
  16. //Security Checked: POST/GET/SESSION/DB/returnGlobal
  17. function updateset($lid)
  18. {
  19. $clang = Yii::app()->lang;
  20. // Get added and deleted languagesid arrays
  21. if ($_POST['languageids'])
  22. $postlanguageids = sanitize_languagecodeS($_POST['languageids']);
  23. if ($_POST['label_name'])
  24. $postlabel_name = sanitize_labelname($_POST['label_name']);
  25. $newlanidarray = explode(" ",trim($postlanguageids));
  26. $oldlangidsarray = array();
  27. $labelset = Labelsets::model()->findByAttributes(array('lid' => $lid));
  28. $oldlangidsarray = explode(' ', $labelset->languages);
  29. $addlangidsarray = array_diff($newlanidarray, $oldlangidsarray);
  30. $dellangidsarray = array_diff($oldlangidsarray, $newlanidarray);
  31. // If new languages are added, create labels' codes and sortorder for the new languages
  32. $result = Label::model()->findAllByAttributes(array('lid' => $lid), array('order' => 'code, sortorder, assessment_value'));
  33. if ($result)
  34. foreach ($result as $row)
  35. $oldcodesarray[$row['code']] = array('sortorder'=> $row['sortorder'], 'assessment_value'=> $row['assessment_value']);
  36. if (isset($oldcodesarray) && count($oldcodesarray) > 0 )
  37. foreach ($addlangidsarray as $addedlangid)
  38. foreach ($oldcodesarray as $oldcode => $olddata)
  39. $sqlvalues[]= array('lid' => $lid, 'code' => $oldcode, 'sortorder' => $olddata['sortorder'], 'language' => $addedlangid, 'assessment_value' => $olddata['assessment_value']);
  40. if (isset($sqlvalues)) {
  41. foreach ($sqlvalues as $sqlvalue) {
  42. $label = new Label();
  43. foreach ($sqlvalue as $name => $value) {
  44. $label->setAttribute($name, $value);
  45. }
  46. $label->save();
  47. }
  48. }
  49. // If languages are removed, delete labels for these languages
  50. $criteria = new CDbCriteria;
  51. $criteria->addColumnCondition(array('lid' => $lid));
  52. $langcriteria = new CDbCriteria();
  53. foreach ($dellangidsarray as $dellangid)
  54. $langcriteria->addColumnCondition(array('language' => $dellangid), 'OR');
  55. $criteria->mergeWith($langcriteria);
  56. if (!empty($dellangidsarray))
  57. $result = Label::model()->deleteAll($criteria);
  58. // Update the label set itself
  59. $labelset->label_name = $postlabel_name;
  60. $labelset->languages = $postlanguageids;
  61. $labelset->save();
  62. }
  63. /**
  64. * Deletes a label set alog with its labels
  65. *
  66. * @param mixed $lid Label ID
  67. * @return boolean Returns always true
  68. */
  69. function deletelabelset($lid)
  70. {
  71. $query = "DELETE FROM {{labels}} WHERE lid=$lid";
  72. $result = Yii::app()->db->createCommand($query)->execute();
  73. $query = "DELETE FROM {{labelsets}} WHERE lid=$lid";
  74. $result = Yii::app()->db->createCommand($query)->execute();
  75. return true;
  76. }
  77. function insertlabelset()
  78. {
  79. //global $labelsoutput;
  80. // $labelsoutput.= $_POST['languageids']; For debug purposes
  81. $clang = Yii::app()->lang;
  82. if (!empty($_POST['languageids']))
  83. {
  84. $postlanguageids=sanitize_languagecodeS($_POST['languageids']);
  85. }
  86. if (!empty($_POST['label_name']))
  87. {
  88. $postlabel_name=sanitize_labelname($_POST['label_name']);
  89. }
  90. //postlabel_name = dbQuoteAll($postlabel_name,true);
  91. //$postlanguageids = dbQuoteAll($postlanguageids,true);
  92. $data = array(
  93. 'label_name' => $postlabel_name,
  94. 'languages' => $postlanguageids
  95. );
  96. //$query = "INSERT INTO ".db_table_name('labelsets')." (label_name,languages) VALUES ({$postlabel_name},{$postlanguageids})";
  97. $result=Labelsets::model()->insertRecords($data);
  98. if (!$result)
  99. {
  100. safeDie("Inserting the label set failed:<br />".$query."<br />");
  101. }
  102. else
  103. {
  104. return $result;
  105. }
  106. }
  107. function modlabelsetanswers($lid)
  108. {
  109. //global $labelsoutput;
  110. $clang = Yii::app()->lang;
  111. $ajax = false;
  112. if (isset($_POST['ajax']) && $_POST['ajax'] == "1"){
  113. $ajax = true;
  114. }
  115. if (!isset($_POST['method'])) {
  116. $_POST['method'] = $clang->gT("Save");
  117. }
  118. $sPostData = Yii::app()->getRequest()->getPost('dataToSend');
  119. $sPostData=str_replace("\t", '', $sPostData);
  120. if (get_magic_quotes_gpc())
  121. {
  122. $data = json_decode(stripslashes($sPostData));
  123. }
  124. else
  125. {
  126. $data = json_decode($sPostData);
  127. }
  128. if ($ajax)
  129. $lid = insertlabelset();
  130. if (count(array_unique($data->{'codelist'})) == count($data->{'codelist'}))
  131. {
  132. $query = "DELETE FROM {{labels}} WHERE lid = '$lid'";
  133. $result = Yii::app()->db->createCommand($query)->execute();
  134. foreach($data->{'codelist'} as $index=>$codeid){
  135. $codeObj = $data->$codeid;
  136. $actualcode = $codeObj->{'code'};
  137. //$codeid = dbQuoteAll($codeid,true);
  138. $assessmentvalue = (int)($codeObj->{'assessmentvalue'});
  139. foreach($data->{'langs'} as $lang){
  140. $strTemp = 'text_'.$lang;
  141. $title = $codeObj->$strTemp;
  142. $p = new CHtmlPurifier();
  143. if (Yii::app()->getConfig('filterxsshtml'))
  144. $title = $p->purify($title);
  145. else
  146. $title = html_entity_decode($title, ENT_QUOTES, "UTF-8");
  147. // Fix bug with FCKEditor saving strange BR types
  148. $title = fixCKeditorText($title);
  149. $sort_order = $index;
  150. $insertdata = array(
  151. 'lid' => $lid,
  152. 'code' => $actualcode,
  153. 'title' => $title,
  154. 'sortorder' => $sort_order,
  155. 'assessment_value' => $assessmentvalue,
  156. 'language' => $lang
  157. );
  158. //$query = "INSERT INTO ".db_table_name('labels')." (`lid`,`code`,`title`,`sortorder`, `assessment_value`, `language`)
  159. // VALUES('$lid',$actualcode,$title,$sort_order,$assessmentvalue,$lang)";
  160. $result = Yii::app()->db->createCommand()->insert('{{labels}}', $insertdata);
  161. }
  162. }
  163. Yii::app()->session['flashmessage'] = $clang->gT("Labels sucessfully updated");
  164. }
  165. else
  166. {
  167. $labelsoutput= "<script type=\"text/javascript\">\n<!--\n alert(\"".$clang->gT("Can't update labels because you are using duplicated codes","js")."\")\n //-->\n</script>\n";
  168. }
  169. if ($ajax){ die(); }
  170. if (isset($labelsoutput))
  171. {
  172. echo $labelsoutput;
  173. exit();
  174. }
  175. }
  176. /**
  177. * Function rewrites the sortorder for a label set
  178. *
  179. * @param mixed $lid Label set ID
  180. */
  181. function fixorder($lid) {
  182. $clang = Yii::app()->lang;
  183. $qulabelset = "SELECT * FROM {{labelsets}} WHERE lid=$lid";
  184. $rslabelset = Yii::app()->db->createCommand($qulabelset)->query();
  185. $rwlabelset=$rslabelset->read();
  186. $lslanguages=explode(" ", trim($rwlabelset['languages']));
  187. foreach ($lslanguages as $lslanguage)
  188. {
  189. $query = "SELECT lid, code, title, sortorder FROM {{labels}} WHERE lid=:lid and language=:lang ORDER BY sortorder, code";
  190. $result = Yii::app()->createCommand($query)->query(array(':lid' => $lid, ':lang' => $lslanguage)); // or safeDie("Can't read labels table: $query // (lid=$lid, language=$lslanguage) "
  191. $position=0;
  192. foreach ($result->readAll() as $row)
  193. {
  194. $position=sprintf("%05d", $position);
  195. $query2="UPDATE {{labels}} SET sortorder='$position' WHERE lid=".$row['lid']." AND code=".$row['code']." AND title=".$row['title']." AND language='$lslanguage' ";
  196. $result2=Yii::app()->db->createCommand($query2)->execute();
  197. $position++;
  198. }
  199. }
  200. }