Example #1
0
 /**
  * Get the number of comments to this post
  *
  * @return bool
  */
 public function getCommentsCount()
 {
     return (int) Default_Model_DbTable_BlogPostComment::GetPostsComments_Count($this->_db, array('post_id' => array($this->getId())));
 }
Example #2
0
 /**
  * Action - comments
  * actions for user comments
  *
  * Access to the action is possible in the following paths:
  * шаблон раутера - user/:username/post/:post_id/comments/*
  * 
  * - /user/user1/post/27/comments
  *
  * @return void
  */
 public function commentsAction()
 {
     $json = array();
     $result = TRUE;
     //-----------------------
     // Получим обьект запроса
     $request = $this->getRequest();
     $params = $request->getParams();
     $type_action = $params['type_action'];
     $post_id = (int) $request->getUserParam('post_id');
     $username = trim($request->getUserParam('username'));
     try {
         if ($type_action == 'delete') {
             // Удалим комментарий
             // Получим массив комментариев для удаления
             $comment_ids = $params["comment_ids"];
             $comment_ids = Zend_Json::decode($comment_ids);
             $parent_comment_id = $comment_ids[0];
             // Удалим комментарии из базы данных
             $comment = new Default_Model_DbTable_BlogPostComment($this->db);
             foreach ($comment_ids as $comment_id) {
                 if ($comment->loadForPost($post_id, $comment_id)) {
                     $comment->delete();
                 } else {
                     $result = FALSE;
                     break;
                 }
             }
             if ($result) {
                 $json = array('deleted' => true, 'result' => $this->Translate('Комментарий удален из сообщения блога'), 'comment_id' => $parent_comment_id);
             } else {
                 $json = array('class_message' => 'warning', 'messages' => array('<em>' . $this->Translate('Ошибка при добавлении / удалении комментария в блог') . '</em>'));
             }
         } else {
             if ($type_action == 'add' || $type_action == 'reply' || $type_action == 'edit') {
                 // Добавим/изменим комментарий на сообщение
                 $allParams = $this->_getAllParams();
                 $reply_id = $params["reply_id"];
                 $formAddComment = new Default_Form_AddComment($username, $post_id);
                 $result = $formAddComment->isValid($allParams);
                 if ($result) {
                     $comment = new Default_Model_DbTable_BlogPostComment($this->db);
                     if ($type_action == 'edit') {
                         if ($comment->loadForPost($post_id, $reply_id)) {
                             $comment->comment = $formAddComment->getValue('ckeditor_comment');
                         } else {
                             $json = array('class_message' => 'warning', 'messages' => array('<em>' . $this->Translate('Ошибка при сохранении данных') . '</em>'));
                         }
                     } else {
                         $comment->user_id = $this->_identity->user_id;
                         $comment->post_id = $post_id;
                         $comment->reply_id = $reply_id;
                         $comment->comment = $formAddComment->getValue('ckeditor_comment');
                     }
                     if ($comment->save()) {
                         if ($type_action == 'edit') {
                             $html = $formAddComment->getValue('ckeditor_comment');
                             $result = $this->Translate('Комментарий изменен');
                         } else {
                             // Получим параметр для шаблона
                             $treeComments = Default_Model_DbTable_BlogPostComment::getTreeComments($this->db, $this->user->getId(), array('post_id' => $post_id, 'comment_id' => $comment->getId()));
                             // Создадим обьект шаблона
                             $templater = Default_Plugin_SysBox::createViewSmarty();
                             //Установим параметры шаблона
                             $templater->treeComments = $treeComments;
                             $templater->authenticated = true;
                             $templater->isAdmin = $this->_isAdmin;
                             $templater->identity = $this->_identity;
                             // Получим результат шаблона
                             $html = $templater->render('user/lib/comment-item.tpl');
                             $result = $this->Translate('Добавлен комментарий к сообщению блога');
                         }
                         $json = array('added' => true, 'result' => $result, 'comment_id' => $comment->getId(), 'html' => $html);
                     } else {
                         // Ошибка записи в базу данных
                         $json = array('class_message' => 'warning', 'messages' => array('<em>' . $this->Translate('Ошибка при сохранении данных') . '</em>'));
                     }
                 } else {
                     // неверно заполнены поля формы
                     $json = array('class_message' => 'warning', 'messages' => $this->getFormMessages($formAddComment));
                 }
             }
         }
     } catch (Exception $e) {
         $json = array('class_message' => 'warning', 'messages' => array('<em>' . $this->Translate('Ошибка при добавлении / удалении комментария в блог') . '</em>', Default_Plugin_SysBox::getMessageError($e)));
     }
     $this->sendJson($json);
 }