Beispiel #1
0
 public function execute()
 {
     $post_id = max(0, waRequest::get('id', 0, waRequest::TYPE_INT));
     if (!$post_id) {
         throw new waException(_w('Post not found'), 404);
     }
     $post_model = new blogPostModel();
     $search_options = array('id' => $post_id);
     $extend_options = array('comments' => array(20), 'user' => array('photo_url_50'), 'status' => 'view');
     $post = $post_model->search($search_options, $extend_options)->fetchSearchItem();
     if (!$post) {
         throw new waException(_w('Post not found'), 404);
     }
     $post['rights'] = $this->getRights("blog.{$post['blog_id']}");
     $posts = array(&$post);
     blogHelper::extendRights($posts, array(), $this->getUser()->getId());
     blogPhotosBridge::loadAlbums($posts);
     if (isset($post['comments']) && $post['comments']) {
         $post['comments'] = blogCommentModel::extendRights($post['comments'], array($post_id => $post));
     }
     $blog_model = new blogBlogModel();
     $blog = $blog_model->getById($post['blog_id']);
     if ($blog['status'] != blogBlogModel::STATUS_PUBLIC || $post['status'] != blogPostModel::STATUS_PUBLISHED) {
         blogHelper::checkRights($post['blog_id'], true, blogRightConfig::RIGHT_READ);
     }
     $items = $blog_model->prepareView(array($blog));
     $blog = array_shift($items);
     $this->setLayout(new blogDefaultLayout());
     $this->getResponse()->setTitle($post['title']);
     /**
      * Backend post view page
      * UI hook allow extends post view page
      * @event backend_post
      * @param array[string]mixed $post Current page post item data
      * @param array[string]int $post['id'] Post ID
      * @param array[string]int $post['blog_id'] Post blog ID
      * @return array[string][string]string $backend_post['%plugin_id%']['footer'] Plugin %plugin_id% footer html
      */
     $this->view->assign('backend_post', wa()->event('backend_post', $post, array('footer')));
     $user = $this->getUser();
     $this->view->assign('current_contact', array('id' => $user->getId(), 'name' => $user->getName(), 'photo20' => $user->getPhoto(20)));
     $this->view->assign('blog_id', $blog['id']);
     $this->view->assign('blog', $blog);
     $this->view->assign('contact_rights', $this->getUser()->getRights('contacts', 'backend'));
     if ($this->getConfig()->getOption('can_use_smarty')) {
         try {
             $post['text'] = $this->view->fetch("string:{$post['text']}", $this->cache_id);
         } catch (SmartyException $ex) {
             $post['text'] = blogPost::handleTemplateException($ex, $post);
         }
     }
     $this->view->assign('post', $post);
 }
 /**
  * Supplement the items data
  *
  * $extend_options
  *  - link: if it false - not add author posts
  *  - plugin: if false suppress plugin provided data
  *  - datetime:
  *  - test:
  * @param $items
  * @param array @extend_options
  * @param array $extend_data
  * @return mixed
  */
 public function prepareView($items, $extend_options = array(), $extend_data = array())
 {
     $extend_options = array_merge($this->extend_options, (array) $extend_options);
     $extend_data = array_merge($this->extend_data, (array) $extend_data);
     $extend_author_link = (!isset($extend_options['link']) || $extend_options['link']) && (!isset($extend_options['author_link']) || $extend_options['author_link']);
     // get user info & photo by post
     if ($item = current($items)) {
         if (isset($item['contact_id']) && (!isset($extend_options['user']) || $extend_options['user'])) {
             if (!isset($extend_options['user'])) {
                 $extend_options['user'] = array('photo_url_20');
             } elseif (!is_array($extend_options['user'])) {
                 $extend_options['user'] = preg_split('/,[\\s]*/', $extend_options['user']);
             }
             blogHelper::extendUser($items, $extend_options['user'], $extend_author_link);
         }
         if (isset($item['id'])) {
             if (!empty($extend_options['datetime'])) {
                 $comment_model = new blogCommentModel();
                 $comment_datetime = $comment_model->getDatetime(array_keys($items));
                 foreach ($items as $id => &$item) {
                     $item['comment_datetime'] = isset($comment_datetime[$id]) ? $comment_datetime[$id] : 0;
                     unset($item);
                 }
             } elseif (!isset($extend_options['comments'])) {
                 blogHelper::extendPostComments($items);
             } elseif ($extend_options['comments']) {
                 $fields = array();
                 if ($extend_options['comments'] !== true) {
                     foreach ((array) $extend_options['comments'] as $size) {
                         $fields[] = "photo_url_{$size}";
                     }
                 }
                 $activity_datetime = blogActivity::getUserActivity();
                 $comment_model = new blogCommentModel();
                 $comment_options = array('user' => $extend_author_link, 'datetime' => $activity_datetime, 'escape' => !empty($extend_options['escape']));
                 foreach ($items as $id => &$item) {
                     $item['comments'] = $comment_model->get($id, $fields, $comment_options);
                     $item['comment_count'] = 0;
                     $item['comment_new_count'] = 0;
                     foreach ($item['comments'] as &$comment) {
                         if ($comment['status'] == blogCommentModel::STATUS_PUBLISHED) {
                             ++$item['comment_count'];
                             if (!empty($comment['new'])) {
                                 ++$item['comment_new_count'];
                             }
                         }
                         unset($comment);
                     }
                     unset($item);
                 }
             }
             if (!empty($extend_options['status'])) {
                 blogHelper::extendPostState($items, $extend_options['status']);
             }
             if (!empty($extend_options['rights']) && isset($extend_data['blog'])) {
                 blogHelper::extendRights($items, $extend_data['blog'], wa()->getUser()->getId());
             }
             if (!empty($extend_options['params'])) {
                 $params_model = new blogPostParamsModel();
                 $params = $params_model->getByField('post_id', array_keys($items), true);
             }
         }
     }
     if (isset($params)) {
         foreach ($params as $param) {
             if (isset($items[$param['post_id']])) {
                 $items[$param['post_id']] += array($param['name'] => $param['value']);
             }
         }
     }
     foreach ($items as &$item) {
         #data holders for plugin events handlers
         $item['plugins'] = array('before' => array(), 'after' => array(), 'post_title' => array(), 'post_title_right' => array());
         if (isset($item['blog_id'])) {
             $blog_id = $item['blog_id'];
             if (isset($extend_data['blog']) && isset($extend_data['blog'][$blog_id])) {
                 $blog = $extend_data['blog'][$blog_id];
                 if (isset($blog['url'])) {
                     $item['blog_url'] = $blog['url'];
                 }
                 $item['icon'] = isset($blog['icon_html']) ? $blog['icon_html'] : '';
                 $item['color'] = isset($blog['color']) ? $blog['color'] : '';
                 if (isset($blog['status'])) {
                     $item['blog_status'] = $blog['status'];
                 }
                 if (isset($blog['url'])) {
                     $item['blog_url'] = $blog['url'];
                 }
                 if (isset($blog['name'])) {
                     $item['blog_name'] = $blog['name'];
                 }
             } else {
                 $item['color'] = '';
                 $item['icon'] = '';
             }
         }
         if (isset($item['comment_count'])) {
             /**
              * Backward compatibility with older themes
              * @deprecated
              */
             $item['comment_str_translate'] = _w('comment', 'comments', $item['comment_count']);
         }
         if (!isset($extend_options['link']) || $extend_options['link']) {
             if (isset($item['blog_status']) && $item['blog_status'] != blogBlogModel::STATUS_PUBLIC) {
                 $item['link'] = false;
             } else {
                 $item['link'] = blogPost::getUrl($item);
             }
         }
         if (empty($item['title'])) {
             $item['title'] = _w("(empty title)");
         }
         if (!empty($extend_options['escape'])) {
             $item['title'] = htmlspecialchars($item['title'], ENT_QUOTES, 'utf-8');
             $item['user']['name'] = htmlspecialchars($item['user']['name'], ENT_QUOTES, 'utf-8');
         }
         unset($item);
     }
     if (!isset($extend_options['plugin']) || $extend_options['plugin']) {
         /**
          * Prepare post data
          * Extend each post item via plugins data
          * @event prepare_posts_frontend
          * @event prepare_posts_backend
          * @example public function preparePost(&$items)
          * {
          *     foreach ($items as &$item) {
          *         $item['post_title'][$this->id] = 'Extra post title html code here';
          *     }
          * }
          * @param array [int][string]mixed $items Post items
          * @param array [int][string]mixed $items[id] Post item
          * @param array [int][string]int $items[id]['id'] Post item ID
          * @param array [int][string][string]string $items[id]['before'][%plugin_id%] Placeholder for plugin %plugin_id% output
          * @param array [int][string][string]string $items[id]['after'][%plugin_id%] Placeholder for plugin %plugin_id% output
          * @param array [int][string][string]string $items[id]['post_title'][%plugin_id%] Placeholder for plugin %plugin_id% output
          * @param array [int][string][string]string $items[id]['post_title_right'][%plugin_id%] Placeholder for plugin %plugin_id% output
          * @return void
          */
         wa()->event('prepare_posts_' . wa()->getEnv(), $items);
     }
     return $items;
 }
 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;
 }