Пример #1
0
 public function field($model, $attribute, $options = [])
 {
     if (!isset($options['labelOptions']['label'])) {
         $options['labelOptions']['label'] = Html::encode($model->getAttributeLabel($attribute)) . ':';
     }
     return parent::field($model, $attribute, $options);
 }
Пример #2
0
 /**
  * @inheritdoc
  */
 public function field($model, $attribute, $options = [])
 {
     if (!array_key_exists('class', $options)) {
         $type = null;
         if (array_key_exists('type', $options)) {
             $type = $options['type'];
             unset($options['type']);
         } else {
             // schema
             if ($model instanceof ActiveRecord) {
                 $columnSchema = $model::getTableSchema()->getColumn($attribute);
                 if ($columnSchema) {
                     $type = $columnSchema->type;
                     if (in_array($columnSchema->type, [Schema::TYPE_PK, Schema::TYPE_BIGPK, Schema::TYPE_SMALLINT, Schema::TYPE_INTEGER, Schema::TYPE_BIGINT])) {
                         if ($columnSchema->size == 1 && $columnSchema->unsigned) {
                             $type = Schema::TYPE_BOOLEAN;
                         } else {
                             $options = array_merge($options, ['size' => $columnSchema->size, 'unsigned' => $columnSchema->unsigned]);
                         }
                     } elseif ($columnSchema->type == Schema::TYPE_STRING) {
                         $options['size'] = $columnSchema->size;
                     } elseif (in_array($columnSchema->type, [Schema::TYPE_DECIMAL, Schema::TYPE_MONEY])) {
                         $options = array_merge($options, ['size' => $columnSchema->size, 'scale' => $columnSchema->scale, 'unsigned' => $columnSchema->unsigned]);
                     }
                 }
             }
             // rules
             foreach ($model->rules() as $rule) {
                 if (is_array($rule[0]) && in_array($attribute, $rule[0]) || $rule[0] == $attribute) {
                     if (in_array($rule[1], ['email', 'url'])) {
                         $type = $rule[1];
                     } elseif ($rule[1] == 'string') {
                         if (is_null($type)) {
                             $type = $rule[1];
                         }
                         if (array_key_exists('max', $rule)) {
                             $options['size'] = $rule['max'];
                         }
                         if (array_key_exists('length', $rule)) {
                             if (is_array($rule['length'])) {
                                 if (count($rule['length']) == 2) {
                                     $options['size'] = $rule['length'][1];
                                 }
                             } else {
                                 $options['size'] = $rule['length'];
                             }
                         }
                     }
                 }
             }
         }
         if (!is_null($type)) {
             $options['class'] = 'yii\\mozayka\\form\\fields\\' . ucfirst($type) . 'Field';
         }
     }
     return parent::field($model, $attribute, $options);
 }
Пример #3
0
 /**
  * Scan directory with templates and show them in dropdown list
  *
  * @param \yii\bootstrap\ActiveForm $form
  *
  * @return string
  */
 public function layoutSelector($form)
 {
     $items = [];
     $pathToTemplates = Yii::getAlias('@app/templates/');
     Yii::$app->assetManager->publish($pathToTemplates);
     $assetUrl = Yii::$app->assetManager->getPublishedUrl($pathToTemplates);
     $layoutFolders = scandir($pathToTemplates);
     foreach ($layoutFolders as $layoutFolder) {
         if (!in_array($layoutFolder, ['.', '..']) && is_dir($pathToTemplates . $layoutFolder)) {
             $items[$layoutFolder] = Html::img($assetUrl . '/' . $layoutFolder . '/backend_image.png');
         }
     }
     return $form->field($this, 'layout')->radioList($items);
 }
Пример #4
0
 public function field($model, $attribute, $options = [])
 {
     $fields = [];
     $isMultilingualOption = isset($options['multilingual']) && $options['multilingual'];
     $isMultilingualAttribute = method_exists($model, 'isMultilingual') && $model->isMultilingual() && $model->hasLangAttribute($attribute);
     if ($isMultilingualOption || $isMultilingualAttribute) {
         $languages = array_keys(Yii::$app->yee->languages);
         foreach ($languages as $language) {
             $fields[] = parent::field($model, $attribute, array_merge($options, ['language' => $language]));
         }
     } else {
         return parent::field($model, $attribute, $options);
     }
     return new MultilingualFieldContainer(['fields' => $fields]);
 }
Пример #5
0
 /**
  * @param \yii\bootstrap\ActiveForm $form
  * @return string
  */
 public function formFields($form)
 {
     $itemPropClass = Kiwi::getItemPropClass();
     $html = '';
     foreach ($this->itemProps as $key => $itemProp) {
         $options = $itemProp->toArray();
         foreach ($options as $field => $value) {
             $options['data-' . str_replace('_', '-', $field)] = $value;
             unset($options[$field]);
         }
         /** @var \yii\bootstrap\ActiveField $field */
         $field = $form->field($this, $key, ['options' => $options, 'inputOptions' => $options]);
         if (in_array($itemProp->type, [$itemPropClass::ITEM_PROP_TYPE_SELECT, $itemPropClass::ITEM_PROP_TYPE_CHECKBOX])) {
             $items = [];
             foreach ($itemProp->propValues as $propValue) {
                 $items[$propValue->prop_value_id] = $propValue->name;
             }
             $itemOptions = array_merge($options, ['container' => false, 'labelOptions' => ['class' => 'radio-inline']]);
             $field = $itemProp->type == $itemPropClass::ITEM_PROP_TYPE_SELECT ? $field->inline()->radioList($items, ['itemOptions' => $itemOptions]) : $field->inline()->checkboxList($items, ['itemOptions' => $itemOptions]);
         }
         $html .= $field->render();
     }
     return $html;
 }
Пример #6
0
 /**
  * Generate field of form with require construction.
  * 
  * @param Model $model The data model.
  * @param string $attribute Attribute name.
  * @param array $options The additional configurations for the field object.
  * @return ActiveField
  */
 public function field($model, $attribute = 'value', $options = [])
 {
     $field = parent::field($model, "[{$model->id}]{$attribute}", $options);
     $field = call_user_func_array([$field, Module::typeList($model->type)], $model->options);
     return $field;
 }
Пример #7
0
 /**
  * @param \yii\bootstrap\ActiveForm $form
  * @param string                    $name
  *
  * @return \yii\bootstrap\ActiveField
  */
 public function field($form, $name)
 {
     $fieldObject = $form->field($this, $name);
     $fieldArray = $this->findField($name);
     $label = ArrayHelper::getValue($fieldArray, self::POS_RUS_NAME, '');
     if ($label != '') {
         $fieldObject->label($label);
     }
     $hint = ArrayHelper::getValue($fieldArray, self::POS_HINT, '');
     if ($hint != '') {
         $fieldObject->hint($hint);
     }
     $widget = ArrayHelper::getValue($fieldArray, 'widget', '');
     if ($widget != '') {
         $fieldObject->widget($widget[0], ArrayHelper::getValue($widget, 1, []));
     }
     return $fieldObject;
 }
Пример #8
0
    /**
     * get the config
     * @param \yii\bootstrap\ActiveForm $form
     * @param array $options
     * @return string
     */
    public function renderForm($form, $options = [])
    {
        $config = $this->getSystemConfig();
        $tabItems = [];
        foreach ($config as $tabKey => $tab) {
            $groupItems = [];
            foreach ($tab['groups'] as $groupKey => $group) {
                $groupContent = '';
                foreach ($group['fields'] as $fieldKey => $field) {
                    $inlineRadioListTemplate = "<label class = \"control-label col-sm-1\">" . $field['label'] . "</label>\n<div class=\"col-sm-11\">{input}\n{hint}\n{error}</div>";
                    $inlineCheckboxListTemplate = "<label class = \"control-label col-sm-1\">" . $field['label'] . "</label>\n<div class=\"col-sm-11\">{input}\n{hint}\n{error}</div>";
                    $options['template'] = "<label class = \"control-label col-sm-1\">" . $field['label'] . "</label>\n<div class=\"col-sm-11\">{input}\n<div style=\"width:60%\">{hint}\n</div>{error}</div>";
                    $fieldOptions = array_merge($options, ['options' => ['class' => 'form-group'], 'inputOptions' => ['name' => 'setting_code[' . $field['setting_code'] . ']']]);
                    /** @var \yii\bootstrap\ActiveField $activeField */
                    $fieldClass = Yii::createObject(SettingFields::className());
                    $fieldModel = $fieldClass::findOne(['setting_code' => $field['setting_code']]);
                    if ($field['inputType'] == 3) {
                        $fieldModel->chosen_value = json_decode($fieldModel->chosen_value, true);
                    }
                    $activeField = $form->field($fieldModel, 'chosen_value', $fieldOptions);
                    switch ($field['inputType']) {
                        case 3:
                            $activeField->inline()->checkboxList($field['value'], ['name' => 'setting_code[' . $field['setting_code'] . ']', 'template' => $inlineCheckboxListTemplate]);
                            break;
                        case 2:
                            $activeField->inline()->radioList($field['value'], ['name' => 'setting_code[' . $field['setting_code'] . ']', 'template' => $inlineRadioListTemplate]);
                            break;
                        case 1:
                            $activeField->textInput();
                            break;
                            //                        case 'select':
                            //                            $activeField->dropDownList($dataList);
                            //                            break;
                            //                        case 'textarea':
                            //                            $activeField->textarea();
                            //                            break;
                            //                        case 'password':
                            //                            $activeField->passwordInput();
                            //                            break;
                    }
                    if (isset($field['hint'])) {
                        $activeField->hint($field['hint']);
                    }
                    $groupContent .= $activeField->render();
                }
                $groupItems[$group['label']] = ['label' => $group['label'], 'content' => $groupContent];
            }
            $tabContent = Collapse::widget(['items' => $groupItems]);
            $tabItems[] = ['label' => $tab['label'], 'content' => $tabContent];
        }
        $js = <<<JS
var modelName = 'SettingModel'
\$(document).on('change', 'select, input[type=checkbox], input[type=radio]', function() {
    var name = \$(this).attr('name')
    var dataKey = name.substring(modelName.length + 1, name.length - 1);
    var dataValue = \$(this).val();
    if (\$('[data-depend-key='+dataKey+']').length) {
        \$('[data-depend-key='+dataKey+']').hide();
    }
    if (\$('[data-depend-key='+dataKey+'][data-depend-value='+dataValue+']').length) {
        \$('[data-depend-key='+dataKey+'][data-depend-value='+dataValue+']').show();
    }
});

\$('[data-depend-key]').each(function() {
    var input = \$(this);
    var valueInput = \$('[name="'+modelName+'['+input.data('depend-key')+']"]');
    if (valueInput.val() == input.data('depend-key')) {
        input.show();
    } else {
        input.hide();
    }
});
JS;
        Yii::$app->view->registerJs($js);
        return Tabs::widget(['items' => $tabItems]);
    }
Пример #9
0
 /**
  * @inheritdoc
  * @return \bright\theme\yii2\aceadmin\ActiveField
  */
 public function field($model, $attribute, $options = [])
 {
     return parent::field($model, $attribute, $options);
 }
Пример #10
0
 /**
  * @param \yii\bootstrap\ActiveForm $form
  * @param array $options
  * @return string
  */
 public function formFields($form, $options = [])
 {
     if (!$options) {
         $template = "{label}\n<div class=\"col-sm-11\">{input}\n{hint}\n{error}</div>";
         $labelOptions = ['class' => 'control-label col-sm-1'];
         $options = ['template' => $template, 'labelOptions' => $labelOptions];
     }
     $html = '';
     $html .= $form->field($this, 'username', $options);
     $html .= $form->field($this, 'email', $options);
     $html .= $form->field($this, 'password', $options)->passwordInput();
     $html .= $form->field($this, 'roles', $options)->inline()->checkboxList($this->getAttributeData('roles'));
     return $html;
 }
Пример #11
0
 /**
  * get the config
  * @param \yii\bootstrap\ActiveForm $form
  * @param array $options
  * @return array
  */
 public function formFields($form, $options = [])
 {
     if (!$options) {
         $template = "{label}\n<div class=\"col-sm-11\">{input}\n{hint}\n{error}</div>";
         $labelOptions = ['class' => 'control-label col-sm-1'];
         $options = ['template' => $template, 'labelOptions' => $labelOptions];
     }
     $groups = [];
     foreach ($this->_permissions as $moduleKey => $attributes) {
         $label = $this->_permissionsFromFile[$moduleKey]['label'];
         $content = '';
         foreach ($attributes as $attribute => $dataValue) {
             $content .= $form->field($this, $attribute, $options)->inline()->checkboxList($this->getAttributeData($attribute));
         }
         $groups[$moduleKey] = ['label' => $label, 'content' => $content];
     }
     return $groups;
 }
Пример #12
0
 /**
  * get the config
  * @param \yii\bootstrap\ActiveForm $form
  * @param array $options
  * @return string
  */
 public function formFields($form, $options = [])
 {
     if (!$options) {
         $template = "{label}\n<div class=\"col-sm-11\">{input}\n{hint}\n{error}</div>";
         $labelOptions = ['class' => 'control-label col-sm-1'];
         $options = ['template' => $template, 'labelOptions' => $labelOptions];
     }
     /** @var \core\setting\Module $settingModule */
     $settingModule = \Yii::$app->getModule('core_setting');
     $config = $settingModule->getConfigFromFile();
     $tabItems = [];
     foreach ($config as $tabKey => $tab) {
         $groupItems = [];
         foreach ($tab['groups'] as $groupKey => $group) {
             $groupContent = '';
             foreach ($group['fields'] as $fieldKey => $field) {
                 $key = implode($this->keySeparator, [$tabKey, $groupKey, $fieldKey]);
                 /** @var \yii\bootstrap\ActiveField $activeField */
                 $activeField = $form->field($this, $key, $options);
                 $data = [];
                 if (isset($field['data'])) {
                     if (is_array($field['data'])) {
                         $data = $field['data'];
                     } else {
                         list($class, $func) = explode('/', $field['data']);
                         $getClass = 'get' . ucfirst($class);
                         $data = Kiwi::$getClass()->{$func}();
                     }
                 }
                 switch ($field['type']) {
                     case 'select':
                         $activeField->dropDownList($data);
                         break;
                     case 'checkbox':
                         $activeField->inline()->checkboxList($data);
                         break;
                     case 'radio':
                         $activeField->inline()->radioList($data);
                         break;
                     case 'textarea':
                         $activeField->textarea();
                         break;
                 }
                 if (isset($field['hint'])) {
                     $activeField->hint($field['hint']);
                 }
                 $groupContent .= $activeField->render();
             }
             $groupItems[$group['label']] = ['label' => $group['label'], 'content' => $groupContent];
         }
         $tabContent = Collapse::widget(['items' => $groupItems]);
         $tabItems[] = ['label' => $tab['label'], 'content' => $tabContent];
     }
     return Tabs::widget(['items' => $tabItems]);
 }