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

BackendLayout::getIconPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace TYPO3\CMS\Backend\View\BackendLayout;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use TYPO3\CMS\Backend\Utility\BackendUtility;
18
use TYPO3\CMS\Backend\View\BackendLayout\Grid\Grid;
19
use TYPO3\CMS\Backend\View\BackendLayout\Grid\GridColumn;
20
use TYPO3\CMS\Backend\View\BackendLayout\Grid\GridRow;
21
use TYPO3\CMS\Backend\View\BackendLayout\Grid\LanguageColumn;
22
use TYPO3\CMS\Backend\View\Drawing\BackendLayoutRenderer;
23
use TYPO3\CMS\Backend\View\Drawing\DrawingConfiguration;
24
use TYPO3\CMS\Core\Localization\LanguageService;
25
use TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
28
/**
29
 * Class to represent a backend layout.
30
 */
31
class BackendLayout
32
{
33
    /**
34
     * @var string
35
     */
36
    protected $identifier;
37
38
    /**
39
     * @var string
40
     */
41
    protected $title;
42
43
    /**
44
     * @var string
45
     */
46
    protected $description;
47
48
    /**
49
     * @var string
50
     */
51
    protected $iconPath;
52
53
    /**
54
     * @var string
55
     */
56
    protected $configuration;
57
58
    /**
59
     * @var array
60
     */
61
    protected $configurationArray;
62
63
    /**
64
     * @var array
65
     */
66
    protected $data;
67
68
    /**
69
     * @var DrawingConfiguration
70
     */
71
    protected $drawingConfiguration;
72
73
    /**
74
     * @var ContentFetcher
75
     */
76
    protected $contentFetcher;
77
78
    /**
79
     * @var LanguageColumn
80
     */
81
    protected $languageColumns = [];
82
83
    /**
84
     * @var RecordRememberer
85
     */
86
    protected $recordRememberer;
87
88
    /**
89
     * @param string $identifier
90
     * @param string $title
91
     * @param string|array $configuration
92
     * @return BackendLayout
93
     */
94
    public static function create($identifier, $title, $configuration)
95
    {
96
        return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
97
            static::class,
98
            $identifier,
0 ignored issues
show
Bug introduced by
$identifier of type string 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

98
            /** @scrutinizer ignore-type */ $identifier,
Loading history...
99
            $title,
100
            $configuration
101
        );
102
    }
103
104
    /**
105
     * @param string $identifier
106
     * @param string $title
107
     * @param string|array $configuration
108
     */
109
    public function __construct($identifier, $title, $configuration)
110
    {
111
        $this->drawingConfiguration = GeneralUtility::makeInstance(DrawingConfiguration::class);
112
        $this->contentFetcher = GeneralUtility::makeInstance(ContentFetcher::class, $this);
0 ignored issues
show
Bug introduced by
$this 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

112
        $this->contentFetcher = GeneralUtility::makeInstance(ContentFetcher::class, /** @scrutinizer ignore-type */ $this);
Loading history...
113
        $this->recordRememberer = GeneralUtility::makeInstance(RecordRememberer::class);
114
        $this->setIdentifier($identifier);
115
        $this->setTitle($title);
116
        if (is_array($configuration)) {
117
            $this->setConfigurationArray($configuration);
118
        } else {
119
            $this->setConfiguration($configuration);
120
        }
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function getIdentifier()
127
    {
128
        return $this->identifier;
129
    }
130
131
    /**
132
     * @param string $identifier
133
     * @throws \UnexpectedValueException
134
     */
135
    public function setIdentifier($identifier)
136
    {
137
        if (strpos($identifier, '__') !== false) {
138
            throw new \UnexpectedValueException(
139
                'Identifier "' . $identifier . '" must not contain "__"',
140
                1381597630
141
            );
142
        }
143
144
        $this->identifier = $identifier;
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public function getTitle()
151
    {
152
        return $this->title;
153
    }
154
155
    /**
156
     * @param string $title
157
     */
158
    public function setTitle($title)
159
    {
160
        $this->title = $title;
161
    }
162
163
    /**
164
     * @return string
165
     */
166
    public function getDescription()
167
    {
168
        return $this->description;
169
    }
170
171
    /**
172
     * @param string $description
173
     */
174
    public function setDescription($description)
175
    {
176
        $this->description = $description;
177
    }
178
179
    /**
180
     * @return string
181
     */
182
    public function getIconPath()
183
    {
184
        return $this->iconPath;
185
    }
186
187
    /**
188
     * @param string $iconPath
189
     */
190
    public function setIconPath($iconPath)
191
    {
192
        $this->iconPath = $iconPath;
193
    }
194
195
    /**
196
     * @return string
197
     */
198
    public function getConfiguration()
199
    {
200
        return $this->configuration;
201
    }
202
203
    /**
204
     * @param array $configurationArray
205
     */
206
    public function setConfigurationArray(array $configurationArray): void
207
    {
208
        if (!isset($configurationArray['__colPosList'], $configurationArray['__items'])) {
209
            // Backend layout configuration is unprocessed, process it now to extract counts and column item lists
210
            $colPosList = [];
211
            $items = [];
212
            $rowIndex = 0;
213
            foreach ($configurationArray['backend_layout.']['rows.'] as $row) {
214
                $index = 0;
215
                $colCount = 0;
216
                $columns = [];
217
                foreach ($row['columns.'] as $column) {
218
                    if (!isset($column['colPos'])) {
219
                        continue;
220
                    }
221
                    $colPos = $column['colPos'];
222
                    $colPos = (int)$colPos;
223
                    $colPosList[$colPos] = $colPos;
224
                    $key = ($index + 1) . '.';
225
                    $columns[$key] = $column;
226
                    $items[$colPos] = [
227
                        (string)$this->getLanguageService()->sL($column['name']),
228
                        $colPos,
229
                        $column['icon']
230
                    ];
231
                    $colCount += $column['colspan'] ? $column['colspan'] : 1;
232
                    ++ $index;
233
                }
234
                ++ $rowIndex;
235
            }
236
237
            $configurationArray['__config'] = $configurationArray;
238
            $configurationArray['__colPosList'] = $colPosList;
239
            $configurationArray['__items'] = $items;
240
        }
241
        $this->configurationArray = $configurationArray;
242
    }
243
244
    /**
245
     * @return array
246
     */
247
    public function getConfigurationArray(): array
248
    {
249
        return $this->configurationArray;
250
    }
251
252
    /**
253
     * @param string $configuration
254
     */
255
    public function setConfiguration($configuration)
256
    {
257
        $this->configuration = $configuration;
258
        $this->parseConfigurationStringAndSetConfigurationArray($configuration);
259
    }
260
261
    /**
262
     * @return array
263
     */
264
    public function getData()
265
    {
266
        return $this->data;
267
    }
268
269
    /**
270
     * @param array $data
271
     */
272
    public function setData(array $data)
273
    {
274
        $this->data = $data;
275
    }
276
277
    /**
278
     * @return LanguageColumn[]
279
     */
280
    public function getLanguageColumns(): iterable
281
    {
282
        if (empty($this->languageColumns)) {
283
            $defaultLanguageElements = [];
284
            $contentByColumn = $this->getContentFetcher()->getContentRecordsPerColumn(null, 0);
285
            if (!empty($contentByColumn)) {
286
                $defaultLanguageElements = array_merge(...$contentByColumn);
287
            }
288
            foreach ($this->getDrawingConfiguration()->getSiteLanguages() as $siteLanguage) {
289
                if (!in_array($siteLanguage->getLanguageId(), $this->getDrawingConfiguration()->getLanguageColumns())) {
290
                    continue;
291
                }
292
                $backendLayout = clone $this;
293
                $backendLayout->getDrawingConfiguration()->setLanguageColumnsPointer($siteLanguage->getLanguageId());
294
                $this->languageColumns[] = GeneralUtility::makeInstance(LanguageColumn::class, $backendLayout, $siteLanguage, $defaultLanguageElements);
0 ignored issues
show
Bug introduced by
$siteLanguage of type TYPO3\CMS\Core\Site\Entity\SiteLanguage 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

294
                $this->languageColumns[] = GeneralUtility::makeInstance(LanguageColumn::class, $backendLayout, /** @scrutinizer ignore-type */ $siteLanguage, $defaultLanguageElements);
Loading history...
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

294
                $this->languageColumns[] = GeneralUtility::makeInstance(LanguageColumn::class, /** @scrutinizer ignore-type */ $backendLayout, $siteLanguage, $defaultLanguageElements);
Loading history...
295
            }
296
        }
297
        return $this->languageColumns;
298
    }
299
300
    public function getGrid(): Grid
301
    {
302
        $grid = GeneralUtility::makeInstance(Grid::class, $this);
0 ignored issues
show
Bug introduced by
$this 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

302
        $grid = GeneralUtility::makeInstance(Grid::class, /** @scrutinizer ignore-type */ $this);
Loading history...
303
        foreach ($this->getConfigurationArray()['__config']['backend_layout.']['rows.'] as $row) {
304
            $rowObject = GeneralUtility::makeInstance(GridRow::class, $this);
305
            foreach ($row['columns.'] as $column) {
306
                $columnObject = GeneralUtility::makeInstance(GridColumn::class, $this, $column);
307
                $rowObject->addColumn($columnObject);
308
            }
309
            $grid->addRow($rowObject);
310
        }
311
        $allowInconsistentLanguageHandling = (bool)(BackendUtility::getPagesTSconfig($this->id)['mod.']['web_layout.']['allowInconsistentLanguageHandling'] ?? false);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on TYPO3\CMS\Backend\View\BackendLayout\BackendLayout. Did you maybe forget to declare it?
Loading history...
312
        if (!$allowInconsistentLanguageHandling && $this->getLanguageModeIdentifier() === 'connected') {
313
            $grid->setAllowNewContent(false);
314
        }
315
        return $grid;
316
    }
317
318
    public function getColumnPositionNumbers(): array
319
    {
320
        return $this->getConfigurationArray()['__colPosList'];
321
    }
322
323
    public function getContentFetcher(): ContentFetcher
324
    {
325
        return $this->contentFetcher;
326
    }
327
328
    public function setContentFetcher(ContentFetcher $contentFetcher): void
329
    {
330
        $this->contentFetcher = $contentFetcher;
331
    }
332
333
    public function getDrawingConfiguration(): DrawingConfiguration
334
    {
335
        return $this->drawingConfiguration;
336
    }
337
338
    public function getBackendLayoutRenderer(): BackendLayoutRenderer
339
    {
340
        return GeneralUtility::makeInstance(BackendLayoutRenderer::class, $this);
0 ignored issues
show
Bug introduced by
$this 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

340
        return GeneralUtility::makeInstance(BackendLayoutRenderer::class, /** @scrutinizer ignore-type */ $this);
Loading history...
341
    }
342
343
    public function getRecordRememberer(): RecordRememberer
344
    {
345
        return $this->recordRememberer;
346
    }
347
348
    public function getLanguageModeIdentifier(): string
349
    {
350
        $contentRecordsPerColumn = $this->contentFetcher->getContentRecordsPerColumn(null, $this->drawingConfiguration->getLanguageColumnsPointer());
351
        $contentRecords = empty($contentRecordsPerColumn) ? [] : array_merge(...$contentRecordsPerColumn);
352
        $translationData = $this->contentFetcher->getTranslationData($contentRecords, $this->drawingConfiguration->getLanguageColumnsPointer());
353
        return $translationData['mode'] ?? '';
354
    }
355
356
    protected function parseConfigurationStringAndSetConfigurationArray(string $configuration): void
357
    {
358
        $parser = GeneralUtility::makeInstance(TypoScriptParser::class);
359
        $conditionMatcher = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher::class);
360
        $parser->parse(TypoScriptParser::checkIncludeLines($configuration), $conditionMatcher);
361
        $this->setConfigurationArray($parser->setup);
362
    }
363
364
    public function __clone()
365
    {
366
        $this->drawingConfiguration = clone $this->drawingConfiguration;
367
        $this->contentFetcher->setBackendLayout($this);
368
    }
369
370
    protected function getLanguageService(): LanguageService
371
    {
372
        return $GLOBALS['LANG'];
373
    }
374
}
375