From e961c3f35f9e6b4646685dc68bf89038adf4a3e5 Mon Sep 17 00:00:00 2001 From: SoHardKI Date: Tue, 16 Jul 2019 11:58:05 +0300 Subject: [PATCH 1/2] add_holidays --- backend/config/main.php | 3 + backend/modules/holiday/Holiday.php | 21 ++++ .../holiday/controllers/HolidayController.php | 102 ++++++++++++++++++ backend/modules/holiday/models/Holiday.php | 11 ++ .../modules/holiday/models/HolidaySearch.php | 59 ++++++++++ .../modules/holiday/views/holiday/_form.php | 42 ++++++++ .../modules/holiday/views/holiday/_search.php | 28 +++++ .../modules/holiday/views/holiday/create.php | 19 ++++ .../modules/holiday/views/holiday/index.php | 66 ++++++++++++ .../modules/holiday/views/holiday/update.php | 14 +++ .../modules/holiday/views/holiday/view.php | 36 +++++++ backend/views/layouts/left.php | 1 + common/models/Holiday.php | 53 +++++++++ common/models/UserCard.php | 6 ++ .../m190715_124941_create_table_holiday.php | 30 ++++++ 15 files changed, 491 insertions(+) create mode 100644 backend/modules/holiday/Holiday.php create mode 100644 backend/modules/holiday/controllers/HolidayController.php create mode 100644 backend/modules/holiday/models/Holiday.php create mode 100644 backend/modules/holiday/models/HolidaySearch.php create mode 100644 backend/modules/holiday/views/holiday/_form.php create mode 100644 backend/modules/holiday/views/holiday/_search.php create mode 100644 backend/modules/holiday/views/holiday/create.php create mode 100644 backend/modules/holiday/views/holiday/index.php create mode 100644 backend/modules/holiday/views/holiday/update.php create mode 100644 backend/modules/holiday/views/holiday/view.php create mode 100644 common/models/Holiday.php create mode 100644 console/migrations/m190715_124941_create_table_holiday.php diff --git a/backend/config/main.php b/backend/config/main.php index 1bc988e..2459128 100755 --- a/backend/config/main.php +++ b/backend/config/main.php @@ -32,6 +32,9 @@ return [ 'balance' => [ 'class' => 'backend\modules\balance\Balance', ], + 'holiday' => [ + 'class' => 'backend\modules\holiday\Holiday', + ], ], 'components' => [ 'request' => [ diff --git a/backend/modules/holiday/Holiday.php b/backend/modules/holiday/Holiday.php new file mode 100644 index 0000000..f64166f --- /dev/null +++ b/backend/modules/holiday/Holiday.php @@ -0,0 +1,21 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ]; + } + + public function actionIndex() + { + $searchModel = new HolidaySearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + public function actionView($id) + { + $model = $this->findModel($id); + + return $this->render('view', [ + 'model' => $model, + ]); + } + + public function actionCreate() + { + $model = new Holiday(); + + if ($model->load(Yii::$app->request->post())) + { + $model->dt_start = strtotime($model->dt_start); + $model->dt_end = strtotime($model->dt_end); + $model->save(); + + Yii::$app->session->addFlash('success', 'Отпуск добавлен'); + + return $this->redirect(['view', 'id' => $model->id]); + } + + return $this->render('create', [ + 'model' => $model, + ]); + } + + public function actionUpdate($id) + { + $model = $this->findModel($id); + + if ($model->load(Yii::$app->request->post())) { + $model->dt_start = strtotime($model->dt_start); + $model->dt_end = strtotime($model->dt_end); + $model->save(); + + return $this->redirect(['view', 'id' => $model->id]); + } + + return $this->render('update', [ + 'model' => $model, + ]); + } + + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + protected function findModel($id) + { + if (($model = Holiday::findOne($id)) !== null) { + return $model; + } + + throw new NotFoundHttpException('The requested page does not exist.'); + } +} \ No newline at end of file diff --git a/backend/modules/holiday/models/Holiday.php b/backend/modules/holiday/models/Holiday.php new file mode 100644 index 0000000..cc0869f --- /dev/null +++ b/backend/modules/holiday/models/Holiday.php @@ -0,0 +1,11 @@ +with('users'); + + $dataProvider = new ActiveDataProvider([ + 'query' => $query, + ]); + + $this->load($params); + + $dt_start = strtotime($this->dt_start) ? strtotime($this->dt_start) : null; + $dt_end = strtotime($this->dt_end) ? strtotime($this->dt_end) : null; + + 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, + 'card_id' => $this->card_id, + ]); + + $query->andFilterWhere(['between', 'dt_start', $dt_start, $dt_end ]) + ->orFilterWhere(['between', 'dt_end', $dt_start, $dt_end ]); + + return $dataProvider; + } +} \ No newline at end of file diff --git a/backend/modules/holiday/views/holiday/_form.php b/backend/modules/holiday/views/holiday/_form.php new file mode 100644 index 0000000..09740b7 --- /dev/null +++ b/backend/modules/holiday/views/holiday/_form.php @@ -0,0 +1,42 @@ + +
+ + $model, + 'attribute' => 'card_id', + 'data' => \common\models\UserCard::getUserList(), + 'options' => ['placeholder' => 'Начните вводить...','class' => 'form-control'], + 'pluginOptions' => [ + 'allowClear' => true + ], + ]); + ?> + Выберите дату начала отпуска'; + echo '
'; + echo DatePicker::widget([ + 'model' => $model, + 'attribute' => 'dt_start', + 'language' => 'ru', + 'dateFormat' => 'dd-MM-yyyy', + ]); + echo '
'; + echo ''; + echo '
'; + echo DatePicker::widget([ + 'model' => $model, + 'attribute' => 'dt_end', + 'language' => 'ru', + 'dateFormat' => 'dd-MM-yyyy', + ]); + ?> +
+ 'btn btn-success']) ?> +
+ +
diff --git a/backend/modules/holiday/views/holiday/_search.php b/backend/modules/holiday/views/holiday/_search.php new file mode 100644 index 0000000..eef4833 --- /dev/null +++ b/backend/modules/holiday/views/holiday/_search.php @@ -0,0 +1,28 @@ + + diff --git a/backend/modules/holiday/views/holiday/create.php b/backend/modules/holiday/views/holiday/create.php new file mode 100644 index 0000000..d2cd69c --- /dev/null +++ b/backend/modules/holiday/views/holiday/create.php @@ -0,0 +1,19 @@ +title = 'Добавить отпуск'; +$this->params['breadcrumbs'][] = ['label' => 'Список отпусков', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ + render('_form', [ + 'model' => $model, + ]) ?> + +
\ No newline at end of file diff --git a/backend/modules/holiday/views/holiday/index.php b/backend/modules/holiday/views/holiday/index.php new file mode 100644 index 0000000..c454ffc --- /dev/null +++ b/backend/modules/holiday/views/holiday/index.php @@ -0,0 +1,66 @@ +title = 'Список отпусков'; +$this->params['breadcrumps'][] = $this->title; +?> +
+

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

+ $dataProvider, + 'filterModel' => $searchModel, + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], + [ + 'label' => 'ФИО', + 'value' => function($model) + { + return $model->users->fio; + }, + 'filter' => \kartik\select2\Select2::widget([ + 'model' => $searchModel, + 'attribute' => 'card_id', + 'data' => \common\models\UserCard::getUserList(), + 'options' => ['placeholder' => 'Начните вводить...','class' => 'form-control'], + 'pluginOptions' => [ + 'allowClear' => true + ], + ]), + ], + [ + 'attribute' => 'dt_start', + 'filter' => DatePicker::widget([ + 'model' => $searchModel, + 'attribute' => 'dt_start', + 'language' => 'ru', + 'dateFormat' => 'dd-MM-yyyy', + 'options' => [ + 'autocomplete' => 'off', + ], + ]), + ], + [ + 'attribute' => 'dt_end', + 'filter' => DatePicker::widget([ + 'model' => $searchModel, + 'attribute' => 'dt_end', + 'language' => 'ru', + 'dateFormat' => 'dd-MM-yyyy', + 'options' => [ + 'autocomplete' => 'off', + ], + ]), + ], + ['class' => 'yii\grid\ActionColumn'], + ], + ]);?> +
diff --git a/backend/modules/holiday/views/holiday/update.php b/backend/modules/holiday/views/holiday/update.php new file mode 100644 index 0000000..9772666 --- /dev/null +++ b/backend/modules/holiday/views/holiday/update.php @@ -0,0 +1,14 @@ +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/holiday/views/holiday/view.php b/backend/modules/holiday/views/holiday/view.php new file mode 100644 index 0000000..a79784e --- /dev/null +++ b/backend/modules/holiday/views/holiday/view.php @@ -0,0 +1,36 @@ +title = 'Отпуск №' . $model->id; +$this->params['breadcrumbs'][] = ['label' => 'Список отпусков', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+

+ 'btn btn-primary']) ?> + $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', + ], + ]) ?> +

+ + $model, + 'attributes' => [ + [ + 'label' => 'ФИО', + 'value' => function($model) + { + return $model->users->fio; + }, + ], + 'dt_start', + 'dt_end' + ], +]) ?> \ No newline at end of file diff --git a/backend/views/layouts/left.php b/backend/views/layouts/left.php index 38f07e9..c25abb1 100755 --- a/backend/views/layouts/left.php +++ b/backend/views/layouts/left.php @@ -42,6 +42,7 @@ ], ], ['label' => 'Баланс', 'icon' => 'dollar', 'url' => ['/balance/balance']], + ['label' => 'Отпуска', 'icon' => 'user-circle', 'url' => ['/holiday/holiday']], /*['label' => 'Gii', 'icon' => 'file-code-o', 'url' => ['/gii']], ['label' => 'Debug', 'icon' => 'dashboard', 'url' => ['/debug']], diff --git a/common/models/Holiday.php b/common/models/Holiday.php new file mode 100644 index 0000000..3b47a80 --- /dev/null +++ b/common/models/Holiday.php @@ -0,0 +1,53 @@ + 'ID пользователя', + 'dt_start' => 'Дата начала отпуска', + 'dt_end' => 'Дата конца отпуска' + ]; + } + + public function afterFind() + { + parent::afterFind(); // TODO: Change the autogenerated stub + $this->dt_start = date('d-m-Y', $this->dt_start); + $this->dt_end = date('d-m-Y', $this->dt_end); + } + + public function getUsers() + { + return $this->hasOne(UserCard::className(),['id' => 'card_id']); + } +} \ No newline at end of file diff --git a/common/models/UserCard.php b/common/models/UserCard.php index 47c946f..eab2b09 100755 --- a/common/models/UserCard.php +++ b/common/models/UserCard.php @@ -2,6 +2,7 @@ namespace common\models; +use common\classes\Debug; use Yii; use yii\behaviors\TimestampBehavior; use yii\db\Expression; @@ -151,4 +152,9 @@ class UserCard extends \yii\db\ActiveRecord { return ArrayHelper::map(Skill::find()->all(),'id', 'name'); } + + public static function getUserList() + { + return ArrayHelper::map(self::find()->all(), 'id', 'fio'); + } } diff --git a/console/migrations/m190715_124941_create_table_holiday.php b/console/migrations/m190715_124941_create_table_holiday.php new file mode 100644 index 0000000..6f6225f --- /dev/null +++ b/console/migrations/m190715_124941_create_table_holiday.php @@ -0,0 +1,30 @@ +createTable('holiday',[ + 'id' => $this->primaryKey(), + 'card_id' => $this->integer(11)->notNull(), + 'dt_start' => $this->integer(15)->notNull(), + 'dt_end' => $this->integer(15)->notNull(), + ]); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropTable('holiday'); + } +} From 3d915d77bc68c599697c7b6e40bfe25647ac808f Mon Sep 17 00:00:00 2001 From: SoHardKI Date: Tue, 16 Jul 2019 12:16:13 +0300 Subject: [PATCH 2/2] fixes --- backend/modules/holiday/models/Holiday.php | 5 +---- backend/modules/holiday/views/holiday/_search.php | 4 ++-- console/migrations/m190715_124941_create_table_holiday.php | 4 ++-- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/backend/modules/holiday/models/Holiday.php b/backend/modules/holiday/models/Holiday.php index cc0869f..4a491d0 100644 --- a/backend/modules/holiday/models/Holiday.php +++ b/backend/modules/holiday/models/Holiday.php @@ -4,8 +4,5 @@ namespace backend\modules\holiday\models; class Holiday extends \common\models\Holiday { - public function init() - { - parent::init(); - } + } \ No newline at end of file diff --git a/backend/modules/holiday/views/holiday/_search.php b/backend/modules/holiday/views/holiday/_search.php index eef4833..e61d56d 100644 --- a/backend/modules/holiday/views/holiday/_search.php +++ b/backend/modules/holiday/views/holiday/_search.php @@ -19,8 +19,8 @@ use yii\widgets\ActiveForm; field($model, 'dt_end') ?>
- 'btn btn-primary']) ?> - 'btn btn-default']) ?> + 'btn btn-primary']) ?> + 'btn btn-default']) ?>
diff --git a/console/migrations/m190715_124941_create_table_holiday.php b/console/migrations/m190715_124941_create_table_holiday.php index 6f6225f..66bccd2 100644 --- a/console/migrations/m190715_124941_create_table_holiday.php +++ b/console/migrations/m190715_124941_create_table_holiday.php @@ -15,8 +15,8 @@ class m190715_124941_create_table_holiday extends Migration $this->createTable('holiday',[ 'id' => $this->primaryKey(), 'card_id' => $this->integer(11)->notNull(), - 'dt_start' => $this->integer(15)->notNull(), - 'dt_end' => $this->integer(15)->notNull(), + 'dt_start' => $this->integer(11)->notNull(), + 'dt_end' => $this->integer(11)->notNull(), ]); }