GridColumn.php 9.07 KB
Newer Older
1
<?php
2

3
declare(strict_types=1);
4
5
6
7
8
9
10
11
12
13
14
15
16
17

/*
 * 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!
 */

18
19
namespace TYPO3\CMS\Backend\View\BackendLayout\Grid;

20
21
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Utility\BackendUtility;
22
23
use TYPO3\CMS\Backend\View\Event\AfterSectionMarkupGeneratedEvent;
use TYPO3\CMS\Backend\View\Event\BeforeSectionMarkupGeneratedEvent;
24
use TYPO3\CMS\Backend\View\PageLayoutContext;
25
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use TYPO3\CMS\Core\Type\Bitmask\Permission;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * Grid Column
 *
 * Object representation (model/proxy) for a single column from a grid defined
 * in a BackendLayout. Stores GridColumnItem representations of content records
 * and provides getter methods which return various properties associated with
 * a single column, e.g. the "edit all elements in content" URL and the "add
 * new content element" URL of the button that is placed in the top of columns
 * in the page layout.
 *
 * Accessed from Fluid templates.
40
41
 *
 * @internal this is experimental and subject to change in TYPO3 v10 / v11
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
 */
class GridColumn extends AbstractGridObject
{
    /**
     * @var GridColumnItem[]
     */
    protected $items = [];

    /**
     * @var int|null
     */
    protected $columnNumber;

    /**
     * @var string
     */
    protected $columnName = 'default';

    /**
     * @var string|null
     */
    protected $icon;

    /**
     * @var int
     */
    protected $colSpan = 1;

    /**
     * @var int
     */
    protected $rowSpan = 1;

75
76
77
78
79
    /**
     * @var array<string, mixed>
     */
    protected $definition;

80
81
    private EventDispatcher $eventDispatcher;

82
    public function __construct(PageLayoutContext $context, array $definition)
83
    {
84
        parent::__construct($context);
85
86
87
88
89
90
        $this->definition = $definition;
        $this->columnNumber = isset($definition['colPos']) ? (int)$definition['colPos'] : null;
        $this->columnName = $definition['name'] ?? $this->columnName;
        $this->icon = $definition['icon'] ?? $this->icon;
        $this->colSpan = (int)($definition['colspan'] ?? $this->colSpan);
        $this->rowSpan = (int)($definition['rowspan'] ?? $this->rowSpan);
91
        $this->eventDispatcher = GeneralUtility::makeInstance(EventDispatcher::class);
92
93
94
95
96
97
98
99
    }

    /**
     * @return array<string, mixed>
     */
    public function getDefinition(): array
    {
        return $this->definition;
100
101
102
103
    }

    public function isActive(): bool
    {
104
        return $this->columnNumber !== null && in_array($this->columnNumber, $this->context->getDrawingConfiguration()->getActiveColumns());
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
    }

    public function addItem(GridColumnItem $item): void
    {
        $this->items[] = $item;
    }

    /**
     * @return GridColumnItem[]
     */
    public function getItems(): iterable
    {
        return $this->items;
    }

    public function getColumnNumber(): ?int
    {
        return $this->columnNumber;
    }

    public function getColumnName(): string
    {
        return $this->columnName;
    }

    public function getIcon(): ?string
    {
        return $this->icon;
    }

    public function getColSpan(): int
    {
137
        if ($this->context->getDrawingConfiguration()->getLanguageMode()) {
138
139
140
141
142
143
144
            return 1;
        }
        return $this->colSpan;
    }

    public function getRowSpan(): int
    {
145
        if ($this->context->getDrawingConfiguration()->getLanguageMode()) {
146
147
148
149
150
            return 1;
        }
        return $this->rowSpan;
    }

151
152
153
154
    /**
     * @return int[]
     */
    public function getAllContainedItemUids(): array
155
156
157
    {
        $uids = [];
        foreach ($this->items as $columnItem) {
158
            $uids[] = (int)$columnItem->getRecord()['uid'];
159
160
161
162
163
164
165
166
167
        }
        return $uids;
    }

    public function getEditUrl(): ?string
    {
        if (empty($this->items)) {
            return null;
        }
168
        $pageRecord = $this->context->getPageRecord();
169
        if (!$this->getBackendUser()->doesUserHaveAccess($pageRecord, Permission::CONTENT_EDIT)
170
            || !$this->getBackendUser()->checkLanguageAccess($this->context->getSiteLanguage()->getLanguageId())) {
171
172
173
174
175
176
177
            return null;
        }
        $pageTitleParamForAltDoc = '&recTitle=' . rawurlencode(
            BackendUtility::getRecordTitle('pages', $pageRecord, true)
        );
        $editParam = '&edit[tt_content][' . implode(',', $this->getAllContainedItemUids()) . ']=edit' . $pageTitleParamForAltDoc;
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
178
        return $uriBuilder->buildUriFromRoute('record_edit') . $editParam . '&returnUrl=' . rawurlencode($GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getRequestUri());
179
180
181
182
183
    }

    public function getNewContentUrl(): string
    {
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
184
185
186
187
188
189
190
191
        $pageId = $this->context->getPageId();

        if ($this->context->getDrawingConfiguration()->getShowNewContentWizard()) {
            $urlParameters = [
                'id' => $pageId,
                'sys_language_uid' => $this->context->getSiteLanguage()->getLanguageId(),
                'colPos' => $this->getColumnNumber(),
                'uid_pid' => $pageId,
192
                'returnUrl' => $GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getRequestUri(),
193
194
195
196
197
198
199
            ];
            $routeName = BackendUtility::getPagesTSconfig($pageId)['mod.']['newContentElementWizard.']['override']
                ?? 'new_content_element_wizard';
        } else {
            $urlParameters = [
                'edit' => [
                    'tt_content' => [
200
201
                        $pageId => 'new',
                    ],
202
203
204
205
                ],
                'defVals' => [
                    'tt_content' => [
                        'colPos' => $this->getColumnNumber(),
206
207
                        'sys_language_uid' => $this->context->getSiteLanguage()->getLanguageId(),
                    ],
208
                ],
209
                'returnUrl' => $GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getRequestUri(),
210
211
212
213
            ];
            $routeName = 'record_edit';
        }

214
215
216
217
218
219
        return (string)$uriBuilder->buildUriFromRoute($routeName, $urlParameters);
    }

    public function getTitle(): string
    {
        $columnNumber = $this->getColumnNumber();
220
        $colTitle = BackendUtility::getProcessedValue('tt_content', 'colPos', (string)$columnNumber) ?? '';
221
        foreach ($this->context->getBackendLayout()->getUsedColumns() as $colPos => $title) {
222
223
            if ($colPos === $columnNumber) {
                $colTitle = (string)$this->getLanguageService()->sL($title);
224
225
226
227
228
229
230
231
232
233
234
235
            }
        }
        return $colTitle;
    }

    public function getTitleInaccessible(): string
    {
        return $this->getLanguageService()->sL($this->columnName) . ' (' . $this->getLanguageService()->getLL('noAccess') . ')';
    }

    public function getTitleUnassigned(): string
    {
236
        return $this->getLanguageService()->sL($this->columnName) . ' (' . $this->getLanguageService()->getLL('notAssigned') . ')';
237
238
    }

239
240
    public function getBeforeSectionMarkup(): string
    {
241
        $event = new BeforeSectionMarkupGeneratedEvent($this->definition, $this->context, $this->getRecords());
242
243
244
245
246
247
        $this->eventDispatcher->dispatch($event);
        return $event->getContent();
    }

    public function getAfterSectionMarkup(): string
    {
248
        $event = new AfterSectionMarkupGeneratedEvent($this->definition, $this->context, $this->getRecords());
249
250
251
252
        $this->eventDispatcher->dispatch($event);
        return $event->getContent();
    }

253
254
    public function isUnassigned(): bool
    {
255
256
257
258
259
260
        return $this->columnName !== 'unused' && $this->columnNumber === null;
    }

    public function isUnused(): bool
    {
        return $this->columnName === 'unused' && $this->columnNumber === null;
261
262
263
264
265
266
267
268
269
270
    }

    public function isContentEditable(): bool
    {
        if ($this->columnName === 'unused' || $this->columnNumber === null) {
            return false;
        }
        if ($this->getBackendUser()->isAdmin()) {
            return true;
        }
271
        $pageRecord = $this->context->getPageRecord();
272
273
        return !$pageRecord['editlock']
            && $this->getBackendUser()->doesUserHaveAccess($pageRecord, Permission::CONTENT_EDIT)
274
            && $this->getBackendUser()->checkLanguageAccess($this->context->getSiteLanguage()->getLanguageId());
275
    }
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294

    /**
     * Get the raw records for the current column
     *
     * @return array<int, array<string, mixed>>
     */
    protected function getRecords(): array
    {
        if ($this->items === []) {
            return [];
        }

        $records = [];
        foreach ($this->items as $item) {
            $record = $item->getRecord();
            $records[(int)$record['uid']] = $record;
        }
        return $records;
    }
295
}