From 8bc8795316f6549fba5ec641fc19856adbc52075 Mon Sep 17 00:00:00 2001 From: Anastasia Date: Wed, 23 May 2018 13:01:50 +0300 Subject: [PATCH] - questions - comments --- backend/controllers/CommentController.php | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ backend/controllers/QuestionController.php | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ backend/views/comment/_form.php | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ backend/views/comment/update.php | 45 +++++++++++++++++++++++++++++++++++++++++++++ backend/views/layouts/main.php | 6 ++---- backend/views/layouts/menu_items.php | 5 +++++ backend/views/question/_form.php | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ backend/views/question/update.php | 45 +++++++++++++++++++++++++++++++++++++++++++++ common/models/Comment.php | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/models/Question.php | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ console/migrations/m180522_130149_create_question_table.php | 45 +++++++++++++++++++++++++++++++++++++++++++++ console/migrations/m180523_094805_create_table_comment.php | 45 +++++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 788 insertions(+), 4 deletions(-) create mode 100644 backend/controllers/CommentController.php create mode 100644 backend/controllers/QuestionController.php create mode 100644 backend/views/comment/_form.php create mode 100644 backend/views/comment/update.php create mode 100644 backend/views/question/_form.php create mode 100644 backend/views/question/update.php create mode 100644 common/models/Comment.php create mode 100644 common/models/Question.php create mode 100644 console/migrations/m180522_130149_create_question_table.php create mode 100644 console/migrations/m180523_094805_create_table_comment.php diff --git a/backend/controllers/CommentController.php b/backend/controllers/CommentController.php new file mode 100644 index 0000000..19376a3 --- /dev/null +++ b/backend/controllers/CommentController.php @@ -0,0 +1,144 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => [ 'POST' ], + ], + ], + 'access' => [ + 'class' => AccessControl::className(), + 'rules' => [ + [ + 'allow' => true, + 'roles' => [ '@' ], + ], + ], + ], + ]; + } + public function actions() + { + return [ + 'index' => [ + 'class' => Index::className(), + 'columns' => [ + 'name' => [ + 'type' => Index::ACTION_COL, + ], + 'email' => [ + 'type' => Index::STRING_COL + ], + 'status' => [ + 'type' => Index::STATUS_COL, + ], + 'created_at' => [ + 'type' => Index::DATETIME_COL, + ] + ], + 'model' => Comment::className(), + 'hasLanguage' => false, + 'enableMassDelete' => true, + 'modelPrimaryKey' => 'id', + 'defaultSort' => [ + 'created_at' => 'DESC', + ], + 'create' => false + ], + 'view' => [ + 'class' => View::className(), + 'model' => Comment::className(), + 'hasAlias' => false, + 'hasGallery' => false, + 'languageFields' => [ + ], + 'fields' => [ + [ + 'name' => 'name', + 'type' => Form::STRING, + ], + [ + 'name' => 'email', + 'type' => Form::STRING, + ], + [ + 'name' => 'comment', + 'type' => Form::TEXTAREA, + ], + [ + 'name' => 'created_at', + 'type' => Form::STRING, + ], + + ], + ], + 'delete' => [ + 'class' => Delete::className(), + ], + ]; + } + + public function findModel($id) + { + $model = Comment::find() + ->where([ 'id' => $id ]) + ->one(); + if ($model !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } + + public function deleteModel($id) + { + $category = Comment::find() + ->where( + [ + 'id' => $id, + ] + ) + ->one(); + + return $category->delete(); + } + + public function actionUpdate($id) + { + $model = $this->findModel($id); + + if ($model->load(\Yii::$app->request->post()) && $model->save()) { + return $this->redirect('index'); + } else { + return $this->render( + 'update', + [ + 'model' => $model, + ] + ); + } + } + } \ No newline at end of file diff --git a/backend/controllers/QuestionController.php b/backend/controllers/QuestionController.php new file mode 100644 index 0000000..a02749a --- /dev/null +++ b/backend/controllers/QuestionController.php @@ -0,0 +1,145 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => [ 'POST' ], + ], + ], + 'access' => [ + 'class' => AccessControl::className(), + 'rules' => [ + [ + 'allow' => true, + 'roles' => [ '@' ], + ], + ], + ], + ]; + } + public function actions() + { + return [ + 'index' => [ + 'class' => Index::className(), + 'columns' => [ + 'name' => [ + 'type' => Index::ACTION_COL, + ], + 'email' => [ + 'type' => Index::STRING_COL + ], + 'status' => [ + 'type' => Index::STATUS_COL, + ], + 'created_at' => [ + 'type' => Index::DATETIME_COL, + ] + ], + 'model' => Question::className(), + 'hasLanguage' => false, + 'enableMassDelete' => true, + 'modelPrimaryKey' => 'id', + 'defaultSort' => [ + 'created_at' => 'DESC', + ], + 'create' => false + ], + 'view' => [ + 'class' => View::className(), + 'model' => Question::className(), + 'hasAlias' => false, + 'hasGallery' => false, + 'languageFields' => [ + ], + 'fields' => [ + [ + 'name' => 'name', + 'type' => Form::STRING, + ], + [ + 'name' => 'email', + 'type' => Form::STRING, + ], + [ + 'name' => 'question', + 'type' => Form::TEXTAREA, + ], + [ + 'name' => 'created_at', + 'type' => Form::STRING, + ], + + ], + ], + 'delete' => [ + 'class' => Delete::className(), + ], + ]; + } + + public function findModel($id) + { + $model = Question::find() + ->where([ 'id' => $id ]) + ->one(); + if ($model !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } + + public function deleteModel($id) + { + $category = Question::find() + ->where( + [ + 'id' => $id, + ] + ) + ->one(); + + return $category->delete(); + } + + public function actionUpdate($id) + { + $model = $this->findModel($id); + + $model->scenario = Question::SCENARIO_ANSWER; + if ($model->load(\Yii::$app->request->post()) && $model->save()) { + return $this->redirect('index'); + } else { + return $this->render( + 'update', + [ + 'model' => $model, + ] + ); + } + } + } \ No newline at end of file diff --git a/backend/views/comment/_form.php b/backend/views/comment/_form.php new file mode 100644 index 0000000..3b6ca38 --- /dev/null +++ b/backend/views/comment/_form.php @@ -0,0 +1,54 @@ +registerJs($js, View::POS_READY); +?> + +
+ + + + field($model, 'name') + ->textInput([ 'maxlength' => true ]) ?> + + field($model, 'email') + ->textInput([ 'maxlength' => true ]) ?> + + field($model, 'comment') + ->textarea([ 'rows' => 6 ]) ?> + + field($model, 'status') + ->checkbox( + [ + 'class' => 'switchery', + ] + ) ?> + +
+ isNewRecord ? Yii::t('core', 'Create') : Yii::t('core', 'Update'), + [ 'class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary' ] + ) ?> +
+ + + +
diff --git a/backend/views/comment/update.php b/backend/views/comment/update.php new file mode 100644 index 0000000..0188871 --- /dev/null +++ b/backend/views/comment/update.php @@ -0,0 +1,45 @@ +title = Yii::t( + 'app', + 'Update {modelClass}: ', + [ + 'modelClass' => 'Comment', + ] + ) . $model->name; + $this->params[ 'breadcrumbs' ][] = [ + 'label' => Yii::t('app', 'Comments'), + 'url' => [ 'index' ], + ]; + $this->params[ 'breadcrumbs' ][] = [ + 'label' => $model->name, + 'url' => [ + 'view', + 'id' => $model->id, + ], + ]; + $this->params[ 'breadcrumbs' ][] = Yii::t('core', 'Update'); +?> + + $this->title, + 'options' => [ + 'class' => 'x_panel comment-update', + ], + ] +) ?> + +render( + '_form', + [ + 'model' => $model, + ] +) ?> + + diff --git a/backend/views/layouts/main.php b/backend/views/layouts/main.php index edabbd4..7c46fd6 100755 --- a/backend/views/layouts/main.php +++ b/backend/views/layouts/main.php @@ -7,16 +7,13 @@ use artbox\core\assets\ArtboxCoreAsset; use artbox\core\models\User; - use artbox\core\models\UserData; use artbox\core\widgets\FeedbackWidget; use artbox\core\widgets\FlashWidget; - use artbox\order\assets\OrderAsset; - use noam148\imagemanager\components\ImageManagerGetPath; use yii\bootstrap\Html; + use yii\helpers\Url; use yii\web\View; use yii\widgets\Breadcrumbs; use yiister\gentelella\widgets\Menu; - use backend\assets\AppAsset; ArtboxCoreAsset::register($this); // AppAsset::register($this); @@ -169,6 +166,7 @@
  • +
  • Вопрос-ответ
  • diff --git a/backend/views/layouts/menu_items.php b/backend/views/layouts/menu_items.php index 0f9b6cf..e9a88a6 100755 --- a/backend/views/layouts/menu_items.php +++ b/backend/views/layouts/menu_items.php @@ -50,6 +50,11 @@ 'url' => [ '/service/index' ], ], + [ + 'label' => \Yii::t('app', 'Comments'), + 'url' => [ '/comment/index' ], + + ], ], ], diff --git a/backend/views/question/_form.php b/backend/views/question/_form.php new file mode 100644 index 0000000..d1b8686 --- /dev/null +++ b/backend/views/question/_form.php @@ -0,0 +1,57 @@ +registerJs($js, View::POS_READY); +?> + +
    + + + + field($model, 'name') + ->textInput([ 'maxlength' => true ]) ?> + + field($model, 'email') + ->textInput([ 'maxlength' => true ]) ?> + + field($model, 'question') + ->textarea([ 'rows' => 6 ]) ?> + + field($model, 'answer') + ->textarea([ 'rows' => 6 ]) ?> + + field($model, 'status') + ->checkbox( + [ + 'class' => 'switchery', + ] + ) ?> + +
    + isNewRecord ? Yii::t('core', 'Create') : Yii::t('core', 'Update'), + [ 'class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary' ] + ) ?> +
    + + + +
    diff --git a/backend/views/question/update.php b/backend/views/question/update.php new file mode 100644 index 0000000..b52dc90 --- /dev/null +++ b/backend/views/question/update.php @@ -0,0 +1,45 @@ +title = Yii::t( + 'app', + 'Update {modelClass}: ', + [ + 'modelClass' => 'Question', + ] + ) . $model->name; + $this->params[ 'breadcrumbs' ][] = [ + 'label' => Yii::t('app', 'Questions'), + 'url' => [ 'index' ], + ]; + $this->params[ 'breadcrumbs' ][] = [ + 'label' => $model->name, + 'url' => [ + 'view', + 'id' => $model->id, + ], + ]; + $this->params[ 'breadcrumbs' ][] = Yii::t('core', 'Update'); +?> + + $this->title, + 'options' => [ + 'class' => 'x_panel question-update', + ], + ] +) ?> + +render( + '_form', + [ + 'model' => $model, + ] +) ?> + + diff --git a/common/models/Comment.php b/common/models/Comment.php new file mode 100644 index 0000000..c18ec4b --- /dev/null +++ b/common/models/Comment.php @@ -0,0 +1,59 @@ + null], + [['service_id', 'created_at', 'updated_at'], 'integer'], + [['name', 'email'], 'string', 'max' => 255], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => Yii::t('app', 'ID'), + 'name' => Yii::t('app', 'Name'), + 'email' => Yii::t('app', 'Email'), + 'comment' => Yii::t('app', 'Comment'), + 'status' => Yii::t('app', 'Status'), + 'service_id' => Yii::t('app', 'Service ID'), + 'created_at' => Yii::t('app', 'Created At'), + 'updated_at' => Yii::t('app', 'Updated At'), + ]; + } +} diff --git a/common/models/Question.php b/common/models/Question.php new file mode 100644 index 0000000..96cc287 --- /dev/null +++ b/common/models/Question.php @@ -0,0 +1,142 @@ + [ + 'name', + 'email', + 'question', + ], + self::SCENARIO_ANSWER => [ + 'answer', + 'status' + ], + ] + ); + return $scenarios; + } + /** + * @inheritdoc + */ + public function behaviors() + { + return [ + [ + 'class' => TimestampBehavior::className(), + ], + ]; + } + /** + * {@inheritdoc} + */ + public function rules() + { + return [ + [ + ['name', 'email', 'question'], 'required', 'on' => self::SCENARIO_QUESTION + ], + [ + [ 'status' ], + 'boolean', + ], + [ + [ + 'service_id', + 'created_at', + 'updated_at', + ], + 'default', + 'value' => null, + ], + [ + [ + 'service_id', + 'created_at', + 'updated_at', + ], + 'integer', + ], + [ + [ + 'name', + 'email', + + ], + 'string', + 'max' => 255, + ], + [ + ['question', 'answer'], 'string' + ], + [ + [ 'service_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => Service::className(), + 'targetAttribute' => [ 'service_id' => 'id' ], + ], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => Yii::t('app', 'ID'), + 'name' => Yii::t('app', 'Name'), + 'email' => Yii::t('app', 'Email'), + 'question' => Yii::t('app', 'Question'), + 'status' => Yii::t('app', 'Status'), + 'service_id' => Yii::t('app', 'Service ID'), + 'created_at' => Yii::t('app', 'Created At'), + 'updated_at' => Yii::t('app', 'Updated At'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getService() + { + return $this->hasOne(Service::className(), [ 'id' => 'service_id' ]); + } + } diff --git a/console/migrations/m180522_130149_create_question_table.php b/console/migrations/m180522_130149_create_question_table.php new file mode 100644 index 0000000..2bff3e4 --- /dev/null +++ b/console/migrations/m180522_130149_create_question_table.php @@ -0,0 +1,45 @@ +createTable('question', [ + 'id' => $this->primaryKey(), + 'name' => $this->string(), + 'email' => $this->string(), + 'question' => $this->text(), + 'answer' => $this->text(), + 'status' => $this->boolean(), + 'service_id' => $this->integer(), + 'created_at' => $this->integer(), + 'updated_at' => $this->integer(), + + ]); + + $this->addForeignKey('question_service_fk', + 'question', + 'service_id', + 'service', + 'id', + 'SET NULL', + 'CASCADE'); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropForeignKey('question_service_fk', 'question'); + $this->dropTable('question'); + } +} diff --git a/console/migrations/m180523_094805_create_table_comment.php b/console/migrations/m180523_094805_create_table_comment.php new file mode 100644 index 0000000..5647677 --- /dev/null +++ b/console/migrations/m180523_094805_create_table_comment.php @@ -0,0 +1,45 @@ +createTable('comment', [ + 'id' => $this->primaryKey(), + 'name' => $this->string(), + 'email' => $this->string(), + 'comment' => $this->text(), + 'status' => $this->boolean(), + 'service_id' => $this->integer(), + 'created_at' => $this->integer(), + 'updated_at' => $this->integer(), + + ]); + + $this->addForeignKey('comment_service_fk', + 'question', + 'service_id', + 'service', + 'id', + 'SET NULL', + 'CASCADE'); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropForeignKey('comment_service_fk', 'question'); + $this->dropTable('comment'); + } + +} -- libgit2 0.21.4