소스 검색

Ticket #685 - unique checking in Forms engine.

AntonLV 9 년 전
부모
커밋
2bc2737b27
4개의 변경된 파일과 43개의 추가작업 그리고 2개의 파일을 삭제
  1. 28 2
      inc/classes/BxDolForm.php
  2. 13 0
      inc/classes/BxDolFormQuery.php
  3. 1 0
      modules/boonex/english/data/langs/system/en.xml
  4. 1 0
      modules/boonex/russian/data/langs/system/ru.xml

+ 28 - 2
inc/classes/BxDolForm.php

@@ -801,6 +801,7 @@ class BxDolForm extends BxDol implements iBxDolReplaceable
 
         $oChecker = new BxDolFormChecker($this->_sCheckerHelper);
         $oChecker->setFormMethod($this->aFormAttrs['method'], $aSpecificValues);
+        $oChecker->setFormParams($this->aParams);
 
         // init form with default values
 
@@ -825,7 +826,7 @@ class BxDolForm extends BxDol implements iBxDolReplaceable
 
             $oChecker->enableFormCsrfChecking(isset($this->aParams['csrf']['disable']) && $this->aParams['csrf']['disable'] === true ? false : true);
 
-            $this->_isValid = $oChecker->check($this->aInputs);
+            $this->_isValid = $oChecker->check($this->aInputs, $aValues);
 
             if (!$this->_initCheckerNestedForms ())
                 $this->_isValid = false;
@@ -1108,6 +1109,7 @@ class BxDolFormChecker
 {
     protected $_oChecker;
     protected $_sFormMethod;
+    protected $_aFormParams;
     protected $_bFormCsrfChecking;
     protected $_aSpecificValues;
 
@@ -1126,13 +1128,18 @@ class BxDolFormChecker
         $this->_aSpecificValues = $aSpecificValues;
     }
 
+    function setFormParams($aParams)
+    {
+        $this->_aFormParams = $aParams;
+    }
+    
     function enableFormCsrfChecking($bFormCsrfChecking)
     {
         $this->_bFormCsrfChecking = $bFormCsrfChecking;
     }
 
     // check function
-    function check (&$aInputs)
+    function check (&$aInputs, $aValues = array())
     {
         $oChecker = $this->_oChecker;
         $iErrors = 0;
@@ -1168,6 +1175,25 @@ class BxDolFormChecker
             if (isset(BxDolForm::$TYPES_FILE[$a['type']]))
                 $val = isset($_FILES[$a['name']]['name']) ? $_FILES[$a['name']]['name'] : '';
 
+            // check for unique
+            if(isset($aInputs[$k]['unique']) && $aInputs[$k]['unique'] && !empty($this->_aFormParams['db']['key']) && !empty($this->_aFormParams['db']['table'])) {
+                $sKey = $this->_aFormParams['db']['key'];
+                $sTable = $this->_aFormParams['db']['table'];
+                $sError = _t('_sys_form_err_not_unique_value');
+
+                if(!empty($aValues[$sKey])) {
+                    $sValDb = BxDolFormQuery::fieldGetValue($sTable, $aInputs[$k]['name'], $sKey, $aValues[$sKey]);
+                    if($val != $sValDb && !BxDolFormQuery::fieldCheckUnique($sTable, $aInputs[$k]['name'], $val)) {
+                        ++$iErrors;
+                        $aInputs[$k]['error'] = $sError;
+                    }
+                }
+                else if(!BxDolFormQuery::fieldCheckUnique($sTable, $aInputs[$k]['name'], $val)) {
+                    ++$iErrors;
+                    $aInputs[$k]['error'] = $sError;
+                }
+            }
+
             if (!isset ($a['checker']))  {
                 if (isset(BxDolForm::$TYPES_CHECKBOX[$a['type']]))
                     $aInputs[$k]['checked'] = (isset($aInputs[$k]['value']) && $aInputs[$k]['value'] == $val);

+ 13 - 0
inc/classes/BxDolFormQuery.php

@@ -97,6 +97,7 @@ class BxDolFormQuery extends BxDolDb
                 'caption' => _t($a['caption']),
                 'info' => $a['info'] ? _t($a['info']) : '',
                 'required' => $a['required'] ? true : false,
+            	'unique' => $a['unique'] ? true : false,
                 'collapsed' => $a['collapsed'] ? true : false,
                 'html' => $a['html'],
                 'attrs' => $a['attrs'] ? unserialize($a['attrs']) : false,
@@ -187,6 +188,18 @@ class BxDolFormQuery extends BxDolDb
         return $aRet;
     }
 
+    static public function fieldCheckUnique($sTable, $sField, $sValue)
+    {
+        return uriCheckUniq($sValue, $sTable, $sField);
+    } 
+
+    static public function fieldGetValue($sTable, $sField, $sFieldKey, $sFieldKeyValue)
+    {
+        return BxDolDb::getInstance()->getOne("SELECT `" . $sField . "` FROM `" . $sTable . "` WHERE `" . $sFieldKey . "`=:field_key LIMIT 1", array(
+            'field_key' => $sFieldKeyValue
+        ));
+    }
+
     public function getFormInputs()
     {
         $sQuery = $this->prepare("SELECT * FROM `sys_form_inputs` WHERE `object` = ? ORDER BY `order` ASC", $this->_aObject['object']);

+ 1 - 0
modules/boonex/english/data/langs/system/en.xml

@@ -1883,6 +1883,7 @@ If it is not enabled then please consider implement this optimization, since it
 	<string name="_sys_form_unsubscribe_input_receive_news"><![CDATA[Receive Newsletters]]></string>
 	<string name="_sys_form_unsubscribe_input_receive_updates"><![CDATA[Receive Site Updates]]></string>
 	<string name="_sys_form_unsubscribe_input_submit"><![CDATA[Submit]]></string>
+	<string name="_sys_form_err_not_unique_value"><![CDATA[The field should have unique value]]></string>
 	<string name="_sys_form_txt_select_file"><![CDATA[Select a file]]></string>
 	<string name="_sys_format_size_b"><![CDATA[{0} bytes]]></string>
 	<string name="_sys_format_size_gb"><![CDATA[{0} GB]]></string>

+ 1 - 0
modules/boonex/russian/data/langs/system/ru.xml

@@ -1881,6 +1881,7 @@
 	<string name="_sys_form_unsubscribe_input_receive_news"><![CDATA[Получать Новости]]></string>
 	<string name="_sys_form_unsubscribe_input_receive_updates"><![CDATA[Получать Обновления Сайта]]></string>
 	<string name="_sys_form_unsubscribe_input_submit"><![CDATA[Выполнить]]></string>
+	<string name="_sys_form_err_not_unique_value"><![CDATA[Поле должно иметь уникальное значение]]></string>
 	<string name="_sys_form_txt_select_file"><![CDATA[Выбрать файл]]></string>
 	<string name="_sys_format_size_b"><![CDATA[{0} байт]]></string>
 	<string name="_sys_format_size_gb"><![CDATA[{0} ГБ]]></string>