From f7177975a8378ca6bab0399261d3f0f283ef6263 Mon Sep 17 00:00:00 2001 From: Leorne Date: Thu, 5 Dec 2019 15:05:33 +0300 Subject: [PATCH] add Notes --- backend/config/main.php | 3 + backend/modules/notes/Notes.php | 24 ++++ .../notes/controllers/NotesController.php | 116 ++++++++++++++++++ backend/modules/notes/models/Note.php | 29 +++++ backend/modules/notes/models/NoteSearch.php | 57 +++++++++ backend/modules/notes/views/notes/_form.php | 66 ++++++++++ backend/modules/notes/views/notes/create.php | 19 +++ backend/modules/notes/views/notes/index.php | 33 +++++ backend/modules/notes/views/notes/update.php | 17 +++ backend/modules/notes/views/notes/view.php | 52 ++++++++ backend/views/layouts/left.php | 1 + common/models/FieldsValueNew.php | 1 + common/models/Note.php | 66 ++++++++++ common/models/UseField.php | 4 +- common/models/UseStatus.php | 4 +- .../m191205_092846_create_note_table.php | 31 +++++ 16 files changed, 521 insertions(+), 2 deletions(-) create mode 100644 backend/modules/notes/Notes.php create mode 100644 backend/modules/notes/controllers/NotesController.php create mode 100644 backend/modules/notes/models/Note.php create mode 100644 backend/modules/notes/models/NoteSearch.php create mode 100755 backend/modules/notes/views/notes/_form.php create mode 100755 backend/modules/notes/views/notes/create.php create mode 100644 backend/modules/notes/views/notes/index.php create mode 100644 backend/modules/notes/views/notes/update.php create mode 100644 backend/modules/notes/views/notes/view.php create mode 100644 common/models/Note.php create mode 100644 console/migrations/m191205_092846_create_note_table.php diff --git a/backend/config/main.php b/backend/config/main.php index 9af44d9..f00ca39 100755 --- a/backend/config/main.php +++ b/backend/config/main.php @@ -38,6 +38,9 @@ return [ 'holiday' => [ 'class' => 'backend\modules\holiday\Holiday', ], + 'notes' => [ + 'class' => 'backend\modules\notes\Notes', + ], ], 'components' => [ 'request' => [ diff --git a/backend/modules/notes/Notes.php b/backend/modules/notes/Notes.php new file mode 100644 index 0000000..89b7665 --- /dev/null +++ b/backend/modules/notes/Notes.php @@ -0,0 +1,24 @@ +search(); + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider + ]); + } + + /** + * Creates a new note model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new Note(); + + if ($model->load(\Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } + + return $this->render('create', [ + 'model' => $model + ]); + } + + /** + * Displays a single note model. + * @param integer $id + * @return mixed + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionView($id) + { + $additionalDataProvider = new ActiveDataProvider([ + 'query' => FieldsValueNew::find() + ->where(['item_id' => $id, 'item_type' => FieldsValueNew::TYPE_NOTE]) + ->orderBy('order'), + 'pagination' => [ + 'pageSize' => 200, + ], + ]); + + return $this->render('view', [ + 'model' => Note::findOne($id), + 'additionalDataProvider' => $additionalDataProvider + ]); + } + + 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 Note 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 note model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return Note the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = Note::findOne($id)) !== null) { + return $model; + } + + throw new NotFoundHttpException('The requested page does not exist.'); + } +} diff --git a/backend/modules/notes/models/Note.php b/backend/modules/notes/models/Note.php new file mode 100644 index 0000000..6ebedbf --- /dev/null +++ b/backend/modules/notes/models/Note.php @@ -0,0 +1,29 @@ +request->post('Note'); + + FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_NOTE]); + + foreach ( $post['fields'] as $item) { + $fieldsValue = new FieldsValueNew(); + $fieldsValue->field_id = $item['field_id']; + $fieldsValue->item_id = $this->id; + $fieldsValue->item_type = FieldsValueNew::TYPE_NOTE; + $fieldsValue->order = $item['order']; + $fieldsValue->value = $item['value']; + + $fieldsValue->save(); + } + + parent::afterSave($insert, $changedAttributes); + } +} \ No newline at end of file diff --git a/backend/modules/notes/models/NoteSearch.php b/backend/modules/notes/models/NoteSearch.php new file mode 100644 index 0000000..d424a1c --- /dev/null +++ b/backend/modules/notes/models/NoteSearch.php @@ -0,0 +1,57 @@ +request->queryParams; + $query = Note::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, + 'created_at' => $this->created_at, + 'updated_at' => $this->updated_at, + ]); + + $query->andFilterWhere(['like', 'name', $this->name]) + ->andFilterWhere(['like', 'description', $this->description]); + $query->orderBy('created_at DESC'); + + return $dataProvider; + } +} \ No newline at end of file diff --git a/backend/modules/notes/views/notes/_form.php b/backend/modules/notes/views/notes/_form.php new file mode 100755 index 0000000..ef68ef5 --- /dev/null +++ b/backend/modules/notes/views/notes/_form.php @@ -0,0 +1,66 @@ + + +
+ + + + field($model, 'name')->textInput(['maxlength' => true]) ?> + + field($model, 'description')->textarea(['rows' => 6]) ?> + +
+
+ field($model, 'fields')->widget(MultipleInput::class, [ + + 'columns' => [ + [ + 'name' => 'field_id', + 'type' => 'dropDownList', + 'title' => 'Поле', + 'defaultValue' => null, + 'items' => \yii\helpers\ArrayHelper::map(AdditionalFields::find() + ->joinWith('useFields') + ->where(['`use_field`.`use`' => \common\models\UseStatus::USE_NOTE]) + ->all(), + 'id', 'name'), + 'options' => ['prompt' => 'Выберите'] + ], + [ + 'name' => 'value', + 'title' => 'Значение', + 'enableError' => true, + 'options' => [ + 'class' => 'input-priority' + ] + ], + [ + 'name' => 'order', + 'title' => 'Приоритет', + 'enableError' => true, + 'options' => [ + 'class' => 'input-priority' + ] + ] + ] + ])->label('Дополнительно'); + ?> +
+
+ +
+ 'btn btn-success']) ?> +
+ + + +
diff --git a/backend/modules/notes/views/notes/create.php b/backend/modules/notes/views/notes/create.php new file mode 100755 index 0000000..5248f1d --- /dev/null +++ b/backend/modules/notes/views/notes/create.php @@ -0,0 +1,19 @@ +title = 'Создать заметку'; +$this->params['breadcrumbs'][] = ['label' => 'Заметки', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/backend/modules/notes/views/notes/index.php b/backend/modules/notes/views/notes/index.php new file mode 100644 index 0000000..e5fa61a --- /dev/null +++ b/backend/modules/notes/views/notes/index.php @@ -0,0 +1,33 @@ +title = 'Заметки'; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

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

+ + $dataProvider, + 'filterModel' => $searchModel, + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], +// 'id', + 'name', + 'description', + 'created_at', +// 'updated_at', + + ['class' => 'yii\grid\ActionColumn'], + ], + ]); ?> +
\ No newline at end of file diff --git a/backend/modules/notes/views/notes/update.php b/backend/modules/notes/views/notes/update.php new file mode 100644 index 0000000..3efc9bd --- /dev/null +++ b/backend/modules/notes/views/notes/update.php @@ -0,0 +1,17 @@ +title = 'Редактировать заметку: ' . $model->name; +$this->params['breadcrumbs'][] = ['label' => 'Заметки', 'url' => ['index']]; +$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]]; +$this->params['breadcrumbs'][] = 'Редактировать'; +?> +
+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/backend/modules/notes/views/notes/view.php b/backend/modules/notes/views/notes/view.php new file mode 100644 index 0000000..f236d2c --- /dev/null +++ b/backend/modules/notes/views/notes/view.php @@ -0,0 +1,52 @@ +title = $model->name; +$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' => [ + 'id', + 'name', + 'description:ntext', + 'created_at', + 'updated_at', + ], + ]) ?> + +

Дополнительные сведения

+ $additionalDataProvider, + 'layout' => "{items}", + 'columns' => [ + 'field.name:text:Поле', + [ + 'attribute' => 'value', + 'label' => 'Значение' + ], + ], + ]); ?> + +
diff --git a/backend/views/layouts/left.php b/backend/views/layouts/left.php index 71b6b5c..fe2f1d8 100755 --- a/backend/views/layouts/left.php +++ b/backend/views/layouts/left.php @@ -46,6 +46,7 @@ ['label' => 'Баланс', 'icon' => 'dollar', 'url' => ['/balance/balance'], 'active' => \Yii::$app->controller->id == 'balance'], ['label' => 'Отпуска', 'icon' => 'plane', 'url' => ['/holiday/holiday'], 'active' => \Yii::$app->controller->id == 'holiday'], ['label' => 'Доступы', 'icon' => '', 'url' => ['/accesses/accesses'], 'active' => \Yii::$app->controller->id == 'accesses'], + ['label' => 'Заметки', 'icon' => '', 'url' => ['/notes/notes'], 'active' => \Yii::$app->controller->id == 'notes'], /*['label' => 'Gii', 'icon' => 'file-code-o', 'url' => ['/gii']], ['label' => 'Debug', 'icon' => 'dashboard', 'url' => ['/debug']], diff --git a/common/models/FieldsValueNew.php b/common/models/FieldsValueNew.php index d386bde..6c085f5 100644 --- a/common/models/FieldsValueNew.php +++ b/common/models/FieldsValueNew.php @@ -22,6 +22,7 @@ class FieldsValueNew extends \yii\db\ActiveRecord const TYPE_PROJECT = 1; const TYPE_COMPANY = 2; const TYPE_BALANCE = 3; + const TYPE_NOTE = 4; /** * {@inheritdoc} */ diff --git a/common/models/Note.php b/common/models/Note.php new file mode 100644 index 0000000..572e8c5 --- /dev/null +++ b/common/models/Note.php @@ -0,0 +1,66 @@ + TimestampBehavior::class, + 'createdAtAttribute' => 'created_at', + 'updatedAtAttribute' => 'updated_at', + 'value' => new Expression('NOW()'), + ], + ]; + } + + /** + * {@inheritdoc} + */ + public function rules() + { + return [ + [['name'], 'required'], + [['description'], 'string'], + [['created_at', 'updated_at'], 'safe'], + [['name'], 'string', 'max' => 255], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'name' => 'Название', + 'description' => 'Описание', + 'created_at' => 'Created At', + 'updated_at' => 'Updated At', + ]; + } +} diff --git a/common/models/UseField.php b/common/models/UseField.php index 5e5d35c..c451261 100755 --- a/common/models/UseField.php +++ b/common/models/UseField.php @@ -22,6 +22,7 @@ class UseField extends \yii\db\ActiveRecord const USE_PROJECT = 1; const USE_COMPANY = 2; const USE_BALANCE = 3; + const USE_NOTE = 4; /** @@ -70,7 +71,8 @@ class UseField extends \yii\db\ActiveRecord self::USE_PROFILE => 'Профиль', self::USE_PROJECT => 'Проект', self::USE_COMPANY => 'Компания', - self::USE_BALANCE => 'Баланс' + self::USE_BALANCE => 'Баланс', + self::USE_NOTE => 'Заметка' ]; } diff --git a/common/models/UseStatus.php b/common/models/UseStatus.php index a7facca..f063694 100755 --- a/common/models/UseStatus.php +++ b/common/models/UseStatus.php @@ -22,6 +22,7 @@ class UseStatus extends \yii\db\ActiveRecord const USE_PROJECT = 1; const USE_COMPANY = 2; const USE_BALANCE = 3; + const USE_NOTE= 4; /** * {@inheritdoc} @@ -69,7 +70,8 @@ class UseStatus extends \yii\db\ActiveRecord self::USE_PROFILE => 'Профиль', self::USE_PROJECT => 'Проект', self::USE_COMPANY => 'Компания', - self::USE_BALANCE => 'Баланс' + self::USE_BALANCE => 'Баланс', + self::USE_NOTE => 'Заметка' ]; } diff --git a/console/migrations/m191205_092846_create_note_table.php b/console/migrations/m191205_092846_create_note_table.php new file mode 100644 index 0000000..0307443 --- /dev/null +++ b/console/migrations/m191205_092846_create_note_table.php @@ -0,0 +1,31 @@ +createTable('note', [ + 'id' => $this->primaryKey(), + 'name' => $this->string()->notNull(), + 'description' => $this->text(), + 'created_at' => $this->dateTime(), + 'updated_at' => $this->dateTime(), + ]); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropTable('note'); + } +}