/**
  * @inheritdoc
  */
 protected function findCurrent()
 {
     $alias = (string) Input::getAutoItem($this->getUrlKey(), false, true);
     if ('' === $alias) {
         return null;
     }
     /** @var PageModel $objPage */
     global $objPage;
     if (($archives = NewsArchiveModel::findBy('jumpTo', $objPage->id)) === null) {
         return null;
     }
     // Fix Contao bug that returns a collection (see contao-changelanguage#71)
     $options = ['limit' => 1, 'return' => 'Model'];
     return NewsModel::findPublishedByParentAndIdOrAlias($alias, $archives->fetchEach('id'), $options);
 }
 protected function compile()
 {
     global $objPage;
     // Get the current news item
     $objNewsItem = NewsModel::findPublishedByParentAndIdOrAlias(Input::get('items'), $this->news_archives);
     if ($objNewsItem === null) {
         parent::compile();
     }
     $objPage->canonicalType = $objNewsItem->canonicalType;
     $objPage->canonicalJumpTo = $objNewsItem->canonicalJumpTo;
     $objPage->canonicalWebsite = $objNewsItem->canonicalWebsite;
     if ($objNewsItem->canonicalType == 'self') {
         $objPage->canonicalType = 'external';
         $objPage->canonicalWebsite = Environment::get('url') . TL_PATH . '/' . Environment::get('request');
     }
     parent::compile();
 }
 /**
  * Render a news.
  *
  * @param GetNewsEvent             $event           The event.
  *
  * @param string                   $eventName       The event name.
  *
  * @param EventDispatcherInterface $eventDispatcher The event dispatcher.
  *
  * @return void
  *
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  * @SuppressWarnings(PHPMD.Superglobals)
  * @SuppressWarnings(PHPMD.CamelCaseVariableName)
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function handleNews(GetNewsEvent $event, $eventName, EventDispatcherInterface $eventDispatcher)
 {
     if ($event->getNewsHtml()) {
         return;
     }
     $newsArchiveCollection = NewsArchiveModel::findAll();
     $newsArchiveIds = $newsArchiveCollection ? $newsArchiveCollection->fetchEach('id') : array();
     $newsModel = NewsModel::findPublishedByParentAndIdOrAlias($event->getNewsId(), $newsArchiveIds);
     if (!$newsModel) {
         return;
     }
     $newsModel = $newsModel->current();
     $newsArchiveModel = $newsModel->getRelated('pid');
     $objPage = PageModel::findWithDetails($newsArchiveModel->jumpTo);
     $objTemplate = new FrontendTemplate($event->getTemplate());
     $objTemplate->setData($newsModel->row());
     $objTemplate->class = $newsModel->cssClass != '' ? ' ' . $newsModel->cssClass : '';
     $objTemplate->newsHeadline = $newsModel->headline;
     $objTemplate->subHeadline = $newsModel->subheadline;
     $objTemplate->hasSubHeadline = $newsModel->subheadline ? true : false;
     $objTemplate->linkHeadline = $this->generateLink($eventDispatcher, $newsModel->headline, $newsModel);
     $objTemplate->more = $this->generateLink($eventDispatcher, $GLOBALS['TL_LANG']['MSC']['more'], $newsModel, false, true);
     $objTemplate->link = $this->generateNewsUrl($eventDispatcher, $newsModel);
     $objTemplate->archive = $newsModel->getRelated('pid');
     $objTemplate->count = 0;
     $objTemplate->text = '';
     // Clean the RTE output.
     if ($newsModel->teaser != '') {
         if ($objPage->outputFormat == 'xhtml') {
             $objTemplate->teaser = StringHelper::toXhtml($newsModel->teaser);
         } else {
             $objTemplate->teaser = StringHelper::toHtml5($newsModel->teaser);
         }
         $objTemplate->teaser = StringHelper::encodeEmail($objTemplate->teaser);
     }
     // Display the "read more" button for external/article links.
     if ($newsModel->source != 'default') {
         $objTemplate->text = true;
     } else {
         // Compile the news text.
         $objElement = ContentModel::findPublishedByPidAndTable($newsModel->id, 'tl_news');
         if ($objElement !== null) {
             while ($objElement->next()) {
                 $getContentElementEvent = new GetContentElementEvent($objElement->id);
                 $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_GET_CONTENT_ELEMENT, $getContentElementEvent);
                 $objTemplate->text .= $getContentElementEvent->getContentElementHtml();
             }
         }
     }
     $arrMeta = $this->getMetaFields($newsModel);
     // Add the meta information.
     $objTemplate->date = $arrMeta['date'];
     $objTemplate->hasMetaFields = !empty($arrMeta);
     $objTemplate->numberOfComments = $arrMeta['ccount'];
     $objTemplate->commentCount = $arrMeta['comments'];
     $objTemplate->timestamp = $newsModel->date;
     $objTemplate->author = $arrMeta['author'];
     $objTemplate->datetime = date('Y-m-d\\TH:i:sP', $newsModel->date);
     $objTemplate->addImage = false;
     // Add an image.
     if ($newsModel->addImage && $newsModel->singleSRC != '') {
         $objModel = FilesModel::findByUuid($newsModel->singleSRC);
         if ($objModel === null) {
             if (!Validator::isUuid($newsModel->singleSRC)) {
                 $objTemplate->text = '<p class="error">' . $GLOBALS['TL_LANG']['ERR']['version2format'] . '</p>';
             }
         } elseif (is_file(TL_ROOT . '/' . $objModel->path)) {
             // Do not override the field now that we have a model registry (see #6303).
             $arrArticle = $newsModel->row();
             // Override the default image size.
             // This is always false!
             if ($this->imgSize != '') {
                 $size = deserialize($this->imgSize);
                 if ($size[0] > 0 || $size[1] > 0) {
                     $arrArticle['size'] = $this->imgSize;
                 }
             }
             $arrArticle['singleSRC'] = $objModel->path;
             $addImageToTemplateEvent = new AddImageToTemplateEvent($arrArticle, $objTemplate);
             $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_ADD_IMAGE_TO_TEMPLATE, $addImageToTemplateEvent);
         }
     }
     $objTemplate->enclosure = array();
     // Add enclosures.
     if ($newsModel->addEnclosure) {
         $addEnclosureToTemplateEvent = new AddEnclosureToTemplateEvent($newsModel->row(), $objTemplate);
         $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_ADD_ENCLOSURE_TO_TEMPLATE, $addEnclosureToTemplateEvent);
     }
     $news = $objTemplate->parse();
     $event->setNewsHtml($news);
 }
Example #4
0
 /**
  * Generate the module
  */
 protected function compile()
 {
     /** @var PageModel $objPage */
     global $objPage;
     $this->Template->articles = '';
     $this->Template->referer = 'javascript:history.go(-1)';
     $this->Template->back = $GLOBALS['TL_LANG']['MSC']['goBack'];
     // Get the news item
     $objArticle = \NewsModel::findPublishedByParentAndIdOrAlias(\Input::get('items'), $this->news_archives);
     if (null === $objArticle) {
         throw new PageNotFoundException('Page not found');
     }
     $arrArticle = $this->parseArticle($objArticle);
     $this->Template->articles = $arrArticle;
     // Overwrite the page title (see #2853 and #4955)
     if ($objArticle->headline != '') {
         $objPage->pageTitle = strip_tags(strip_insert_tags($objArticle->headline));
     }
     // Overwrite the page description
     if ($objArticle->teaser != '') {
         $objPage->description = $this->prepareMetaDescription($objArticle->teaser);
     }
     $bundles = \System::getContainer()->getParameter('kernel.bundles');
     // HOOK: comments extension required
     if ($objArticle->noComments || !isset($bundles['ContaoCommentsBundle'])) {
         $this->Template->allowComments = false;
         return;
     }
     /** @var NewsArchiveModel $objArchive */
     $objArchive = $objArticle->getRelated('pid');
     $this->Template->allowComments = $objArchive->allowComments;
     // Comments are not allowed
     if (!$objArchive->allowComments) {
         return;
     }
     // Adjust the comments headline level
     $intHl = min(intval(str_replace('h', '', $this->hl)), 5);
     $this->Template->hlc = 'h' . ($intHl + 1);
     $this->import('Comments');
     $arrNotifies = array();
     // Notify the system administrator
     if ($objArchive->notify != 'notify_author') {
         $arrNotifies[] = $GLOBALS['TL_ADMIN_EMAIL'];
     }
     // Notify the author
     if ($objArchive->notify != 'notify_admin') {
         /** @var UserModel $objAuthor */
         if (($objAuthor = $objArticle->getRelated('author')) !== null && $objAuthor->email != '') {
             $arrNotifies[] = $objAuthor->email;
         }
     }
     $objConfig = new \stdClass();
     $objConfig->perPage = $objArchive->perPage;
     $objConfig->order = $objArchive->sortOrder;
     $objConfig->template = $this->com_template;
     $objConfig->requireLogin = $objArchive->requireLogin;
     $objConfig->disableCaptcha = $objArchive->disableCaptcha;
     $objConfig->bbcode = $objArchive->bbcode;
     $objConfig->moderate = $objArchive->moderate;
     $this->Comments->addCommentsToTemplate($this->Template, $objConfig, 'tl_news', $objArticle->id, $arrNotifies);
 }