comments-bundle/src/Resources/contao/classes/Comments.php
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

		// Tag the response
		if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
		{
			/** @var ResponseTagger $responseTagger */
			$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
			$responseTagger->addTags(array(sprintf('contao.comments.%s.%s', $strSource, $intParent)));
		}

		// 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) ?? 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';
			}

			$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('&#123;&#123;', '&#125;&#125;'), $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->hlcText = $GLOBALS['TL_LANG']['MSC']['comments'];
		$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);
	}
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

		// Tag the response
		if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
		{
			/** @var ResponseTagger $responseTagger */
			$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
			$responseTagger->addTags(array(sprintf('contao.comments.%s.%s', $strSource, $intParent)));
		}

		// 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) ?? 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';
			}

			$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('&#123;&#123;', '&#125;&#125;'), $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->hlcText = $GLOBALS['TL_LANG']['MSC']['comments'];
		$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);
	}
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 // Tag the response if ( System :: getContainer ( ) -> has ( 'fos_http_cache.http.symfony_response_tagger' ) ) { /** @var ResponseTagger $responseTagger */ $ responseTagger = System :: getContainer ( ) -> get ( 'fos_http_cache.http.symfony_response_tagger' ) ; $ responseTagger -> addTags ( array ( sprintf ( 'contao.comments.%s.%s' , $ strSource , $ intParent ) ) ) ; } // 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 ) ?? 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' ; } $ 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 ( '&#123;&#123;' , '&#125;&#125;' ) , $ 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 -> hlcText = $ GLOBALS [ 'TL_LANG' ] [ 'MSC' ] [ 'comments' ] ; $ 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 ) ; }
Add comments to a template

@param FrontendTemplate $objTemplate
@param \stdClass        $objConfig
@param string           $strSource
@param integer          $intParent
@param mixed            $varNotifies
Add comments to a template