Skip to content

Commit

Permalink
Various compatibility fixes
Browse files Browse the repository at this point in the history
* Fixed link generation which ignored the jumpto option in the tag objects
* Added ASCII conversion for CSS class names to prevent special characters in CSS class names
  • Loading branch information
hschottm committed Sep 23, 2020
1 parent a36e01e commit a0c155f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 66 deletions.
2 changes: 1 addition & 1 deletion classes/CalendarTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private function getTagsForTableAndId($table, $id, $url = false)
}
else
{
$strUrl = $objPage->getFrontendUrl('/articles/' . ((!$GLOBALS['TL_CONFIG']['disableAlias'] && strlen($objEvent->aAlias)) ? $objEvent->aAlias : $objEvent->aId));
$strUrl = \TagHelper::getPageObj()->getFrontendUrl('/articles/' . ((!$GLOBALS['TL_CONFIG']['disableAlias'] && strlen($objEvent->aAlias)) ? $objEvent->aAlias : $objEvent->aId));
}
break;
}
Expand Down
48 changes: 19 additions & 29 deletions classes/TagHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ public function __construct()
$this->import('Database');
}

public static function getPageObj($jumpTo = null)
{
global $objPage;
if(!empty($jumpTo))
{
return (new PageModel())->findPublishedById($jumpTo);
}
else
{
return $objPage;
}
}

public function encode($tag)
{
return str_replace('/', 'x2F', $tag);
Expand Down Expand Up @@ -334,24 +347,12 @@ public function parseArticlesHook($objTemplate, $row)
$objTemplate->showTags = $news_showtags;
if ($news_showtags)
{
$pageArr = array();
if (strlen($news_jumpto))
{
$objFoundPage = $this->Database->prepare("SELECT id, alias FROM tl_page WHERE id=?")
->limit(1)
->execute($news_jumpto);
$pageArr = ($objFoundPage->numRows) ? $objFoundPage->fetchAssoc() : array();
}
if (count($pageArr) == 0)
{
global $objPage;
$pageArr = $objPage->row();
}
$pageObj = self::getPageObj($news_jumpto);
$tags = $this->getTags($row['id'], 'tl_news');
$taglist = array();
foreach ($tags as $id => $tag)
{
$strUrl = ampersand($objPage->getFrontendUrl($items . '/tag/' . \TagHelper::encode($tag)));
$strUrl = ampersand($pageObj->getFrontendUrl($items . '/tag/' . \TagHelper::encode($tag)));
$tags[$id] = '<a href="' . $strUrl . '">' . StringUtil::specialchars($tag) . '</a>';
$taglist[$id] = array(
'url' => $tags[$id],
Expand All @@ -364,27 +365,16 @@ public function parseArticlesHook($objTemplate, $row)
$objTemplate->taglist = $taglist;
}
}

public function getTagsAndTaglistForIdAndTable($id, $table, $jumpto)
{
$pageArr = array();
if (strlen($jumpto))
{
$objFoundPage = $this->Database->prepare("SELECT id, alias FROM tl_page WHERE id=?")
->limit(1)
->execute($jumpto);
$pageArr = ($objFoundPage->numRows) ? $objFoundPage->fetchAssoc() : array();
}
if (count($pageArr) == 0)
{
global $objPage;
$pageArr = $objPage->row();
}
$pageObj = self::getPageObj($jumpto);

$tags = $this->getTags($id, $table);
$taglist = array();
foreach ($tags as $id => $tag)
{
$strUrl = ampersand($objPage->getFrontendUrl($items . '/tag/' . \TagHelper::encode($tag)));
$strUrl = StringUtil::ampersand($pageObj->getFrontendUrl($items . '/tag/' . \TagHelper::encode($tag)));
if (strlen(\Environment::get('queryString'))) $strUrl .= "?" . \Environment::get('queryString');
$tags[$id] = '<a href="' . $strUrl . '">' . StringUtil::specialchars($tag) . '</a>';
$taglist[$id] = array(
Expand Down
6 changes: 4 additions & 2 deletions classes/TagList.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Contao;

use Patchwork\Utf8;

/**
* Contao Open Source CMS - tags extension
*
Expand Down Expand Up @@ -337,12 +339,12 @@ protected function cloud_tags($tags)
*/
protected function getTagNameClass($tag)
{
return str_replace('"', '', str_replace(' ', '_', $tag));
return Utf8::toAscii(StringUtil::standardize($tag));
}

public static function _getTagNameClass($tag)
{
return str_replace('"', '', str_replace(' ', '_', $tag));
return Utf8::toAscii(StringUtil::standardize($tag));
}

protected function getRelevantPages($page_id)
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"require":{
"php":">=5.3",
"contao/core-bundle": "~4.9.0",
"patchwork/utf8": "^1.3",
"contao-community-alliance/composer-plugin": "~3.0"
},
"extra":{
Expand Down
42 changes: 19 additions & 23 deletions modules/ModuleTagCloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ protected function compile()
*/
protected function showTags()
{
global $objPage;
$this->loadLanguageFile('tl_module');
$strUrl = ampersand(\Environment::get('request'));
// Get target page
$objPageObject = $this->Database->prepare("SELECT id, alias FROM tl_page WHERE id=?")
->limit(1)
->execute($this->tag_jumpTo);
global $objPage;
$default = ($objPage != null) ? $objPage->row() : array();
$pageArr = ($objPageObject->numRows) ? $objPageObject->fetchAssoc() : $default;

$pageObj = \TagHelper::getPageObj($this->tag_jumpTo);

// $default = ($objPage != null) ? $objPage->row() : array();
// $pageArr = ($objPageObject->numRows) ? $objPageObject->fetchAssoc() : $default;
$strParams = '';

if ($this->keep_url_params)
Expand All @@ -109,9 +109,9 @@ protected function showTags()

foreach ($this->arrTags as $idx => $tag)
{
if (count($pageArr))
if (!empty($pageObj))
{
$strUrl = ampersand($objPage->getFrontendUrl('/tag/' . \TagHelper::encode($tag['tag_name'])));
$strUrl = ampersand($pageObj->getFrontendUrl('/tag/' . \TagHelper::encode($tag['tag_name'])));
if (strlen($strParams))
{
if (strpos($strUrl, '?') !== false)
Expand Down Expand Up @@ -144,7 +144,6 @@ protected function showTags()
}
if ($this->checkForContentElementOnPage)
{
global $objPage;
// get articles on page
$arrArticles = $this->Database->prepare("SELECT id FROM tl_article WHERE pid = ?")
->execute($objPage->id)->fetchEach('id');
Expand All @@ -164,10 +163,10 @@ protected function showTags()
$relatedlist = (strlen(\TagHelper::decode(\Input::get('related')))) ? preg_split("/,/", \TagHelper::decode(\Input::get('related'))) : array();
foreach ($this->arrRelated as $idx => $tag)
{
if (count($pageArr))
if (!empty($pageObj))
{
$allrelated = array_merge($relatedlist, array($tag['tag_name']));
$strUrl = ampersand($objPage->getFrontendUrl('/tag/' . \TagHelper::encode(\TagHelper::decode(\Input::get('tag'))) . '/related/' . \TagHelper::encode(join($allrelated, ','))));
$strUrl = ampersand($pageObj->getFrontendUrl('/tag/' . \TagHelper::encode(\TagHelper::decode(\Input::get('tag'))) . '/related/' . \TagHelper::encode(join($allrelated, ','))));
}
$this->arrRelated[$idx]['tag_url'] = $strUrl;
}
Expand Down Expand Up @@ -198,26 +197,23 @@ protected function showTags()
$this->Template->lngEmpty = $GLOBALS['TL_LANG']['tl_module']['tag_clear_tags'];
}
$GLOBALS['TL_JAVASCRIPT'][] = 'system/modules/tags/assets/tagcloud.js';
if (count($pageArr))
if (!empty($pageObj))
{
$this->Template->topten = $this->tag_topten;
if ($this->tag_topten)
{
foreach ($this->arrTopTenTags as $idx => $tag)
{
if (count($pageArr))
$strUrl = ampersand($pageObj->getFrontendUrl('/tag/' . \TagHelper::encode($tag['tag_name'])));
if (strlen($strParams))
{
$strUrl = ampersand($objPage->getFrontendUrl('/tag/' . \TagHelper::encode($tag['tag_name'])));
if (strlen($strParams))
if (strpos($strUrl, '?') !== false)
{
$strUrl .= '&amp;' . $strParams;
}
else
{
if (strpos($strUrl, '?') !== false)
{
$strUrl .= '&amp;' . $strParams;
}
else
{
$strUrl .= '?' . $strParams;
}
$strUrl .= '?' . $strParams;
}
}
$this->arrTopTenTags[$idx]['tag_url'] = $strUrl;
Expand Down
6 changes: 3 additions & 3 deletions modules/ModuleTagContentList.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ protected function getArticlesForTagSource($sourcetable)
{
if ($this->linktoarticles)
{ // link to articles
$articles[] = array('content' => '<a href="' . $objPage->getFrontendUrl('/articles/' . ((!$GLOBALS['TL_CONFIG']['disableAlias'] && strlen($objArticle->aAlias)) ? $objArticle->aAlias : $objArticle->aId)) . '" title="' . StringUtil::specialchars($objArticle->title) . '">' . $objArticle->title . '</a>', 'tags' => $taglist, 'data' => $objArticle->row());
$articles[] = array('content' => '<a href="' . \TagHelper::getPageObj($objArticle->tags_jumpto)->getFrontendUrl('/articles/' . ((!$GLOBALS['TL_CONFIG']['disableAlias'] && strlen($objArticle->aAlias)) ? $objArticle->aAlias : $objArticle->aId)) . '" title="' . StringUtil::specialchars($objArticle->title) . '">' . $objArticle->title . '</a>', 'tags' => $taglist, 'data' => $objArticle->row());
}
else
{ // link to pages
$articles[] = array('content' => '<a href="' . $objPage->getFrontendUrl() . '" title="' . StringUtil::specialchars($objArticle->title) . '">' . $objArticle->title . '</a>', 'tags' => $taglist, 'data' => $objArticle->row());
$articles[] = array('content' => '<a href="' . \TagHelper::getPageObj($objArticle->tags_jumpto)->getFrontendUrl() . '" title="' . StringUtil::specialchars($objArticle->title) . '">' . $objArticle->title . '</a>', 'tags' => $taglist, 'data' => $objArticle->row());
}
}
}
Expand All @@ -102,7 +102,7 @@ protected function getArticlesForTagSource($sourcetable)
$this->Template->articles = $articles;
$this->Template->empty = $GLOBALS['TL_LANG']['MSC']['emptyarticles'];
}

protected function getPages()
{
$pages = array();
Expand Down
14 changes: 6 additions & 8 deletions modules/ModuleTagScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ protected function compile()
$this->Template->lngTags = (strlen($this->clear_text)) ? $this->clear_text : $GLOBALS['TL_LANG']['tl_module']['tags'];
$this->Template->jumpTo = $this->jumpTo;
$this->Template->arrTags = $this->arrTags;
$objPageObject = $this->Database->prepare("SELECT id, alias FROM tl_page WHERE id=?")
->limit(1)
->execute($this->tag_jumpTo);
$pageArr = ($objPageObject->numRows) ? $objPageObject->fetchAssoc() : array();

$pageObj = \TagHelper::getPageObj($this->tag_jumpTo);
$strParams = '';
if ($this->keep_url_params)
{
Expand All @@ -75,9 +73,9 @@ protected function compile()
$tagurls = array();
foreach ($this->arrTags as $idx => $tag)
{
if (count($pageArr))
if (!empty($pageObj))
{
$strUrl = ampersand($objPage->getFrontendUrl('/tag/' . \TagHelper::encode($tag)));
$strUrl = ampersand($pageObj->getFrontendUrl('/tag/' . \TagHelper::encode($tag)));
if (strlen($strParams))
{
if (strpos($strUrl, '?') !== false)
Expand All @@ -93,7 +91,7 @@ protected function compile()
}
}
$this->Template->tag_urls = $tagurls;
$strEmptyUrl = ampersand($objPage->getFrontendUrl());
$strEmptyUrl = ampersand($pageObj->getFrontendUrl());
if (strlen($strParams))
{
if (strpos($strEmptyUrl, '?') !== false)
Expand Down Expand Up @@ -131,7 +129,7 @@ protected function compile()
$related = array_slice($newarr, 1);
$tagpath .= '/related/' . join($related, ',');
}
$strUrl = ampersand($objPage->getFrontendUrl($tagpath));
$strUrl = ampersand($pageObj->getFrontendUrl($tagpath));
if (strlen($strParams))
{
if (strpos($strUrl, '?') !== false)
Expand Down

0 comments on commit a0c155f

Please sign in to comment.