/
InheritArticleListener.php
232 lines (183 loc) · 7.48 KB
/
InheritArticleListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
declare(strict_types=1);
/*
* This file is part of the InheritArticle Bundle.
*
* (c) inspiredminds
*
* @license LGPL-3.0-or-later
*/
namespace InheritArticleBundle\EventListener;
use Contao\CoreBundle\Framework\FrameworkAwareInterface;
use Contao\CoreBundle\Framework\FrameworkAwareTrait;
use Contao\LayoutModel;
use Contao\PageModel;
use Contao\PageRegular;
use Doctrine\DBAL\Connection;
class InheritArticleListener implements FrameworkAwareInterface
{
use FrameworkAwareTrait;
protected $columns;
protected $sections;
protected $modules;
protected $db;
public function __construct(Connection $db)
{
$this->db = $db;
}
public function onGetPageLayout(PageModel $pageModel, LayoutModel $layoutModel, PageRegular $pageRegular): void
{
$this->framework->initialize();
$stringUtil = $this->framework->getAdapter(\Contao\StringUtil::class);
$moduleModel = $this->framework->getAdapter(\Contao\ModuleModel::class);
// Reset the cached data
$this->columns = [];
$this->sections = [];
$this->modules = serialize([]);
// 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 = [];
// 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;
}
protected function getFrontendModule(PageModel $page, $module, string $column): string
{
$generatedModule = $this->framework->getAdapter(\Contao\Controller::class)->getFrontendModule($module, $column);
if (\is_object($module) && (0 !== $module || '0' !== $module)) {
return $generatedModule;
}
// Initialize pid
$pid = $page->id;
// 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();
// If there are no parents anymore, break the loop
if (!$parent) {
break;
}
// Get the parent id
$pid = $parent['pid'];
// Store id
$parents[] = $pid;
} while ($pid);
// Initialize rendered article modules
$renderedArticles = [];
$renderedArticles[0] = $generatedModule;
// Go through each parent
$level = 1;
foreach ($parents as $pid) {
$inheritArticles = $this->getInheritedArticles($pid, $column, $level);
if (!empty($inheritArticles)) {
foreach ($inheritArticles as $priority => $article) {
$renderedArticles[$priority] = $article.($renderedArticles[$priority] ?? '');
}
}
// Increase level
++$level;
}
// Sort by key
krsort($renderedArticles);
// Return combined articles
return implode('', $renderedArticles);
}
protected function getInheritedArticles($pid, string $column, int $level): array
{
// Get adapters
$controller = $this->framework->getAdapter(\Contao\Controller::class);
$articleModel = $this->framework->getAdapter(\Contao\ArticleModel::class);
$pageModel = $this->framework->getAdapter(\Contao\PageModel::class);
$t = $articleModel->getTable();
$columns = [
"$t.pid = ?",
"$t.inColumn = ?",
"$t.inherit = '1'",
"($t.inheritLevel = '0' OR $t.inheritLevel >= ?)",
];
$values = [
$pid,
$column,
$level,
];
$options = ['order' => "$t.sorting"];
$renderedArticles = [];
if (null !== ($articles = $articleModel->findBy($columns, $values, $options))) {
// Override global page object
global $objPage;
$currentPage = $objPage;
$objPage = $pageModel->findById($pid);
foreach ($articles as $article) {
$published = $article->published;
if (!$article->published && $article->inheritUnpublished) {
$article->published = true;
}
if (!isset($renderedArticles[$article->inheritPriority])) {
$renderedArticles[$article->inheritPriority] = '';
}
$renderedArticles[$article->inheritPriority] .= $controller->getArticle($article->id, false, false, $column);
$article->published = $published;
}
// Reset global page object
$objPage = $currentPage;
}
return $renderedArticles;
}
}