skill category

This commit is contained in:
andrey 2021-06-08 17:05:54 +03:00
parent 9f1db817a5
commit 23773eff4e
13 changed files with 487 additions and 0 deletions

View File

@ -0,0 +1,127 @@
<?php
namespace backend\modules\settings\controllers;
use Yii;
use common\models\SkillCategory;
use backend\modules\settings\models\SkillCategorySearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* SkillCategoryController implements the CRUD actions for SkillCategory model.
*/
class SkillCategoryController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all SkillCategory models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new SkillCategorySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single SkillCategory 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 SkillCategory model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new SkillCategory();
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 SkillCategory 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 SkillCategory 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 SkillCategory model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return SkillCategory the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = SkillCategory::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace backend\modules\settings\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\SkillCategory;
/**
* SkillCategorySearch represents the model behind the search form of `common\models\SkillCategory`.
*/
class SkillCategorySearch extends SkillCategory
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id'], 'integer'],
[['name'], '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 = SkillCategory::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,
]);
$query->andFilterWhere(['like', 'name', $this->name]);
return $dataProvider;
}
}

View File

@ -0,0 +1,23 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model common\models\SkillCategory */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="skill-category-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<div class="form-group">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -0,0 +1,29 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\settings\models\SkillCategorySearch */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="skill-category-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'id') ?>
<?= $form->field($model, 'name') ?>
<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 common\models\SkillCategory */
$this->title = 'Create Skill Category';
$this->params['breadcrumbs'][] = ['label' => 'Skill Categories', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="skill-category-create">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@ -0,0 +1,34 @@
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel backend\modules\settings\models\SkillCategorySearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Skill Categories';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="skill-category-index">
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Добавить категорию', ['create'], ['class' => 'btn btn-success']) ?>
<?= Html::a('Навыки', ['/settings/skill'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>

View File

@ -0,0 +1,19 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model common\models\SkillCategory */
$this->title = 'Update Skill Category: ' . $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Skill Categories', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = 'Update';
?>
<div class="skill-category-update">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@ -0,0 +1,36 @@
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model common\models\SkillCategory */
$this->title = $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Skill Categories', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<div class="skill-category-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',
'name',
],
]) ?>
</div>

View File

@ -13,6 +13,9 @@ use yii\widgets\ActiveForm;
<?php $form = ActiveForm::begin(); ?> <?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?> <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'category_id')->dropDownList(
\yii\helpers\ArrayHelper::map(\common\models\SkillCategory::getAll(), 'id', 'name')
); ?>
<div class="form-group"> <div class="form-group">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?> <?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>

View File

@ -16,6 +16,7 @@ $this->params['breadcrumbs'][] = $this->title;
<p> <p>
<?= Html::a('Добавить', ['create'], ['class' => 'btn btn-success']) ?> <?= Html::a('Добавить', ['create'], ['class' => 'btn btn-success']) ?>
<?= Html::a('Категории', ['/settings/skill-category'], ['class' => 'btn btn-success']) ?>
</p> </p>
<?= GridView::widget([ <?= GridView::widget([
@ -26,6 +27,12 @@ $this->params['breadcrumbs'][] = $this->title;
'id', 'id',
'name', 'name',
[
'attribute' => 'category_id',
'value' => function ($model) {
return $model->category ? $model->category->name : '-';
}
],
['class' => 'yii\grid\ActionColumn'], ['class' => 'yii\grid\ActionColumn'],
], ],

View File

@ -9,6 +9,7 @@ use Yii;
* *
* @property int $id * @property int $id
* @property string $name * @property string $name
* @property integer $category_id
* *
* @property CardSkill[] $cardSkills * @property CardSkill[] $cardSkills
*/ */
@ -29,6 +30,7 @@ class Skill extends \yii\db\ActiveRecord
{ {
return [ return [
[['name'], 'required'], [['name'], 'required'],
[['category_id'], 'integer'],
[['name'], 'string', 'max' => 100], [['name'], 'string', 'max' => 100],
]; ];
} }
@ -51,4 +53,9 @@ class Skill extends \yii\db\ActiveRecord
{ {
return $this->hasMany(CardSkill::className(), ['skill_id' => 'id']); return $this->hasMany(CardSkill::className(), ['skill_id' => 'id']);
} }
public function getCategory()
{
return $this->hasOne(SkillCategory::class, ['id' => 'category_id']);
}
} }

View File

@ -0,0 +1,59 @@
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "skill_category".
*
* @property int $id
* @property string $name
*
* @property Skill[] $skills
*/
class SkillCategory extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'skill_category';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['name'], 'required'],
[['name'], 'string', 'max' => 255],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getSkills()
{
return $this->hasMany(Skill::className(), ['category' => 'id']);
}
public static function getAll()
{
return self::find()->all();
}
}

View File

@ -0,0 +1,57 @@
<?php
use yii\db\Migration;
/**
* Handles the creation of table `{{%skill_category}}`.
*/
class m210608_131432_create_skill_category_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->createTable('{{%skill_category}}', [
'id' => $this->primaryKey(),
'name' => $this->string(255)->notNull(),
]);
$this->addColumn('skill', 'category_id', $this->integer(11));
$this->createIndex(
'idx-skill-category_id',
'skill',
'category_id'
);
$this->addForeignKey(
'fk-skill-category_id',
'skill',
'category_id',
'skill_category',
'id',
'CASCADE'
);
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropForeignKey(
'fk-skill-category_id',
'skill'
);
$this->dropIndex(
'idx-skill-category_id',
'skill'
);
$this->dropColumn('skill', 'category_id');
$this->dropTable('{{%skill_category}}');
}
}