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

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