Skip to content

Commit

Permalink
simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg committed Sep 3, 2021
1 parent 8a48d45 commit 43b4068
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 164 deletions.
175 changes: 16 additions & 159 deletions src/EventListener/InheritArticleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,128 +12,21 @@

namespace InheritArticleBundle\EventListener;

use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\Image\PictureFactory;
use Contao\LayoutModel;
use Contao\PageModel;
use Contao\PageRegular;
use Contao\ArticleModel;
use Contao\Controller;
use Doctrine\DBAL\Connection;
use Symfony\Contracts\Service\ResetInterface;

class InheritArticleListener implements ResetInterface
class InheritArticleListener
{
/** @var array */
protected $columns;

/** @var array */
protected $sections;

/** @var string */
protected $modules;

/** @var bool */
protected $isGetArticlesHook = false;
private $isGetArticlesHook = false;

/** @var Connection */
protected $db;
private $db;

/** @var ContaoFramework */
protected $framework;

/** @var PictureFactory */
protected $pictureFactory;

public function __construct(Connection $db, ContaoFramework $framework, PictureFactory $pictureFactory)
public function __construct(Connection $db)
{
$this->db = $db;
$this->framework = $framework;
$this->pictureFactory = $pictureFactory;
}

public function onGetPageLayout(PageModel $pageModel, LayoutModel $layoutModel, PageRegular $pageRegular): void
{
$stringUtil = $this->framework->getAdapter(\Contao\StringUtil::class);
$moduleModel = $this->framework->getAdapter(\Contao\ModuleModel::class);

// Reset the cached data
$this->reset();

// Initialize modules and sections
$arrSections = ['header', 'left', 'right', 'main', 'footer'];
$arrModules = $stringUtil->deserialize($layoutModel->modules);
$arrModuleIds = [];

// Filter the disabled modules
foreach ($arrModules as $module) {
if ($module['enable']) {
$arrModuleIds[] = $module['mod'];
}
}

// Get all modules in a single DB query
$objModules = $moduleModel->findMultipleByIds($arrModuleIds);

if (null !== $objModules || 0 === $arrModules[0]['mod'] || '0' === $arrModules[0]['mod']) { // see #4137
$arrMapper = [];

// Set theme and layout related information in the page object (see #8)
$this->applyThemeAndLayout($pageModel, $layoutModel);

// Create a mapper array in case a module is included more than once (see #4849)
if (null !== $objModules) {
while ($objModules->next()) {
$arrMapper[$objModules->id] = $objModules->current();
}
}

foreach ($arrModules as $arrModule) {
// Disabled module
if (!$arrModule['enable']) {
continue;
}

// Replace the module ID with the module model
if ($arrModule['mod'] > 0 && isset($arrMapper[$arrModule['mod']])) {
$arrModule['mod'] = $arrMapper[$arrModule['mod']];
}

// Generate the modules
if (\in_array($arrModule['col'], $arrSections, true)) {
// Filter active sections (see #3273)
if ('header' === $arrModule['col'] && '2rwh' !== $layoutModel->rows && '3rw' !== $layoutModel->rows) {
continue;
}
if ('left' === $arrModule['col'] && '2cll' !== $layoutModel->cols && '3cl' !== $layoutModel->cols) {
continue;
}
if ('right' === $arrModule['col'] && '2clr' !== $layoutModel->cols && '3cl' !== $layoutModel->cols) {
continue;
}
if ('footer' === $arrModule['col'] && '2rwf' !== $layoutModel->rows && '3rw' !== $layoutModel->rows) {
continue;
}

$this->columns[$arrModule['col']] .= $this->getFrontendModule($pageModel, $arrModule['mod'], $arrModule['col']);
} else {
$this->sections[$arrModule['col']] .= $this->getFrontendModule($pageModel, $arrModule['mod'], $arrModule['col']);
}
}
}

// Empty the modules in the layout
$this->modules = $layoutModel->modules;
$layoutModel->modules = serialize([]);
}

public function onGeneratePage(PageModel $pageModel, LayoutModel $layoutModel, PageRegular $pageRegular): void
{
foreach ($this->columns as $column => $content) {
$pageRegular->Template->{$column} = $content;
}

$pageRegular->Template->sections = $this->sections;

$layoutModel->modules = $this->modules ?? serialize([]);
}

public function onGetArticles(int $pageId, string $column): ?string
Expand All @@ -145,38 +38,27 @@ public function onGetArticles(int $pageId, string $column): ?string

$this->isGetArticlesHook = true;

$articles = $this->getFrontendModule(PageModel::findById($pageId), 0, $column);
$articles = $this->getRenderedInheritedArticles($pageId, $column);

$this->isGetArticlesHook = false;

return $articles;
}

public function reset(): void
{
$this->columns = [];
$this->sections = [];
$this->modules = null;
}

protected function getFrontendModule(PageModel $page, $module, string $column): string
private function getRenderedInheritedArticles(int $pageId, string $column): string
{
$generatedModule = $this->framework->getAdapter(\Contao\Controller::class)->getFrontendModule($module, $column);

if (\is_object($module) && (0 !== $module || '0' !== $module)) {
return $generatedModule;
}
$baseArticles = Controller::getFrontendModule(0, $column);

// Initialize pid
$pid = $page->id;
$pid = $pageId;

// Get all the parents
$parents = [];

// Search for next parent ids while parent id > 0
do {
// Get the next pid
$parent = $this->db->executeQuery('SELECT pid FROM tl_page WHERE id=?', [$pid])->fetch();
$parent = $this->db->executeQuery('SELECT pid FROM tl_page WHERE id=?', [$pid])->fetchAssociative();

// If there are no parents anymore, break the loop
if (!$parent) {
Expand All @@ -192,7 +74,7 @@ protected function getFrontendModule(PageModel $page, $module, string $column):

// Initialize rendered article modules
$renderedArticles = [];
$renderedArticles[0] = $generatedModule;
$renderedArticles[0] = $baseArticles;

// Go through each parent
$level = 1;
Expand All @@ -216,13 +98,9 @@ protected function getFrontendModule(PageModel $page, $module, string $column):
return implode('', $renderedArticles);
}

protected function getInheritedArticles($pid, string $column, int $level): array
private function getInheritedArticles($pid, string $column, int $level): array
{
// Get adapters
$controller = $this->framework->getAdapter(\Contao\Controller::class);
$articleModel = $this->framework->getAdapter(\Contao\ArticleModel::class);

$t = $articleModel->getTable();
$t = ArticleModel::getTable();
$columns = [
"$t.pid = ?",
"$t.inColumn = ?",
Expand All @@ -240,7 +118,7 @@ protected function getInheritedArticles($pid, string $column, int $level): array

$renderedArticles = [];

if (null !== ($articles = $articleModel->findBy($columns, $values, $options))) {
if (null !== ($articles = ArticleModel::findBy($columns, $values, $options))) {
foreach ($articles as $article) {
$published = $article->published;

Expand All @@ -252,33 +130,12 @@ protected function getInheritedArticles($pid, string $column, int $level): array
$renderedArticles[$article->inheritPriority] = '';
}

$renderedArticles[$article->inheritPriority] .= $controller->getArticle($article, false, false, $column);
$renderedArticles[$article->inheritPriority] .= Controller::getArticle($article, false, false, $column);

$article->published = $published;
}
}

return $renderedArticles;
}

/**
* Sets theme and layout related information in the page object,
* which is usually done by Contao after the getPageLayout hook.
*/
protected function applyThemeAndLayout(PageModel $page, LayoutModel $layout): void
{
/** @var \Contao\ThemeModel $theme */
$theme = $layout->getRelated('pid');
$this->pictureFactory->setDefaultDensities($theme->defaultImageDensities);
$page->layoutId = $layout->id;
$page->template = $layout->template ?: 'fe_page';
$page->templateGroup = $theme->templates;
$page->minifyMarkup = $theme->minifyMarkup;

if (null !== $layout->doctype) {
[$format, $variant] = explode('_', $layout->doctype);
$page->outputFormat = $format;
$page->outputVariant = $variant;
}
}
}
4 changes: 1 addition & 3 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
services:
InheritArticleBundle\EventListener\InheritArticleListener:
arguments:
- '@doctrine.dbal.default_connection'
- '@contao.framework'
- '@contao.image.picture_factory'
- '@database_connection'
public: true
2 changes: 0 additions & 2 deletions src/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@

use InheritArticleBundle\EventListener\InheritArticleListener;

$GLOBALS['TL_HOOKS']['getPageLayout'][] = [InheritArticleListener::class, 'onGetPageLayout'];
$GLOBALS['TL_HOOKS']['generatePage'][] = [InheritArticleListener::class, 'onGeneratePage'];
$GLOBALS['TL_HOOKS']['getArticles'][] = [InheritArticleListener::class, 'onGetArticles'];

0 comments on commit 43b4068

Please sign in to comment.