示例#1
0
 public static function getSimilarPosts($postId, $params)
 {
     $db = DiscussHelper::getDBO();
     $post = DiscussHelper::getTable('Posts');
     $post->load($postId);
     $title = $post->title;
     $categoryId = $post->category_id;
     $search = trim($title);
     if (empty($title)) {
         return array();
     }
     $limit = (int) $params->get('count', 5);
     $search = preg_replace("/(?![.=\$'â‚-])\\p{P}/u", "", $search);
     $numwords = explode(' ', $search);
     //$fulltextType 	= ( $numwords <= 2 ) ? ' WITH QUERY EXPANSION' : '';
     $fulltextType = ' WITH QUERY EXPANSION';
     // get the total score and count.
     $query = 'select count(a.`id`) as totalcnt , sum( MATCH(a.`title`,a.`content`) AGAINST (' . $db->Quote($search) . $fulltextType . ') ) as totalscore';
     $query .= ' FROM `#__discuss_posts` as a';
     $query .= ' WHERE MATCH(a.`title`,a.`content`) AGAINST (' . $db->Quote($search) . $fulltextType . ')';
     $query .= ' AND a.`published` = ' . $db->Quote('1');
     $query .= ' AND a.`parent_id` = ' . $db->Quote('0');
     if ($params->get('resolved_only', 0)) {
         $query .= ' AND a.`isresolve` = 1';
     }
     $query .= ' AND a.`category_id` = ' . $db->Quote($categoryId);
     $query .= ' and a.`id` != ' . $db->Quote($postId);
     $db->setQuery($query);
     $totalData = $db->loadObject();
     //var_dump( $totalData );
     $totalScore = $totalData->totalscore;
     $totalItem = round($totalData->totalcnt);
     //$db->setQuery( $query );
     $result = array();
     if ($totalItem) {
         $date = DiscussHelper::getDate();
         // now try to get the main topic
         $query = 'SELECT DATEDIFF(' . $db->Quote($date->toMySQL()) . ', a.`created`) as `noofdays`, ';
         $query .= ' DATEDIFF(' . $db->Quote($date->toMySQL()) . ', a.`created`) as `daydiff`, TIMEDIFF(' . $db->Quote($date->toMySQL()) . ', a.`created`) as `timediff`';
         $query .= ', a.`id`,  a.`title`, MATCH(a.`title`,a.`content`) AGAINST (' . $db->Quote($search) . $fulltextType . ') AS score';
         $query .= ', b.`id` as `category_id`, b.`title` as `category_name`';
         $query .= ' FROM `#__discuss_posts` as a';
         $query .= ' inner join `#__discuss_category` as b ON a.category_id = b.id';
         $query .= ' WHERE MATCH(a.`title`,a.`content`) AGAINST (' . $db->Quote($search) . $fulltextType . ')';
         $query .= ' AND a.`published` = ' . $db->Quote('1');
         $query .= ' AND a.`parent_id` = ' . $db->Quote('0');
         // Hack by mark
         $query .= ' AND a.`category_id` = ' . $db->Quote($categoryId);
         if ($params->get('resolved_only', 0)) {
             $query .= ' AND a.`isresolve` = 1';
         }
         $query .= ' and a.`id` != ' . $db->Quote($postId);
         $query .= ' ORDER BY score DESC';
         $query .= ' LIMIT ' . $limit;
         // echo $query;
         //exit;
         $db->setQuery($query);
         $result = $db->loadObjectList();
         for ($i = 0; $i < count($result); $i++) {
             $item =& $result[$i];
             $score = round($item->score) * 100 / $totalScore;
             $item->score = $score;
             //get post duration so far.
             $durationObj = new stdClass();
             $durationObj->daydiff = $item->daydiff;
             $durationObj->timediff = $item->timediff;
             $item->duration = DiscussHelper::getDurationString($durationObj);
         }
     }
     //var_dump( $result );
     //exit;
     return $result;
 }
示例#2
0
 public static function formatComments($comments)
 {
     $config = DiscussHelper::getConfig();
     if (!$comments) {
         return false;
     }
     $result = array();
     foreach ($comments as $row) {
         $duration = new StdClass();
         $duration->daydiff = $row->daydiff;
         $duration->timediff = $row->timediff;
         $comment = DiscussHelper::getTable('Comment');
         $comment->bind($row);
         $comment->duration = DiscussHelper::getDurationString($duration);
         $creator = DiscussHelper::getTable('Profile');
         $creator->load($comment->user_id);
         $comment->creator = $creator;
         if ($config->get('main_content_trigger_comments')) {
             // process content plugins
             $comment->content = $comment->comment;
             DiscussEventsHelper::importPlugin('content');
             DiscussEventsHelper::onContentPrepare('comment', $comment);
             $comment->event = new stdClass();
             $results = DiscussEventsHelper::onContentBeforeDisplay('comment', $comment);
             $comment->event->beforeDisplayContent = trim(implode("\n", $results));
             $results = DiscussEventsHelper::onContentAfterDisplay('comment', $comment);
             $comment->event->afterDisplayContent = trim(implode("\n", $results));
             $comment->comment = $comment->content;
             unset($comment->content);
             $comment->comment = DiscussHelper::wordFilter($comment->comment);
         }
         $result[] = $comment;
     }
     return $result;
 }
示例#3
0
 /**
  * Responsible to process a comment for saving.
  *
  * @since	3.0
  * @access	public
  */
 public function save()
 {
     $id = JRequest::getInt('id', 0);
     $my = JFactory::getUser();
     $acl = DiscussHelper::getHelper('ACL');
     $ajax = DiscussHelper::getHelper('Ajax');
     $config = DiscussHelper::getConfig();
     // Load the post item.
     $post = DiscussHelper::getTable('Post');
     $state = $post->load($id);
     // Test if a valid post id is provided.
     if (!$id || !$state) {
         $ajax->reject(JText::_('COM_EASYDISCUSS_COMMENTS_INVALID_POST_ID'));
         return $ajax->send();
     }
     $category = DiscussHelper::getTable('Category');
     $category->load($post->category_id);
     $access = $post->getAccess($category);
     // Test if the user is allowed to add comment or not.
     if (!$access->canComment()) {
         $ajax->reject(JText::_('COM_EASYDISCUSS_COMMENTS_NOT_ALLOWED'));
         return $ajax->send();
     }
     // Test if the comment message exists.
     $message = JRequest::getVar('comment', '');
     if (empty($message)) {
         $ajax->reject(JText::_('COM_EASYDISCUSS_COMMENT_IS_EMPTY'));
     }
     // Test if the user checked the terms and conditions box.
     if ($config->get('main_comment_tnc')) {
         $acceptedTerms = JRequest::getInt('tnc', 0);
         if (!$acceptedTerms) {
             $ajax->reject(JText::_('COM_EASYDISCUSS_TERMS_PLEASE_ACCEPT'));
             return $ajax->send();
         }
     }
     // Load user profile's object.
     $profile = DiscussHelper::getTable('Profile');
     $profile->load($my->id);
     // Build up comment object.
     $commentData = new stdClass();
     $commentData->user_id = $my->id;
     $commentData->name = $profile->getName();
     $commentData->email = $my->email;
     $commentData->comment = $message;
     $commentData->post_id = $post->id;
     // Run through akismet screening if necessary.
     if ($config->get('antispam_akismet') && $config->get('antispam_akismet_key')) {
         require_once DISCUSS_CLASSES . '/akismet.php';
         $data = array('author' => $my->name, 'email' => $my->email, 'website' => DISCUSS_JURIROOT, 'body' => $commentData->comment, 'alias' => '');
         $akismet = new Akismet(DISCUSS_JURIROOT, $config->get('antispam_akismet_key'), $data);
         if ($akismet->isSpam()) {
             $ajax->reject(JText::_('COM_EASYDISCUSS_AKISMET_SPAM_DETECTED'));
             return $ajax->send();
         }
     }
     $comment = DiscussHelper::getTable('Comment');
     $comment->bind($commentData, true);
     if (!$comment->store()) {
         $ajax->reject($comment->getError());
         return $ajax->send();
     }
     // Get post duration.
     $durationObj = new stdClass();
     $durationObj->daydiff = 0;
     $durationObj->timediff = '00:00:01';
     $comment->duration = DiscussHelper::getDurationString($durationObj);
     // Set the comment creator.
     $comment->creator = $profile;
     // Try to detect if the comment is posted to the main question or a reply.
     $liveNotificationText = '';
     if ($post->parent_id) {
         $question = DiscussHelper::getTable('Post');
         $question->load($post->parent_id);
         $liveNotificationText = 'COM_EASYDISCUSS_COMMENT_REPLY_NOTIFICATION_TITLE';
     } else {
         $question = DiscussHelper::getTable('Post');
         $question->load($id);
         $liveNotificationText = 'COM_EASYDISCUSS_COMMENT_QUESTION_NOTIFICATION_TITLE';
     }
     // Create notification item in EasySocial
     DiscussHelper::getHelper('EasySocial')->notify('new.comment', $post, $question, $comment);
     if ($comment->published && !$question->private) {
         // AUP integrations
         DiscussHelper::getHelper('Aup')->assign(DISCUSS_POINTS_NEW_COMMENT, $comment->user_id, '');
         // jomsocial activity stream
         DiscussHelper::getHelper('jomsocial')->addActivityComment($post, $question);
         DiscussHelper::getHelper('easysocial')->commentDiscussionStream($comment, $post, $question);
     }
     // Add notification to the post owner.
     if ($post->user_id != $my->id && $comment->published && $config->get('main_notifications_comments')) {
         $notification = DiscussHelper::getTable('Notifications');
         $notification->bind(array('title' => JText::sprintf($liveNotificationText, $question->title), 'cid' => $question->id, 'type' => DISCUSS_NOTIFICATIONS_COMMENT, 'target' => $post->user_id, 'author' => $my->id, 'permalink' => 'index.php?option=com_easydiscuss&view=post&id=' . $question->id));
         $notification->store();
     }
     // Try to assign badge and points to the current user.
     // Only assign points and badge when they are commenting a post that are not posted by them
     //	if( $my->id != $post->user_id )
     //	{
     // Add logging for user.
     DiscussHelper::getHelper('History')->log('easydiscuss.new.comment', $my->id, JText::_('COM_EASYDISCUSS_BADGES_HISTORY_NEW_COMMENT'), $post->id);
     // Assign badge for EasySocial
     DiscussHelper::getHelper('EasySocial')->assignBadge('create.comment', $my->id, JText::_('COM_EASYDISCUSS_BADGES_HISTORY_NEW_COMMENT'));
     DiscussHelper::getHelper('Badges')->assign('easydiscuss.new.comment', $my->id);
     DiscussHelper::getHelper('Points')->assign('easydiscuss.new.comment', $my->id, $comment);
     //	}
     // Apply badword filtering for the comment.
     $comment->comment = DiscussHelper::wordFilter($comment->comment);
     $emailData = array();
     $emailData['commentContent'] = $comment->comment;
     $emailData['commentAuthor'] = $profile->getName();
     $emailData['commentAuthorAvatar'] = $profile->getAvatar();
     $emailData['postTitle'] = $question->title;
     $emailData['postLink'] = DiscussRouter::getRoutedURL('index.php?option=com_easydiscuss&view=post&id=' . $question->id, false, true);
     $emails = array();
     // Send email to the post owner only if the commenter is not the post owner.
     if ($post->user_id != 0 && $post->id != $my->id) {
         $user = JFactory::getUser($post->user_id);
         $emails[] = $user->email;
     }
     // Retrieve the list of user emails from the list of comments made on the post.
     $existingComments = $post->getComments();
     if ($existingComments) {
         foreach ($existingComments as $existingComment) {
             // Only add the email when the user id is not the current logged in user who is posting the comment.
             // It should not send email to the post owner as well since the post owner will already get a notification.
             if ($existingComment->user_id != 0 && $existingComment->user_id != $my->id && $existingComment->user_id != $post->user_id) {
                 $user = JFactory::getUser($existingComment->user_id);
                 $emails[] = $user->email;
             }
         }
     }
     // Ensure the emails are all unique.
     $emails = array_unique($emails);
     // Only send email when email is not empty.
     if (!empty($emails)) {
         $notify = DiscussHelper::getNotification();
         $notify->addQueue($emails, JText::sprintf('COM_EASYDISCUSS_EMAIL_TITLE_NEW_COMMENT', JString::substr($question->content, 0, 15)) . '...', '', 'email.post.comment.new.php', $emailData);
     }
     //revert the comment form
     // $ajax->script('discuss.comment.cancel()');
     // Process comment triggers.
     if ($config->get('main_content_trigger_comments')) {
         $comment->content = $comment->comment;
         // process content plugins
         DiscussEventsHelper::importPlugin('content');
         DiscussEventsHelper::onContentPrepare('comment', $comment);
         $comment->event = new stdClass();
         $results = DiscussEventsHelper::onContentBeforeDisplay('comment', $comment);
         $comment->event->beforeDisplayContent = trim(implode("\n", $results));
         $results = DiscussEventsHelper::onContentAfterDisplay('comment', $comment);
         $comment->event->afterDisplayContent = trim(implode("\n", $results));
         $comment->comment = $comment->content;
     }
     // Get the parent post post id
     $postId = $post->parent_id ? $post->parent_id : $post->id;
     // Get the result of the posted comment.
     $theme = new DiscussThemes();
     $theme->set('comment', $comment);
     $theme->set('postId', $postId);
     $output = $theme->fetch('post.reply.comment.item.php');
     $ajax->resolve($output);
     return $ajax->send();
 }