-
-
Notifications
You must be signed in to change notification settings - Fork 351
/
SimplePrevNextViewHelper.php
248 lines (222 loc) · 8.6 KB
/
SimplePrevNextViewHelper.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
<?php
/*
* This file is part of the "news" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/
namespace GeorgRinger\News\ViewHelpers;
use GeorgRinger\News\Domain\Model\News;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
use TYPO3\CMS\Core\Context\LanguageAspect;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
/**
* ViewHelper for a **simple** prev/next link.
* Only the pid is taken into account, nothing else!
*
* # Example: Basic example
* <code>
*
* <n:simplePrevNext pidList="{newsItem.pid}" news="{newsItem}" as="paginated" sortField="datetime">
* <f:if condition="{paginated}">
* <ul class="prev-next">
* <f:if condition="{paginated.prev}">
* <li class="previous">
* <n:link newsItem="{paginated.prev}" settings="{settings}">
* {paginated.prev.title}
* </n:link>
* </li>
* </f:if>
* <f:if condition="{paginated.next}">
* <li class="next">
* <n:link newsItem="{paginated.next}" settings="{settings}" class="next">
* {paginated.next.title}
* </n:link>
* </li>
* </f:if>
* </ul>
* </f:if>
* </n:simplePrevNext>
*
* The attributes includeExternalType & includeInternalType allow to include internal and
* external news types.
*
* </code>
* <output>
* Menu with 2 li items with the link to the previous and next news item.
* </output>
*/
class SimplePrevNextViewHelper extends AbstractViewHelper
{
/* @var $dataMapper \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper */
protected $dataMapper;
/**
* @var bool
*/
protected $escapeOutput = false;
/**
* Inject the DataMapper
*
* @param \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper
*/
public function injectDataMapper(DataMapper $dataMapper): void
{
$this->dataMapper = $dataMapper;
}
/**
* Initialize arguments.
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('news', News::class, 'news item', true);
$this->registerArgument('pidList', 'string', 'pid list', false, '');
$this->registerArgument('sortField', 'string', 'sort field', false, 'datetime');
$this->registerArgument('as', 'string', 'as', true);
$this->registerArgument('includeInternalType', 'boolean', 'Include internal news types', false, false);
$this->registerArgument('includeExternalType', 'bool', 'Include external news types', false, false);
}
/**
* @return string
*/
public function render(): string
{
$neighbours = $this->getNeighbours($this->arguments['news'], $this->arguments['pidList'], $this->arguments['sortField']);
$as = $this->arguments['as'];
$mapped = $this->mapResultToObjects($neighbours);
$this->templateVariableContainer->add($as, $mapped);
$output = $this->renderChildren();
$this->templateVariableContainer->remove($as);
return $output;
}
/**
* Map the array from DB to an understandable output
*
* @param array $result
* @return array
*/
protected function mapResultToObjects(array $result): array
{
$out = [];
foreach ($result as $_id => $single) {
$out[$_id] = $this->getObject($single['uid']);
}
return $out;
}
/**
* Get the news object from the given id
*
* @param int $id
* @return mixed|null
* @throws AspectNotFoundException
*/
protected function getObject($id)
{
$record = null;
$rawRecord = $this->getRawRecord($id);
/** @var LanguageAspect $languageAspect */
$languageAspect = GeneralUtility::makeInstance(Context::class)->getAspect('language');
if (isset($GLOBALS['TSFE']) && is_object($GLOBALS['TSFE']) && $languageAspect->getContentId() > 0) {
// @extensionScannerIgnoreLine
$overlay = $GLOBALS['TSFE']->sys_page->getRecordOverlay(
'tx_news_domain_model_news',
$rawRecord,
$languageAspect->getContentId(),
$languageAspect->getLegacyOverlayType()
);
if (!is_null($overlay)) {
$rawRecord = $overlay;
}
}
if (is_array($rawRecord)) {
$records = $this->dataMapper->map(News::class, [$rawRecord]);
$record = array_shift($records);
}
return $record;
}
/**
* @param News $news
* @param string $pidList
* @param string $sortField
* @return array
*/
protected function getNeighbours(News $news, string $pidList, string $sortField): array
{
$data = [];
$pidList = empty($pidList) ? (string)$news->getPid() : $pidList;
$connection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_news_domain_model_news');
foreach (['prev', 'next'] as $label) {
$queryBuilder = $connection->createQueryBuilder();
$extraWhere = [
$queryBuilder->expr()->neq('uid', $queryBuilder->createNamedParameter($news->getUid(), Connection::PARAM_INT)),
];
if ((bool)($this->arguments['includeInternalType'] ?? false) === false) {
$extraWhere[] = $queryBuilder->expr()->neq('type', $queryBuilder->createNamedParameter(1, Connection::PARAM_INT));
}
if ((bool)($this->arguments['includeExternalType'] ?? false) === false) {
$extraWhere[] = $queryBuilder->expr()->neq('type', $queryBuilder->createNamedParameter(2, Connection::PARAM_INT));
}
$getter = 'get' . ucfirst($sortField) . '';
if ($news->$getter() instanceof \DateTime) {
if ($label === 'prev') {
$extraWhere[] = $queryBuilder->expr()->lt($sortField, $queryBuilder->createNamedParameter($news->$getter()->getTimestamp(), Connection::PARAM_INT));
} else {
$extraWhere[] = $queryBuilder->expr()->gt($sortField, $queryBuilder->createNamedParameter($news->$getter()->getTimestamp(), Connection::PARAM_INT));
}
} else {
if ($label === 'prev') {
$extraWhere[] = $queryBuilder->expr()->lt($sortField, $queryBuilder->createNamedParameter($news->$getter(), Connection::PARAM_STR));
} else {
$extraWhere[] = $queryBuilder->expr()->gt($sortField, $queryBuilder->createNamedParameter($news->$getter(), Connection::PARAM_STR));
}
}
$row = $queryBuilder
->select('*')
->from('tx_news_domain_model_news')
->where(
$queryBuilder->expr()->eq('sys_language_uid', $queryBuilder->createNamedParameter(0, Connection::PARAM_INT)),
$queryBuilder->expr()->in('pid', $queryBuilder->createNamedParameter(explode(',', $pidList), Connection::PARAM_INT_ARRAY))
)
->andWhere(...$extraWhere)
->setMaxResults(1)
->orderBy($sortField, $label === 'prev' ? 'desc' : 'asc')
->executeQuery()->fetchAssociative();
if (is_array($row)) {
$data[$label] = $row;
}
}
return $data;
}
/**
* @return QueryBuilder
*/
protected function getQueryBuilder(): QueryBuilder
{
return GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_news_domain_model_news');
}
/**
* @param int $id
* @return array
*/
protected function getRawRecord(int $id): ?array
{
$queryBuilder = $this->getQueryBuilder();
$rawRecord = $queryBuilder
->select('*')
->from('tx_news_domain_model_news')
->where(
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($id, Connection::PARAM_INT))
)
->setMaxResults(1)
->executeQuery()->fetchAssociative();
return $rawRecord;
}
}