/
ModuleNewsList.php
255 lines (214 loc) · 6.1 KB
/
ModuleNewsList.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao;
use Contao\CoreBundle\Exception\PageNotFoundException;
use Contao\Model\Collection;
/**
* Front end module "news list".
*
* @property array $news_archives
* @property string $news_featured
* @property string $news_order
*/
class ModuleNewsList extends ModuleNews
{
/**
* Template
* @var string
*/
protected $strTemplate = 'mod_newslist';
/**
* Display a wildcard in the back end
*
* @return string
*/
public function generate()
{
$request = System::getContainer()->get('request_stack')->getCurrentRequest();
if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request))
{
$objTemplate = new BackendTemplate('be_wildcard');
$objTemplate->wildcard = '### ' . $GLOBALS['TL_LANG']['FMD']['newslist'][0] . ' ###';
$objTemplate->title = $this->headline;
$objTemplate->id = $this->id;
$objTemplate->link = $this->name;
$objTemplate->href = StringUtil::specialcharsUrl(System::getContainer()->get('router')->generate('contao_backend', array('do'=>'themes', 'table'=>'tl_module', 'act'=>'edit', 'id'=>$this->id)));
return $objTemplate->parse();
}
$this->news_archives = $this->sortOutProtected(StringUtil::deserialize($this->news_archives));
// Return if there are no archives
if (empty($this->news_archives) || !\is_array($this->news_archives))
{
return '';
}
// Show the newsreader if an item has been selected
if ($this->news_readerModule > 0 && Input::get('auto_item') !== null)
{
return $this->getFrontendModule($this->news_readerModule, $this->strColumn);
}
// Tag the news archives (see #2137)
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
{
$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
$responseTagger->addTags(array_map(static function ($id) { return 'contao.db.tl_news_archive.' . $id; }, $this->news_archives));
}
return parent::generate();
}
/**
* Generate the module
*/
protected function compile()
{
$limit = null;
$offset = $this->skipFirst;
// Maximum number of items
if ($this->numberOfItems > 0)
{
$limit = $this->numberOfItems;
}
// Handle featured news
if ($this->news_featured == 'featured')
{
$blnFeatured = true;
}
elseif ($this->news_featured == 'unfeatured')
{
$blnFeatured = false;
}
else
{
$blnFeatured = null;
}
$this->Template->articles = array();
$this->Template->empty = $GLOBALS['TL_LANG']['MSC']['emptyList'];
// Get the total number of items
$intTotal = $this->countItems($this->news_archives, $blnFeatured);
if ($intTotal < 1)
{
return;
}
$total = $intTotal - $offset;
// Split the results
if ($this->perPage > 0 && (!isset($limit) || $this->numberOfItems > $this->perPage))
{
// Adjust the overall limit
if (isset($limit))
{
$total = min($limit, $total);
}
// Get the current page
$id = 'page_n' . $this->id;
$page = (int) (Input::get($id) ?? 1);
// Do not index or cache the page if the page number is outside the range
if ($page < 1 || $page > max(ceil($total/$this->perPage), 1))
{
throw new PageNotFoundException('Page not found: ' . Environment::get('uri'));
}
// Set limit and offset
$limit = $this->perPage;
$offset += (max($page, 1) - 1) * $this->perPage;
$skip = $this->skipFirst;
// Overall limit
if ($offset + $limit > $total + $skip)
{
$limit = $total + $skip - $offset;
}
// Add the pagination menu
$objPagination = new Pagination($total, $this->perPage, Config::get('maxPaginationLinks'), $id);
$this->Template->pagination = $objPagination->generate("\n ");
}
$objArticles = $this->fetchItems($this->news_archives, $blnFeatured, $limit ?: 0, $offset);
// Add the articles
if ($objArticles !== null)
{
$this->Template->articles = $this->parseArticles($objArticles);
}
$this->Template->archives = $this->news_archives;
}
/**
* Count the total matching items
*
* @param array $newsArchives
* @param boolean $blnFeatured
*
* @return integer
*/
protected function countItems($newsArchives, $blnFeatured)
{
// HOOK: add custom logic
if (isset($GLOBALS['TL_HOOKS']['newsListCountItems']) && \is_array($GLOBALS['TL_HOOKS']['newsListCountItems']))
{
foreach ($GLOBALS['TL_HOOKS']['newsListCountItems'] as $callback)
{
if (($intResult = System::importStatic($callback[0])->{$callback[1]}($newsArchives, $blnFeatured, $this)) === false)
{
continue;
}
if (\is_int($intResult))
{
return $intResult;
}
}
}
return NewsModel::countPublishedByPids($newsArchives, $blnFeatured);
}
/**
* Fetch the matching items
*
* @param array $newsArchives
* @param boolean $blnFeatured
* @param integer $limit
* @param integer $offset
*
* @return Collection|NewsModel|null
*/
protected function fetchItems($newsArchives, $blnFeatured, $limit, $offset)
{
// HOOK: add custom logic
if (isset($GLOBALS['TL_HOOKS']['newsListFetchItems']) && \is_array($GLOBALS['TL_HOOKS']['newsListFetchItems']))
{
foreach ($GLOBALS['TL_HOOKS']['newsListFetchItems'] as $callback)
{
if (($objCollection = System::importStatic($callback[0])->{$callback[1]}($newsArchives, $blnFeatured, $limit, $offset, $this)) === false)
{
continue;
}
if ($objCollection === null || $objCollection instanceof Collection)
{
return $objCollection;
}
}
}
// Determine sorting
$t = NewsModel::getTable();
$order = '';
if ($this->news_featured == 'featured_first')
{
$order .= "$t.featured DESC, ";
}
switch ($this->news_order)
{
case 'order_headline_asc':
$order .= "$t.headline";
break;
case 'order_headline_desc':
$order .= "$t.headline DESC";
break;
case 'order_random':
$order .= "RAND()";
break;
case 'order_date_asc':
$order .= "$t.date";
break;
default:
$order .= "$t.date DESC";
}
return NewsModel::findPublishedByPids($newsArchives, $blnFeatured, $limit, $offset, array('order'=>$order));
}
}