Commit 2f3cd090b8f8e93712f140fcbab5d7082b4e690c
1 parent
9576f402
Setting migrate
Showing
3 changed files
with
203 additions
and
0 deletions
Show diff stats
1 | +<?php | ||
2 | + namespace backend\controllers; | ||
3 | + | ||
4 | + use common\models\Settings; | ||
5 | + use Codeception\Exception\ConfigurationException; | ||
6 | + use yii\filters\AccessControl; | ||
7 | + use yii\web\Controller; | ||
8 | + use Yii; | ||
9 | + | ||
10 | + /** | ||
11 | + * Class SettingsController | ||
12 | + * | ||
13 | + * @package artbox\core\controllers | ||
14 | + */ | ||
15 | + class SettingsController extends Controller | ||
16 | + { | ||
17 | + | ||
18 | + public function behaviors() | ||
19 | + { | ||
20 | + return [ | ||
21 | + 'access' => [ | ||
22 | + 'class' => AccessControl::className(), | ||
23 | + 'rules' => [ | ||
24 | + [ | ||
25 | + 'actions' => [ | ||
26 | + 'login', | ||
27 | + 'error', | ||
28 | + ], | ||
29 | + 'allow' => true, | ||
30 | + ], | ||
31 | + [ | ||
32 | + 'actions' => [ | ||
33 | + 'logout', | ||
34 | + 'index', | ||
35 | + ], | ||
36 | + 'allow' => true, | ||
37 | + 'roles' => [ '@' ], | ||
38 | + ], | ||
39 | + ], | ||
40 | + ], | ||
41 | + ]; | ||
42 | + } | ||
43 | + /** | ||
44 | + * @return bool|string | ||
45 | + */ | ||
46 | + public function getViewPath() | ||
47 | + { | ||
48 | + return \Yii::getAlias('@artbox/core/views/settings'); | ||
49 | + } | ||
50 | + | ||
51 | + public function actionIndex() | ||
52 | + { | ||
53 | + $model = $this->findSettings(); | ||
54 | + | ||
55 | + if ($model->load(Yii::$app->request->post()) && $model->save()) { | ||
56 | + Yii::$app->session->setFlash('success', 'Settings saved'); | ||
57 | + | ||
58 | + return $this->goHome(); | ||
59 | + } | ||
60 | + | ||
61 | + return $this->render( | ||
62 | + 'settings', | ||
63 | + [ | ||
64 | + 'model' => $model, | ||
65 | + ] | ||
66 | + ); | ||
67 | + } | ||
68 | + | ||
69 | + public function findSettings() | ||
70 | + { | ||
71 | + if ($model = Settings::find() | ||
72 | + ->one() | ||
73 | + ) { | ||
74 | + return $model; | ||
75 | + } else { | ||
76 | + throw new ConfigurationException('Settings file not found'); | ||
77 | + } | ||
78 | + } | ||
79 | + } | ||
0 | \ No newline at end of file | 80 | \ No newline at end of file |
1 | +<?php | ||
2 | + /** | ||
3 | + * @var View $this | ||
4 | + * @var Settings $model | ||
5 | + */ | ||
6 | + | ||
7 | + use common\models\Settings; | ||
8 | + use artbox\gentelella\widgets\XPanel; | ||
9 | + use yii\bootstrap\ActiveForm; | ||
10 | + use yii\bootstrap\Html; | ||
11 | + use yii\web\View; | ||
12 | + | ||
13 | + $this->title = 'Settings'; | ||
14 | + | ||
15 | + $this->params[ 'breadcrumbs' ][] = $this->title; | ||
16 | +?> | ||
17 | + | ||
18 | +<?php $panel = XPanel::begin( | ||
19 | + [ | ||
20 | + 'title' => $this->title, | ||
21 | + 'toolbar' => false, | ||
22 | + ] | ||
23 | +); ?> | ||
24 | + | ||
25 | +<div class="settings-form"> | ||
26 | + | ||
27 | + <?php | ||
28 | + $form = ActiveForm::begin(); | ||
29 | + | ||
30 | + echo $form->field($model, 'id') | ||
31 | + ->textInput(); | ||
32 | + | ||
33 | + echo $form->field($model, 'name') | ||
34 | + ->textInput(); | ||
35 | + | ||
36 | + echo $form->field($model, 'description') | ||
37 | + ->textInput(); | ||
38 | + | ||
39 | + echo $form->field($model, 'analytics') | ||
40 | + ->textarea( | ||
41 | + [ | ||
42 | + 'rows' => 11, | ||
43 | + ] | ||
44 | + ); | ||
45 | + | ||
46 | + echo Html::submitButton( | ||
47 | + 'Save', | ||
48 | + [ | ||
49 | + 'class' => 'btn btn-primary', | ||
50 | + ] | ||
51 | + ); | ||
52 | + ActiveForm::end(); | ||
53 | + ?> | ||
54 | + | ||
55 | +</div> | ||
56 | + | ||
57 | +<?php $panel::end(); ?> |
1 | +<?php | ||
2 | + namespace common\models; | ||
3 | + | ||
4 | + use yii2tech\filedb\ActiveRecord; | ||
5 | + use Yii; | ||
6 | + | ||
7 | + /** | ||
8 | + * Class Settings | ||
9 | + * | ||
10 | + * @package artbox\core\models | ||
11 | + * @property string $name | ||
12 | + * @property string $id | ||
13 | + * @property string $description | ||
14 | + * @property string $analytics | ||
15 | + */ | ||
16 | + class Settings extends ActiveRecord | ||
17 | + { | ||
18 | + /** | ||
19 | + * @inheritdoc | ||
20 | + */ | ||
21 | + public function rules() | ||
22 | + { | ||
23 | + return [ | ||
24 | + [ | ||
25 | + [ | ||
26 | + 'name', | ||
27 | + 'description', | ||
28 | + 'id', | ||
29 | + 'analytics', | ||
30 | + ], | ||
31 | + 'string', | ||
32 | + ], | ||
33 | + ]; | ||
34 | + } | ||
35 | + | ||
36 | + /** | ||
37 | + * @inheritdoc | ||
38 | + */ | ||
39 | + public static function fileName() | ||
40 | + { | ||
41 | + return 'settings'; | ||
42 | + } | ||
43 | + | ||
44 | + /** | ||
45 | + * @inheritdoc | ||
46 | + */ | ||
47 | + public function attributeLabels() | ||
48 | + { | ||
49 | + return [ | ||
50 | + 'id' => Yii::t('core', 'ID'), | ||
51 | + 'name' => Yii::t('core', 'Name'), | ||
52 | + 'description' => Yii::t('core', 'Description'), | ||
53 | + 'analytics' => Yii::t('core', 'Google Analytics Code'), | ||
54 | + ]; | ||
55 | + } | ||
56 | + | ||
57 | + /** | ||
58 | + * Get Settings model instance | ||
59 | + * | ||
60 | + * @return Settings | ||
61 | + */ | ||
62 | + public static function getInstance() | ||
63 | + { | ||
64 | + return self::findOne([ 'id' => 1 ]); | ||
65 | + } | ||
66 | + } | ||
67 | + | ||
0 | \ No newline at end of file | 68 | \ No newline at end of file |