/** * Displays a single Post model. * * @param integer $id * @return mixed */ public function actionView($slug) { try { Yii::trace('Trace :' . __METHOD__, __METHOD__); $model = Post::find()->where(['slug' => $slug])->one(); if ($model === null) { throw new NotFoundHttpException(); } $response = null; $commentForm = new Comment(['scenario' => 'create']); if ($commentForm->load($_POST) === true && $commentForm->validate() === true) { $commentForm->created = Yii::$app->formatter->asDateTime('now', 'php:Y-m-d H:i:s'); $commentForm->post_id = $model->id; if ($commentForm->save() === true) { $response = $this->redirect(['/post/view', 'slug' => $slug]); } } //get all comments $comments = $model->getComments()->orderBy('created DESC')->all(); if ($response === null) { $response = $this->render('view', ['model' => $model, 'comments' => $comments, 'commentForm' => $commentForm]); } return $response; } catch (Exception $e) { Yii::error($e->getMessage(), __METHOD__); throw $e; } }
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 actionReply($id) { $request = Yii::$app->getRequest(); $me = Yii::$app->getUser()->getIdentity(); $topic = $this->findTopicModel($id, ['node', 'author']); if (!$me->canReply($topic)) { throw new ForbiddenHttpException('您没有权限回复或此主题已关闭回复。'); } $model = new Comment(); if ($model->load($request->post()) && $model->validate()) { $model->user_id = $me->id; $cid = new \app\models\Commentid(['id' => null]); $cid->save(false); $model->id = $cid->id; $model->link('topic', $topic); $this->redirect(Topic::getRedirectUrl($id, $model->position)); } return $this->render('add', ['comment' => $model, 'topic' => Util::convertModelToArray($topic)]); }