/** * Add comments to a template * * @param FrontendTemplate|object $objTemplate * @param \stdClass $objConfig * @param string $strSource * @param integer $intParent * @param mixed $varNotifies */ public function addCommentsToTemplate(FrontendTemplate $objTemplate, \stdClass $objConfig, $strSource, $intParent, $varNotifies) { /** @var PageModel $objPage */ global $objPage; $limit = 0; $offset = 0; $total = 0; $gtotal = 0; $arrComments = array(); $objTemplate->comments = array(); // see #4064 // Pagination if ($objConfig->perPage > 0) { // Get the total number of comments $intTotal = \CommentsModel::countPublishedBySourceAndParent($strSource, $intParent); $total = $gtotal = $intTotal; // Calculate the key (e.g. tl_form_field becomes page_cff12) $key = ''; $chunks = explode('_', substr($strSource, strncmp($strSource, 'tl_', 3) === 0 ? 3 : 0)); foreach ($chunks as $chunk) { $key .= substr($chunk, 0, 1); } // Get the current page $id = 'page_c' . $key . $intParent; // see #4141 $page = \Input::get($id) !== null ? \Input::get($id) : 1; // Do not index or cache the page if the page number is outside the range if ($page < 1 || $page > max(ceil($total / $objConfig->perPage), 1)) { throw new PageNotFoundException('Page not found: ' . \Environment::get('uri')); } // Set limit and offset $limit = $objConfig->perPage; $offset = ($page - 1) * $objConfig->perPage; // Initialize the pagination menu $objPagination = new \Pagination($total, $objConfig->perPage, \Config::get('maxPaginationLinks'), $id); $objTemplate->pagination = $objPagination->generate("\n "); } $objTemplate->allowComments = true; // Get all published comments if ($limit) { $objComments = \CommentsModel::findPublishedBySourceAndParent($strSource, $intParent, $objConfig->order == 'descending', $limit, $offset); } else { $objComments = \CommentsModel::findPublishedBySourceAndParent($strSource, $intParent, $objConfig->order == 'descending'); } // Parse the comments if ($objComments !== null && ($total = $objComments->count()) > 0) { $count = 0; if ($objConfig->template == '') { $objConfig->template = 'com_default'; } /** @var FrontendTemplate|object $objPartial */ $objPartial = new \FrontendTemplate($objConfig->template); while ($objComments->next()) { $objPartial->setData($objComments->row()); // Clean the RTE output $objPartial->comment = \StringUtil::toHtml5($objComments->comment); $objPartial->comment = trim(str_replace(array('{{', '}}'), array('{{', '}}'), $objPartial->comment)); $objPartial->datim = \Date::parse($objPage->datimFormat, $objComments->date); $objPartial->date = \Date::parse($objPage->dateFormat, $objComments->date); $objPartial->class = ($count < 1 ? ' first' : '') . ($count >= $total - 1 ? ' last' : '') . ($count % 2 == 0 ? ' even' : ' odd'); $objPartial->by = $GLOBALS['TL_LANG']['MSC']['com_by']; $objPartial->id = 'c' . $objComments->id; $objPartial->timestamp = $objComments->date; $objPartial->datetime = date('Y-m-d\\TH:i:sP', $objComments->date); $objPartial->addReply = false; // Reply if ($objComments->addReply && $objComments->reply != '') { if (($objAuthor = $objComments->getRelated('author')) instanceof UserModel) { $objPartial->addReply = true; $objPartial->rby = $GLOBALS['TL_LANG']['MSC']['com_reply']; $objPartial->reply = $this->replaceInsertTags($objComments->reply); $objPartial->author = $objAuthor; // Clean the RTE output $objPartial->reply = \StringUtil::toHtml5($objPartial->reply); } } $arrComments[] = $objPartial->parse(); ++$count; } } $objTemplate->comments = $arrComments; $objTemplate->addComment = $GLOBALS['TL_LANG']['MSC']['addComment']; $objTemplate->name = $GLOBALS['TL_LANG']['MSC']['com_name']; $objTemplate->email = $GLOBALS['TL_LANG']['MSC']['com_email']; $objTemplate->website = $GLOBALS['TL_LANG']['MSC']['com_website']; $objTemplate->commentsTotal = $limit ? $gtotal : $total; // Add a form to create new comments $this->renderCommentForm($objTemplate, $objConfig, $strSource, $intParent, $varNotifies); }
/** * Return the meta fields of a news article as array. * * @param NewsModel $objArticle The model. * * @return array * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.Superglobals) * @SuppressWarnings(PHPMD.CamelCaseVariableName) */ protected function getMetaFields($objArticle) { $meta = deserialize($this->news_metaFields); if (!is_array($meta)) { return array(); } $return = array(); foreach ($meta as $field) { switch ($field) { case 'date': $return['date'] = Date::parse($GLOBALS['objPage']->datimFormat, $objArticle->date); break; case 'author': if (($objAuthor = $objArticle->getRelated('author')) !== null) { if ($objAuthor->google != '') { $return['author'] = $GLOBALS['TL_LANG']['MSC']['by'] . ' <a href="https://plus.google.com/' . $objAuthor->google . '" rel="author" target="_blank">' . $objAuthor->name . '</a>'; } else { $return['author'] = $GLOBALS['TL_LANG']['MSC']['by'] . ' ' . $objAuthor->name; } } break; case 'comments': if ($objArticle->noComments || $objArticle->source != 'default') { break; } $intTotal = CommentsModel::countPublishedBySourceAndParent('tl_news', $objArticle->id); $return['ccount'] = $intTotal; $return['comments'] = sprintf($GLOBALS['TL_LANG']['MSC']['commentCount'], $intTotal); break; default: } } return $return; }