1, ], [ [ 'comment_pid' ], 'exist', 'targetAttribute' => 'comment_id', 'filter' => [ 'model' => $this->model, 'model_id' => $this->model_id, ], ], ]; } public function scenarios() { return [ self::SCENARIO_GUEST => [ 'user_name', 'user_email', 'text', 'comment_pid', ], self::SCENARIO_USER => [ 'text', 'comment_pid', ], ]; } /** * @inheritdoc */ public function behaviors() { return [ [ 'class' => \yii\behaviors\TimestampBehavior::className(), 'createdAtAttribute' => 'date_add', 'updatedAtAttribute' => 'date_update', 'value' => new \yii\db\Expression('NOW()'), ], ]; } public function afterSave($insert, $changedAttributes) { if($this->model == User::className()) { if($user = User::findOne($this->model_id)) { /** * @var User $user */ $user->updateRating(); } } parent::afterSave($insert, $changedAttributes); } /** * @inheritdoc */ public static function tableName() { return '{{%comment}}'; } /** * @inheritdoc */ public function attributeLabels() { return [ 'text' => \Yii::t('app', 'Комментарий'), 'user_name' => \Yii::t('app', 'Имя'), 'user_email' => \Yii::t('app', 'Email'), ]; } public function getGuestComment() { return $this->guestComment; } public function setGuestComment($value) { $this->guestComment = $value; } /** * @param string $model * @param integer $model_id * * @return ActiveQuery */ public static function getComments($model, $model_id) { return self::find() ->where([ 'comment.model' => $model, 'comment.model_id' => $model_id, 'comment.status' => 1, ]) ->with('rating'); } public function postComment() { if($this->checkCreate()) { if(!empty($this->comment_pid) && !$this->checkReply()) { $this->addError('comment_id', 'You can`t reply to this message'); return false; } if($this->insert()) { $this->clearSafe(); return true; } else { return false; } } $this->addError('comment_id', 'You can`t post comment here'); return false; } public function updateComment() { if($this->checkUpdate()) { if(empty( $this->comment_id )) { $this->addError('comment_id', 'Comment ID not found'); return false; } else { if($this->update()) { // $this->clearSafe(); Clears safe attributes after AJAX update return true; } else { return false; } } } else { $this->addError('comment_id', 'You can`t update this post'); return false; } } public function deleteComment() { if($this->checkDelete()) { if(empty( $this->comment_id )) { $this->addError('comment_id', 'Comment ID not found'); return false; } else { if($this->user_id == \Yii::$app->user->id) { if($this->delete()) { return true; } else { $this->addError('comment_id', 'Can\'t delete post.'); return false; } } else { if($this->status == self::STATUS_DELETED) { return false; } $this->status = self::STATUS_DELETED; if($this->update()) { $this->clearSafe(); return true; } else { return false; } } } } else { $this->addError('comment_id', 'You can`t delete this post'); return false; } } 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) { return false; } else { return $this->allowReply; } } protected function clearSafe($setNew = true) { $safe = $this->safeAttributes(); $count = count($safe); $values = array_fill(0, $count, NULL); $result = array_combine($safe, $values); $this->setAttributes($result); $this->setIsNewRecord($setNew); } public function getParent() { return $this->hasOne(self::className(), [ 'comment_id' => 'comment_pid' ]); } public function getAuthorName() { if(!empty( $this->author )) { return $this->author->name; } else { return $this->user_name; } } public function getAuthor($guestMark = '') { if(!empty( $this->user )) { return $this->user->name; } else { $name = $this->user_name; if(!empty( $guestMark )) { $name .= $guestMark; } return $name; } } public function checkRating() { $rating = $this->hasOne(\common\modules\comment\models\Rating::className(), [ 'model_id' => 'comment_id', ]) ->andWhere([ 'model' => $this->className(), ]) ->one(); if(!$rating instanceof \common\modules\comment\models\Rating && !empty( $this->primaryKey )) { $rating = new \common\modules\comment\models\Rating([ 'model' => $this->className(), 'model_id' => $this->comment_id, 'user_id' => $this->user_id, ]); $rating->save(); } } public function getRating() { $this->checkRating(); return $this->hasOne(\common\modules\comment\models\Rating::className(), [ 'model_id' => 'comment_id', ]) ->andWhere([ 'model' => $this->className() ]); } public function hasRating($return = true) { $rating = $this->hasOne(\common\modules\comment\models\Rating::className(), [ 'model_id' => 'comment_id', ]) ->andWhere([ 'model' => $this->className() ]) ->andWhere([ 'not', [ 'value' => NULL ], ]) ->one(); if($return) { return $rating; } else { return $rating ? true : false; } } public function getUser() { return $this->hasOne(User::className(), [ 'id' => 'user_id' ]); } public function buildButtons( $buttons = [ 'delete', 'update', 'reply', ] ) { if(in_array('delete', $buttons)) { if($this->checkDelete()) { $this->buttons[ 'delete' ] = Url::to([ 'artbox-comment/delete', 'comment_id' => $this->comment_id, ]); } } if(in_array('update', $buttons)) { if($this->checkUpdate()) { $this->buttons[ 'update' ] = Url::to([ 'artbox-comment/update', 'comment_id' => $this->comment_id, ]); } } if(in_array('reply', $buttons)) { if($this->checkReply()) { $this->buttons[ 'reply' ] = Url::to([ 'artbox-comment/reply', 'comment_id' => $this->comment_id, ]); } } } public function getAllowReply() { return $this->hasAttribute('comment_pid'); } }