public function get($code, $default = null) { if (!$code) { return $default; } $setting = SettingModel::find()->where(['code' => $code])->one(); if ($setting) { return $setting->value; } else { return $default; } }
public function actionIndex() { //if(!Yii::$app->user->can('readPost')) throw new HttpException(403, 'No Auth'); if (Yii::$app->request->isPost) { $setting = Yii::$app->request->post('Setting'); foreach ($setting as $key => $value) { Setting::updateAll(['value' => $value], ['code' => $key]); } Yii::$app->session->setFlash('alert', ['body' => \Yii::t('backend', 'Settings has been successfully saved'), 'options' => ['class' => 'alert-success']]); $tabHash = Yii::$app->request->post('tabHash', ''); return $this->refresh($tabHash); } $settingParent = Setting::find()->where(['parent_id' => 0])->orderBy(['sort_order' => SORT_ASC])->all(); return $this->render('index', ['settingParent' => $settingParent]); }
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; use yii\helpers\ArrayHelper; use itzen\setting\models\Setting; use itzen\setting\Module; /* @var $this yii\web\View */ $this->title = Module::t('setting', 'Setting'); $this->params['breadcrumbs'][] = $this->title; $items = []; foreach ($settingParent as $parent) { $item['label'] = Module::t('setting', $parent->name); $str = ''; $children = Setting::find()->where(['parent_id' => $parent->id])->orderBy(['sort_order' => SORT_ASC, 'id' => SORT_ASC])->all(); foreach ($children as $child) { $str .= '<div class="form-group field-blogcatalog-parent_id"><label class="col-lg-2 control-label" for="blogcatalog-parent_id">' . Module::t('setting', $child->name) . '</label><div class="col-lg-10">'; if ($child->type == 'text') { $str .= Html::textInput("Setting[{$child->code}]", $child->value, ["class" => "form-control"]); } elseif ($child->type == 'textarea') { $str .= Html::textarea("Setting[{$child->code}]", $child->value, ["class" => "form-control"]); } elseif ($child->type == 'email') { $str .= Html::textInput("Setting[{$child->code}]", $child->value, ["class" => "form-control email"]); } elseif ($child->type == 'password') { $str .= Html::passwordInput("Setting[{$child->code}]", $child->value, ["class" => "form-control"]); } elseif ($child->type == 'select') { $options = []; $arrayOptions = explode(',', $child->store_range); foreach ($arrayOptions as $option) { $options[$option] = Module::t('setting', $option); }