public function actionView($id) { $note = Note::find()->where(['id' => $id])->with('comments')->with('user')->one(); if (!$note) { throw new NotFoundHttpException(); } if (Yii::$app->user->can('viewNote', ['note' => $note])) { $query = Note::find()->with('user'); $previousNote = $query->where(['<=', 'created_at', $note->created_at])->andWhere(['<', 'id', $note->id])->andWhere(['visibility' => Note::VIS_PUBLIC_LISTED])->orderBy('created_at DESC, id DESC')->one(); $nextNote = $query->where(['>=', 'created_at', $note->created_at])->andWhere(['>', 'id', $note->id])->andWhere(['visibility' => Note::VIS_PUBLIC_LISTED])->orderBy('created_at ASC, id ASC')->one(); $comment = new Comment(); if ($comment->load(Yii::$app->request->post()) && $comment->validate()) { $parentId = Yii::$app->request->post('parentId'); if ($parentId !== null && CommentClosure::find()->where(['child_id' => $parentId])->max('depth') >= Yii::$app->params['maxCommentsDepth']) { throw new ForbiddenHttpException(); } $comment->user_id = Yii::$app->user->getId(); $comment->note_id = $note->id; $comment->save(false); CommentClosure::insertComment($comment->id, $parentId); return $this->refresh(); } return $this->render('view', ['note' => $note, 'previousNote' => $previousNote, 'nextNote' => $nextNote, 'comment' => $comment]); } else { throw new ForbiddenHttpException(); } }
public function actionDelete($id) { if (Yii::$app->user->can('deleteComment')) { $noteId = $this->findComment($id)->note_id; Comment::deleteAll(['id' => CommentClosure::findChildrenIds($id)]); return $this->redirect(['note/view', 'id' => $noteId]); } else { throw new ForbiddenHttpException(); } }
public function getCommentClosures() { return $this->hasMany(CommentClosure::className(), ['parent_id' => 'id'])->where(['depth' => 1]); }