From b9f919204c6e8a0d4dd90196b955568dc94c1f6b Mon Sep 17 00:00:00 2001 From: andrey Date: Wed, 9 Jun 2021 18:06:13 +0300 Subject: [PATCH] skills on main page api and admin --- backend/config/main.php | 3 + backend/modules/options/Options.php | 24 ++++ .../options/controllers/DefaultController.php | 20 +++ .../options/controllers/OptionsController.php | 127 ++++++++++++++++++ backend/modules/options/models/Options.php | 10 ++ .../modules/options/models/OptionsSearch.php | 71 ++++++++++ .../modules/options/views/default/index.php | 12 ++ .../modules/options/views/options/_form.php | 34 +++++ .../modules/options/views/options/_search.php | 35 +++++ .../modules/options/views/options/create.php | 18 +++ .../modules/options/views/options/index.php | 36 +++++ .../modules/options/views/options/update.php | 19 +++ .../modules/options/views/options/view.php | 39 ++++++ .../settings/controllers/SkillController.php | 18 +++ .../settings/models/SkillsOnMainPageForm.php | 67 +++++++++ .../modules/settings/views/skill/index.php | 1 + .../views/skill/skills-on-main-page.php | 74 ++++++++++ backend/views/layouts/left.php | 1 + common/models/Options.php | 91 +++++++++++++ common/models/Skill.php | 6 + .../m210609_102606_create_options_table.php | 31 +++++ frontend/config/main.php | 4 + frontend/modules/api/Api.php | 24 ++++ .../api/controllers/DefaultController.php | 20 +++ .../api/controllers/SkillsController.php | 36 +++++ frontend/modules/api/views/default/index.php | 12 ++ frontend/modules/api/views/skills/index.php | 9 ++ 27 files changed, 842 insertions(+) create mode 100644 backend/modules/options/Options.php create mode 100644 backend/modules/options/controllers/DefaultController.php create mode 100644 backend/modules/options/controllers/OptionsController.php create mode 100644 backend/modules/options/models/Options.php create mode 100644 backend/modules/options/models/OptionsSearch.php create mode 100644 backend/modules/options/views/default/index.php create mode 100644 backend/modules/options/views/options/_form.php create mode 100644 backend/modules/options/views/options/_search.php create mode 100644 backend/modules/options/views/options/create.php create mode 100644 backend/modules/options/views/options/index.php create mode 100644 backend/modules/options/views/options/update.php create mode 100644 backend/modules/options/views/options/view.php create mode 100644 backend/modules/settings/models/SkillsOnMainPageForm.php create mode 100644 backend/modules/settings/views/skill/skills-on-main-page.php create mode 100644 common/models/Options.php create mode 100644 console/migrations/m210609_102606_create_options_table.php create mode 100644 frontend/modules/api/Api.php create mode 100644 frontend/modules/api/controllers/DefaultController.php create mode 100644 frontend/modules/api/controllers/SkillsController.php create mode 100644 frontend/modules/api/views/default/index.php create mode 100644 frontend/modules/api/views/skills/index.php diff --git a/backend/config/main.php b/backend/config/main.php index 6a5dcb7..a8a0865 100755 --- a/backend/config/main.php +++ b/backend/config/main.php @@ -50,6 +50,9 @@ return [ 'reports' => [ 'class' => 'backend\modules\reports\Reports', ], + 'options' => [ + 'class' => 'backend\modules\options\Options', + ], ], 'components' => [ 'request' => [ diff --git a/backend/modules/options/Options.php b/backend/modules/options/Options.php new file mode 100644 index 0000000..1b0ec3d --- /dev/null +++ b/backend/modules/options/Options.php @@ -0,0 +1,24 @@ +render('index'); + } +} diff --git a/backend/modules/options/controllers/OptionsController.php b/backend/modules/options/controllers/OptionsController.php new file mode 100644 index 0000000..0756ef7 --- /dev/null +++ b/backend/modules/options/controllers/OptionsController.php @@ -0,0 +1,127 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ]; + } + + /** + * Lists all Options models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new OptionsSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single Options model. + * @param integer $id + * @return mixed + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new Options model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new Options(); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } + + return $this->render('create', [ + 'model' => $model, + ]); + } + + /** + * Updates an existing Options model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param integer $id + * @return mixed + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionUpdate($id) + { + $model = $this->findModel($id); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } + + return $this->render('update', [ + 'model' => $model, + ]); + } + + /** + * Deletes an existing Options model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the Options model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return Options the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = Options::findOne($id)) !== null) { + return $model; + } + + throw new NotFoundHttpException('The requested page does not exist.'); + } +} diff --git a/backend/modules/options/models/Options.php b/backend/modules/options/models/Options.php new file mode 100644 index 0000000..07227f5 --- /dev/null +++ b/backend/modules/options/models/Options.php @@ -0,0 +1,10 @@ + $query, + ]); + + $this->load($params); + + if (!$this->validate()) { + // uncomment the following line if you do not want to return any records when validation fails + // $query->where('0=1'); + return $dataProvider; + } + + // grid filtering conditions + $query->andFilterWhere([ + 'id' => $this->id, + 'status' => $this->status, + ]); + + $query->andFilterWhere(['like', 'label', $this->label]) + ->andFilterWhere(['like', 'key', $this->key]) + ->andFilterWhere(['like', 'value', $this->value]); + + return $dataProvider; + } +} diff --git a/backend/modules/options/views/default/index.php b/backend/modules/options/views/default/index.php new file mode 100644 index 0000000..d179132 --- /dev/null +++ b/backend/modules/options/views/default/index.php @@ -0,0 +1,12 @@ +
+

context->action->uniqueId ?>

+

+ This is the view content for action "context->action->id ?>". + The action belongs to the controller "context) ?>" + in the "context->module->id ?>" module. +

+

+ You may customize this page by editing the following file:
+ +

+
diff --git a/backend/modules/options/views/options/_form.php b/backend/modules/options/views/options/_form.php new file mode 100644 index 0000000..46a5cda --- /dev/null +++ b/backend/modules/options/views/options/_form.php @@ -0,0 +1,34 @@ + + +
+ + + + field($model, 'label')->textInput(['maxlength' => true]) ?> + + field($model, 'key')->textInput(['maxlength' => true]) ?> + + field($model, 'value')->textarea(['rows' => 6]) ?> + + field($model, 'status')->dropDownList( + [ + 1 => 'Активна', + 0 => 'Не активна' + ] + ); ?> + +
+ 'btn btn-success']) ?> +
+ + + +
diff --git a/backend/modules/options/views/options/_search.php b/backend/modules/options/views/options/_search.php new file mode 100644 index 0000000..39cf244 --- /dev/null +++ b/backend/modules/options/views/options/_search.php @@ -0,0 +1,35 @@ + + + diff --git a/backend/modules/options/views/options/create.php b/backend/modules/options/views/options/create.php new file mode 100644 index 0000000..4b6da91 --- /dev/null +++ b/backend/modules/options/views/options/create.php @@ -0,0 +1,18 @@ +title = 'Добавить опцию'; +$this->params['breadcrumbs'][] = ['label' => 'Опции', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/backend/modules/options/views/options/index.php b/backend/modules/options/views/options/index.php new file mode 100644 index 0000000..1ee6a76 --- /dev/null +++ b/backend/modules/options/views/options/index.php @@ -0,0 +1,36 @@ +title = 'Опции'; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ + render('_search', ['model' => $searchModel]); ?> + +

+ 'btn btn-success']) ?> +

+ + $dataProvider, + 'filterModel' => $searchModel, + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], + +// 'id', + 'label', + 'key', + 'value:ntext', + 'status', + + ['class' => 'yii\grid\ActionColumn'], + ], + ]); ?> +
diff --git a/backend/modules/options/views/options/update.php b/backend/modules/options/views/options/update.php new file mode 100644 index 0000000..098082a --- /dev/null +++ b/backend/modules/options/views/options/update.php @@ -0,0 +1,19 @@ +title = 'Редактировать опцию: ' . $model->id; +$this->params['breadcrumbs'][] = ['label' => 'Опции', 'url' => ['index']]; +$this->params['breadcrumbs'][] = ['label' => $model->id, 'url' => ['view', 'id' => $model->id]]; +$this->params['breadcrumbs'][] = 'Ред.'; +?> +
+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/backend/modules/options/views/options/view.php b/backend/modules/options/views/options/view.php new file mode 100644 index 0000000..3c3ca61 --- /dev/null +++ b/backend/modules/options/views/options/view.php @@ -0,0 +1,39 @@ +title = $model->key; +$this->params['breadcrumbs'][] = ['label' => 'Опции', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +\yii\web\YiiAsset::register($this); +?> +
+ +

+ $model->id], ['class' => 'btn btn-primary']) ?> + $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> + 'btn btn-primary']) ?> +

+ + $model, + 'attributes' => [ + 'id', + 'label', + 'key', + 'value:ntext', + 'status', + ], + ]) ?> + +
diff --git a/backend/modules/settings/controllers/SkillController.php b/backend/modules/settings/controllers/SkillController.php index 3786a54..cc40c23 100755 --- a/backend/modules/settings/controllers/SkillController.php +++ b/backend/modules/settings/controllers/SkillController.php @@ -2,6 +2,9 @@ namespace backend\modules\settings\controllers; +use backend\modules\options\Options; +use backend\modules\settings\models\SkillsOnMainPageForm; +use common\classes\Debug; use Yii; use backend\modules\settings\models\Skill; use backend\modules\settings\models\SkillSearch; @@ -105,6 +108,21 @@ class SkillController extends Controller ]); } + public function actionSkillsOnMainPage() + { + $model = new SkillsOnMainPageForm(); + + if ($model->load(Yii::$app->request->post())) { + $model->saveSkills(); + $model->showMsg = true; + } else { + $skills = \common\models\Options::getValue('skills_on_main_page'); + $model->load(json_decode($skills, true)); + } + + return $this->render('skills-on-main-page', ['model' => $model]); + } + /** * Deletes an existing Skill model. * If deletion is successful, the browser will be redirected to the 'index' page. diff --git a/backend/modules/settings/models/SkillsOnMainPageForm.php b/backend/modules/settings/models/SkillsOnMainPageForm.php new file mode 100644 index 0000000..ac3c647 --- /dev/null +++ b/backend/modules/settings/models/SkillsOnMainPageForm.php @@ -0,0 +1,67 @@ +_task)) { + $this->addError('_task', 'X is not array!'); + } + } + + public function saveSkills() + { + $res = []; + $resToFront = []; + if ($this->skills_back) { + $res['SkillsOnMainPageForm']['skills_back'] = $this->skills_back; + foreach ($this->skills_back as $item) { + $resToFront['skills_back'][] = [$item => \common\models\Skill::getNameById($item)]; + } + } + if ($this->skills_front) { + $res['SkillsOnMainPageForm']['skills_front'] = $this->skills_front; + foreach ($this->skills_front as $item) { + $resToFront['skills_front'][] = [$item => \common\models\Skill::getNameById($item)]; + } + } + if ($this->skills_design) { + $res['SkillsOnMainPageForm']['skills_design'] = $this->skills_design; + foreach ($this->skills_design as $item) { + $resToFront['skills_design'][] = [$item => \common\models\Skill::getNameById($item)]; + } + } + + Options::setValue('skills_on_main_page', json_encode($res)); + Options::setValue('skills_on_main_page_to_front', json_encode($resToFront)); + } + +} \ No newline at end of file diff --git a/backend/modules/settings/views/skill/index.php b/backend/modules/settings/views/skill/index.php index 9dc6ee9..1c2fc89 100755 --- a/backend/modules/settings/views/skill/index.php +++ b/backend/modules/settings/views/skill/index.php @@ -17,6 +17,7 @@ $this->params['breadcrumbs'][] = $this->title;

'btn btn-success']) ?> 'btn btn-success']) ?> + 'btn btn-success']) ?>

+
+ + showMsg): ?> + + + + + +
+
+ field($model, 'skills_back')->widget( + Select2::class, + [ + 'data' => \yii\helpers\ArrayHelper::map(\common\models\Skill::find()->all(), 'id', 'name'), + 'options' => ['placeholder' => '...', 'class' => 'form-control', 'multiple' => true], + 'pluginOptions' => [ + 'allowClear' => true + ], + ] + )->label('Навыки Backend'); ?> +
+
+
+
+ field($model, 'skills_front')->widget( + Select2::class, + [ + 'data' => \yii\helpers\ArrayHelper::map(\common\models\Skill::find()->all(), 'id', 'name'), + 'options' => ['placeholder' => '...', 'class' => 'form-control', 'multiple' => true], + 'pluginOptions' => [ + 'allowClear' => true + ], + ] + )->label('Навыки FrontEnd'); ?> +
+
+
+
+ field($model, 'skills_design')->widget( + Select2::class, + [ + 'data' => \yii\helpers\ArrayHelper::map(\common\models\Skill::find()->all(), 'id', 'name'), + 'options' => ['placeholder' => '...', 'class' => 'form-control', 'multiple' => true], + 'pluginOptions' => [ + 'allowClear' => true + ], + ] + )->label('Навыки Design'); ?> +
+
+ + +
+ 'btn btn-success']) ?> +
+ + + +
diff --git a/backend/views/layouts/left.php b/backend/views/layouts/left.php index d14669e..f40fccf 100755 --- a/backend/views/layouts/left.php +++ b/backend/views/layouts/left.php @@ -49,6 +49,7 @@ ['label' => 'Заметки', 'icon' => 'sticky-note', 'url' => ['/notes/notes'], 'active' => \Yii::$app->controller->id == 'notes'], ['label' => 'Календарь ДР', 'icon' => 'calendar', 'url' => ['/calendar/calendar'], 'active' => \Yii::$app->controller->id == 'calendar'], ['label' => 'Отчеты', 'icon' => 'list-alt', 'url' => ['/reports/reports'], 'active' => \Yii::$app->controller->id == 'reports'], + ['label' => 'Опции', 'icon' => 'list-alt', 'url' => ['/options/options'], 'active' => \Yii::$app->controller->id == 'options'], /*['label' => 'Gii', 'icon' => 'file-code-o', 'url' => ['/gii']], ['label' => 'Debug', 'icon' => 'dashboard', 'url' => ['/debug']], diff --git a/common/models/Options.php b/common/models/Options.php new file mode 100644 index 0000000..5e4082d --- /dev/null +++ b/common/models/Options.php @@ -0,0 +1,91 @@ + 255], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'label' => 'Label', + 'key' => 'Key', + 'value' => 'Value', + 'status' => 'Status', + ]; + } + + /** + * @param $key + * @return string|null + */ + public static function getValue($key) + { + $value = self::find()->where(['key' => $key])->one(); + if ($value){ + return $value->value; + } + return null; + } + + /** + * @param $id + * @return array|\yii\db\ActiveRecord|null + */ + public static function getById($id) + { + return self::find()->where(['id' => $id])->one(); + } + + /** + * @param $key + * @param $value + * @return bool + */ + public static function setValue($key, $value) + { + $model = self::find()->where(['key' => $key])->one(); + if(!$model) { + $model = new self(); + $model->key = $key; + } + $model->value = $value; + + return $model->save(); + } +} diff --git a/common/models/Skill.php b/common/models/Skill.php index 9ce0128..dac7b95 100755 --- a/common/models/Skill.php +++ b/common/models/Skill.php @@ -58,4 +58,10 @@ class Skill extends \yii\db\ActiveRecord { return $this->hasOne(SkillCategory::class, ['id' => 'category_id']); } + + public static function getNameById($id) + { + $model = self::find()->where(['id' => $id])->one(); + return $model ? $model->name : null; + } } diff --git a/console/migrations/m210609_102606_create_options_table.php b/console/migrations/m210609_102606_create_options_table.php new file mode 100644 index 0000000..e320341 --- /dev/null +++ b/console/migrations/m210609_102606_create_options_table.php @@ -0,0 +1,31 @@ +createTable('{{%options}}', [ + 'id' => $this->primaryKey(), + 'label' => $this->string(255), + 'key' => $this->string(255)->notNull(), + 'value' => $this->text(), + 'status' => $this->integer(1), + ]); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropTable('{{%options}}'); + } +} diff --git a/frontend/config/main.php b/frontend/config/main.php index 7d0a06b..da4c0ca 100755 --- a/frontend/config/main.php +++ b/frontend/config/main.php @@ -14,6 +14,9 @@ return [ 'controllerNamespace' => 'frontend\controllers', 'modules' => [ + 'api' => [ + 'class' => 'frontend\modules\api\Api', + ], 'access' => [ 'class' => 'frontend\modules\access\Access', ], @@ -58,6 +61,7 @@ return [ 'rules' => [ 'site/index' => 'card/user-card/index', '' => 'card/user-card/index', + ['class' => 'yii\rest\UrlRule', 'controller' => 'skills'], ], ], diff --git a/frontend/modules/api/Api.php b/frontend/modules/api/Api.php new file mode 100644 index 0000000..df9e98a --- /dev/null +++ b/frontend/modules/api/Api.php @@ -0,0 +1,24 @@ +render('index'); + } +} diff --git a/frontend/modules/api/controllers/SkillsController.php b/frontend/modules/api/controllers/SkillsController.php new file mode 100644 index 0000000..40c8234 --- /dev/null +++ b/frontend/modules/api/controllers/SkillsController.php @@ -0,0 +1,36 @@ + \yii\filters\ContentNegotiator::className(), + 'formats' => [ + 'application/json' => \yii\web\Response::FORMAT_JSON, + ], + ], + ]; + } + + public function actionIndex() + { + return ['some' => 'rrr']; + } + + public function actionSkillsOnMainPage() + { + $data = \common\models\Options::getValue('skills_on_main_page_to_front'); + if ($data) $data = json_decode($data, true); + else return []; + + return $data; + } + +} diff --git a/frontend/modules/api/views/default/index.php b/frontend/modules/api/views/default/index.php new file mode 100644 index 0000000..aa260c4 --- /dev/null +++ b/frontend/modules/api/views/default/index.php @@ -0,0 +1,12 @@ +
+

context->action->uniqueId ?>

+

+ This is the view content for action "context->action->id ?>". + The action belongs to the controller "context) ?>" + in the "context->module->id ?>" module. +

+

+ You may customize this page by editing the following file:
+ +

+
diff --git a/frontend/modules/api/views/skills/index.php b/frontend/modules/api/views/skills/index.php new file mode 100644 index 0000000..7ff8f27 --- /dev/null +++ b/frontend/modules/api/views/skills/index.php @@ -0,0 +1,9 @@ + +

skills/index

+ +

+ You may change the content of this page by modifying + the file . +