public function execute()
 {
     $this->getResponse()->addHeader('Content-type', 'application/json');
     if ($comment_id = $this->getRequest()->post('id', 0, waRequest::TYPE_INT)) {
         $comment_model = new blogCommentModel();
         $comment = $comment_model->getById($comment_id);
         if (!$comment) {
             throw new waException(_w('Comment not found'), 404);
         }
         $post_model = new blogPostModel();
         if (!($post = $post_model->getBlogPost(array('id' => $comment['post_id'], 'blog_id' => $comment['blog_id'])))) {
             throw new waException(_w('Post not found'), 404);
         }
         $user_id = $this->getUser()->getId();
         $rights = blogHelper::checkRights($comment['blog_id'], $user_id, blogRightConfig::RIGHT_READ_WRITE);
         if ($rights == blogRightConfig::RIGHT_READ_WRITE && $user_id != $post['contact_id']) {
             throw new waRightsException(_w('Access denied'), 403);
         }
         $status = $this->getRequest()->post('status', blogCommentModel::STATUS_DELETED);
         if ($status != blogCommentModel::STATUS_DELETED) {
             $status = blogCommentModel::STATUS_PUBLISHED;
         }
         $changed = $comment_model->updateById($comment_id, array('status' => $status));
         $count = $comment_model->getCount($comment['blog_id'], $comment['post_id']);
         if ($changed) {
             if ($status == blogCommentModel::STATUS_DELETED) {
                 $this->log('comment_delete', 1);
             } else {
                 $this->log('comment_restore', 1);
             }
         }
         $this->response = array('count_str' => $count . " " . _w('comment', 'comments', $count), 'status' => $status, 'changed' => $changed);
     }
 }
 public function execute()
 {
     $data = waRequest::post();
     $exclude = array('left_key', 'right_key', 'type', 'full_url', 'parent_id');
     foreach ($exclude as $k) {
         if (isset($data[$k])) {
             unset($data[$k]);
         }
     }
     // check required params
     $this->post('text', true);
     $post_id = $this->get('post_id', true);
     $post_model = new blogPostModel();
     $post = $post_model->getBlogPost($post_id);
     if (!$post) {
         throw new waAPIException('invalid_param', 'Post not found', 404);
     }
     $parent_id = $this->post('parent_id');
     $comment_model = new blogCommentModel();
     if ($parent_id) {
         $parent = $comment_model->getById($parent_id);
         if (!$parent) {
             throw new waAPIException('invalid_param', 'Parent comment not found', 404);
         }
     }
     $contact_id = wa()->getUser()->getId();
     // check rights
     try {
         blogHelper::checkRights($post['blog_id'], $contact_id, blogRightConfig::RIGHT_READ);
     } catch (waException $e) {
         throw new waAPIException('access_denied', 403);
     }
     // check comment mode
     if (!$post['comments_allowed']) {
         throw new waAPIException('invalid_param', "Isn't allowed comment to this post", 404);
     }
     $data = array_merge($data, array('blog_id' => $post['blog_id'], 'post_id' => $post_id, 'contact_id' => $contact_id, 'auth_provider' => blogCommentModel::AUTH_USER));
     $messages = $comment_model->validate($data);
     if ($messages) {
         throw new waAPIException('invalid_param', 'Validate messages: ' . implode("\n", $messages), 404);
     }
     $id = $comment_model->add($data, $parent_id);
     $_GET['id'] = $id;
     $method = new blogPostCommentsGetInfoMethod();
     $this->response = $method->getResponse(true);
 }
 public function execute()
 {
     $this->post_id = max(0, $this->getRequest()->get('id', 0, waRequest::TYPE_INT));
     $this->parent_id = max(0, $this->getRequest()->post('parent', 0, waRequest::TYPE_INT));
     $comment_model = new blogCommentModel();
     $post_model = new blogPostModel();
     /**
      *
      * Parent comment data
      * @var array
      */
     $parent = null;
     $stream = false;
     //find comment parent
     if ($this->parent_id && ($parent = $comment_model->getById($this->parent_id))) {
         if ($this->post_id && $this->post_id != $parent['post_id']) {
             throw new waRightsException(_w('Access denied'));
         }
         if (!$this->post_id) {
             $stream = true;
         }
         $this->post_id = $parent['post_id'];
     } else {
         $this->parent_id = 0;
     }
     //find post
     if (!$this->post_id || !($post = $post_model->getBlogPost($this->post_id))) {
         throw new waException(_w('Post not found'), 404);
     }
     $contact_id = $this->getUser()->getId();
     #check rights
     $rights = blogHelper::checkRights($post['blog_id'], $contact_id, blogRightConfig::RIGHT_READ);
     //check comment mode
     if (!$post['comments_allowed']) {
         throw new waException(_w("Isn't allowed comment to this post"));
     }
     $comment = array('blog_id' => $post['blog_id'], 'post_id' => $this->post_id, 'contact_id' => $contact_id, 'text' => $this->getRequest()->post('text'), 'auth_provider' => blogCommentModel::AUTH_USER);
     $this->errors += $comment_model->validate($comment);
     if (count($this->errors) > 0) {
         return;
     }
     $id = $comment_model->add($comment, $this->parent_id);
     $this->logAction('comment_add', $id);
     $comment = $comment_model->getById($id);
     //$comment['new'] = false;
     $comment['parent'] = $this->parent_id;
     if ($stream) {
         $comment['parent_text'] = $parent ? $parent['text'] : null;
         $comment['parent_status'] = $parent ? $parent['status'] : null;
     } else {
         $count = $comment_model->getCount($post['blog_id'], $this->post_id);
         $this->response['count_str'] = $count . " " . _w('comment', 'comments', $count);
     }
     $comment['rights'] = $rights;
     $comment['post'] =& $post;
     $post['comments'] = $comment_model->prepareView(array($comment), array('photo_url_20'));
     blogHelper::extendRights($post['comments'], array(), $contact_id);
     if ($stream) {
         $posts = array($this->post_id => &$post);
         $blog_model = new blogBlogModel();
         $extend_data = array('blog' => $blog_model->search(array('id' => $this->post_id))->fetchSearchAll());
         $post_model->prepareView($posts, array('link' => true), $extend_data);
     } else {
         unset($comment['post']);
     }
     $view = wa()->getView();
     $view->assign('post', $post);
     $view->assign('contact_rights', $this->getUser()->getRights('contacts', 'backend'));
     $template = $view->fetch('templates/actions/post/include.comments.html');
     $this->getResponse()->addHeader('Content-type', 'application/json');
     $this->response['template'] = $template;
 }