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()
 {
     $contact_photo_size = 20;
     $comments_per_page = max(1, intval($this->getConfig()->getOption('comments_per_page')));
     $page = max(1, waRequest::get('page', 1, waRequest::TYPE_INT));
     $blog_models = new blogBlogModel();
     $user = $this->getUser();
     $blogs = blogHelper::getAvailable();
     $comment_model = new blogCommentModel();
     $offset = $comments_per_page * ($page - 1);
     $prepare_options = array('datetime' => blogActivity::getUserActivity());
     $fields = array("photo_url_{$contact_photo_size}");
     $blog_ids = array_keys($blogs);
     $comments = $comment_model->getList($offset, $comments_per_page, $blog_ids, $fields, $prepare_options);
     $comments_all_count = $comment_model->getCount($blog_ids, null, null, null, null, null);
     $post_ids = array();
     foreach ($comments as $comment) {
         $post_ids[$comment['post_id']] = true;
     }
     //get related posts info
     $post_model = new blogPostModel();
     $search_options = array('id' => array_keys($post_ids));
     $extend_options = array('user' => false, 'link' => true, 'rights' => true, 'plugin' => false, 'comments' => false);
     $extend_data = array('blog' => $blogs);
     $posts = $post_model->search($search_options, $extend_options, $extend_data)->fetchSearchAll(false);
     $comments = blogCommentModel::extendRights($comments, $posts);
     $comments_count = ($page - 1) * $comments_per_page + count($comments);
     if ($page == 1) {
         $this->setLayout(new blogDefaultLayout());
         $this->getResponse()->setTitle(_w('Comments'));
     }
     /**
      * Backend comments view page
      * UI hook allow extends backend comments view page
      * @event backend_comments
      * @param array[int][string]mixed $comments
      * @param array[int][string]int $comments[%id%][id] comment id
      * @return array[string][string]string $return[%plugin_id%]['toolbar'] Comment's toolbar html
      */
     $this->view->assign('backend_comments', wa()->event('backend_comments', $comments));
     $this->view->assign('comments', $comments);
     $this->view->assign('comments_count', $comments_count);
     $this->view->assign('comments_total_count', $comments_all_count);
     $this->view->assign('comments_per_page', $comments_per_page);
     $this->view->assign('pages', ceil($comments_all_count / $comments_per_page));
     $this->view->assign('page', $page);
     $this->view->assign('contact_rights', $this->getUser()->getRights('contacts', 'backend'));
     $this->view->assign('current_contact_id', $user->getId());
     $this->view->assign('current_contact', array('id' => $user->getId(), 'name' => $user->getName(), 'photo20' => $user->getPhoto($contact_photo_size)));
 }
 private function displayComment()
 {
     $this->getResponse()->addHeader('Content-type', 'application/json');
     if ($this->comment_id && ($comment = $this->comment_model->getById($this->comment_id))) {
         $count = $this->comment_model->getCount($comment['blog_id'], $comment['post_id']);
         $comments = $this->comment_model->prepareView(array($comment), array('photo_url_20'), array('user' => true, 'escape' => true));
         $theme = waRequest::param('theme', 'default');
         $theme_path = wa()->getDataPath('themes', true) . '/' . $theme;
         if (!file_exists($theme_path) || !file_exists($theme_path . '/theme.xml')) {
             $theme_path = wa()->getAppPath() . '/themes/' . $theme;
         }
         $template = 'file:comment.html';
         $view = wa()->getView(array('template_dir' => $theme_path));
         $view->assign('comment', array_shift($comments));
         $this->response['template'] = $view->fetch($template);
         $this->response['count_str'] = $count . " " . _w('comment', 'comments', $count);
         $this->response['parent'] = $this->parent_id;
     } else {
         throw new waException(_w('Comment not found'), 404);
     }
 }
Exemple #4
0
 public function onCount()
 {
     $full = !func_get_args();
     $app = $this->getApplication();
     $user = waSystem::getInstance()->getUser();
     $user_id = $user->getId();
     $type = explode(':', $user->getSettings($app, 'type_items_count'));
     $type = array_filter(array_map('trim', $type), 'strlen');
     if (!$type) {
         $type = array('posts', 'comments_to_my_post', 'overdue');
     }
     $activity_datetime = blogActivity::getUserActivity($user_id, false);
     $blogs = array_keys(blogHelper::getAvailable(false));
     $counter = array();
     $post_model = new blogPostModel();
     if (in_array('posts', $type) && $full && $blogs) {
         $post_new_count = $post_model->getAddedPostCount($activity_datetime, $blogs);
         $post_new_count = array_sum($post_new_count);
         $counter['posts'] = $post_new_count;
     } else {
         $counter['posts'] = false;
     }
     if (in_array('comments', $type) && $full && $blogs) {
         $comment_model = new blogCommentModel();
         $counter['comments'] = $comment_model->getCount($blogs, null, $activity_datetime, 0);
     } else {
         $counter['comments'] = false;
     }
     if (in_array('comments_to_my_post', $type) && $full && $blogs) {
         $comment_model = new blogCommentModel();
         $counter['comments_to_my_post'] = $comment_model->getCount($blogs, null, $activity_datetime, 0, $user_id);
     } else {
         $counter['comments_to_my_post'] = false;
     }
     if (in_array('overdue', $type) && $blogs) {
         if (!isset($post_model)) {
             $post_model = new blogPostModel();
         }
         $where = "status = '" . blogPostModel::STATUS_DEADLINE . "'";
         $where .= " AND blog_id IN (" . implode(', ', $blogs) . ")";
         $where .= " AND contact_id = {$user_id}";
         $where .= " AND datetime <= '" . waDateTime::date("Y-m-d") . "'";
         $count_overdue = $post_model->select("count(id)")->where($where)->fetchField();
         $counter['overdue'] = $count_overdue ? $count_overdue : 0;
     } else {
         $counter['overdue'] = false;
     }
     $count = array_sum($counter);
     $url = $this->getBackendUrl(true) . $this->application . '/';
     if ($count) {
         switch ($count) {
             case $counter['comments']:
             case $counter['comments_to_my_post']:
                 $url .= '?module=comments';
                 break;
             case $counter['overdue']:
                 $url .= '?action=calendar';
                 break;
         }
     }
     //debug
     //$counter['type'] = $type;
     //$counter['activity_datetime'] = $activity_datetime;
     //$counter['current_datetime'] = date("Y-m-d H:i:s",time());
     //waLog::log('$counter = '.var_export($counter,true),"blog-counter-{$user_id}.log");
     return array('count' => $count == 0 ? null : $count, 'url' => $url);
 }
Exemple #5
0
 /**
  *
  * Get comments for posts
  * @param array $posts
  */
 public static function extendPostComments(&$posts)
 {
     $comment_model = new blogCommentModel();
     $post_ids = array_keys($posts);
     $comment_count = $comment_model->getCount(null, $post_ids);
     $comment_new_count = $comment_model->getCount(null, $post_ids, blogActivity::getUserActivity());
     foreach ($posts as $id => &$post) {
         $post['comment_count'] = isset($comment_count[$id]) ? $comment_count[$id] : 0;
         $post['comment_new_count'] = isset($comment_new_count[$id]) ? $comment_new_count[$id] : 0;
         unset($post);
     }
 }
 public function getComments($search_options, $fields, $prepare_options)
 {
     $comment_model = new blogCommentModel();
     $post_ids = null;
     $blog_ids = $search_options['blog_id'];
     if (is_numeric($search_options['filter'])) {
         $k = array_search((int) $search_options['filter'], $blog_ids);
         if ($k !== false) {
             $blog_ids = $blog_ids[$k];
         } else {
             $blog_ids = array();
         }
         $search_options['blog_id'] = $blog_ids;
     } else {
         if ($search_options['filter'] == 'myposts') {
             $post_model = new blogPostModel();
             $post_ids = array_keys($post_model->select('id')->where('contact_id=' . $this->getUser()->getId())->fetchAll('id'));
             $search_options['post_id'] = $post_ids;
         }
     }
     $counts = (array) $comment_model->getCount($blog_ids, $post_ids, null, null, null, null);
     return array('comments' => $comment_model->getList($search_options, $fields, $prepare_options), 'comments_all_count' => array_sum($counts));
 }
 public function execute()
 {
     $this->user = $this->getUser();
     $blog_id = waRequest::get('blog', null, 'int');
     $post_id = waRequest::get('id', null, 'int');
     $request = waRequest::get();
     $module = waRequest::get('module');
     $action = waRequest::get('action');
     if (!$action) {
         $action = waRequest::get('module');
     }
     $view_all_posts = waRequest::get('all', null) !== null || empty($request);
     $blog_model = new blogBlogModel();
     $blogs = $blog_model->getAvailable($this->user, array(), null, array('new' => true, 'expire' => 1, 'link' => false));
     $blog_ids = array_keys($blogs);
     $comment_model = new blogCommentModel();
     $comment_count = $comment_model->getCount($blog_ids);
     $activity_datetime = blogActivity::getUserActivity();
     $comment_new_count = $comment_model->getCount($blog_ids, null, $activity_datetime, 1);
     $post_count = 0;
     $new_post_count = 0;
     $writable_blogs = false;
     foreach ($blogs as $blog) {
         $post_count += $blog['qty'];
         if ($blog['rights'] >= blogRightConfig::RIGHT_READ_WRITE) {
             $writable_blogs = true;
         }
         if (isset($blog['new_post']) && $blog['new_post'] > 0) {
             $new_post_count += $blog['new_post'];
         }
     }
     if ($writable_blogs) {
         $post_model = new blogPostModel();
         $search_options = array('status' => array(blogPostModel::STATUS_DRAFT, blogPostModel::STATUS_DEADLINE, blogPostModel::STATUS_SCHEDULED));
         if (!$this->user->isAdmin($this->getApp())) {
             $search_options['contact_id'] = $this->user->getId();
         }
         $search_options['sort'] = 'overdue';
         $drafts = $post_model->search($search_options, array('status' => true, 'link' => false, 'plugin' => false, 'comments' => false), array('blog' => $blogs))->fetchSearchAll(false);
         $where = "status = '" . blogPostModel::STATUS_DEADLINE . "' AND datetime <= '" . waDateTime::date("Y-m-d") . "'";
         if (!$this->getUser()->isAdmin($this->getApp())) {
             $where .= " AND contact_id = {$this->getUser()->getId()}";
             $where .= " AND blog_id IN (" . implode(', ', array_keys($blogs)) . ")";
         }
         $count_overdue = $post_model->select("count(id)")->where($where)->fetchField();
         $count_overdue = $count_overdue ? $count_overdue : 0;
     } else {
         $drafts = false;
         $count_overdue = false;
     }
     /**
      * Extend backend sidebar
      * Add extra sidebar items (menu items, additional sections, system output)
      * @event backend_sidebar
      * @example #event handler example
      * public function sidebarAction()
      * {
      *     $output = array();
      *
      *     #add external link into sidebar menu
      *     $output['menu']='<li>
      *         <a href="http://www.webasyst.com">
      *             http://www.webasyst.com
      *         </a>
      *     </li>';
      *
      *     #add section into sidebar menu
      *     $output['section']='';
      *
      *     #add system link into sidebar menu
      *     $output['system']='';
      *
      *     return $output;
      * }
      * @return array[string][string]string $return[%plugin_id%]['menu'] Single menu items
      * @return array[string][string]string $return[%plugin_id%]['section'] Sections menu items
      * @return array[string][string]string $return[%plugin_id%]['system'] Extra menu items
      */
     $this->view->assign('backend_sidebar', wa()->event('backend_sidebar'));
     $this->view->assign('blog_id', $blog_id);
     $this->view->assign('blogs', $blogs);
     $this->view->assign('view_all_posts', $view_all_posts);
     $this->view->assign('action', $action);
     $this->view->assign('module', $module);
     $this->view->assign('post_id', $post_id);
     $this->view->assign('new_post', waRequest::get('action') == 'edit' && waRequest::get('id') == '');
     $this->view->assign('drafts', $drafts);
     $this->view->assign('comment_count', $comment_count);
     $this->view->assign('comment_new_count', $comment_new_count);
     $this->view->assign('post_count', $post_count);
     $this->view->assign('new_post_count', $new_post_count);
     $this->view->assign('count_draft_overdue', $count_overdue);
     $this->view->assign('writable_blogs', $writable_blogs);
 }
 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;
 }