vendor/contao/news-bundle/src/Resources/contao/modules/ModuleNewsReader.php line 144

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Contao.
  4. *
  5. * (c) Leo Feyer
  6. *
  7. * @license LGPL-3.0-or-later
  8. */
  9. namespace Contao;
  10. use Contao\CoreBundle\Exception\InternalServerErrorException;
  11. use Contao\CoreBundle\Exception\PageNotFoundException;
  12. use Contao\CoreBundle\Exception\RedirectResponseException;
  13. use Contao\CoreBundle\Routing\ResponseContext\HtmlHeadBag\HtmlHeadBag;
  14. use Contao\CoreBundle\Util\UrlUtil;
  15. /**
  16. * Front end module "news reader".
  17. *
  18. * @property Comments $Comments
  19. * @property string $com_template
  20. * @property array $news_archives
  21. */
  22. class ModuleNewsReader extends ModuleNews
  23. {
  24. /**
  25. * Template
  26. * @var string
  27. */
  28. protected $strTemplate = 'mod_newsreader';
  29. /**
  30. * Display a wildcard in the back end
  31. *
  32. * @throws InternalServerErrorException
  33. *
  34. * @return string
  35. */
  36. public function generate()
  37. {
  38. $request = System::getContainer()->get('request_stack')->getCurrentRequest();
  39. if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request))
  40. {
  41. $objTemplate = new BackendTemplate('be_wildcard');
  42. $objTemplate->wildcard = '### ' . $GLOBALS['TL_LANG']['FMD']['newsreader'][0] . ' ###';
  43. $objTemplate->title = $this->headline;
  44. $objTemplate->id = $this->id;
  45. $objTemplate->link = $this->name;
  46. $objTemplate->href = StringUtil::specialcharsUrl(System::getContainer()->get('router')->generate('contao_backend', array('do'=>'themes', 'table'=>'tl_module', 'act'=>'edit', 'id'=>$this->id)));
  47. return $objTemplate->parse();
  48. }
  49. // Set the item from the auto_item parameter
  50. if (!isset($_GET['items']) && isset($_GET['auto_item']) && Config::get('useAutoItem'))
  51. {
  52. Input::setGet('items', Input::get('auto_item'));
  53. }
  54. // Return an empty string if "items" is not set (to combine list and reader on same page)
  55. if (!Input::get('items'))
  56. {
  57. return '';
  58. }
  59. $this->news_archives = $this->sortOutProtected(StringUtil::deserialize($this->news_archives));
  60. if (empty($this->news_archives) || !\is_array($this->news_archives))
  61. {
  62. throw new InternalServerErrorException('The news reader ID ' . $this->id . ' has no archives specified.');
  63. }
  64. return parent::generate();
  65. }
  66. /**
  67. * Generate the module
  68. */
  69. protected function compile()
  70. {
  71. $this->Template->articles = '';
  72. if ($this->overviewPage)
  73. {
  74. $this->Template->referer = PageModel::findById($this->overviewPage)->getFrontendUrl();
  75. $this->Template->back = $this->customLabel ?: $GLOBALS['TL_LANG']['MSC']['newsOverview'];
  76. }
  77. else
  78. {
  79. trigger_deprecation('contao/news-bundle', '4.13', 'If you do not select an overview page in the news reader module, the "go back" link will no longer be shown in Contao 5.0.');
  80. $this->Template->referer = 'javascript:history.go(-1)';
  81. $this->Template->back = $GLOBALS['TL_LANG']['MSC']['goBack'];
  82. }
  83. // Get the news item
  84. $objArticle = NewsModel::findPublishedByParentAndIdOrAlias(Input::get('items'), $this->news_archives);
  85. // The news item does not exist (see #33)
  86. if ($objArticle === null)
  87. {
  88. throw new PageNotFoundException('Page not found: ' . Environment::get('uri'));
  89. }
  90. // Redirect if the news item has a target URL (see #1498)
  91. switch ($objArticle->source) {
  92. case 'internal':
  93. if ($page = PageModel::findPublishedById($objArticle->jumpTo))
  94. {
  95. throw new RedirectResponseException($page->getAbsoluteUrl(), 301);
  96. }
  97. throw new InternalServerErrorException('Invalid "jumpTo" value or target page not public');
  98. case 'article':
  99. if (($article = ArticleModel::findByPk($objArticle->articleId)) && ($page = PageModel::findPublishedById($article->pid)))
  100. {
  101. throw new RedirectResponseException($page->getAbsoluteUrl('/articles/' . ($article->alias ?: $article->id)), 301);
  102. }
  103. throw new InternalServerErrorException('Invalid "articleId" value or target page not public');
  104. case 'external':
  105. if ($objArticle->url)
  106. {
  107. $url = System::getContainer()->get('contao.insert_tag.parser')->replaceInline($objArticle->url);
  108. $url = UrlUtil::makeAbsolute($url, Environment::get('base'));
  109. throw new RedirectResponseException($url, 301);
  110. }
  111. throw new InternalServerErrorException('Empty target URL');
  112. }
  113. // Set the default template
  114. if (!$this->news_template)
  115. {
  116. $this->news_template = 'news_full';
  117. }
  118. $arrArticle = $this->parseArticle($objArticle);
  119. $this->Template->articles = $arrArticle;
  120. // Overwrite the page metadata (see #2853, #4955 and #87)
  121. $responseContext = System::getContainer()->get('contao.routing.response_context_accessor')->getResponseContext();
  122. if ($responseContext && $responseContext->has(HtmlHeadBag::class))
  123. {
  124. /** @var HtmlHeadBag $htmlHeadBag */
  125. $htmlHeadBag = $responseContext->get(HtmlHeadBag::class);
  126. $htmlDecoder = System::getContainer()->get('contao.string.html_decoder');
  127. if ($objArticle->pageTitle)
  128. {
  129. $htmlHeadBag->setTitle($objArticle->pageTitle); // Already stored decoded
  130. }
  131. elseif ($objArticle->headline)
  132. {
  133. $htmlHeadBag->setTitle($htmlDecoder->inputEncodedToPlainText($objArticle->headline));
  134. }
  135. if ($objArticle->description)
  136. {
  137. $htmlHeadBag->setMetaDescription($htmlDecoder->inputEncodedToPlainText($objArticle->description));
  138. }
  139. elseif ($objArticle->teaser)
  140. {
  141. $htmlHeadBag->setMetaDescription($htmlDecoder->htmlToPlainText($objArticle->teaser));
  142. }
  143. if ($objArticle->robots)
  144. {
  145. $htmlHeadBag->setMetaRobots($objArticle->robots);
  146. }
  147. }
  148. $bundles = System::getContainer()->getParameter('kernel.bundles');
  149. // HOOK: comments extension required
  150. if ($objArticle->noComments || !isset($bundles['ContaoCommentsBundle']))
  151. {
  152. $this->Template->allowComments = false;
  153. return;
  154. }
  155. /** @var NewsArchiveModel $objArchive */
  156. $objArchive = $objArticle->getRelated('pid');
  157. $this->Template->allowComments = $objArchive->allowComments;
  158. // Comments are not allowed
  159. if (!$objArchive->allowComments)
  160. {
  161. return;
  162. }
  163. // Adjust the comments headline level
  164. $intHl = min((int) str_replace('h', '', $this->hl), 5);
  165. $this->Template->hlc = 'h' . ($intHl + 1);
  166. $this->import(Comments::class, 'Comments');
  167. $arrNotifies = array();
  168. // Notify the system administrator
  169. if ($objArchive->notify != 'notify_author' && isset($GLOBALS['TL_ADMIN_EMAIL']))
  170. {
  171. $arrNotifies[] = $GLOBALS['TL_ADMIN_EMAIL'];
  172. }
  173. /** @var UserModel $objAuthor */
  174. if ($objArchive->notify != 'notify_admin' && ($objAuthor = $objArticle->getRelated('author')) instanceof UserModel && $objAuthor->email)
  175. {
  176. $arrNotifies[] = $objAuthor->email;
  177. }
  178. $objConfig = new \stdClass();
  179. $objConfig->perPage = $objArchive->perPage;
  180. $objConfig->order = $objArchive->sortOrder;
  181. $objConfig->template = $this->com_template;
  182. $objConfig->requireLogin = $objArchive->requireLogin;
  183. $objConfig->disableCaptcha = $objArchive->disableCaptcha;
  184. $objConfig->bbcode = $objArchive->bbcode;
  185. $objConfig->moderate = $objArchive->moderate;
  186. $this->Comments->addCommentsToTemplate($this->Template, $objConfig, 'tl_news', $objArticle->id, $arrNotifies);
  187. }
  188. }
  189. class_alias(ModuleNewsReader::class, 'ModuleNewsReader');