-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathDataHandlerSlugUpdateHook.php
197 lines (177 loc) · 7.57 KB
/
DataHandlerSlugUpdateHook.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
<?php
declare(strict_types=1);
namespace Wazum\Sluggi\Backend\Hook;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\DataHandling\SlugHelper;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use Wazum\Sluggi\Backend\Service\SlugService;
use Wazum\Sluggi\Helper\Configuration;
use Wazum\Sluggi\Helper\PermissionHelper;
use Wazum\Sluggi\Helper\SlugHelper as SluggiSlugHelper;
use function array_merge;
use function in_array;
use function str_replace;
use function strpos;
use function substr;
/**
* Class DataHandlerSlugUpdateHook
*
* @author Wolfgang Klinger <[email protected]>
*/
class DataHandlerSlugUpdateHook
{
/**
* @var SlugService
*/
protected $slugService;
/**
* @var string[]
*/
protected $slugSetIncoming;
public function __construct(SlugService $slugService)
{
$this->slugService = $slugService;
}
/**
* @param string|int $id (id could be string, for this reason no type hint)
*/
public function processDatamap_preProcessFieldArray(
array &$incomingFieldArray,
string $table,
$id,
DataHandler $dataHandler
): void {
if (
'pages' === $table && !MathUtility::canBeInterpretedAsInteger($id)){
//new page was created
$synchronizeOn = (bool) Configuration::get('synchronize_on');
if ($synchronizeOn){
$incomingFieldArray['tx_sluggi_sync'] = 1;
}
return;
}
if (
'pages' !== $table
// This is set in \TYPO3\CMS\Backend\History\RecordHistoryRollback::performRollback,
// so we use it as a flag to ignore the update
|| $dataHandler->dontProcessTransformations
|| !MathUtility::canBeInterpretedAsInteger($id)
|| !$dataHandler->checkRecordUpdateAccess($table, $id, $incomingFieldArray)
|| $this->isNestedHookInvocation($dataHandler)
) {
return;
}
if (!empty($incomingFieldArray['slug'])) {
$this->slugSetIncoming[(int) $id] = true;
}
$record = BackendUtility::getRecordWSOL($table, (int) $id);
$locked = (bool) $record['tx_sluggi_lock'];
if (isset($incomingFieldArray['tx_sluggi_lock'])) {
$locked = (bool) $incomingFieldArray['tx_sluggi_lock'];
}
$synchronize = (bool)Configuration::get('synchronize');
$allowOnlyLastSegment = (bool)Configuration::get('last_segment_only');
if (isset($incomingFieldArray['tx_sluggi_sync']) && (bool)$incomingFieldArray['tx_sluggi_sync'] === false) {
$synchronize = false;
}
if (!$locked && $synchronize) {
$data = array_merge($record, $incomingFieldArray);
if ($data['tx_sluggi_sync']) {
$fieldConfig = $GLOBALS['TCA']['pages']['columns']['slug']['config'] ?? [];
/** @var SlugHelper $helper */
$helper = GeneralUtility::makeInstance(SlugHelper::class, 'pages', 'slug', $fieldConfig);
$incomingFieldArray['slug'] = $helper->generate($data, (int)$data['pid']);
}
} elseif (isset($incomingFieldArray['slug']) && $allowOnlyLastSegment && !PermissionHelper::hasFullPermission()) {
$languageId = $record['sys_language_uid'];
$inaccessibleSlugSegments = $this->getInaccessibleSlugSegments($id, $languageId);
// Prepend the parent page slug
$parentSlug = SluggiSlugHelper::getSlug($record['pid'], $languageId);
if (false !== strpos(substr($incomingFieldArray['slug'], 1), '/')) {
$this->setFlashMessage(
LocalizationUtility::translate('message.slashesNotAllowed', 'sluggi'),
FlashMessage::WARNING
);
}
$incomingFieldArray['slug'] = $inaccessibleSlugSegments .
str_replace($inaccessibleSlugSegments, '', $parentSlug) .
'/' . str_replace('/', '-', substr($incomingFieldArray['slug'], 1));
}
}
/**
* @param int|string $id
*/
public function processDatamap_postProcessFieldArray(
string $status,
string $table,
$id,
array $incomingFieldArray,
DataHandler $dataHandler
): void {
if (
$table !== 'pages'
|| $status !== 'update'
// This is set in \TYPO3\CMS\Backend\History\RecordHistoryRollback::performRollback,
// so we use it as a flag to ignore the update
|| $dataHandler->dontProcessTransformations
|| $this->isNestedHookInvocation($dataHandler)
) {
return;
}
// We have to double check again here,
// as the slug is empty e.g. if the title is changed via inline editing in page tree
if (!isset($this->slugSetIncoming[(int)$id])) {
$synchronize = (bool)Configuration::get('synchronize');
if (isset($incomingFieldArray['tx_sluggi_sync']) && (bool)$incomingFieldArray['tx_sluggi_sync'] === false) {
$synchronize = false;
}
if ($synchronize) {
$record = BackendUtility::getRecordWSOL($table, (int)$id);
$data = array_merge($record, $incomingFieldArray);
if ($data['tx_sluggi_sync']) {
$fieldConfig = $GLOBALS['TCA']['pages']['columns']['slug']['config'] ?? [];
/** @var SlugHelper $helper */
$helper = GeneralUtility::makeInstance(SlugHelper::class, 'pages', 'slug', $fieldConfig);
$incomingFieldArray['slug'] = $helper->generate($data, (int)$data['pid']);
}
if (!empty($incomingFieldArray['slug']) && $incomingFieldArray['slug'] !== $record['slug']) {
$this->slugService->rebuildSlugsForSlugChange(
$id,
(string) $record['slug'],
$incomingFieldArray['slug'],
$dataHandler->getCorrelationId()
);
}
}
}
}
protected function getInaccessibleSlugSegments(int $pageId, int $languageId): string
{
$mountRootPage = PermissionHelper::getTopmostAccessiblePage($pageId);
return SluggiSlugHelper::getSlug($mountRootPage['pid'], $languageId);
}
protected function setFlashMessage(string $text, int $severity): void
{
/** @var FlashMessage $message */
$message = GeneralUtility::makeInstance(FlashMessage::class, $text, '', $severity);
/** @var FlashMessageService $flashMessageService */
$flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
$messageQueue = $flashMessageService->getMessageQueueByIdentifier();
$messageQueue->addMessage($message);
}
/**
* Determines whether our identifier is part of correlation id aspects.
* In that case it would be a nested call which has to be ignored.
*/
protected function isNestedHookInvocation(DataHandler $dataHandler): bool
{
$correlationId = $dataHandler->getCorrelationId();
$correlationIdAspects = $correlationId ? $correlationId->getAspects() ?? [] : [];
return in_array(\TYPO3\CMS\Redirects\Service\SlugService::CORRELATION_ID_IDENTIFIER, $correlationIdAspects, true);
}
}