Skip to content

Commit

Permalink
Redirect if a news/event has an external target and is called via the…
Browse files Browse the repository at this point in the history
… default URL (see #1732)

Description
-----------

| Q                | A
| -----------------| ---
| Fixed issues     | Fixes #1707 #1498
| Docs PR or issue | -

Commits
-------

972627f Redirect if a news/event has an external target and is called via the default URL
ef11121 Show the teaser if there are no details
  • Loading branch information
leofeyer committed May 12, 2020
1 parent 41c7ba2 commit ed3a3f4
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 8 deletions.
1 change: 0 additions & 1 deletion calendar-bundle/src/Resources/contao/classes/Events.php
Expand Up @@ -333,7 +333,6 @@ protected function addEvent($objEvents, $intStart, $intEnd, $intBegin, $intLimit
// Display the "read more" button for external/article links
if ($objEvents->source != 'default')
{
$arrEvent['details'] = true;
$arrEvent['hasDetails'] = true;
}

Expand Down
36 changes: 33 additions & 3 deletions calendar-bundle/src/Resources/contao/modules/ModuleEventReader.php
Expand Up @@ -12,6 +12,7 @@

use Contao\CoreBundle\Exception\InternalServerErrorException;
use Contao\CoreBundle\Exception\PageNotFoundException;
use Contao\CoreBundle\Exception\RedirectResponseException;
use FOS\HttpCache\ResponseTagger;
use Patchwork\Utf8;

Expand Down Expand Up @@ -91,12 +92,42 @@ protected function compile()
// Get the current event
$objEvent = CalendarEventsModel::findPublishedByParentAndIdOrAlias(Input::get('events'), $this->cal_calendar);

// The event does not exist or has an external target (see #33)
if (null === $objEvent || $objEvent->source != 'default')
// The event does not exist (see #33)
if ($objEvent === null)
{
throw new PageNotFoundException('Page not found: ' . Environment::get('uri'));
}

// Redirect if the event has a target URL (see #1498)
switch ($objEvent->source) {
case 'internal':
if ($page = PageModel::findPublishedById($objEvent->jumpTo))
{
throw new RedirectResponseException($page->getAbsoluteUrl(), 301);
}

throw new InternalServerErrorException('Invalid "jumpTo" value or target page not public');
break;

case 'article':
if (($article = ArticleModel::findByPk($objEvent->articleId)) && ($page = PageModel::findPublishedById($article->pid)))
{
throw new RedirectResponseException($page->getAbsoluteUrl('/articles/' . ($article->alias ?: $article->id)), 301);
}

throw new InternalServerErrorException('Invalid "articleId" value or target page not public');
break;

case 'external':
if ($objEvent->url)
{
throw new RedirectResponseException($objEvent->url, 301);
}

throw new InternalServerErrorException('Empty target URL');
break;
}

// Overwrite the page title (see #2853, #4955 and #87)
if ($objEvent->pageTitle)
{
Expand Down Expand Up @@ -228,7 +259,6 @@ protected function compile()
// Display the "read more" button for external/article links
if ($objEvent->source != 'default')
{
$objTemplate->details = true;
$objTemplate->hasDetails = true;
}

Expand Down
Expand Up @@ -18,7 +18,7 @@
</p>
<?php endif; ?>

<?php if ($this->hasDetails): ?>
<?php if ($this->details): ?>
<?= $this->details ?>
<?php else: ?>
<div class="ce_text block">
Expand Down
Expand Up @@ -23,7 +23,7 @@
<?php $this->insert('image', $this->arrData); ?>
<?php endif; ?>

<?php if ($this->hasDetails): ?>
<?php if ($this->details): ?>
<?= $this->details ?>
<?php else: ?>
<div class="ce_text block" itemprop="description">
Expand Down
35 changes: 33 additions & 2 deletions news-bundle/src/Resources/contao/modules/ModuleNewsReader.php
Expand Up @@ -12,6 +12,7 @@

use Contao\CoreBundle\Exception\InternalServerErrorException;
use Contao\CoreBundle\Exception\PageNotFoundException;
use Contao\CoreBundle\Exception\RedirectResponseException;
use Patchwork\Utf8;

/**
Expand Down Expand Up @@ -89,12 +90,42 @@ protected function compile()
// Get the news item
$objArticle = NewsModel::findPublishedByParentAndIdOrAlias(Input::get('items'), $this->news_archives);

// The news item does not exist or has an external target (see #33)
if (null === $objArticle || $objArticle->source != 'default')
// The news item does not exist (see #33)
if ($objArticle === null)
{
throw new PageNotFoundException('Page not found: ' . Environment::get('uri'));
}

// Redirect if the news item has a target URL (see #1498)
switch ($objArticle->source) {
case 'internal':
if ($page = PageModel::findPublishedById($objArticle->jumpTo))
{
throw new RedirectResponseException($page->getAbsoluteUrl(), 301);
}

throw new InternalServerErrorException('Invalid "jumpTo" value or target page not public');
break;

case 'article':
if (($article = ArticleModel::findByPk($objArticle->articleId)) && ($page = PageModel::findPublishedById($article->pid)))
{
throw new RedirectResponseException($page->getAbsoluteUrl('/articles/' . ($article->alias ?: $article->id)), 301);
}

throw new InternalServerErrorException('Invalid "articleId" value or target page not public');
break;

case 'external':
if ($objArticle->url)
{
throw new RedirectResponseException($objArticle->url, 301);
}

throw new InternalServerErrorException('Empty target URL');
break;
}

// Set the default template
if (!$this->news_template)
{
Expand Down

0 comments on commit ed3a3f4

Please sign in to comment.