diff --git a/.gitignore b/.gitignore index 7773b6d..5ac7134 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +/frontend/web/js/Validation.ts +/frontend/web/js/typescript.ts /node_modules # yii console command /yii diff --git a/common/modules/comment/interfaces/CommentInterface.php b/common/modules/comment/interfaces/CommentInterface.php index 471dddf..352c423 100644 --- a/common/modules/comment/interfaces/CommentInterface.php +++ b/common/modules/comment/interfaces/CommentInterface.php @@ -4,7 +4,7 @@ interface CommentInterface { public function load($data, $formName = null); public function formName(); - public function getComments($model, $model_id); + public static function getComments($model, $model_id); public function postComment(); public function deleteComment(); public function updateComment(); diff --git a/common/modules/comment/models/Comment.php b/common/modules/comment/models/Comment.php index 24cf9bb..a1fe7f5 100644 --- a/common/modules/comment/models/Comment.php +++ b/common/modules/comment/models/Comment.php @@ -173,9 +173,9 @@ * * @return ActiveQuery */ - public function getComments($model, $model_id) + public static function getComments($model, $model_id) { - return $this->find() + return self::find() ->where([ 'comment.model' => $model, 'comment.model_id' => $model_id, @@ -258,8 +258,9 @@ return true; } else { return \Yii::$app->user->can(\common\modules\comment\Permissions::CREATE, [ - 'model' => $this->model, - 'model_id' => $this->model_id, + 'model' => $this->model, + 'model_id' => $this->model_id, + 'comment_model' => $this, ]); } } @@ -333,11 +334,11 @@ public function getAuthor($guestMark = '') { - if(!empty($this->user)) { + if(!empty( $this->user )) { return $this->user->name; } else { $name = $this->user_name; - if(!empty($guestMark)) { + if(!empty( $guestMark )) { $name .= $guestMark; } return $name; @@ -395,8 +396,13 @@ return $this->hasOne(User::className(), [ 'id' => 'user_id' ]); } - public function buildButtons($buttons = ['delete', 'update', 'reply']) - { + public function buildButtons( + $buttons = [ + 'delete', + 'update', + 'reply', + ] + ) { if(in_array('delete', $buttons)) { if($this->checkDelete()) { $this->buttons[ 'delete' ] = Url::to([ diff --git a/common/modules/comment/models/CommentProject.php b/common/modules/comment/models/CommentProject.php index 20411d9..f1cd1ec 100644 --- a/common/modules/comment/models/CommentProject.php +++ b/common/modules/comment/models/CommentProject.php @@ -222,7 +222,7 @@ * * @return ActiveQuery */ - public function getComments($model, $model_id) + public static function getComments($model, $model_id) { /** * @var User $user @@ -231,7 +231,7 @@ if(!empty($user)) { $project = Project::findOne($model_id); if(!empty($project) && $user->id == $project->user_id) { - return $this->find() + return self::find() ->where([ 'comment_project.model' => $model, 'comment_project.model_id' => $model_id, @@ -240,7 +240,7 @@ ->with('currency', 'user', 'user.userInfo', 'user.companyInfo', 'user.comments'); } } - $query = $this->find() + $query = self::find() ->where([ 'comment_project.model' => $model, 'comment_project.model_id' => $model_id, diff --git a/common/modules/comment/models/CommentProjectAnswer.php b/common/modules/comment/models/CommentProjectAnswer.php new file mode 100644 index 0000000..e8f9ec6 --- /dev/null +++ b/common/modules/comment/models/CommentProjectAnswer.php @@ -0,0 +1,174 @@ + 1, + ], + [ + [ 'comment_pid' ], + 'exist', + 'targetAttribute' => 'comment_id', + 'filter' => [ + 'model' => $this->model, + 'model_id' => $this->model_id, + ], + ], + ]; + } + + /** + * @param string $model + * @param integer $model_id + * + * @return ActiveQuery + */ + public static function getComments($model, $model_id) + { + $query = self::find() + ->where([ + 'comment.model' => $model, + 'comment.model_id' => $model_id, + 'comment.status' => 1, + 'comment.comment_pid' => NULL, + ]) + ->with('parent'); + $project = Project::findOne($model_id); + $user = \Yii::$app->user; + if(empty( $user ) || $project->user_id != $user->id) { + $query->innerJoin([ 'child' => 'comment' ], 'comment.comment_id = child.comment_pid') + ->andWhere([ + 'not', + [ 'child.comment_id' => NULL ], + ]); + } + return $query; + } + + public function checkCreate() + { + if($this->getGuestComment()) { + return true; + } else { + return \Yii::$app->user->can(\common\modules\comment\Permissions::CREATE, [ + 'model' => $this->model, + 'model_id' => $this->model_id, + 'comment_model' => $this, + ]); + } + } + + public function checkUpdate() + { + if($this->scenario == self::SCENARIO_GUEST) { + return false; + } else { + return \Yii::$app->user->can(\common\modules\comment\Permissions::UPDATE, [ + 'model' => $this->model, + 'model_id' => $this->model_id, + 'comment' => $this, + ]) || \Yii::$app->user->can(\common\modules\comment\Permissions::UPDATE_OWN, [ + 'model' => $this->model, + 'model_id' => $this->model_id, + 'comment' => $this, + ]); + } + } + + public function checkDelete() + { + if($this->scenario == self::SCENARIO_GUEST) { + return false; + } else { + return ( \Yii::$app->user->can(\common\modules\comment\Permissions::DELETE, [ + 'model' => $this->model, + 'model_id' => $this->model_id, + 'comment' => $this, + ]) || \Yii::$app->user->can(\common\modules\comment\Permissions::DELETE_OWN, [ + 'model' => $this->model, + 'model_id' => $this->model_id, + 'comment' => $this, + ]) ); + } + } + + public function checkReply() + { + if($this->scenario != self::SCENARIO_GUEST) { + if(!$this->isNewRecord && empty( $this->comment_pid )) { + $project = Project::findOne($this->model_id); + if($project->user_id == \Yii::$app->user->id) { + return true; + } + } + } + return false; + } + + public function getChild() + { + return $this->hasOne($this->className(), [ 'comment_pid' => 'comment_id' ]); + } + + } diff --git a/common/modules/comment/rbac/ArtboxCommentCreateRule.php b/common/modules/comment/rbac/ArtboxCommentCreateRule.php index 9dbf0a3..10397bc 100644 --- a/common/modules/comment/rbac/ArtboxCommentCreateRule.php +++ b/common/modules/comment/rbac/ArtboxCommentCreateRule.php @@ -2,6 +2,7 @@ namespace common\modules\comment\rbac; + use common\modules\comment\models\CommentProject; use yii\rbac\Rule; class ArtboxCommentCreateRule extends Rule @@ -11,7 +12,7 @@ public function execute($user, $item, $params) { - if($params[ 'model' ] == \common\models\Project::className()) { + if($params[ 'model' ] == \common\models\Project::className() && !empty($params['comment_model']) && $params['comment_model'] == CommentProject::className()) { return $this->checkProject($user, $item, $params); } return true; diff --git a/common/modules/comment/widgets/CommentWidget.php b/common/modules/comment/widgets/CommentWidget.php index 726bf94..bd7b278 100644 --- a/common/modules/comment/widgets/CommentWidget.php +++ b/common/modules/comment/widgets/CommentWidget.php @@ -57,6 +57,8 @@ */ public $class_options = [ ]; + public $provider_options = [ ]; + /** * @var bool Wheather to display comment list */ @@ -76,6 +78,7 @@ * @var bool Whether to allow one user post multiple comments */ public $allow_multiple = true; + public $allow_reply = true; /** @@ -197,10 +200,10 @@ if($this->display_comment_form && $this->comment_class->checkCreate()) { $tag = ArrayHelper::remove($this->form_options, 'tag', 'div'); $view = ArrayHelper::remove($this->form_options, 'view'); - if(!empty($this->form_options['class'])) { - $this->form_options['class'] .= ' '.self::$baseClass['form_container']; + if(!empty( $this->form_options[ 'class' ] )) { + $this->form_options[ 'class' ] .= ' ' . self::$baseClass[ 'form_container' ]; } else { - $this->form_options['class'] = self::$baseClass['form_container']; + $this->form_options[ 'class' ] = self::$baseClass[ 'form_container' ]; } $this->parts[ 'form' ] = Html::tag($tag, $this->renderForm($view), $this->form_options); } @@ -209,15 +212,15 @@ public function createDataProvider() { $this->dataProvider = new \yii\data\ActiveDataProvider([ - 'query' => $this->comment_class->getComments($this->model, $this->model_id), - 'sort' => new Sort([ + 'query' => ArrayHelper::remove($this->provider_options, 'query', $this->comment_class->getComments($this->model, $this->model_id)), + 'sort' => ArrayHelper::remove($this->provider_options, 'sort', new Sort([ 'defaultOrder' => [ 'date_add' => SORT_DESC, ], - ]), - 'pagination' => [ + ])), + 'pagination' => ArrayHelper::remove($this->provider_options, 'query', [ 'pageSize' => 10, - ], + ]), ]); } @@ -258,10 +261,10 @@ $template = preg_replace('/{list}/', ArrayHelper::remove($parts, 'list', ''), $template); $template = preg_replace('/{form}/', ArrayHelper::remove($parts, 'form', ''), $template); $tag = ArrayHelper::remove($options, 'tag', 'div'); - if(!empty($options['class'])) { - $options['class'] .= ' '.self::$baseClass['widget_container']; + if(!empty( $options[ 'class' ] )) { + $options[ 'class' ] .= ' ' . self::$baseClass[ 'widget_container' ]; } else { - $options['class'] = self::$baseClass['widget_container']; + $options[ 'class' ] = self::$baseClass[ 'widget_container' ]; } return Html::tag($tag, $template, $options); } diff --git a/common/modules/comment/widgets/views/_question_comment_view.php b/common/modules/comment/widgets/views/_question_comment_view.php new file mode 100644 index 0000000..5ec0ceb --- /dev/null +++ b/common/modules/comment/widgets/views/_question_comment_view.php @@ -0,0 +1,61 @@ +user; + $model->buildButtons(['reply']); +?> +
+ +