/**
  * Displays a single Post model.
  * Also added functionality of adding comments, related to particular Post model.
  * @param integer $id
  * @return mixed
  */
 public function actionView($id)
 {
     $comments = Comments::find()->where(['post_id' => $id])->all();
     $newComment = new Comments();
     if ($newComment->load(Yii::$app->request->post()) && $newComment->validate()) {
         $newComment->created_at = time();
         $newComment->post_id = $id;
         $newComment->save();
         return $this->redirect(['view', 'id' => $id]);
     } else {
         return $this->render('view', ['model' => $this->findModel($id), 'comments' => $comments, 'newComment' => $newComment]);
     }
 }
Beispiel #2
0
 function run()
 {
     // && $model->contact(Yii::$app->params['adminEmail'])
     $this->enable_form = $this->allow_comments();
     $comment = new Comments();
     $comment->parent_id = 0;
     if ($this->enable_form && $comment->load(Yii::$app->request->post()) && $comment->validate()) {
         Yii::$app->session->setFlash('commentFormSubmitted');
         $comment->active = true;
         $comment->user_id = Yii::$app->user->isGuest ? null : Yii::$app->user->identity->id;
         $comment->ip = ip2long(Yii::$app->request->getUserIP());
         $comment->agent = Yii::$app->request->getUserAgent();
         $comment->saveUploadedImage('file');
         $comment->save();
         return Yii::$app->getResponse()->redirect(Yii::$app->getRequest()->getUrl());
     }
     $query = new Query();
     $query->addSelect('c.*, f.filename, f.thumb_filename, f.size, u.username')->from([Comments::tableName() . ' c'])->leftJoin(User::tableName() . ' u', 'u.id = c.user_id')->leftJoin(Files::tableName() . ' f', 'f.id = c.image_id')->where(['c.active' => true])->andWhere(['c.post_id' => $this->post_id]);
     $comment->post_id = $this->post_id;
     return $this->render('comments/comments_box', ['comments' => $this->buildTree($query->all()), 'model' => $comment, 'enable' => $this->enable_form]);
 }