AntonLV 8 лет назад
Родитель
Сommit
a6e5191c28

+ 11 - 0
modules/boonex/polls/classes/BxPollsDb.php

@@ -125,6 +125,17 @@ class BxPollsDb extends BxBaseModTextDb
         $sSql = "DELETE FROM `" . $CNF['TABLE_SUBENTRIES'] . "` WHERE " . $this->arrayToSQL($aParams, " AND ");
         return $this->query($sSql);
     }
+
+    public function deleteSubentryById($mixedId)
+    {
+        $CNF = &$this->_oConfig->CNF;
+
+        if(!is_array($mixedId))
+            $mixedId = array($mixedId);
+
+        $sSql = "DELETE FROM `" . $CNF['TABLE_SUBENTRIES'] . "` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")";
+        return $this->query($sSql);
+    }
 }
 
 /** @} */

+ 28 - 18
modules/boonex/polls/classes/BxPollsFormEntry.php

@@ -13,10 +13,7 @@ class BxPollsFormEntryCheckerHelper extends BxDolFormCheckerHelper
 {
     static public function checkAvailSubentries ($s)
     {
-        if(!self::_isFullArray($s))
-            return false;
-
-        return count($s) >= 2;
+        return !self::_isEmptyArray($s) && count($s) >= 2;
     }
 }
 /**
@@ -112,9 +109,14 @@ class BxPollsFormEntry extends BxBaseModTextFormEntry
         if(!isset($this->aInputs[$sField]))
             return true;
 
-        //--- Update existed and remove empty
         $aSubentriesIds = $this->getCleanValue($sField . '_ids');
         $aSubentriesValues = $this->getCleanValue($sField);
+
+        //--- Remove deleted
+        $aSubentriesDb = $this->_oModule->_oDb->getSubentries(array('type' => 'entry_id_pairs', 'entry_id' => $iContentId));
+        $this->_oModule->_oDb->deleteSubentryById(array_diff(array_keys($aSubentriesDb), $aSubentriesIds));
+
+        //--- Update existed and remove empty
         foreach($aSubentriesIds as $iIndex => $iId)
             if(!empty($aSubentriesValues[$iIndex]))
                 $this->_oModule->_oDb->updateSubentry(array('title' => bx_process_input($aSubentriesValues[$iIndex])), array('id' => (int)$iId));
@@ -142,22 +144,28 @@ class BxPollsFormEntry extends BxBaseModTextFormEntry
 
     protected function genCustomInputSubentries(&$aInput)
     {
-        $sResult = '';
+        $sJsObject = $this->_oModule->_oConfig->getJsObject('form');
 
-        if(empty($aInput['value']) || !is_array($aInput['value'])) {
-            $sResult .= $this->genCustomInputSubentriesText($aInput);
-            $sResult .= $this->genCustomInputSubentriesText($aInput);
-        }
-        else
+        if(!empty($aInput['value']) && is_array($aInput['value'])) {
+            $aTmplVarsSubentries = array();
             foreach($aInput['value'] as $iKey => $sValue) {
-                $sResult .= $this->genCustomInputSubentriesText($aInput, $sValue);
+                $sInput = $this->genCustomInputSubentriesText($aInput, $sValue);
                 if(!empty($aInput['value_ids'][$iKey]))
-                    $sResult .= $this->genCustomInputSubentriesHidden($aInput, (int)$aInput['value_ids'][$iKey]);
-            }
-
-        $sResult .= $this->genCustomInputSubentriesButton($aInput);
+                    $sInput .= $this->genCustomInputSubentriesHidden($aInput, (int)$aInput['value_ids'][$iKey]);
 
-        return $sResult;
+                $aTmplVarsSubentries[] = array('js_object' => $sJsObject, 'input_text' => $sInput);
+            }
+        }
+        else 
+            $aTmplVarsSubentries = array(
+                array('js_object' => $sJsObject, 'input_text' => $this->genCustomInputSubentriesText($aInput)),
+                array('js_object' => $sJsObject, 'input_text' => $this->genCustomInputSubentriesText($aInput))
+            );
+
+        return $this->_oModule->_oTemplate->parseHtmlByName('form_subentries.html', array(
+            'bx_repeat:subentries' => $aTmplVarsSubentries,
+            'btn_add' => $this->genCustomInputSubentriesButton($aInput)
+        ));
     }
     
     protected function genCustomInputSubentriesText($aInput, $mixedValue = '')
@@ -181,11 +189,13 @@ class BxPollsFormEntry extends BxBaseModTextFormEntry
 
     protected function genCustomInputSubentriesButton($aInput)
     {
+        $sName = $aInput['name'];
+
         $aInput['type'] = 'button';
         $aInput['name'] .= '_add';
         $aInput['value'] = _t('_bx_polls_form_entry_input_subentries_add');
         $aInput['attrs']['class'] = 'bx-def-margin-sec-top';
-        $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('form') . '.addMore(this);';
+        $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('form') . ".subentryAdd(this, '" . $sName . "');";
 
         return $this->genInputButton($aInput);
     }

+ 12 - 4
modules/boonex/polls/js/form.js

@@ -19,8 +19,16 @@ function BxPollsForm(oOptions) {
     this._oRequestParams = oOptions.oRequestParams == undefined ? {} : oOptions.oRequestParams;
 }
 
-BxPollsForm.prototype.addMore = function(oButton) {
-	var oButton = $(oButton);
+BxPollsForm.prototype.subentryAdd = function(oButton, sName) {
+    var oButton = $(oButton);
 
-	oButton.before(oButton.siblings('.bx-form-input-text:first').clone().val(''));
-};
+    var oSubentry = oButton.parents('#bx-form-element-' + sName).find('.bx-form-input-subentry:first').clone();
+    oSubentry.find("input[type = 'text']").val('');
+    oSubentry.find("input[type = 'hidden']").remove();
+
+    oButton.parents('.bx-form-input-subentry-add:first').before(oSubentry);
+};
+
+BxPollsForm.prototype.subentryDelete = function(oButton) {
+    $(oButton).parents('.bx-form-input-subentry:first').remove();
+};

+ 25 - 3
modules/boonex/polls/template/css/forms.css

@@ -1,5 +1,27 @@
 @import url(../../../../base/text/template/css/forms.css);
 
-#bx-form-element-subentries .bx-form-input-wrapper-custom .bx-btn {
-	float: right;
-}
+/*-- Subentries custom element --*/
+.bx-form-input-subentry {
+    display: -webkit-flex;
+    display: flex;
+    -webkit-align-items: center;
+    align-items: center;
+}
+.bx-form-input-subentry-cell.bx-fi-sc-txt {
+    -webkit-flex: 1 1 auto;
+    -ms-flex: 1 1 auto;
+    flex: 1 1 auto;
+}
+.bx-form-input-subentry-cell.bx-fi-sc-btn {
+    -webkit-flex: 0 0 auto;
+    -ms-flex: 0 0 auto;
+    flex: 0 0 auto;
+}
+
+.bx-form-input-subentry-add {
+    text-align: right;
+}
+.bx-form-input-subentry-add .bx-form-input-button {
+    display: inline-block;
+    float: none;
+}

+ 11 - 0
modules/boonex/polls/template/form_subentries.html

@@ -0,0 +1,11 @@
+<div class="bx-form-input-subentries">
+    <bx_repeat:subentries>
+        <div class="bx-form-input-subentry bx-def-padding-thd-topbottom">
+            <div class="bx-form-input-subentry-cell bx-fi-sc-txt">__input_text__</div>
+            <div class="bx-form-input-subentry-cell bx-fi-sc-btn bx-def-padding-sec-left">
+                <a class="bx-btn" href="javascript:void(0)" onclick="javascript:__js_object__.subentryDelete(this)"><i class="sys-icon times"></i></a>
+            </div>
+        </div>
+    </bx_repeat:subentries>
+    <div class="bx-form-input-subentry-add">__btn_add__</div>
+</div>