/application/helpers/admin/label_helper.php

https://bitbucket.org/sammousa/valuematchbv-ls2 · PHP · 250 lines · 168 code · 45 blank · 37 comment · 23 complexity · dedaa0419ed4680e16fe9fa9de0709a6 MD5 · raw file

  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. $aErrors=array();
  131. if (count(array_unique($data->{'codelist'})) == count($data->{'codelist'}))
  132. {
  133. $query = "DELETE FROM {{labels}} WHERE lid = '$lid'";
  134. $result = Yii::app()->db->createCommand($query)->execute();
  135. foreach($data->{'codelist'} as $index=>$codeid){
  136. $codeObj = $data->$codeid;
  137. $actualcode = $codeObj->{'code'};
  138. //$codeid = dbQuoteAll($codeid,true);
  139. $assessmentvalue = (int)($codeObj->{'assessmentvalue'});
  140. foreach($data->{'langs'} as $lang){
  141. $strTemp = 'text_'.$lang;
  142. $title = $codeObj->$strTemp;
  143. $sortorder = $index;
  144. $oLabel = new Label();
  145. $oLabel->lid=$lid;
  146. $oLabel->code=$actualcode;
  147. $oLabel->title=$title;
  148. $oLabel->sortorder=$sortorder;
  149. $oLabel->assessment_value=$assessmentvalue;
  150. $oLabel->language=$lang;
  151. if($oLabel->validate())
  152. {
  153. $result=$oLabel->save();
  154. }
  155. else
  156. {
  157. $aErrors[]=$oLabel->getErrors();
  158. }
  159. }
  160. }
  161. if(count($aErrors))
  162. {
  163. Yii::app()->session['flashmessage'] = $clang->gT("Labels updated but with some error");
  164. }
  165. else
  166. {
  167. Yii::app()->session['flashmessage'] = $clang->gT("Labels sucessfully updated");
  168. }
  169. }
  170. else
  171. {
  172. $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";
  173. }
  174. if ($ajax){ die(); }
  175. if (isset($labelsoutput))
  176. {
  177. echo $labelsoutput;
  178. exit();
  179. }
  180. }
  181. /**
  182. * Function rewrites the sortorder for a label set
  183. *
  184. * @param mixed $lid Label set ID
  185. */
  186. function fixorder($lid) {
  187. $clang = Yii::app()->lang;
  188. $qulabelset = "SELECT * FROM {{labelsets}} WHERE lid=$lid";
  189. $rslabelset = Yii::app()->db->createCommand($qulabelset)->query();
  190. $rwlabelset=$rslabelset->read();
  191. $lslanguages=explode(" ", trim($rwlabelset['languages']));
  192. foreach ($lslanguages as $lslanguage)
  193. {
  194. $query = "SELECT lid, code, title, sortorder FROM {{labels}} WHERE lid=:lid and language=:lang ORDER BY sortorder, code";
  195. $result = Yii::app()->createCommand($query)->query(array(':lid' => $lid, ':lang' => $lslanguage)); // or safeDie("Can't read labels table: $query // (lid=$lid, language=$lslanguage) "
  196. $position=0;
  197. foreach ($result->readAll() as $row)
  198. {
  199. $position=sprintf("%05d", $position);
  200. $query2="UPDATE {{labels}} SET sortorder='$position' WHERE lid=".$row['lid']." AND code=".$row['code']." AND title=".$row['title']." AND language='$lslanguage' ";
  201. $result2=Yii::app()->db->createCommand($query2)->execute();
  202. $position++;
  203. }
  204. }
  205. }