Просмотр исходного кода

Ticket #3399 - Related core changes (Forms engine).

AntonLV 5 лет назад
Родитель
Сommit
14e5cd5b54

+ 0 - 4
template/css/forms.css

@@ -52,10 +52,6 @@
     display:none;
 }
 
-.bx-form-element-wrapper:first-child {
-    margin-top:0px;
-}
-
 .bx-form-caption {
     margin-bottom:5px;
 }

+ 1 - 0
template/form_field_button.html

@@ -0,0 +1 @@
+<button __attrs__>__value__</button>

+ 5 - 5
template/form_field_custom_suggestions.html

@@ -1,8 +1,8 @@
-<div id="__id__" class="__class__">
-    __vals__
-    <bx_if:input>
-        <input type="text" class="bx-def-font-inputs bx-form-input-text" __attrs__ />
-    </bx_if:input>
+<div __attrs__>
+    <bx_repeat:vals>
+        <b class="val bx-def-color-bg-hl bx-def-round-corners">__item_unit____item_name__<input type="hidden" name="__name__[]" value="__value__" /></b>
+    </bx_repeat:vals>
+    <bx_if:input>__input__</bx_if:input>
 </div>
 <bx_if:init>
     <script language="javascript">

+ 8 - 0
template/form_field_select.html

@@ -0,0 +1,8 @@
+<select __attrs__>
+    <bx_repeat:options>
+        <bx_if:show_option_group_begin><optgroup label="__opt_label__"></bx_if:show_option_group_begin>
+        <bx_if:show_option_group_end></optgroup></bx_if:show_option_group_end>
+        <bx_if:show_option><option value="__opt_value__" __opt_attrs__<bx_if:show_selected> selected="selected"</bx_if:show_selected>>__opt_title__</option></bx_if:show_option>
+    </bx_repeat:options>
+</select>
+__content__

+ 1 - 0
template/form_field_standard.html

@@ -0,0 +1 @@
+<input __attrs__ />

+ 7 - 0
template/form_field_switcher.html

@@ -0,0 +1,7 @@
+<div class="bx-switcher-cont __class__">__checkbox__
+    <div class="bx-switcher-canvas">
+        <div class="bx-switcher-on"><i class="sys-icon check"></i></div>
+        <div class="bx-switcher-off"><i class="sys-icon times"></i></div>
+        <div class="bx-switcher-handler">&nbsp;</div>
+    </div>
+</div>

+ 1 - 0
template/form_field_textarea.html

@@ -0,0 +1 @@
+<textarea __attrs__>__value__</textarea>

+ 208 - 108
template/scripts/BxBaseFormView.php

@@ -824,7 +824,7 @@ BLAH;
         $sClassWrapper = 'bx-form-element-wrapper';
         if($isOneLine)
             $sClassWrapper .= ' ' . $sClassWrapper . '-oneline';
-        $sClassWrapper .= ' bx-def-margin-top';
+        $sClassWrapper .= ' bx-def-margin-top-auto';
 
         if (isset($aInput['name']))
             $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
@@ -1180,6 +1180,15 @@ BLAH;
      * @return string
      */
     function genInputStandard(&$aInput)
+    {
+        $aAttrs = $this->_genInputStandardAttrs($aInput);
+
+        return  $this->oTemplate->parseHtmlByName('form_field_standard.html', [
+            'attrs' => bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}")
+        ]);
+    }
+
+    protected function _genInputStandardAttrs(&$aInput)
     {
         // clone attributes for system use ;)
         $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
@@ -1221,9 +1230,7 @@ BLAH;
         if (isset($aInput['checked']) and $aInput['checked'])
             $aAttrs['checked'] = 'checked';
 
-        $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}");
-
-        return  "<input $sAttrs />\n";
+        return $aAttrs;
     }
 
     /**
@@ -1236,20 +1243,17 @@ BLAH;
     {
         $aInput['type'] = 'checkbox';
         $sCheckbox = $this->genInputStandard($aInput);
+
         $aInput['type'] = 'switcher';
 
         $sClass = 'off';
-        if (isset($aInput['checked']) and $aInput['checked'])
+        if(isset($aInput['checked']) && $aInput['checked'])
             $sClass = 'on';
 
-        return '
-            <div class="bx-switcher-cont ' . $sClass . '">' . $sCheckbox . '
-                <div class="bx-switcher-canvas">
-                    <div class="bx-switcher-on"><i class="sys-icon check"></i></div>
-                    <div class="bx-switcher-off"><i class="sys-icon times"></i></div>
-                    <div class="bx-switcher-handler">&nbsp;</div>
-                </div>
-            </div>';
+        return $this->oTemplate->parseHtmlByName('form_field_switcher.html', [
+            'class' => $sClass,
+            'checkbox' => $sCheckbox
+        ]);
     }
 
     /**
@@ -1260,19 +1264,31 @@ BLAH;
      */
     function genInputButton(&$aInput)
     {
-        // clone attributes for system use ;)
-        $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
+        $aAttrs = $this->_genInputButtonAttrs($aInput);
+
+        $sClassAdd = "bx-def-font-inputs bx-form-input-{$aInput['type']} bx-btn";
+        if($aInput['type'] == 'submit')
+            $sClassAdd .= ' bx-btn-primary';
+
+        return $this->oTemplate->parseHtmlByName('form_field_button.html', [
+            'attrs' => bx_convert_array2attrs($aAttrs, $sClassAdd),
+            'value' => $aInput['value']
+        ]);
+    }
+    
+    protected function _genInputButtonAttrs(&$aInput)
+    {
+        $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : [];
 
         // add default className to attributes
         $aAttrs['type'] = $aInput['type'];
-        if (isset($aInput['value']))
+        if(isset($aInput['value']))
             $aAttrs['value'] = $aInput['value'];
 
-        if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
-
-        $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']} bx-btn" . ('submit' == $aInput['type'] ? ' bx-btn-primary' : ''));
-
-        return  "<button $sAttrs>" . $aInput['value'] . "</button>\n";
+        if(isset($aInput['name'])) 
+            $aAttrs['name'] = $aInput['name'];
+        
+        return $aAttrs;
     }
 
     /**
@@ -1283,20 +1299,31 @@ BLAH;
      */
     function genInputTextarea(&$aInput)
     {
-        // clone attributes for system use ;)
-        $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
+        $aAttrs = $this->_genInputTextareaAttrs($aInput);
+
+        $sClassAdd = "bx-def-font-inputs bx-form-input-{$aInput['type']}";
+        if(isset($aInput['html']) && $aInput['html'] && $this->addHtmlEditor($aInput['html'], $aInput))
+              $sClassAdd .= ' bx-form-input-html';
+
+        $sValue = isset($aInput['value']) ? bx_process_output((isset($aInput['html']) && $aInput['html']) || (isset($aInput['code']) && $aInput['code']) ? $aInput['value'] : strip_tags($aInput['value']), BX_DATA_TEXT, array('no_process_macros')) : '';
+
+        return $this->oTemplate->parseHtmlByName('form_field_textarea.html', [
+            'attrs' => bx_convert_array2attrs($aAttrs, $sClassAdd),
+            'value' => $sValue
+        ]);
+    }
+
+    protected function _genInputTextareaAttrs(&$aInput)
+    {
+        $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : array();
 
         $aAttrs['name'] = $aInput['name'];
 
         // for inputs with labels generate id
         if (isset($aInput['label']))
             $aAttrs['id'] = $this->getInputId($aInput);
-
-        $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}" . ((isset($aInput['html']) and $aInput['html'] and $this->addHtmlEditor($aInput['html'], $aInput)) ? ' bx-form-input-html' : ''));
-
-        $sValue = isset($aInput['value']) ? bx_process_output((isset($aInput['html']) && $aInput['html']) || (isset($aInput['code']) && $aInput['code']) ? $aInput['value'] : strip_tags($aInput['value']), BX_DATA_TEXT, array('no_process_macros')) : '';
-
-        return "<textarea $sAttrs>$sValue</textarea>";
+        
+        return $aAttrs;
     }
 
     function isHtmlEditor($iViewMode, &$aInput)
@@ -1436,53 +1463,53 @@ BLAH;
 
     protected function genCustomInputUsernamesSuggestions ($aInput)
     {
-        $this->addCssJsUi();
+        $bDisabled = isset($aInput['attrs']['disabled']) && $aInput['attrs']['disabled'] == 'disabled';
+
+        $aAttrs = $this->_genCustomInputUsernamesSuggestionsAttrs($aInput, $bDisabled);
 
-        $sVals = '';
+        $aTmplVarsVals = [];
         if(!empty($aInput['value'])) {
             if(is_array($aInput['value'])) {
                 foreach($aInput['value'] as $sVal) {
                     if(!$sVal || !($oProfile = BxDolProfile::getInstance($sVal)))
                         continue;
 
-                   $sVals .= '<b class="val bx-def-color-bg-hl bx-def-round-corners">' . $oProfile->getUnit(0, array('template' => 'unit_wo_info')) . $oProfile->getDisplayName() . '<input type="hidden" name="' . $aInput['name'] . '[]" value="' . $sVal . '" /></b>';
+                   $aTmplVarsVals[] = [
+                       'item_unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info')),
+                       'item_name' => $oProfile->getDisplayName(),
+                       'name' => $aInput['name'],
+                       'value' => $sVal
+                   ];
                 }
-                $sVals = trim($sVals, ',');
             }
             else if(is_string($aInput['value']))
-                $sVals = $aInput['value'];
+                $aTmplVarsVals = $aInput['value'];
         }
 
-        $bDisabled = isset($aInput['attrs']['disabled']) && $aInput['attrs']['disabled'] == 'disabled';
+        $aTmplVarsInputText = [];
+        if(!$bDisabled) {
+            $aInputText = $aInput;
+            $aInputText['type'] = 'text';
+            $aInputText['value'] = '';
+            $aInputText['attrs'] = $this->_genCustomInputUsernamesSuggestionsTextAttrs($aInputText, $bDisabled);       
 
-        $sId = $aInput['name'] . time() . mt_rand(0, 100);
-        $sClass = 'bx-form-input-autotoken bx-def-font-inputs bx-form-input-text';
-        if($bDisabled)
-            $sClass .= ' bx-form-input-disabled';
-        if(!empty($aInput['attrs']['class'])) {
-            $sClass .= ' ' . $aInput['attrs']['class'];
-            unset($aInput['attrs']['class']);
+            $aTmplVarsInputText['input'] = $this->genInputStandard($aInputText);
         }
 
-        $aAttrs = array('value' => '', 'autocomplete' => 'off', 'autocapitalize' => 'off', 'autocorrect' => 'off');
-        if(isset($aInput['attrs']) && is_array($aInput['attrs']))
-            $aAttrs = array_merge($aAttrs, $aInput['attrs']);
+        $this->addCssJsUi();
 
         return $this->oTemplate->parseHtmlByName('form_field_custom_suggestions.html', array(
-            'id' => $sId,
             'name' => $aInput['name'],
-            'class' => $sClass,
-            'vals' => $sVals,
+            'attrs' => bx_convert_array2attrs($aAttrs),
+            'bx_repeat:vals' => $aTmplVarsVals,
             'bx_if:input' => array(
                 'condition' => !$bDisabled,
-                'content' => array(
-                    'attrs' => bx_convert_array2attrs($aAttrs),
-                )
+                'content' => $aTmplVarsInputText
             ),
             'bx_if:init' => array(
                 'condition' => !$bDisabled,
                 'content' => array(
-                    'id' => $sId,
+                    'id' => $aAttrs['id'],
                     'name' => $aInput['name'],
                     'url_get_recipients' => $aInput['ajax_get_suggestions'],
                     'b_img' => isset($aInput['custom']['b_img']) ? (int)$aInput['custom']['b_img'] : 1,
@@ -1494,6 +1521,35 @@ BLAH;
         ));
     }
 
+    protected function _genCustomInputUsernamesSuggestionsAttrs (&$aInput, $bDisabled = false)
+    {
+        $aAttrs = [
+            'id' => $aInput['name'] . time() . mt_rand(0, 100),
+        ];
+
+        $aAttrs['class'] = 'bx-form-input-autotoken bx-form-input-text bx-def-font-inputs';
+        if($bDisabled)
+            $aAttrs['class'] .= ' bx-form-input-disabled';
+        if(!empty($aInput['attrs']['class']))
+            $aAttrs['class'] .= ' ' . $aInput['attrs']['class'];
+
+        return $aAttrs;
+    }
+
+    protected function _genCustomInputUsernamesSuggestionsTextAttrs (&$aInput, $bDisabled = false)
+    {
+        $aAttrs = ['value' => '', 'autocomplete' => 'off', 'autocapitalize' => 'off', 'autocorrect' => 'off'];
+
+        if(!empty($aInput['attrs']) && is_array($aInput['attrs'])) {
+            if(isset($aInput['attrs']['class']))
+                unset($aInput['attrs']['class']);
+
+            $aAttrs = array_merge($aAttrs, $aInput['attrs']);
+        }
+
+        return $aAttrs;
+    }
+
     protected function genCustomViewRowValueLabels ($aInput)
     {        
         if (empty($aInput['value']) || !($oMetatags = BxDolMetatags::getObjectInstance($aInput['meta_object'])) || !$oMetatags->keywordsIsEnabled())
@@ -1617,81 +1673,125 @@ BLAH;
 
     function _genInputSelect(&$aInput, $isMultiple, $mixedCurrentVal, $sIsSelectedFunc)
     {
-        $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
-
-        $aAttrs['name'] = $aInput['name'];
-        if ($isMultiple) {
-            $aAttrs['name'] .= '[]';
-            $aAttrs['multiple'] = 'multiple';
-        }
-
-        // for inputs with labels generate id
-        if (isset($aInput['label']))
-            $aAttrs['id'] = $this->getInputId($aInput);
-
-        $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}");
+        $aOptions = [];
+        if(isset($aInput['values']) && is_array($aInput['values'])) {
+            foreach ($aInput['values'] as $sOptValue => $sOptTitle) {
+                $aOption = array(
+                    'bx_if:show_option_group_begin' => [
+                        'condition' => false,
+                        'content' => []
+                    ],
+                    'bx_if:show_option_group_end' => [
+                        'condition' => false,
+                        'content' => []
+                    ],
+                    'bx_if:show_option' => [
+                        'condition' => false,
+                        'content' => []
+                    ]
+                );
 
-        // generate options
-        $sOptions = '';
-        if (isset($aInput['values']) and is_array($aInput['values'])) {
-            foreach ($aInput['values'] as $sValue => $sTitle) {
-                $sAttrsOpt = "";
-                if(is_array($sTitle)) {
-                    if(isset($sTitle['type'])) {
-                        switch($sTitle['type']) {
+                $sOptAttrs = "";
+                if(is_array($sOptTitle)) {
+                    if(isset($sOptTitle['type'])) {
+                        switch($sOptTitle['type']) {
                             case 'group_header':
-                                $sTitle = bx_process_output($sTitle['value']);
-                                $sOptions .= <<<BLAH
-                                       <optgroup label="$sTitle">
-BLAH;
+                                $aOption = array_merge($aOption, [
+                                    'bx_if:show_option_group_begin' => [
+                                        'condition' => true,
+                                        'content' => [
+                                            'opt_label' => bx_process_output($sOptTitle['value'])
+                                        ]
+                                    ]
+                                ]);
+                                $aOptions[] = $aOption;
                                 break;
+
                             case 'group_end':
-                                $sOptions .= <<<BLAH
-                                       </optgroup>
-BLAH;
+                                $aOption = array_merge($aOption, [
+                                    'bx_if:show_option_group_end' => [
+                                        'condition' => true,
+                                        'content' => []
+                                    ]
+                                ]);
+                                $aOptions[] = $aOption;
                                 break;
                         }
+
                         continue;
                     }
 
-                    $aAttrsOpt = array();
-                    if(!empty($sTitle['attrs']) && is_array($sTitle['attrs']))
-                        $aAttrsOpt = $sTitle['attrs'];
+                    $aOptAttrs = $this->_genInputSelectOptionAttrs($sOptTitle);
+                    if(!empty($aOptAttrs))
+                        $sOptAttrs = bx_convert_array2attrs($aOptAttrs);
 
-                    if(isset($sTitle['class']))
-                        $aAttrsOpt['class'] = !empty($aAttrsOpt['class']) ? $aAttrsOpt['class'] . ' ' . $sTitle['class'] : $sTitle['class'];
-
-                    if(isset($sTitle['style']))
-                        $aAttrsOpt['style'] = !empty($aAttrsOpt['style']) ? $aAttrsOpt['style'] . ' ' . $sTitle['style'] : $sTitle['style'];
+                    $sOptValue = $sOptTitle['key'];
+                    $sOptTitle = $sOptTitle['value'];
+                }
 
-                    $sAttrsOpt = !empty($aAttrsOpt) ? bx_convert_array2attrs($aAttrsOpt) : '';
+                $aOption = array_merge($aOption, [
+                    'bx_if:show_option' => [
+                        'condition' => true,
+                        'content' => [
+                            'opt_title' => bx_process_output($sOptTitle),
+                            'opt_value' => bx_html_attribute($sOptValue),
+                            'opt_attrs' => $sOptAttrs,
+                            'bx_if:show_selected' => [
+                                'condition' => $this->$sIsSelectedFunc($sOptValue, $mixedCurrentVal),
+                                'content' => []
+                            ]
+                        ]
+                    ]
+                ]);
+
+                $aOptions[] = $aOption;
+            }
+        }
 
-                    $sValue = $sTitle['key'];
-                    $sTitle = $sTitle['value'];
-                }
-                $sValueC = bx_html_attribute($sValue);
-                $sTitleC = bx_process_output($sTitle);
+        $aAttrs = $this->_genInputSelectAttrs($aInput, $isMultiple);
 
-                $sSelected = $this->$sIsSelectedFunc($sValue, $mixedCurrentVal) ? 'selected="selected"' : '';
+        return $this->_parseInputSelect('form_field_select.html', [
+            'attrs' => bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}"),
+            'bx_repeat:options' => $aOptions,
+            'content' => !empty($aInput['content']) ? $aInput['content'] : ''
+        ]);
+    }
 
-                $sOptions .= <<<BLAH
-                   <option value="$sValueC" $sAttrsOpt $sSelected>$sTitleC</option>
-BLAH;
+    protected function _genInputSelectAttrs(&$aInput, $isMultiple)
+    {
+        $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : [];
 
-            }
+        $aAttrs['name'] = $aInput['name'];
+        if ($isMultiple) {
+            $aAttrs['name'] .= '[]';
+            $aAttrs['multiple'] = 'multiple';
         }
 
-        // generate element
-        $sCode = <<<BLAH
-            <select $sAttrs>
-                $sOptions
-            </select>
-BLAH;
+        // for inputs with labels generate id
+        if (isset($aInput['label']))
+            $aAttrs['id'] = $this->getInputId($aInput);
+        
+        return $aAttrs;
+    }
 
-        if(!empty($aInput['content']))
-            $sCode .= $aInput['content'];
+    protected function _genInputSelectOptionAttrs(&$aOption)
+    {
+        $aAttrs = [];
+        if(!empty($aOption['attrs']) && is_array($aOption['attrs']))
+            $aAttrs = $aOption['attrs'];
 
-        return $sCode;
+        if(isset($aOption['class']))
+            $aAttrs['class'] = !empty($aAttrs['class']) ? $aAttrs['class'] . ' ' . $aOption['class'] : $aOption['class'];
+
+        if(isset($aOption['style']))
+            $aAttrs['style'] = !empty($aAttrs['style']) ? $aAttrs['style'] . ' ' . $aOption['style'] : $aOption['style'];
+        
+        return $aAttrs;
+    }
+
+    protected function _parseInputSelect($sTmplName, $aTmplVars)
+    {
+        return $this->oTemplate->parseHtmlByName($sTmplName, $aTmplVars);
     }
 
     function _genInputsSet(&$aInput, $sType, $mixedCurrentVal, $sIsCheckedFunc, $sNameAppend = '')