From fcc8d984e002b6b0c505847141b53a72554bc4ee Mon Sep 17 00:00:00 2001 From: Denis Chenu Date: Fri, 4 Oct 2013 16:01:47 +0200 Subject: [PATCH] Fixed issue: #08206: Add answers to the set of labels is not possible in language code for "-" eg: pt-BR. Dev: move control to rules Dev: Add isLanguage and isLanguageMulti in LSYii_Validators : maybe best is to Add an LSlang_Validators ? Dev: TODO : use new rules if possible --- application/controllers/admin/labels.php | 4 +-- application/core/LSYii_Validators.php | 39 ++++++++++++++++++++++ application/helpers/admin/label_helper.php | 31 ++++------------- application/models/LabelSet.php | 14 ++++++++ 4 files changed, 61 insertions(+), 27 deletions(-) diff --git a/application/controllers/admin/labels.php b/application/controllers/admin/labels.php index 61c813cb8a8..adf6b08e957 100644 --- a/application/controllers/admin/labels.php +++ b/application/controllers/admin/labels.php @@ -392,8 +392,8 @@ public function ajaxSets() if ($lid == 0) { $lset = new LabelSet; - $lset->label_name = sanitize_xss_string(Yii::app()->getRequest()->getPost('laname')); - $lset->languages = sanitize_xss_string($language); + $lset->label_name = Yii::app()->getRequest()->getPost('laname'); + $lset->languages = $language; $lset->save(); $lid = getLastInsertID($lset->tableName()); diff --git a/application/core/LSYii_Validators.php b/application/core/LSYii_Validators.php index 731b9165b68..e1ca0fe422d 100644 --- a/application/core/LSYii_Validators.php +++ b/application/core/LSYii_Validators.php @@ -24,6 +24,16 @@ class LSYii_Validators extends CValidator { * @var boolean */ public $isUrl=false; + /** + * Filter attribute for isLanguage + * @var boolean + */ + public $isLanguage=false; + /** + * Filter attribute for isLanguageMulti (multi language string) + * @var boolean + */ + public $isLanguageMulti=false; public function __construct() { @@ -41,6 +51,14 @@ protected function validateAttribute($object,$attribute) if ($object->$attribute== 'http://' || $object->$attribute=='https://') {$object->$attribute="";} $object->$attribute=html_entity_decode($object->$attribute, ENT_QUOTES, "UTF-8"); } + if($this->isLanguage) + { + $object->$attribute=$this->languageFilter($object->$attribute); + } + if($this->isLanguageMulti) + { + $object->$attribute=$this->multiLanguageFilter($object->$attribute); + } } /** @@ -63,4 +81,25 @@ public function xssFilter($value) ); return $filter->purify($value); } + /** + * Defines the customs validation rule for language string + * + * @param mixed $value + */ + public function languageFilter($value) + { + // Maybe use the array of language ? + return preg_replace('/[^a-z0-9-]/i', '', $value); + } + /** + * Defines the customs validation rule for multi language string + * + * @param mixed $value + */ + public function multiLanguageFilter($value) + { + $aValue=explode(" ",trim($value)); + $aValue=array_map("sanitize_languagecode",$aValue); + return implode(" ",$aValue); + } } diff --git a/application/helpers/admin/label_helper.php b/application/helpers/admin/label_helper.php index 5f55e1168b1..f0c8502eb17 100644 --- a/application/helpers/admin/label_helper.php +++ b/application/helpers/admin/label_helper.php @@ -20,12 +20,8 @@ function updateset($lid) $clang = Yii::app()->lang; // Get added and deleted languagesid arrays - if ($_POST['languageids']) - $postlanguageids = sanitize_languagecodeS($_POST['languageids']); - - if ($_POST['label_name']) - $postlabel_name = sanitize_labelname($_POST['label_name']); - + $postlanguageids=Yii::app()->getRequest()->getPost('languageids'); + $postlabel_name=Yii::app()->getRequest()->getPost('label_name'); $newlanidarray = explode(" ",trim($postlanguageids)); $oldlangidsarray = array(); @@ -96,16 +92,8 @@ function insertlabelset() // $labelsoutput.= $_POST['languageids']; For debug purposes $clang = Yii::app()->lang; - - if (!empty($_POST['languageids'])) - { - $postlanguageids=sanitize_languagecodeS($_POST['languageids']); - } - - if (!empty($_POST['label_name'])) - { - $postlabel_name=sanitize_labelname($_POST['label_name']); - } + $postlanguageids=Yii::app()->getRequest()->getPost('languageids'); + $postlabel_name=Yii::app()->getRequest()->getPost('label_name'); //postlabel_name = dbQuoteAll($postlabel_name,true); //$postlanguageids = dbQuoteAll($postlanguageids,true); @@ -118,7 +106,7 @@ function insertlabelset() $result=LabelSet::model()->insertRecords($data); if (!$result) { - safeDie("Inserting the label set failed:
".$query."
"); + Yii::app()->session['flashmessage'] = $clang->gT("Inserting the label set failed."); } else { @@ -207,17 +195,10 @@ function modlabelsetanswers($lid) } else { - $labelsoutput= "\n"; + Yii::app()->setFlashMessage($clang->gT("Can't update labels because you are using duplicated codes"),'error'); } if ($ajax){ die(); } - - if (isset($labelsoutput)) - { - echo $labelsoutput; - exit(); - } - } /** diff --git a/application/models/LabelSet.php b/application/models/LabelSet.php index 3c3c0dbfca5..daea8859155 100644 --- a/application/models/LabelSet.php +++ b/application/models/LabelSet.php @@ -49,6 +49,20 @@ public static function model($class = __CLASS__) { return parent::model($class); } + /** + * Returns this model's validation rules + * + */ + public function rules() + { + return array( + array('label_name','required'), + array('label_name','length', 'min' => 1, 'max'=>100), + array('label_name','LSYii_Validators'), + array('languages','required'), + array('languages','LSYii_Validators','isLanguageMulti'=>true), + ); + } function getAllRecords($condition=FALSE) {