Completed
Push — master ( f66149...a7372c )
by
unknown
130:24 queued 111:56
created

GridColumn::__construct()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 6
nop 3
dl 0
loc 13
rs 9.9
1
<?php
2
declare(strict_types = 1);
3
namespace TYPO3\CMS\Backend\View\BackendLayout\Grid;
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
use TYPO3\CMS\Backend\Routing\UriBuilder;
19
use TYPO3\CMS\Backend\Utility\BackendUtility;
20
use TYPO3\CMS\Backend\View\BackendLayout\BackendLayout;
21
use TYPO3\CMS\Core\Type\Bitmask\Permission;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24
/**
25
 * Grid Column
26
 *
27
 * Object representation (model/proxy) for a single column from a grid defined
28
 * in a BackendLayout. Stores GridColumnItem representations of content records
29
 * and provides getter methods which return various properties associated with
30
 * a single column, e.g. the "edit all elements in content" URL and the "add
31
 * new content element" URL of the button that is placed in the top of columns
32
 * in the page layout.
33
 *
34
 * Accessed from Fluid templates.
35
 */
36
class GridColumn extends AbstractGridObject
37
{
38
    /**
39
     * @var GridColumnItem[]
40
     */
41
    protected $items = [];
42
43
    /**
44
     * @var int|null
45
     */
46
    protected $columnNumber;
47
48
    /**
49
     * @var string
50
     */
51
    protected $columnName = 'default';
52
53
    /**
54
     * @var string|null
55
     */
56
    protected $icon;
57
58
    /**
59
     * @var int
60
     */
61
    protected $colSpan = 1;
62
63
    /**
64
     * @var int
65
     */
66
    protected $rowSpan = 1;
67
68
    /**
69
     * @var array
70
     */
71
    protected $records;
72
73
    public function __construct(BackendLayout $backendLayout, array $columnDefinition, ?array $records = null)
74
    {
75
        parent::__construct($backendLayout);
76
        $this->columnNumber = isset($columnDefinition['colPos']) ? (int)$columnDefinition['colPos'] : $this->columnNumber;
77
        $this->columnName = $columnDefinition['name'] ?? $this->columnName;
78
        $this->icon = $columnDefinition['icon'] ?? $this->icon;
79
        $this->colSpan = (int)($columnDefinition['colspan'] ?? $this->colSpan);
80
        $this->rowSpan = (int)($columnDefinition['rowspan'] ?? $this->rowSpan);
81
        if ($this->columnNumber !== null) {
82
            $this->records = $records ?? $backendLayout->getContentFetcher()->getContentRecordsPerColumn($this->columnNumber, $backendLayout->getDrawingConfiguration()->getLanguageColumnsPointer());
83
            foreach ($this->records as $contentRecord) {
84
                $columnItem = GeneralUtility::makeInstance(GridColumnItem::class, $backendLayout, $this, $contentRecord);
0 ignored issues
show
Bug introduced by
$backendLayout of type TYPO3\CMS\Backend\View\BackendLayout\BackendLayout is incompatible with the type array|array<mixed,mixed> expected by parameter $constructorArguments of TYPO3\CMS\Core\Utility\G...Utility::makeInstance(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
                $columnItem = GeneralUtility::makeInstance(GridColumnItem::class, /** @scrutinizer ignore-type */ $backendLayout, $this, $contentRecord);
Loading history...
Bug introduced by
$this of type TYPO3\CMS\Backend\View\B...dLayout\Grid\GridColumn is incompatible with the type array|array<mixed,mixed> expected by parameter $constructorArguments of TYPO3\CMS\Core\Utility\G...Utility::makeInstance(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
                $columnItem = GeneralUtility::makeInstance(GridColumnItem::class, $backendLayout, /** @scrutinizer ignore-type */ $this, $contentRecord);
Loading history...
85
                $this->addItem($columnItem);
86
            }
87
        }
88
    }
89
90
    public function isActive(): bool
91
    {
92
        return $this->columnNumber !== null && in_array($this->columnNumber, $this->backendLayout->getDrawingConfiguration()->getActiveColumns());
93
    }
94
95
    public function addItem(GridColumnItem $item): void
96
    {
97
        $this->items[] = $item;
98
    }
99
100
    public function getRecords(): iterable
101
    {
102
        return $this->records;
103
    }
104
105
    /**
106
     * @return GridColumnItem[]
107
     */
108
    public function getItems(): iterable
109
    {
110
        return $this->items;
111
    }
112
113
    public function getColumnNumber(): ?int
114
    {
115
        return $this->columnNumber;
116
    }
117
118
    public function getColumnName(): string
119
    {
120
        return $this->columnName;
121
    }
122
123
    public function getIcon(): ?string
124
    {
125
        return $this->icon;
126
    }
127
128
    public function getColSpan(): int
129
    {
130
        if ($this->backendLayout->getDrawingConfiguration()->getLanguageMode()) {
131
            return 1;
132
        }
133
        return $this->colSpan;
134
    }
135
136
    public function getRowSpan(): int
137
    {
138
        if ($this->backendLayout->getDrawingConfiguration()->getLanguageMode()) {
139
            return 1;
140
        }
141
        return $this->rowSpan;
142
    }
143
144
    public function getAllContainedItemUids(): iterable
145
    {
146
        $uids = [];
147
        foreach ($this->items as $columnItem) {
148
            $uids[] = $columnItem->getRecord()['uid'];
149
        }
150
        return $uids;
151
    }
152
153
    public function getEditUrl(): ?string
154
    {
155
        if (empty($this->items)) {
156
            return null;
157
        }
158
        $pageRecord = $this->backendLayout->getDrawingConfiguration()->getPageRecord();
159
        if (!$this->getBackendUser()->doesUserHaveAccess($pageRecord, Permission::CONTENT_EDIT)
160
            && !$this->getBackendUser()->checkLanguageAccess(0)) {
161
            return null;
162
        }
163
        $pageTitleParamForAltDoc = '&recTitle=' . rawurlencode(
164
            BackendUtility::getRecordTitle('pages', $pageRecord, true)
165
        );
166
        $editParam = '&edit[tt_content][' . implode(',', $this->getAllContainedItemUids()) . ']=edit' . $pageTitleParamForAltDoc;
167
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
168
        return $uriBuilder->buildUriFromRoute('record_edit') . $editParam . '&returnUrl=' . rawurlencode(GeneralUtility::getIndpEnv('REQUEST_URI'));
169
    }
170
171
    public function getNewContentUrl(): string
172
    {
173
        $pageId = $this->backendLayout->getDrawingConfiguration()->getPageId();
174
        $urlParameters = [
175
            'id' => $pageId,
176
            'sys_language_uid' => $this->backendLayout->getDrawingConfiguration()->getLanguageColumnsPointer(),
177
            'colPos' => $this->getColumnNumber(),
178
            'uid_pid' => $pageId,
179
            'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
180
        ];
181
        $routeName = BackendUtility::getPagesTSconfig($pageId)['mod.']['newContentElementWizard.']['override']
182
            ?? 'new_content_element_wizard';
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "??"; newline found
Loading history...
183
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
184
        return (string)$uriBuilder->buildUriFromRoute($routeName, $urlParameters);
185
    }
186
187
    public function getTitle(): string
188
    {
189
        $columnNumber = $this->getColumnNumber();
190
        $colTitle = (string)BackendUtility::getProcessedValue('tt_content', 'colPos', $columnNumber);
191
        $tcaItems = $this->backendLayout->getConfigurationArray()['__items'];
192
        foreach ($tcaItems as $item) {
193
            if ($item[1] === $columnNumber) {
194
                $colTitle = (string)$this->getLanguageService()->sL($item[0]);
195
            }
196
        }
197
        return $colTitle;
198
    }
199
200
    public function getTitleInaccessible(): string
201
    {
202
        return $this->getLanguageService()->sL($this->columnName) . ' (' . $this->getLanguageService()->getLL('noAccess') . ')';
203
    }
204
205
    public function getTitleUnassigned(): string
206
    {
207
        return $this->getLanguageService()->getLL('notAssigned');
208
    }
209
210
    public function isUnassigned(): bool
211
    {
212
        return $this->columnNumber === null;
213
    }
214
215
    public function isContentEditable(): bool
216
    {
217
        if ($this->columnName === 'unused' || $this->columnNumber === null) {
218
            return false;
219
        }
220
        if ($this->getBackendUser()->isAdmin()) {
221
            return true;
222
        }
223
        $pageRecord = $this->backendLayout->getDrawingConfiguration()->getPageRecord();
224
        return !$pageRecord['editlock']
225
            && $this->getBackendUser()->doesUserHaveAccess($pageRecord, Permission::CONTENT_EDIT)
226
            && $this->getBackendUser()->checkLanguageAccess($this->backendLayout->getDrawingConfiguration()->getLanguageColumnsPointer());
227
    }
228
}
229