Пример #1
0
 public static function getCommentsList($object_id, $object_group = 'com_content', $page = 0)
 {
     $object_id = (int) $object_id;
     $object_group = trim($object_group);
     $user = JFactory::getUser();
     $acl = JCommentsFactory::getACL();
     $config = JCommentsFactory::getConfig();
     $comments_per_page = $config->getInt('comments_per_page');
     $limitstart = 0;
     $total = JComments::getCommentsCount($object_id, $object_group);
     if ($acl->canComment() == 0 && $total == 0) {
         return '';
     }
     if ($total > 0) {
         $options = array();
         $options['object_id'] = $object_id;
         $options['object_group'] = $object_group;
         $options['published'] = $acl->canPublish() || $acl->canPublishForObject($object_id, $object_group) ? null : 1;
         $options['votes'] = $config->getInt('enable_voting');
         if ($comments_per_page > 0) {
             $page = (int) $page;
             require_once JCOMMENTS_HELPERS . '/pagination.php';
             $pagination = new JCommentsPagination($object_id, $object_group);
             $pagination->setCurrentPage($page);
             $total_pages = $pagination->getTotalPages();
             $this_page = $pagination->getCurrentPage();
             $limitstart = $pagination->getLimitStart();
             $comments_per_page = $pagination->getCommentsPerPage();
             $options['limit'] = $comments_per_page;
             $options['limitStart'] = $limitstart;
         }
         $rows = JCommentsModel::getCommentsList($options);
     } else {
         $rows = array();
     }
     $tmpl = JCommentsFactory::getTemplate($object_id, $object_group);
     $tmpl->load('tpl_list');
     $tmpl->load('tpl_comment');
     if (count($rows)) {
         $isLocked = $config->getInt('comments_locked', 0) == 1;
         $tmpl->addVar('tpl_list', 'comments-refresh', intval(!$isLocked));
         $tmpl->addVar('tpl_list', 'comments-rss', intval($config->getInt('enable_rss') && !$isLocked));
         $tmpl->addVar('tpl_list', 'comments-can-subscribe', intval($user->id && $acl->check('enable_subscribe') && !$isLocked));
         $tmpl->addVar('tpl_list', 'comments-count', count($rows));
         if ($user->id && $acl->check('enable_subscribe')) {
             require_once JCOMMENTS_SITE . '/jcomments.subscription.php';
             $manager = JCommentsSubscriptionManager::getInstance();
             $isSubscribed = $manager->isSubscribed($object_id, $object_group, $user->id);
             $tmpl->addVar('tpl_list', 'comments-user-subscribed', $isSubscribed);
         }
         if ($config->get('comments_list_order') == 'DESC') {
             if ($comments_per_page > 0) {
                 $i = $total - $comments_per_page * ($page > 0 ? $page - 1 : 0);
             } else {
                 $i = count($rows);
             }
         } else {
             $i = $limitstart + 1;
         }
         JCommentsEventHelper::trigger('onJCommentsCommentsPrepare', array(&$rows));
         if ($acl->check('enable_gravatar')) {
             JCommentsEventHelper::trigger('onPrepareAvatars', array(&$rows));
         }
         $items = array();
         foreach ($rows as $row) {
             // run autocensor, replace quotes, smilies and other pre-view processing
             JComments::prepareComment($row);
             // setup toolbar
             if (!$acl->canModerate($row)) {
                 $tmpl->addVar('tpl_comment', 'comments-panel-visible', 0);
             } else {
                 $tmpl->addVar('tpl_comment', 'comments-panel-visible', 1);
                 $tmpl->addVar('tpl_comment', 'button-edit', $acl->canEdit($row));
                 $tmpl->addVar('tpl_comment', 'button-delete', $acl->canDelete($row));
                 $tmpl->addVar('tpl_comment', 'button-publish', $acl->canPublish($row));
                 $tmpl->addVar('tpl_comment', 'button-ip', $acl->canViewIP($row));
                 $tmpl->addVar('tpl_comment', 'button-ban', $acl->canBan($row));
             }
             $tmpl->addVar('tpl_comment', 'comment-show-vote', $config->getInt('enable_voting'));
             $tmpl->addVar('tpl_comment', 'comment-show-email', $acl->canViewEmail($row));
             $tmpl->addVar('tpl_comment', 'comment-show-homepage', $acl->canViewHomepage($row));
             $tmpl->addVar('tpl_comment', 'comment-show-title', $config->getInt('comment_title'));
             $tmpl->addVar('tpl_comment', 'button-vote', $acl->canVote($row));
             $tmpl->addVar('tpl_comment', 'button-quote', $acl->canQuote($row));
             $tmpl->addVar('tpl_comment', 'button-reply', $acl->canReply($row));
             $tmpl->addVar('tpl_comment', 'button-report', $acl->canReport($row));
             $tmpl->addVar('tpl_comment', 'avatar', $acl->check('enable_gravatar') && !$row->deleted);
             $tmpl->addObject('tpl_comment', 'comment', $row);
             if (isset($row->_number)) {
                 $tmpl->addVar('tpl_comment', 'comment-number', $row->_number);
             } else {
                 $tmpl->addVar('tpl_comment', 'comment-number', $i);
                 if ($config->get('comments_list_order') == 'DESC') {
                     $i--;
                 } else {
                     $i++;
                 }
             }
             $items[$row->id] = $tmpl->renderTemplate('tpl_comment');
         }
         $tmpl->addObject('tpl_list', 'comments-items', $items);
         // build page navigation
         if ($comments_per_page > 0 && $total_pages > 1) {
             $tmpl->addVar('tpl_list', 'comments-nav-first', 1);
             $tmpl->addVar('tpl_list', 'comments-nav-total', $total_pages);
             $tmpl->addVar('tpl_list', 'comments-nav-active', $this_page);
             $pagination = $config->get('comments_pagination');
             // show top pagination
             if ($pagination == 'both' || $pagination == 'top') {
                 $tmpl->addVar('tpl_list', 'comments-nav-top', 1);
             }
             // show bottom pagination
             if ($pagination == 'both' || $pagination == 'bottom') {
                 $tmpl->addVar('tpl_list', 'comments-nav-bottom', 1);
             }
         }
         unset($rows);
     }
     return $tmpl->renderTemplate('tpl_list');
 }
Пример #2
0
 public static function deleteComment($id)
 {
     if (JCommentsSecurity::badRequest() == 1) {
         JCommentsSecurity::notAuth();
     }
     $acl = JCommentsFactory::getACL();
     $db = JCommentsFactory::getDBO();
     $config = JCommentsFactory::getConfig();
     $response = JCommentsFactory::getAjaxResponse();
     $comment = new JCommentsTableComment($db);
     if ($comment->load((int) $id)) {
         if ($acl->isLocked($comment)) {
             $response->addAlert(JText::_('ERROR_BEING_EDITTED'));
         } else {
             if ($acl->canDelete($comment)) {
                 $object_id = $comment->object_id;
                 $object_group = $comment->object_group;
                 $currentPage = 1;
                 if ($config->get('template_view') != 'tree' && $config->getInt('comments_per_page') > 0) {
                     require_once JCOMMENTS_HELPERS . DS . 'pagination.php';
                     $pagination = new JCommentsPagination($object_id, $object_group);
                     $currentPage = $pagination->getCommentPage($object_id, $object_group, $id);
                 }
                 $result = JCommentsEvent::trigger('onJCommentsCommentBeforeDelete', array(&$comment));
                 if (!in_array(false, $result, true)) {
                     if ($config->getInt('delete_mode') == 0) {
                         $comment->delete();
                         $count = JComments::getCommentsCount($object_id, $object_group, '', true);
                         if ($config->get('template_view') == 'tree') {
                             if ($count > 0) {
                                 $response->addScript("jcomments.updateComment('{$id}','');");
                             } else {
                                 $response->addScript("jcomments.updateTree('',null);");
                             }
                         } else {
                             if ($count > 0) {
                                 if ($config->getInt('comments_per_page') > 0) {
                                     $pagination->setCommentsCount($count);
                                     $currentPage = min($currentPage, $pagination->getTotalPages());
                                     $html = JComments::getCommentsList($object_id, $object_group, $currentPage);
                                     $html = JCommentsText::jsEscape($html);
                                     $response->addScript("jcomments.updateList('{$html}','r');");
                                 } else {
                                     $response->addScript("jcomments.updateComment('{$id}','');");
                                 }
                             } else {
                                 $response->addScript("jcomments.updateList('','r');");
                             }
                         }
                     } else {
                         $comment->markAsDeleted();
                         $html = JCommentsText::jsEscape(JComments::getCommentItem($comment));
                         $response->addScript("jcomments.updateComment(" . $comment->id . ", '{$html}');");
                     }
                     JCommentsEvent::trigger('onJCommentsCommentAfterDelete', array(&$comment));
                 }
             } else {
                 $response->addAlert(JText::_('ERROR_CANT_DELETE'));
             }
         }
     }
     return $response;
 }