Example #1
0
 /**
  * Creates a new Comment model.
  * For ajax request will return json object
  * and for non-ajax request if creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $request = Yii::$app->request;
     $model = new Comment();
     if ($request->isAjax) {
         /*
          *   Process for ajax request
          */
         Yii::$app->response->format = Response::FORMAT_JSON;
         if ($request->isGet) {
             return ['code' => '200', 'message' => 'OK', 'data' => $this->renderPartial('create', ['model' => $model])];
         } else {
             if ($model->load($request->post()) && $model->save()) {
                 return ['code' => '200', 'message' => 'Create Comment success'];
             } else {
                 return ['code' => '400', 'message' => 'Validate error', 'data' => $this->renderPartial('create', ['model' => $model])];
             }
         }
     } else {
         /*
          *   Process for non-ajax request
          */
         if ($model->load($request->post()) && $model->save()) {
             return $this->redirect(['view', 'id' => $model->id]);
         } else {
             return $this->render('create', ['model' => $model]);
         }
     }
 }
Example #2
0
 public function actionAddComment()
 {
     if (\Yii::$app->request->isAjax) {
         $model = new Comment();
         if (\Yii::$app->user->isGuest) {
             $model->scenario = 'isGuest';
         }
         $data = ['error' => true];
         if ($model->load(\Yii::$app->request->post())) {
             $parentID = \Yii::$app->request->post('parent_id');
             $root = $model->makeRootIfNotExist();
             if (!$parentID) {
                 $model->appendTo($root);
             } else {
                 $parent = Comment::find()->where(['id' => $parentID])->one();
                 $model->appendTo($parent);
             }
             $articleModel = Article::findOne($model->model_id);
             $data = ['replaces' => [['what' => '#comments', 'data' => $this->renderAjax('@app/themes/basic/modules/article/views/default/_comments', ['model' => $articleModel])]]];
         }
         return Json::encode($data);
     } else {
         throw new NotFoundHttpException(\Yii::t('app', 'Page not found'));
     }
 }
 public function actionAddComment()
 {
     $comment = new Comment();
     $comment->load(Yii::$app->request->post(), '');
     if ($comment->save()) {
         return $comment;
     }
     return $comment->getErrors();
 }
 /**
  * Creates a new Comment model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Comment();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Example #5
0
 /**
  * Creates a new Comment model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     //if(!Yii::$app->user->can('createYourAuth')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth'));
     $model = new Comment();
     $model->loadDefaultValues();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Example #6
0
 public function actionComment($id)
 {
     $model = new Comment();
     $model->load(Yii::$app->request->post());
     $model->article_id = $id;
     if ($model->validate()) {
         $model->save();
         Yii::$app->session->setFlash("success", "Ок");
     }
     $this->redirect(["article/view", "id" => $id]);
 }
Example #7
0
 public function actionView($slug)
 {
     $commentForm = new Comment();
     if ($commentForm->load(Yii::$app->request->post())) {
         if ($commentForm->save()) {
             Yii::$app->session->setFlash('success', '评论发表成功');
         } else {
             Yii::$app->session->setFlash('error', '评论发表失败');
         }
     }
     return $this->render('view', ['article' => Page::getInstance()->getBySlug($slug), 'commentForm' => $commentForm]);
 }
Example #8
0
 public function actionCreate()
 {
     $model = new Comment();
     $model->load(\Yii::$app->request->post());
     $model->user_id = \Yii::$app->user->id;
     $returnUrl = \Yii::$app->request->getReferrer();
     if ($model->save()) {
         \Yii::$app->session->setFlash('success', '评论成功!');
     } else {
         \Yii::$app->session->setFlash('error', '评论失败!');
     }
     return $this->redirect($returnUrl);
 }
Example #9
0
 /**
  * Displays homepage.
  *
  * @return mixed
  */
 public function actionIndex()
 {
     $model = new Comment();
     if ($model->load(Yii::$app->request->post())) {
         $model->is_active = 0;
         $model->created_at = date('Y-m-d H:i:s');
         $model->updated_at = date('Y-m-d H:i:s');
         $model->save();
         Yii::$app->session->setFlash('success', 'Thank you! Your comment has been added successfully. It will be available after moderation ;)');
         return $this->redirect('/');
     }
     return $this->render('comments', ['model' => $model]);
 }
 /**
  * Creates a new Comment model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  * @throws NotFoundHttpException
  */
 public function actionCreate()
 {
     $model = new Comment();
     if (Yii::$app->request->isGet) {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
     $model->author_id = Yii::$app->user->id;
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['../post/view', 'id' => $model->post_id]);
     } else {
         return $this->render('../post/view', ['model' => Post::findOne($model->post_id)]);
     }
 }
Example #11
0
 /**
  * Displays a single Post model.
  * @param integer $id
  * @return mixed
  */
 public function actionView($id)
 {
     $list_comment = new ActiveDataProvider(['query' => Comment::find()->where(['post_id' => $id, 'status' => 1])]);
     $model_comment = new Comment();
     if ($model_comment->load(Yii::$app->request->post())) {
         $model_comment->post_id = $id;
         $model_comment->status = 0;
         if ($model_comment->save()) {
             Yii::$app->session->setFlash('success', 'Ваш комментарий был отправлен администраторам сайта и будет опубликован после проверки..');
         } else {
             Yii::$app->session->setFlash('error', 'Ваш комментарий не был отправлен по техническим причинам. Попробуйте ещё раз.');
         }
     }
     return $this->render('view', ['model' => $this->findModel($id), 'list_comment' => $list_comment, 'model_comment' => $model_comment]);
 }
Example #12
0
 public function actionView($slug)
 {
     $post = Post::findOne(['slug' => $slug]);
     $comment = new Comment();
     $comment->name = '';
     $comment->email = '';
     $comment->body = '';
     if ($comment->load(Yii::$app->request->post())) {
         $comment->status = 0;
         if ($comment->save()) {
             Yii::$app->session->setFlash('success', ['type' => 'success', 'duration' => 12000, 'icon' => 'fa fa-chat', 'message' => 'You\'re comment successfully stored and will shown after approval.', 'title' => 'Saving Comment']);
         } else {
             Yii::$app->session->setFlash('error', ['type' => 'danger', 'duration' => 12000, 'icon' => 'fa fa-chat', 'message' => 'Sorry but we couldn\'t store your comment.', 'title' => 'Saving Comment']);
         }
     }
     return $this->render('view', ['post' => $post, 'comment' => $comment]);
 }
 public function actionDetail($id)
 {
     $postModel = new Comment();
     if ($postModel->load(Yii::$app->request->post())) {
         $postModel->status = Comment::STATUS_INACTIVE;
         $postModel->post_id = $id;
         if ($postModel->save()) {
             $this->added = 1;
         }
     }
     $tags = Tag::findTagWeights();
     $cateModel = new Cate();
     $cateDataProvider = $cateModel->getCates();
     $postData = new Post();
     $postDataProvider = $postData->findRecentPosts();
     $commentModel = new Comment();
     $commentDataProvider = $commentModel->findRecentComments();
     return $this->render('detail', ['tags' => $tags, 'added' => $this->added, 'postModel' => $postModel, 'model' => $this->findModel($id), 'postDataProvider' => $postDataProvider, 'cateDataProvider' => $cateDataProvider, 'commentDataProvider' => $commentDataProvider]);
 }
Example #14
0
 public function actionAdd()
 {
     if (User::thisUser()->reputation < Comment::MIN_REPUTATION_COMMENT_CREATE) {
         return Yii::$app->getResponse()->redirect(Url::home());
     }
     $anchor = '';
     $comment = new Comment();
     if ($comment->load(Yii::$app->request->post())) {
         $comment->description = nl2br($comment->description);
         $comment->description = \yii\helpers\HtmlPurifier::process($comment->description, []);
         $comment->entity_id = (int) $comment->entity_id;
         $comment->parent_id = (int) $comment->parent_id;
         $comment->user_id = Yii::$app->user->identity->getId();
         $comment->like_count = 0;
         if ($comment->save()) {
             $anchor = $comment->id;
         }
     }
     return json_encode(['content' => \frontend\widgets\CommentsWidget::widget(['entity' => $comment->entity, 'entity_id' => $comment->entity_id, 'showDialog' => false]), 'anchor' => $anchor]);
 }
Example #15
0
 public function actionNewComment()
 {
     if ($this->isAjax()) {
         $model = new Comment();
         $model->load($this->getParams());
         $validate = $model->validate();
         $reCaptcha = new ReCaptcha();
         $response = $reCaptcha->check();
         if ($validate && $response->isValid() && $model->save(false)) {
             return $this->getSuccessResponse(['email' => $model->email, 'text' => $model->text]);
         } else {
             $res = [];
             if (!$validate) {
                 $res['modelErrors'] = ActiveForm::validate($model);
             }
             if (!$response->isValid()) {
                 $res['reCaptchaError'] = true;
             }
             return $res;
         }
     }
     return Yii::$app->params['response']['error'];
 }
Example #16
0
 /**
  * Displays a single Post model.
  * @param integer $id
  * @return mixed
  */
 public function actionView($id)
 {
     $id = intval($id);
     $commentmodel = new Comment();
     if ($commentmodel->load(Yii::$app->request->post())) {
         if (Yii::$app->user->isGuest) {
             //登录后才能评论
             Yii::$app->user->returnUrl = Yii::$app->request->getUrl();
             //记录登录后要返回的地址
             return $this->redirect(['/site/login']);
         } else {
             $commentmodel->post_id = $id;
             $result = $commentmodel->dealContent(Yii::$app->request->post());
             if ($result) {
                 $commentmodel->save();
                 $this->refresh();
             } else {
                 Yii::$app->getSession()->setFlash('error', '评论内容过多或盖楼太多,请调整后重新提交评论');
             }
         }
     }
     return $this->render('view', ['model' => $this->findModel($id), 'commentmodel' => $commentmodel]);
 }
Example #17
0
 public function actionView($slug)
 {
     $commentForm = new Comment();
     $article = Content::find()->where(['slug' => $slug, 'type' => Content::TYPE_ARTICLE, 'status' => Content::STATUS_ACTIVE])->with(['createdBy', 'category', 'tags'])->one();
     if (empty($article)) {
         throw new NotFoundHttpException('没有找到相应文章。。。');
     }
     if ($commentForm->load(Yii::$app->request->post())) {
         $commentForm->content_id = $article['id'];
         if ($commentForm->save()) {
             Yii::$app->session->setFlash('success', '评论发表成功');
             return $this->refresh();
         } else {
             Yii::$app->session->setFlash('error', '评论发表失败');
         }
     }
     $article->updateCounters(['view_total' => 1]);
     $model = Comment::find()->where(['content_id' => $article['id'], 'status' => Comment::STATUS_ACTIVE]);
     $countModel = clone $model;
     $commentsPagination = new Pagination(['totalCount' => $countModel->count('id')]);
     $comments = $model->orderBy('created_at')->select(['id', 'ip', 'url', 'text', 'created_at', 'email', 'nickname', 'parent_id'])->asArray()->all();
     return $this->render('view', ['article' => $article, 'commentForm' => $commentForm, 'comments' => $comments, 'commentsPagination' => $commentsPagination]);
 }
Example #18
0
 /**
  * Сохраняет комментарий.
  * @param Comment $comment модель комментария
  * @param array $data данные пришедшие из формы
  * @return bool
  */
 public function save(Comment $comment, array $data)
 {
     $isLoad = $comment->load(['pid' => $data['pid'], 'title' => $data['title'], 'content' => $data['content']], '');
     return $isLoad && $comment->save();
 }