skills on main page api and admin

This commit is contained in:
andrey 2021-06-09 18:06:13 +03:00
parent 23773eff4e
commit b9f919204c
27 changed files with 842 additions and 0 deletions

View File

@ -50,6 +50,9 @@ return [
'reports' => [
'class' => 'backend\modules\reports\Reports',
],
'options' => [
'class' => 'backend\modules\options\Options',
],
],
'components' => [
'request' => [

View File

@ -0,0 +1,24 @@
<?php
namespace backend\modules\options;
/**
* options module definition class
*/
class Options extends \yii\base\Module
{
/**
* {@inheritdoc}
*/
public $controllerNamespace = 'backend\modules\options\controllers';
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
// custom initialization code goes here
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace backend\modules\options\controllers;
use yii\web\Controller;
/**
* Default controller for the `options` module
*/
class DefaultController extends Controller
{
/**
* Renders the index view for the module
* @return string
*/
public function actionIndex()
{
return $this->render('index');
}
}

View File

@ -0,0 +1,127 @@
<?php
namespace backend\modules\options\controllers;
use Yii;
use backend\modules\options\models\Options;
use backend\modules\options\models\OptionsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* OptionsController implements the CRUD actions for Options model.
*/
class OptionsController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'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.');
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace backend\modules\options\models;
class Options extends \common\models\Options
{
}

View File

@ -0,0 +1,71 @@
<?php
namespace backend\modules\options\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\options\models\Options;
/**
* OptionsSearch represents the model behind the search form of `backend\modules\options\models\Options`.
*/
class OptionsSearch extends Options
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id', 'status'], 'integer'],
[['label', 'key', 'value'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Options::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $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;
}
}

View File

@ -0,0 +1,12 @@
<div class="options-default-index">
<h1><?= $this->context->action->uniqueId ?></h1>
<p>
This is the view content for action "<?= $this->context->action->id ?>".
The action belongs to the controller "<?= get_class($this->context) ?>"
in the "<?= $this->context->module->id ?>" module.
</p>
<p>
You may customize this page by editing the following file:<br>
<code><?= __FILE__ ?></code>
</p>
</div>

View File

@ -0,0 +1,34 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\options\models\Options */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="options-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'label')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'key')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'value')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'status')->dropDownList(
[
1 => 'Активна',
0 => 'Не активна'
]
); ?>
<div class="form-group">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -0,0 +1,35 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\options\models\OptionsSearch */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="options-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'id') ?>
<?= $form->field($model, 'label') ?>
<?= $form->field($model, 'key') ?>
<?= $form->field($model, 'value') ?>
<?= $form->field($model, 'status') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -0,0 +1,18 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model backend\modules\options\models\Options */
$this->title = 'Добавить опцию';
$this->params['breadcrumbs'][] = ['label' => 'Опции', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="options-create">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@ -0,0 +1,36 @@
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel backend\modules\options\models\OptionsSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Опции';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="options-index">
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Добавить', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// 'id',
'label',
'key',
'value:ntext',
'status',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>

View File

@ -0,0 +1,19 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model backend\modules\options\models\Options */
$this->title = 'Редактировать опцию: ' . $model->id;
$this->params['breadcrumbs'][] = ['label' => 'Опции', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->id, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = 'Ред.';
?>
<div class="options-update">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@ -0,0 +1,39 @@
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model backend\modules\options\models\Options */
$this->title = $model->key;
$this->params['breadcrumbs'][] = ['label' => 'Опции', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<div class="options-view">
<p>
<?= Html::a('Редактировать', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Удалить', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
<?= Html::a('Список опций', ['index'], ['class' => 'btn btn-primary']) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'label',
'key',
'value:ntext',
'status',
],
]) ?>
</div>

View File

@ -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.

View File

@ -0,0 +1,67 @@
<?php
namespace backend\modules\settings\models;
use common\classes\Debug;
use common\models\Options;
use yii\base\Model;
/**
* Class SkillsOnMainPageForm
* @property array $skills_back
* @property array $skills_front
* @property array $skills_design
* @package backend\modules\settings\models
*/
class SkillsOnMainPageForm extends Model
{
public $skills_back;
public $skills_front;
public $skills_design;
public $showMsg = false;
public function rules()
{
return [
[['skills_back', 'skills_front', 'skills_design'], 'checkIsArray'],
];
}
public function checkIsArray()
{
if (!is_array($this->_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));
}
}

View File

@ -17,6 +17,7 @@ $this->params['breadcrumbs'][] = $this->title;
<p>
<?= Html::a('Добавить', ['create'], ['class' => 'btn btn-success']) ?>
<?= Html::a('Категории', ['/settings/skill-category'], ['class' => 'btn btn-success']) ?>
<?= Html::a('Навыки на главной', ['/settings/skill/skills-on-main-page'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([

View File

@ -0,0 +1,74 @@
<?php
use kartik\select2\Select2;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $form yii\widgets\ActiveForm */
/* @var $model \backend\modules\settings\models\SkillsOnMainPageForm */
?>
<div class="skill-form">
<?php if ($model->showMsg): ?>
<div class="alert alert-success" role="alert">
Данные сохранены.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<?php endif; ?>
<?php $form = ActiveForm::begin(); ?>
<div class="row">
<div class="col-xs-12">
<?= $form->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'); ?>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<?= $form->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'); ?>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<?= $form->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'); ?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -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']],

91
common/models/Options.php Normal file
View File

@ -0,0 +1,91 @@
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "options".
*
* @property int $id
* @property string $label
* @property string $key
* @property string $value
* @property int $status
*/
class Options extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'options';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['key'], 'required'],
[['value'], 'string'],
[['status'], 'integer'],
[['label', 'key'], 'string', 'max' => 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();
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,31 @@
<?php
use yii\db\Migration;
/**
* Handles the creation of table `{{%options}}`.
*/
class m210609_102606_create_options_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->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}}');
}
}

View File

@ -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'],
],
],

View File

@ -0,0 +1,24 @@
<?php
namespace frontend\modules\api;
/**
* api module definition class
*/
class Api extends \yii\base\Module
{
/**
* {@inheritdoc}
*/
public $controllerNamespace = 'frontend\modules\api\controllers';
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
// custom initialization code goes here
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace frontend\modules\api\controllers;
use yii\web\Controller;
/**
* Default controller for the `api` module
*/
class DefaultController extends Controller
{
/**
* Renders the index view for the module
* @return string
*/
public function actionIndex()
{
return $this->render('index');
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace frontend\modules\api\controllers;
use common\models\Options;
use yii\filters\AccessControl;
class SkillsController extends \yii\rest\Controller
{
public function behaviors()
{
return [
[
'class' => \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;
}
}

View File

@ -0,0 +1,12 @@
<div class="api-default-index">
<h1><?= $this->context->action->uniqueId ?></h1>
<p>
This is the view content for action "<?= $this->context->action->id ?>".
The action belongs to the controller "<?= get_class($this->context) ?>"
in the "<?= $this->context->module->id ?>" module.
</p>
<p>
You may customize this page by editing the following file:<br>
<code><?= __FILE__ ?></code>
</p>
</div>

View File

@ -0,0 +1,9 @@
<?php
/* @var $this yii\web\View */
?>
<h1>skills/index</h1>
<p>
You may change the content of this page by modifying
the file <code><?= __FILE__; ?></code>.
</p>