Newer
Older
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
namespace SGalinski\SgNews\Service;
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (https://www.sgalinski.de)
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\SgNews\Domain\Repository\TagRepository;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Database\DatabaseConnection;
use TYPO3\CMS\Core\Database\QueryGenerator;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
* Class Utility
*/
class BackendNewsUtility {
/**
* @var int The category page doktype
*/
const CATEGORY_DOKTYPE = 117;
/**
* @var int The news page doktype
*/
const NEWS_DOKTYPE = 116;
/**
* Retrieves the next site root in the page hierarchy from the current page
*
* @param int $currentPid
* @return int
*/
public static function getRootUidByPageUid($currentPid) {
$rootLine = BackendUtility::BEgetRootLine((int) $currentPid);
$siteRoot = ['uid' => 0];
foreach ($rootLine as $page) {
if ((int) $page['is_siteroot'] === 1) {
$siteRoot = $page;
break;
}
}
return (int) $siteRoot['uid'];
}
/**
* Get an array of alternative pages for the BE Module view
*
* @return array
* @throws \InvalidArgumentException
*/
public static function getAlternativePageOptions() {
$options = [];
/** @var array $rootOptionRows */
$rootOptionRows = BackendUtility::getRecordsByField('pages', 'is_siteroot', 1, '', '', 'sorting');
if ($rootOptionRows) {
foreach ($rootOptionRows as $row) {
$pageInfo = BackendUtility::readPageAccess($row['uid'], $GLOBALS['BE_USER']->getPagePermsClause(1));
if ($pageInfo) {
$options[] = self::getOptionPageInfo($pageInfo);
}
$categories = self::getCategoriesForSiteRoot((int) $row['uid']);
/** @var int $categoryUid */
foreach ($categories as $categoryUid => $categoryTitle) {
if ((int) $pageInfo['uid'] !== $categoryUid) {
$categoryPageInfo = BackendUtility::readPageAccess(
$categoryUid, $GLOBALS['BE_USER']->getPagePermsClause(1)
);
if ($categoryPageInfo) {
$options[] = self::getOptionPageInfo($categoryPageInfo);
}
}
}
}
}
return $options;
}
/**
* Get an array of alternative pages for the BE Module view
*
* @param array $pageInfo
* @return array
*/
private static function getOptionPageInfo(array $pageInfo = []) {
if (isset($pageInfo['uid']) && (int) $pageInfo['uid']) {
$rootline = BackendUtility::BEgetRootLine($pageInfo['uid'], '', TRUE);
ksort($rootline);
$path = '/root';
foreach ($rootline as $page) {
$path .= '/p' . dechex($page['uid']);
}
$pageInfo['path'] = $path;
$pageInfo['_thePathFull'] = substr($pageInfo['_thePathFull'], 1);
$pageInfo['_thePathFull'] = substr($pageInfo['_thePathFull'], 0, -1);
}
return $pageInfo;
}
/**
* Get an array of all category uids => titles below the given site root id
*
* @param int $siteRootUid
* @return array
* @throws \InvalidArgumentException
*/
public static function getCategoriesForSiteRoot($siteRootUid) {
$siteRootUid = (int) $siteRootUid;
// get all pageids below the given siteroot
$queryGenerator = GeneralUtility::makeInstance(QueryGenerator::class);
$childPids = $queryGenerator->getTreeList(
$siteRootUid, PHP_INT_MAX, 0, $GLOBALS['BE_USER']->getPagePermsClause(1)
);
// if doktype = 117 (category) then get the category name (page title)
/** @var DatabaseConnection $databaseConnection */
$databaseConnection = $GLOBALS['TYPO3_DB'];
$where = 'deleted = 0 AND doktype = ' . self::CATEGORY_DOKTYPE . ' AND uid in (' . $childPids . ')';
$result = $databaseConnection->exec_SELECTgetRows('uid, title', 'pages', $where);
$categories = [];
/** @var array $result */
foreach ($result as $page) {
$categoryPageInfo = BackendUtility::readPageAccess(
(int) $page['uid'], $GLOBALS['BE_USER']->getPagePermsClause(1)
);
if ($categoryPageInfo) {
$categories[(int) $page['uid']] = $page['title'];
}
}
return $categories;
}
/**
* Get an array of all tags uids => titles below the given site root id
*
* @param int $pageUid
* @param int $languageUid
* @return array
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
* @throws \InvalidArgumentException
*/
public static function getTagsForPage($pageUid, $languageUid = 0) {
$temporaryTSFEInstance = FALSE;
if (!isset($GLOBALS['TSFE'])) {
$temporaryTSFEInstance = TRUE;
$GLOBALS['TSFE'] = new \stdClass();
$GLOBALS['TSFE']->gr_list = '';
}
$pageUid = (int) $pageUid;
$languageUid = (int) $languageUid;
$tags = [];
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$tagRepository = $objectManager->get(TagRepository::class);
$query = $tagRepository->createQuery();
$querySettings = $query->getQuerySettings();
$querySettings->setLanguageUid($languageUid);
$querySettings->setLanguageOverlayMode(TRUE);
$querySettings->setLanguageMode('content_fallback');
$query->setQuerySettings($querySettings);
if ($pageUid) {
$rootline = BackendUtility::BEgetRootLine($pageUid, '', TRUE);
$pageTS = BackendUtility::getPagesTSconfig($pageUid, $rootline);
$tagsPid = 0;
if (isset($pageTS['TCEFORM.']['pages.']['tx_sgnews_tags.']['PAGE_TSCONFIG_ID'])) {
$tagsPid = (int) $pageTS['TCEFORM.']['pages.']['tx_sgnews_tags.']['PAGE_TSCONFIG_ID'];
}
if ($tagsPid) {
$query->matching($query->equals('pid', $tagsPid));
}
}
$query->setOrderings(['title' => QueryInterface::ORDER_ASCENDING]);
$resultTags = $query->execute(TRUE);
if ($temporaryTSFEInstance) {
unset($GLOBALS['TSFE']);
}
foreach ($resultTags as $tag) {
$tags[(int) $tag['uid']] = trim($tag['title']);
}
return $tags;
}
/**
* Get all news for the given categories (all if filter is empty)
*
* @param int $rootPageUid
* @param array $filters
* @param int $languageUid
* @return array
* @throws \InvalidArgumentException
*/
public static function getNewsByFilters($rootPageUid = 0, array $filters = [], $languageUid = 0) {
$out = [];
$rootPageUid = (int) $rootPageUid;
$languageUid = (int) $languageUid;
if (!$rootPageUid) {
return $out;
}
$categories = [];
if (!isset($filters['categories']) || !is_array($filters['categories']) || !count($filters['categories'])) {
$rootCategories = self::getCategoriesForSiteRoot($rootPageUid);
foreach ($rootCategories as $categoryUid => $categoryTitle) {
$categories[] = (int) $categoryUid;
}
} else {
foreach ($filters['categories'] as $categoryUid) {
$categoryPageInfo = BackendUtility::readPageAccess(
(int) $categoryUid, $GLOBALS['BE_USER']->getPagePermsClause(1)
);
if ($categoryPageInfo) {
$categories[] = (int) $categoryUid;
}
}
}
if (!count($categories)) {
return $out;
}
$queryGenerator = GeneralUtility::makeInstance(QueryGenerator::class);
$allowedUids = [];
foreach ($categories as $categoryUid) {
$allowedUidsTemp = GeneralUtility::intExplode(
',', $queryGenerator->getTreeList(
$categoryUid, 1, 0, $GLOBALS['BE_USER']->getPagePermsClause(1)
), TRUE
);
$allowedUids = array_unique(array_merge($allowedUids, $allowedUidsTemp));
}
if (!count($allowedUids)) {
return $out;
}
list($select, $tables, $where) = self::getNewsQueryParts($allowedUids, $filters, $languageUid);
if ($tables === '') {
return $out;
}
/** @var DatabaseConnection $databaseConnection */
$databaseConnection = $GLOBALS['TYPO3_DB'];
$result = $databaseConnection->exec_SELECTquery($select, $tables, $where, '`pages`.`uid`', '`pages`.`sorting`');
while ($row = $result->fetch_assoc()) {
$out[] = $row;
}
return $out;
}
/**
* Creates news query parts
*
* @param array $allowedUids
* @param array $filters
* @param int $languageUid
* @return array
*/
private static function getNewsQueryParts(array $allowedUids, array $filters = [], $languageUid = 0) {
$out = [0 => '
`pages`.`uid` AS uid,
`pages`.`pid` AS pid,
`pages`.`hidden` AS hidden,
`pages`.`sorting` AS sorting,
`pages`.`doktype` AS doktype,
`pages`.`title` AS title
', 1 => '', 2 => ''];
$out[1] = '`pages`';
$out[2] = '`pages`.`uid` IN(' . implode(',', $allowedUids) . ') ' . BackendUtility::deleteClause('pages') .
' AND `pages`.`doktype` = ' . self::NEWS_DOKTYPE;
if ($languageUid) {
$out[0] .= ', `translation`.`title` AS translation_title';
$out[1] .= ' LEFT JOIN `pages_language_overlay` AS `translation` ON `translation`.`pid` = `pages`.`uid` ' .
BackendUtility::deleteClause('pages_language_overlay', 'translation') .
' AND `translation`.`sys_language_uid` = ' . $languageUid;
}
if (isset($filters['tags']) && is_array($filters['tags']) && count($filters['tags'])) {
$tagUids = [];
foreach ($filters['tags'] as $tagUid) {
if ((int) $tagUid && !in_array((int) $tagUid, $tagUids, TRUE)) {
$tagUids[] = (int) $tagUid;
}
}
if (count($tagUids)) {
$out[1] .= ' INNER JOIN `sys_category_record_mm` AS `tag` ON `tag`.`tablenames` = \'pages\'' .
' AND `tag`.`fieldname` = \'tx_sgnews_tags\' AND `tag`.`uid_foreign` = `pages`.`uid`' .
' AND `tag`.`uid_local` IN (' . implode(',', $filters['tags']) . ')';
}
}
if (isset($filters['search']) && trim($filters['search'])) {
$out[2] .= self::getNewsSearchClause(trim($filters['search']), $languageUid);
}
return $out;
}
/**
* Creates constraints of query for searching news by search-word
*
* @param string $searchString
* @param int $languageUid
* @return string
*/
private static function getNewsSearchClause($searchString = '', $languageUid = 0) {
$out = '';
$searchString = strtolower(trim($searchString));
$languageUid = (int) $languageUid;
if (!$searchString) {
return $out;
}
$out = ' AND (';
/** @var DatabaseConnection $databaseConnection */
$databaseConnection = $GLOBALS['TYPO3_DB'];
$likeString = 'LIKE \'%' . $databaseConnection->escapeStrForLike($searchString, 'pages') . '%\'';
$constraints = [];
$pageFields = [
'title' => TRUE,
'description' => TRUE,
'author' => TRUE,
'abstract' => TRUE,
];
foreach ($pageFields as $fieldName => $isCommonField) {
if ($isCommonField) {
$constraints[] = 'LOWER(`pages`.`' . $fieldName . '`) ' . $likeString;
}
}
if (!$languageUid) {
foreach ($pageFields as $fieldName => $isCommonField) {
if (!$isCommonField) {
$constraints[] = 'LOWER(`pages`.`' . $fieldName . '`) ' . $likeString;
}
}
} else {
foreach ($pageFields as $fieldName => $isCommonField) {
if ($isCommonField) {
$constraints[] = 'LOWER(`translation`.`' . $fieldName . '`) ' . $likeString;
}
}
}
$out .= implode(' OR ', $constraints) . ')';
return $out;
}
/**
* Returns the available languages for the current BE user
*
* @param int $pageUid
* @return array
* @throws \InvalidArgumentException
*/
public static function getAvailableLanguages($pageUid = 0) {
$pageUid = (int) $pageUid;
$rootline = BackendUtility::BEgetRootLine($pageUid, '', TRUE);
$defaultLanguage = LocalizationUtility::translate('backend.language.default', 'SgNews');
$pageTS = BackendUtility::getPagesTSconfig($pageUid, $rootline);
if (isset($pageTS['mod.']['SHARED.']['defaultLanguageLabel'])) {
$defaultLanguage = $pageTS['mod.']['SHARED.']['defaultLanguageLabel'] . ' (' . $defaultLanguage . ')';
}
$defaultLanguageFlag = 'empty-empty';
if (isset($pageTS['mod.']['SHARED.']['defaultLanguageFlag'])) {
$defaultLanguageFlag = 'flags-' . $pageTS['mod.']['SHARED.']['defaultLanguageFlag'];
}
$languages = [
0 => ['title' => $defaultLanguage, 'flag' => $defaultLanguageFlag]
];
/** @var DatabaseConnection $databaseConnection */
$databaseConnection = $GLOBALS['TYPO3_DB'];
$languageRows = $databaseConnection->exec_SELECTgetRows(
'uid, title, flag', 'sys_language', 'hidden = 0', '', 'sorting'
);
if ($languageRows) {
/** @var BackendUserAuthentication $backendUser */
$backendUser = $GLOBALS['BE_USER'];
foreach ($languageRows as $languageRow) {
if ($backendUser->checkLanguageAccess($languageRow['uid'])) {
$languages[(int) $languageRow['uid']] = [
'title' => $languageRow['title'],
'flag' => 'flags-' . $languageRow['flag'],
];
}
}
}
return $languages;
}
}