/** * This is the heart of Komento that does magic * * @param $component string * @param $article object * @param $options array * @return null */ public static function commentify($component, &$article, $options = array()) { $eventTrigger = null; $context = null; $params = array(); $page = 0; if (array_key_exists('trigger', $options)) { $eventTrigger = $options['trigger']; } if (array_key_exists('context', $options)) { $context = $options['context']; } if (array_key_exists('params', $options)) { $params = $options['params']; } if (array_key_exists('page', $options)) { $page = $options['page']; } // TODO: Allow string/int: see line 662 // Sometimes people pass in $article as an array, we convert it to object if (is_array($article)) { $article = (object) $article; } // Check if there is a valid component if (empty($component)) { return false; } // @task: prepare data and checking on plugin level $application = Komento::loadApplication($component); // We verify context and trigger first before going into onBeforeLoad because onBeforeLoad already expects the article to be what Komento want to integrate // @task: verify if context is correct if (!Komento::verifyContext($context, $application->getContext())) { return false; } // @task: verify if event trigger is correct if (!Komento::verifyEventTrigger($eventTrigger, $application->getEventTrigger())) { return false; } // @trigger: onBeforeLoad // we do this checking before load because in some cases, // article is not an object and the article id might be missing. if (!$application->onBeforeLoad($eventTrigger, $context, $article, $params, $page, $options)) { return false; } // @task: set the component self::setCurrentComponent($component); // @task: get all the configuration $config = self::getConfig($component); $konfig = Komento::getKonfig(); // @task: check if enabled if (!$config->get('enable_komento')) { return false; } // @task: disable Komento in tmpl=component mode such as print mode if ($config->get('disable_komento_on_tmpl_component') && JRequest::getString('tmpl', '') === 'component') { return false; } // We accept $article as an int // For $article as a string, onBeforeLoad should already prepare the $article object properly if (is_string($article) || is_int($article)) { $cid = $article; } else { // @task: set cid based on application mapping keys because some component might have custom keys (not necessarily always $article-id) $cid = $article->{$application->_map['id']}; } // Don't proceed if $cid is empty if (empty($cid)) { return false; } // @task: process in-content parameters self::processParameter($article, $options); // terminate if it's disabled if ($options['disable']) { if (!$application->onParameterDisabled($eventTrigger, $context, $article, $params, $page, $options)) { return false; } } // @task: loading article infomation with defined get methods if (!$application->load($cid)) { return false; } // If enabled flag exists, bypass category check if (array_key_exists('enable', $options) && !$options['enable']) { // @task: category access check $categories = $config->get('allowed_categories'); // no categories mode switch ($config->get('allowed_categories_mode')) { // selected categories case 1: if (empty($categories)) { return false; } else { // @task: For some reason $article->catid might not be set. If it it's not set, just return false. $catid = $application->getCategoryId(); if (!$catid) { if (!$application->onRollBack($eventTrigger, $context, $article, $params, $page, $options)) { // raise error } return false; } if (!is_array($categories)) { $categories = explode(',', $categories); } if (!in_array($catid, $categories)) { if (!$application->onRollBack($eventTrigger, $context, $article, $params, $page, $options)) { // raise error } return false; } } break; // except selected categories // except selected categories case 2: if (!empty($categories)) { // @task: For some reason $article->catid might not be set. If it it's not set, just return false. $catid = $application->getCategoryId(); if (!$catid) { if (!$application->onRollBack($eventTrigger, $context, $article, $params, $page, $options)) { // raise error } return false; } if (!is_array($categories)) { $categories = explode(',', $categories); } if (in_array($catid, $categories)) { if (!$application->onRollBack($eventTrigger, $context, $article, $params, $page, $options)) { // raise error } return false; } } break; // no categories // no categories case 3: return false; break; // all categories // all categories case 0: default: break; } } // @trigger: onAfterLoad // Now the article with id has been loaded. if (!$application->onAfterLoad($eventTrigger, $context, $article, $params, $page, $options)) { return false; } // @task: send mail on page load if ($config->get('notification_sendmailonpageload')) { self::getMailQueue()->sendOnPageLoad(); } // @task: clear captcha database if ($konfig->get('database_clearcaptchaonpageload')) { self::clearCaptcha(); } // @task: load necessary css and javascript files. self::getHelper('Document')->loadHeaders(); /**********************************************/ // Run Komento! $commentsModel = Komento::getModel('comments'); $comments = ''; $return = false; $commentCount = $commentsModel->getCount($component, $cid); // Get total ratings $ratings = $commentsModel->getOverallRatings($component, $cid); $totalRating = 0; $totalRatingCount = 0; if ($ratings) { $totalRating = $ratings->value; $totalRatingCount = $ratings->total; } if ($application->isListingView()) { $html = ''; if (!array_key_exists('skipBar', $options)) { $commentOptions = array(); $commentOptions['threaded'] = 0; $commentOptions['limit'] = $config->get('preview_count', '3'); $commentOptions['sort'] = $config->get('preview_sort', 'latest'); $commentOptions['parentid'] = $config->get('preview_parent_only', false) ? 0 : 'all'; $commentOptions['sticked'] = $config->get('preview_sticked_only', false) ? true : 'all'; if ($commentOptions['sort'] == 'popular') { $comments = $commentsModel->getPopularComments($component, $cid, $commentOptions); } else { $comments = $commentsModel->getComments($component, $cid, $commentOptions); } $theme = Komento::getTheme(); $theme->set('commentCount', $commentCount); $theme->set('componentHelper', $application); $theme->set('component', $component); $theme->set('cid', $cid); $theme->set('comments', $comments); $theme->set('article', $article); $html = $theme->fetch('comment/bar.php'); } $return = $application->onExecute($article, $html, 'listing', $options); } if ($application->isEntryView()) { // check for escaped_fragment (google ajax crawler) $fragment = JRequest::getVar('_escaped_fragment_', ''); if ($fragment != '') { $tmp = explode('=', $fragment); $fragment = array($tmp[0] => $tmp[1]); if (isset($fragment['kmt-start'])) { $options['limitstart'] = $fragment['kmt-start']; } } else { // Sort comments oldest first by default. if (!isset($options['sort'])) { $options['sort'] = JRequest::getVar('kmt-sort', $config->get('default_sort')); } if ($config->get('load_previous')) { $options['limitstart'] = $commentCount - $config->get('max_comments_per_page'); if ($options['limitstart'] < 0) { $options['limitstart'] = 0; } } } $options['threaded'] = $config->get('enable_threaded'); $profile = Komento::getProfile(); $my = JFactory::getUser(); if (!$profile->allow('read_others_comment')) { $options['userid'] = $my->id; } if ($profile->allow('read_comment')) { $comments = $commentsModel->getComments($component, $cid, $options); } $contentLink = $application->getContentPermalink(); $theme = Komento::getTheme(); $theme->set('totalRating', $totalRating); $theme->set('totalRatingCount', $totalRatingCount); $theme->set('component', $component); $theme->set('cid', $cid); $theme->set('comments', $comments); $theme->set('options', $options); $theme->set('componentHelper', $application); $theme->set('application', $application); $theme->set('commentCount', $commentCount); $theme->set('contentLink', $contentLink); $html = $theme->fetch('comment/box.php'); /* [KOMENTO_POWERED_BY_LINK] */ // free version powered by link append (for reference only) // $html .= '<div style="text-align: center; padding: 20px 0;"><a href="http://stackideas.com">' . JText::_( 'COM_KOMENTO_POWERED_BY_KOMENTO' ) . '</a></div>'; $return = $application->onExecute($article, $html, 'entry', $options); // @task: Append hidden token into the page. $return .= '<span id="komento-token" style="display:none;"><input type="hidden" name="' . Komento::_('getToken') . '" value="1" /></span>'; } return $return; }