/** * Display all config paramters for $byModule. * @return mixed */ public function actionIndex() { $models = Config::find()->where(['module' => $this->byModule])->orderBy(['id' => SORT_ASC])->indexBy('id')->all(); if (Model::loadMultiple($models, Yii::$app->request->post())) { if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return ActiveForm::validateMultiple($models, 'value'); } $result = true; foreach ($models as $model) { $result = $result && $model->save(true, ['value']); } if ($result && Module::getInstance()->configManager->clearCache()) { Yii::$app->session->setFlash('success', Module::t('SAVE_SUCCESS')); return $this->redirect(['index']); } } return $this->render('index', ['models' => $models]); }
/** * 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; }
<?php use yii\helpers\Html; use bupy7\config\Module; use bupy7\config\widgets\ActiveForm; $this->title = Module::t('SETTINGS'); $this->params['breadcrumbs'][] = $this->title; ?> <div class="page-header"> <h1><?php echo Html::encode($this->title); ?> </h1> </div> <?php $form = ActiveForm::begin(['enableClientScript' => true, 'enableClientValidation' => false, 'enableAjaxValidation' => true, 'validateOnChange' => false, 'validateOnBlur' => false]); foreach ($models as $model) { ?> <?php echo $form->field($model); } echo Html::submitButton(Module::t('SAVE'), ['class' => 'btn btn-success']); ActiveForm::end();
/** * Preparing parameters of config the application. */ protected function prepare() { if (!isset($this->_params)) { $module = Module::getInstance(); if ($module->enableCaching) { if (($this->_params = $module->cache->get([__CLASS__, 'params'])) === false) { $this->_params = Config::paramsArray(); $module->cache->set([__CLASS__, 'params'], $this->_params); } } else { $this->_params = Config::paramsArray(); } } }
/** * @inheritdoc */ public function init() { parent::init(); $this->params = Module::getInstance()->params; }
/** * Unserialize 'options' field. * Unserialize and add rules of field. * Add label of fields. */ public function afterFind() { // unserialize 'options' field $options = unserialize($this->options); if ($options === false) { $options = []; } $this->options = $options; // unserialize and add rules of field $rules = unserialize($this->rules); if ($rules === false) { $rules = []; } foreach ($rules as &$rule) { array_unshift($rule, 'value'); } $this->_rules = $rules; // add label of fields $this->_labels = ['value' => Yii::t(Module::getInstance()->messageCategory, $this->label)]; // add hint of fields $this->_hints = ['value' => Yii::t(Module::getInstance()->messageCategory, $this->hint)]; parent::afterFind(); }