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

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