/
GridColumnItem.php
369 lines (326 loc) · 14 KB
/
GridColumnItem.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
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
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Backend\View\BackendLayout\Grid;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Backend\Preview\StandardPreviewRendererResolver;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Backend\View\Event\PageContentPreviewRenderingEvent;
use TYPO3\CMS\Backend\View\PageLayoutContext;
use TYPO3\CMS\Core\Database\ReferenceIndex;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\Type\Bitmask\Permission;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Grid Column Item
*
* Model/proxy around a single record which appears in a grid column
* in the page layout. Returns titles, urls etc. and performs basic
* assertions on the contained content element record such as
* is-versioned, is-editable, is-delible and so on.
*
* Accessed from Fluid templates.
*
* @internal this is experimental and subject to change in TYPO3 v10 / v11
*/
class GridColumnItem extends AbstractGridObject
{
/**
* @var GridColumnItem[]
*/
protected array $translations = [];
public function __construct(
PageLayoutContext $context,
protected GridColumn $column,
protected array $record,
protected string $table = 'tt_content'
) {
parent::__construct($context);
}
public function isVersioned(): bool
{
return ($this->record['_ORIG_uid'] ?? 0) > 0 || (int)($this->record['t3ver_state'] ?? 0) !== 0;
}
public function getPreview(): string
{
$previewRenderer = GeneralUtility::makeInstance(StandardPreviewRendererResolver::class)
->resolveRendererFor(
$this->table,
$this->record,
$this->context->getPageId()
);
$previewHeader = $previewRenderer->renderPageModulePreviewHeader($this);
// Dispatch event to allow listeners adding an alternative content type
// specific preview or to manipulate the content elements' record data.
$event = GeneralUtility::makeInstance(EventDispatcherInterface::class)->dispatch(
new PageContentPreviewRenderingEvent($this->table, $this->record, $this->context)
);
// Update the modified record data
$this->record = $event->getRecord();
// Get specific preview from listeners. In case non was added,
// fall back to the standard preview rendering workflow.
$previewContent = $event->getPreviewContent();
if ($previewContent === null) {
$previewContent = $previewRenderer->renderPageModulePreviewContent($this);
}
return $previewRenderer->wrapPageModulePreview($previewHeader, $previewContent, $this);
}
public function getWrapperClassName(): string
{
$wrapperClassNames = [];
if ($this->isDisabled()) {
$wrapperClassNames[] = 't3-page-ce-hidden t3js-hidden-record';
}
if ($this->isInconsistentLanguage()) {
$wrapperClassNames[] = 't3-page-ce-warning';
}
return implode(' ', $wrapperClassNames);
}
public function isDelible(): bool
{
$backendUser = $this->getBackendUser();
if (!$backendUser->doesUserHaveAccess($this->context->getPageRecord(), Permission::CONTENT_EDIT)) {
return false;
}
return !($backendUser->getTSConfig()['options.']['disableDelete.'][$this->table] ?? $backendUser->getTSConfig()['options.']['disableDelete'] ?? false);
}
public function getDeleteUrl(): string
{
return (string)GeneralUtility::makeInstance(UriBuilder::class)->buildUriFromRoute(
'tce_db',
[
'cmd' => [
$this->table => [
$this->record['uid'] => [
'delete' => 1,
],
],
],
'redirect' => $GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getRequestUri(),
]
);
}
public function getDeleteMessage(): string
{
$recordInfo = GeneralUtility::fixed_lgd_cs(BackendUtility::getRecordTitle($this->table, $this->record), (int)$this->getBackendUser()->uc['titleLen']);
if ($this->getBackendUser()->shallDisplayDebugInformation()) {
$recordInfo .= ' [' . $this->table . ':' . $this->record['uid'] . ']';
}
$refCountMsg = BackendUtility::referenceCount(
$this->table,
$this->record['uid'],
LF . $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.referencesToRecord'),
(string)$this->getReferenceCount($this->record['uid'])
) . BackendUtility::translationCount(
$this->table,
$this->record['uid'],
LF . $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.translationsOfRecord')
);
return sprintf($this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:deleteWarning'), trim($recordInfo)) . $refCountMsg;
}
public function getFooterInfo(): string
{
$record = $this->getRecord();
$previewRenderer = GeneralUtility::makeInstance(StandardPreviewRendererResolver::class)
->resolveRendererFor(
$this->table,
$record,
$this->context->getPageId()
);
return $previewRenderer->renderPageModulePreviewFooter($this);
}
public function getContentTypeLabel(): string
{
if (($typeColumn = $this->getTypeColumn()) === '') {
return '';
}
$contentType = $this->record[$typeColumn] ?? '';
$contentTypeLabels = $this->context->getContentTypeLabels();
return $contentTypeLabels[$contentType] ??
BackendUtility::getLabelFromItemListMerged((int)($this->record['pid'] ?? 0), $this->table, $typeColumn, $contentType, $this->record);
}
public function getIcons(): string
{
$row = $this->record;
$icons = [];
$icon = $this->iconFactory
->getIconForRecord($this->table, $row, Icon::SIZE_SMALL)
->setTitle(BackendUtility::getRecordIconAltText($row, $this->table))
->render();
if ($this->getBackendUser()->recordEditAccessInternals($this->table, $row)) {
$icon = BackendUtility::wrapClickMenuOnIcon($icon, $this->table, $row['uid']);
}
$icons[] = $icon;
if ($lockInfo = BackendUtility::isRecordLocked($this->table, $row['uid'])) {
$icons[] = '<a href="#" title="' . htmlspecialchars($lockInfo['msg']) . '">'
. $this->iconFactory->getIcon('status-user-backend', Icon::SIZE_SMALL, 'overlay-edit')->render() . '</a>';
}
return implode(' ', $icons);
}
public function getSiteLanguage(): SiteLanguage
{
return $this->context->getSiteLanguage((int)($this->record[$GLOBALS['TCA'][$this->table]['ctrl']['languageField'] ?? null] ?? 0));
}
public function getRecord(): array
{
return $this->record;
}
public function setRecord(array $record): void
{
$this->record = $record;
}
public function getColumn(): GridColumn
{
return $this->column;
}
public function getTranslations(): array
{
return $this->translations;
}
public function addTranslation(int $languageId, GridColumnItem $translation): GridColumnItem
{
$this->translations[$languageId] = $translation;
return $this;
}
public function isDisabled(): bool
{
$row = $this->getRecord();
$enableCols = $GLOBALS['TCA'][$this->table]['ctrl']['enablecolumns'] ?? null;
return is_array($enableCols)
&& (
(($enableCols['disabled'] ?? false) && $row[$enableCols['disabled']])
|| (($enableCols['starttime'] ?? false) && ($row[$enableCols['starttime']] ?? 0) > $GLOBALS['EXEC_TIME'])
|| (($enableCols['endtime'] ?? false) && ($row[$enableCols['endtime']] ?? false) && $row[$enableCols['endtime']] < $GLOBALS['EXEC_TIME'])
);
}
public function isEditable(): bool
{
$backendUser = $this->getBackendUser();
if ($backendUser->isAdmin()) {
return true;
}
$pageRecord = $this->context->getPageRecord();
return !($pageRecord['editlock'] ?? false)
&& $backendUser->doesUserHaveAccess($pageRecord, Permission::CONTENT_EDIT)
&& $backendUser->recordEditAccessInternals($this->table, $this->record);
}
public function isDragAndDropAllowed(): bool
{
$pageRecord = $this->context->getPageRecord();
$typeColumn = $this->getTypeColumn();
return (int)($this->record[$GLOBALS['TCA'][$this->table]['ctrl']['transOrigPointerField'] ?? null] ?? 0) === 0
&& (
$this->getBackendUser()->isAdmin()
|| (
((int)($this->record['editlock'] ?? 0) === 0 && (int)($pageRecord['editlock'] ?? 0) === 0)
&& $this->getBackendUser()->doesUserHaveAccess($pageRecord, Permission::CONTENT_EDIT)
&& $this->getBackendUser()->checkAuthMode($this->table, $typeColumn, $this->record[$typeColumn])
)
)
;
}
public function isInconsistentLanguage(): bool
{
$allowInconsistentLanguageHandling = (bool)(BackendUtility::getPagesTSconfig($this->getContext()->getPageId())['mod.']['web_layout.']['allowInconsistentLanguageHandling'] ?? false);
return !$allowInconsistentLanguageHandling
&& $this->getSiteLanguage()->getLanguageId() !== 0
&& $this->getContext()->getLanguageModeIdentifier() === 'mixed'
&& (int)($this->record[$GLOBALS['TCA'][$this->table]['ctrl']['transOrigPointerField'] ?? null] ?? 0) === 0;
}
public function getNewContentAfterUrl(): string
{
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$pageId = $this->context->getPageId();
return (string)$uriBuilder->buildUriFromRoute('new_content_element_wizard', [
'id' => $pageId,
'sys_language_uid' => $this->context->getSiteLanguage()->getLanguageId(),
'colPos' => $this->column->getColumnNumber(),
'uid_pid' => -$this->record['uid'],
'returnUrl' => $GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getRequestUri(),
]);
}
public function getVisibilityToggleUrl(): string
{
$hiddenField = $GLOBALS['TCA'][$this->table]['ctrl']['enablecolumns']['disabled'] ?? null;
if ($this->record[$hiddenField] ?? false) {
$value = 0;
} else {
$value = 1;
}
return GeneralUtility::makeInstance(UriBuilder::class)->buildUriFromRoute(
'tce_db',
[
'data' => [
$this->table => [
(($this->record['_ORIG_uid'] ?? false) ?: ($this->record['uid'] ?? 0)) => [
$hiddenField => $value,
],
],
],
'redirect' => $GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getRequestUri(),
]
) . '#element-' . $this->table . '-' . $this->record['uid'];
}
public function getVisibilityToggleTitle(): string
{
if ($this->record[$GLOBALS['TCA'][$this->table]['ctrl']['enablecolumns']['disabled'] ?? null] ?? false) {
return $this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:unHide');
}
return $this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:hide');
}
public function getVisibilityToggleIconName(): string
{
return ($this->record[$GLOBALS['TCA'][$this->table]['ctrl']['enablecolumns']['disabled'] ?? null] ?? false) ? 'unhide' : 'hide';
}
public function isVisibilityToggling(): bool
{
$hiddenField = $GLOBALS['TCA'][$this->table]['ctrl']['enablecolumns']['disabled'] ?? null;
return $hiddenField
&& ($GLOBALS['TCA'][$this->table]['columns'][$hiddenField] ?? false)
&& (
!($GLOBALS['TCA'][$this->table]['columns'][$hiddenField]['exclude'] ?? false)
|| $this->getBackendUser()->check('non_exclude_fields', $this->table . ':' . $hiddenField)
)
;
}
public function getEditUrl(): string
{
$urlParameters = [
'edit' => [
$this->table => [
$this->record['uid'] => 'edit',
],
],
'returnUrl' => $GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getRequestUri() . '#element-' . $this->table . '-' . $this->record['uid'],
];
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
return $uriBuilder->buildUriFromRoute('record_edit', $urlParameters) . '#element-' . $this->table . '-' . $this->record['uid'];
}
/**
* Gets the number of records referencing the record with the UID $uid in
* the current table.
*
* @return int The number of references to record $uid in table
*/
protected function getReferenceCount(int $uid): int
{
return GeneralUtility::makeInstance(ReferenceIndex::class)->getNumberOfReferencedRecords($this->table, $uid);
}
protected function getTypeColumn(): string
{
return (string)($GLOBALS['TCA'][$this->table]['ctrl']['type'] ?? '');
}
}