Skip to content

Commit

Permalink
Finish simultaneous contao 4.13 and 5.1 support
Browse files Browse the repository at this point in the history
  • Loading branch information
zoglo committed Aug 3, 2023
1 parent edc6311 commit 185fe7a
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 781 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

---

> Working with **Contao 4.9** and up to **Contao 4.13** (PHP ^7.4 and PHP 8)
> Working with **Contao 4.13** and **Contao 5.1** (PHP ^8.1)
---

Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"description": "A glossary extension for the Contao Open Source CMS. Glossaries are organized in archives similar to news and events and can be displayed via a list and reader module.",
"license": "AGPL-3.0-or-later",
"authors": [
{
"name": "Fabian Ekert",
"homepage": "https://github.com/eki89"
},
{
"name": "Sebastian Zoglowek",
"homepage": "https://github.com/zoglo"
},
{
"name": "Fabian Ekert",
"homepage": "https://github.com/eki89"
}
],
"require": {
Expand Down Expand Up @@ -43,7 +43,7 @@
},
"extra": {
"branch-alias": {
"dev-main": "2.1.x-dev"
"dev-main": "2.2.x-dev"
},
"contao-manager-plugin": "Oveleon\\ContaoGlossaryBundle\\ContaoManager\\Plugin"
},
Expand Down
153 changes: 38 additions & 115 deletions contao/classes/Glossary.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
use Contao\PageModel;
use Contao\StringUtil;
use Contao\System;
use Contao\Validator;
use Oveleon\ContaoGlossaryBundle\Model\GlossaryItemModel;
use Oveleon\ContaoGlossaryBundle\Model\GlossaryModel;
use function Symfony\Component\String\u;

/**
Expand All @@ -41,95 +41,6 @@ class Glossary extends Frontend
*/
private static array $arrUrlCache = [];

/**
* Add glossary items to the indexer.
*/
public function getSearchablePages(array $arrPages, $intRoot = 0, bool $blnIsSitemap = false): array
{
$arrRoot = [];

if ($intRoot > 0)
{
$arrRoot = $this->Database->getChildRecords($intRoot, PageModel::getTable());
}

$arrProcessed = [];
$time = time();

// Get all glossaries
$objGlossary = GlossaryModel::findByProtected('');

// Walk through each glossary
if (null !== $objGlossary)
{
while ($objGlossary->next())
{
// Skip glossaries without target page
if (!$objGlossary->jumpTo)
{
continue;
}

// Skip glossaries outside the root nodes
if (!empty($arrRoot) && !\in_array($objGlossary->jumpTo, $arrRoot))
{
continue;
}

// Get the URL of the jumpTo page
if (!isset($arrProcessed[$objGlossary->jumpTo]))
{
$objParent = PageModel::findWithDetails($objGlossary->jumpTo);

// The target page does not exist
if (null === $objParent)
{
continue;
}

// The target page has not been published (see #5520)
if (!$objParent->published || ($objParent->start && $objParent->start > $time) || ($objParent->stop && $objParent->stop <= $time))
{
continue;
}

if ($blnIsSitemap)
{
// The target page is protected (see #8416)
if ($objParent->protected)
{
continue;
}

// The target page is exempt from the sitemap (see #6418)
if ('noindex,nofollow' === $objParent->robots)
{
continue;
}
}

// Generate the URL
$arrProcessed[$objGlossary->jumpTo] = $objParent->getAbsoluteUrl(Config::get('useAutoItem') ? '/%s' : '/items/%s');
}

$strUrl = $arrProcessed[$objGlossary->jumpTo];

// Get the items
$objArticle = GlossaryItemModel::findPublishedDefaultByPid($objGlossary->id);

if (null !== $objArticle)
{
while ($objArticle->next())
{
$arrPages[] = $this->getLink($objArticle, $strUrl);
}
}
}
}

return $arrPages;
}

/**
* Generate a URL and return it as string.
*/
Expand All @@ -156,7 +67,14 @@ public static function generateUrl(GlossaryItemModel $objItem, bool $blnAbsolute
}
else
{
self::$arrUrlCache[$strCacheKey] = StringUtil::ampersand($objItem->url);
$url = $objItem->url;

if (Validator::isRelativeUrl($url))
{
$url = Environment::get('path') . '/' . $url;
}

self::$arrUrlCache[$strCacheKey] = StringUtil::ampersand($url);
}
break;

Expand Down Expand Up @@ -188,7 +106,7 @@ public static function generateUrl(GlossaryItemModel $objItem, bool $blnAbsolute

if (!$objPage instanceof PageModel)
{
self::$arrUrlCache[$strCacheKey] = StringUtil::ampersand(Environment::get('request'));
self::$arrUrlCache[$strCacheKey] = StringUtil::ampersand(Environment::get('requestUri'));
}
else
{
Expand Down Expand Up @@ -256,8 +174,8 @@ public static function parseGlossaryItem(GlossaryItemModel $objGlossaryItem, str
$objTemplate->teaser = StringUtil::encodeEmail($objTemplate->teaser);

// Replace insert tags within teaser when fetching items via controller (see #13)
// ToDo: rewrite
//$objTemplate->teaser = Controller::replaceInsertTags($objTemplate->teaser);
$parser = System::getContainer()->get('contao.insert_tag.parser');
$objTemplate->teaser = $parser->replace((string) $objTemplate->teaser);
}

// Display the "read more" button for external/article links
Expand Down Expand Up @@ -293,49 +211,54 @@ public static function parseGlossaryItem(GlossaryItemModel $objGlossaryItem, str
$objTemplate->addImage = false;

// Add an image
if ($objGlossaryItem->addImage && $objGlossaryItem->singleSRC)
if ($objGlossaryItem->addImage)
{
$objModel = FilesModel::findByUuid($objGlossaryItem->singleSRC);

if (null !== $objModel && is_file(System::getContainer()->getParameter('kernel.project_dir') . 'Glossary.php/' .$objModel->path))
if (null !== $objModel)
{
// Do not override the field now that we have a model registry
$arrGlossaryItem = $objGlossaryItem->row();

// ToDo: Move method into src
// Override the default image size
if ($imgSize)
{
$size = StringUtil::deserialize($imgSize);

if ($size[0] > 0 || $size[1] > 0 || is_numeric($size[2]) || ($size[2][0] ?? null) === '_')
{
$arrGlossaryItem['size'] = $imgSize;
$imgSize = $imgSize;
}
}

$arrGlossaryItem['singleSRC'] = $objModel->path;
Controller::addImageToTemplate($objTemplate, $arrGlossaryItem, null, null, $objModel);
$figureBuilder = System::getContainer()
->get('contao.image.studio')
->createFigureBuilder()
->from($objModel->path)
->setSize($imgSize)
->enableLightbox((bool) $objGlossaryItem->fullsize);

// Link to the glossary item if no image link has been defined
if (!$objTemplate->fullsize && !$objTemplate->imageUrl)
// If the external link is opened in a new window, open the image link in a new window as well (see #210)
if ('external' === $objTemplate->source && $objTemplate->target)
{
// Load language for 'read more' link
System::loadLanguageFile('default');

// Unset the image title attribute
$picture = $objTemplate->picture;
unset($picture['title']);
$objTemplate->picture = $picture;

// Link to the glossary item
$objTemplate->href = $objTemplate->link;
$objTemplate->linkTitle = StringUtil::specialchars(sprintf($GLOBALS['TL_LANG']['MSC']['readMore'], $objGlossaryItem->keyword), true);
$figureBuilder->setLinkAttribute('target', '_blank');
}

// If the external link is opened in a new window, open the image link in a new window, too
if ('external' === $objTemplate->source && $objTemplate->target && false === strpos($objTemplate->attributes, 'target="_blank"'))
if (null !== ($figure = $figureBuilder->buildIfResourceExists()))
{
// ToDo: intCount (see contao #5708/#5851).
if (!$figure->getLinkHref())
{
$objTemplate->attributes .= ' target="_blank"';
$linkTitle = StringUtil::specialchars(sprintf($GLOBALS['TL_LANG']['MSC']['readMore'], $objGlossaryItem->keyword), true);

$figure = $figureBuilder
->setLinkHref($objTemplate->link)
->setLinkAttribute('title', $linkTitle)
->build();
}

$figure->applyLegacyTemplateData($objTemplate, $objGlossaryItem->imagemargin, $objGlossaryItem->floating);
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions contao/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
$GLOBALS['TL_MODELS']['tl_glossary'] = GlossaryModel::class;
$GLOBALS['TL_MODELS']['tl_glossary_item'] = GlossaryItemModel::class;

// Register hooks
$GLOBALS['TL_HOOKS']['getSearchablePages'][] = ['Oveleon\ContaoGlossaryBundle\Glossary', 'getSearchablePages'];

// Add permissions
$GLOBALS['TL_PERMISSIONS'][] = 'glossarys';
$GLOBALS['TL_PERMISSIONS'][] = 'glossaryp';
Loading

0 comments on commit 185fe7a

Please sign in to comment.