DefaultController.php 5.92 KB
<?php
    
    namespace artbox\webcomment\controllers;
    
    use artbox\webcomment\models\CommentModel;
    use artbox\webcomment\models\RatingModel;
    use artbox\webcomment\Module;
    use yii\filters\AccessControl;
    use yii\filters\VerbFilter;
    use yii\helpers\Json;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    use yii\web\Response;
    
    class DefaultController extends Controller
    {
        
        /**
         * Returns a list of behaviors that this component should behave as.
         *
         * @return array
         */
        public function behaviors()
        {
            return [
                'verbs'  => [
                    'class'   => VerbFilter::className(),
                    'actions' => [
                        'create' => [ 'post' ],
                        'delete' => [
                            'post',
                            'delete',
                        ],
                    ],
                ],
                'access' => [
                    'class' => AccessControl::className(),
                    'only'  => [ 'delete' ],
                    'rules' => [
                        [
                            'allow' => true,
                            'roles' => [ '@' ],
                        ],
                    ],
                ],
            ];
        }
    
        /**
         * Create comment.
         *
         * @param string $entity
         *
         * @return array|null|\yii\web\Response
         * @throws \yii\base\InvalidConfigException
         */
        public function actionCreate(string $entity)
        {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            /* @var $module Module */
            $module = \Yii::$app->getModule(Module::$name);
            if (!$module) {
                Module::registerMe();
            }
            $entity_data_json = \Yii::$app->getSecurity()
                                          ->decryptByKey($entity, $module::$encryptionKey);
            if ($entity_data_json != false) {
                $entity_data = Json::decode($entity_data_json);
                $commentModelClass = $module->commentModelClass;
                /**
                 * @var CommentModel $model
                 */
                $model = new $commentModelClass(
                    [
                        'scenario' => \Yii::$app->user->getIsGuest(
                        ) ? CommentModel::SCENARIO_GUEST : CommentModel::SCENARIO_USER,
                    ]
                );
                if ($model->load(\Yii::$app->request->post())) {
                    $model->setAttributes($entity_data);
                    if ($model->save()) {
                        if (empty($model->parent_id) && $module->enableRating) {
                            $ratingModelClass = $module->ratingModelClass;
                            /**
                             * @var RatingModel $rating
                             */
                            $rating = new $ratingModelClass(
                                [
                                    'model'    => $model::className(),
                                    'model_id' => $model->primaryKey,
                                ]
                            );
                            if ($rating->load(\Yii::$app->request->post())) {
                                $rating->save();
                            }
                        }
                        \Yii::$app->session->setFlash(
                            'artbox_comment_success',
                            \Yii::t('artbox-comment', 'Comment posted')
                        );
                        return [ 'status' => 'success' ];
                    } else {
                        return [
                            'status' => 'error',
                            'errors' => $model->getFirstErrors(),
                        ];
                    }
                }
            }
            return [
                'status'  => 'error',
                'message' => \Yii::t('artbox-comment', 'Oops, something went wrong. Please try again later.'),
            ];
        }
        
        /**
         * Delete comment.
         *
         * @param integer $id Comment ID
         *
         * @return array Comment text
         */
        public function actionDelete($id)
        {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            $model = $this->findModel($id);
            if ($model->deleteComment()) {
                return [
                    'status'  => 'success',
                    'message' => \Yii::t('artbox-comment', 'Comment has been deleted.'),
                ];
            } else {
                \Yii::$app->response->setStatusCode(500);
                return [
                    'status'  => 'error',
                    'message' => \Yii::t('artbox-comment', 'Comment has not been deleted. Please try again!'),
                ];
            }
        }
        
        /**
         * Find model by ID.
         *
         * @param integer|array $id Comment ID
         *
         * @return \artbox\webcomment\models\CommentModel
         * @throws \yii\base\InvalidConfigException
         * @throws \yii\web\NotFoundHttpException
         */
        protected function findModel(int $id): CommentModel
        {
            /**
             * @var Module $module
             */
            $module = \Yii::$app->getModule(Module::$name);
            if (!$module) {
                Module::registerMe();
            }
            /** @var CommentModel $model */
            $commentModelClass = $module->commentModelClass;
            if (method_exists($commentModelClass, 'findOne')) {
                if ($model = $commentModelClass::findOne($id) !== null) {
                    return $model;
                }
            }
            throw new NotFoundHttpException(\Yii::t('artbox-comment', 'The requested page does not exist.'));
        }
    }