Passed
Push — master ( c4c6dd...2cad69 )
by
unknown
20:24
created

BackendLayout::setDrawingConfiguration()   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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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\BackendLayoutView;
23
use TYPO3\CMS\Backend\View\Drawing\BackendLayoutRenderer;
24
use TYPO3\CMS\Backend\View\Drawing\DrawingConfiguration;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27
/**
28
 * Class to represent a backend layout.
29
 */
30
class BackendLayout
31
{
32
    /**
33
     * @var string
34
     */
35
    protected $identifier;
36
37
    /**
38
     * @var string
39
     */
40
    protected $title;
41
42
    /**
43
     * @var string
44
     */
45
    protected $description;
46
47
    /**
48
     * @var string
49
     */
50
    protected $iconPath;
51
52
    /**
53
     * @var string
54
     */
55
    protected $configuration;
56
57
    /**
58
     * The structured data of the configuration represented as array.
59
     *
60
     * @var array
61
     */
62
    protected $structure = [];
63
64
    /**
65
     * @var array
66
     */
67
    protected $data;
68
69
    /**
70
     * @var DrawingConfiguration
71
     */
72
    protected $drawingConfiguration;
73
74
    /**
75
     * @var ContentFetcher
76
     */
77
    protected $contentFetcher;
78
79
    /**
80
     * @var LanguageColumn
81
     */
82
    protected $languageColumns = [];
83
84
    /**
85
     * @var RecordRememberer
86
     */
87
    protected $recordRememberer;
88
89
    /**
90
     * @param string $identifier
91
     * @param string $title
92
     * @param string|array $configuration
93
     * @return BackendLayout
94
     */
95
    public static function create($identifier, $title, $configuration)
96
    {
97
        return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
98
            static::class,
99
            $identifier,
100
            $title,
101
            $configuration
102
        );
103
    }
104
105
    /**
106
     * @param string $identifier
107
     * @param string $title
108
     * @param string|array $configuration
109
     */
110
    public function __construct($identifier, $title, $configuration)
111
    {
112
        $this->drawingConfiguration = GeneralUtility::makeInstance(DrawingConfiguration::class);
113
        $this->contentFetcher = GeneralUtility::makeInstance(ContentFetcher::class, $this);
114
        $this->recordRememberer = GeneralUtility::makeInstance(RecordRememberer::class);
115
        $this->setIdentifier($identifier);
116
        $this->setTitle($title);
117
        if (is_array($configuration)) {
118
            $this->structure = $configuration;
119
            $this->configuration = $configuration['config'] ?? '';
120
        } else {
121
            $this->setConfiguration($configuration);
122
        }
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getIdentifier()
129
    {
130
        return $this->identifier;
131
    }
132
133
    /**
134
     * @param string $identifier
135
     * @throws \UnexpectedValueException
136
     */
137
    public function setIdentifier($identifier)
138
    {
139
        if (strpos($identifier, '__') !== false) {
140
            throw new \UnexpectedValueException(
141
                'Identifier "' . $identifier . '" must not contain "__"',
142
                1381597630
143
            );
144
        }
145
146
        $this->identifier = $identifier;
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getTitle()
153
    {
154
        return $this->title;
155
    }
156
157
    /**
158
     * @param string $title
159
     */
160
    public function setTitle($title)
161
    {
162
        $this->title = $title;
163
    }
164
165
    /**
166
     * @return string
167
     */
168
    public function getDescription()
169
    {
170
        return $this->description;
171
    }
172
173
    /**
174
     * @param string $description
175
     */
176
    public function setDescription($description)
177
    {
178
        $this->description = $description;
179
    }
180
181
    /**
182
     * @return string
183
     */
184
    public function getIconPath()
185
    {
186
        return $this->iconPath;
187
    }
188
189
    /**
190
     * @param string $iconPath
191
     */
192
    public function setIconPath($iconPath)
193
    {
194
        $this->iconPath = $iconPath;
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    public function getConfiguration()
201
    {
202
        return $this->configuration;
203
    }
204
205
    /**
206
     * @param string $configuration
207
     */
208
    public function setConfiguration($configuration)
209
    {
210
        $this->configuration = $configuration;
211
        GeneralUtility::makeInstance(BackendLayoutView::class)->parseStructure($this);
212
    }
213
214
    /**
215
     * Returns the columns registered for this layout as $key => $value pair where the key is the colPos
216
     * and the value is the title.
217
     * "1" => "Left" etc.
218
     * Please note that the title can contain LLL references ready for translation.
219
     * @return array
220
     */
221
    public function getUsedColumns(): array
222
    {
223
        return $this->structure['usedColumns'] ?? [];
224
    }
225
226
    /**
227
     * @return array
228
     */
229
    public function getData()
230
    {
231
        return $this->data;
232
    }
233
234
    /**
235
     * @param array $data
236
     */
237
    public function setData(array $data)
238
    {
239
        $this->data = $data;
240
    }
241
242
    public function setStructure(array $structure)
243
    {
244
        $this->structure = $structure;
245
    }
246
247
    public function getStructure(): array
248
    {
249
        return $this->structure;
250
    }
251
252
    /**
253
     * @return LanguageColumn[]
254
     */
255
    public function getLanguageColumns(): iterable
256
    {
257
        if (empty($this->languageColumns)) {
258
            $availableLanguageColumns = $this->getDrawingConfiguration()->getLanguageColumns();
259
            foreach ($this->getDrawingConfiguration()->getSiteLanguages() as $siteLanguage) {
260
                if (!isset($availableLanguageColumns[$siteLanguage->getLanguageId()])) {
261
                    continue;
262
                }
263
                $backendLayout = clone $this;
264
                $backendLayout->getDrawingConfiguration()->setLanguageColumnsPointer($siteLanguage->getLanguageId());
265
                $this->languageColumns[] = GeneralUtility::makeInstance(LanguageColumn::class, $backendLayout, $siteLanguage);
266
            }
267
        }
268
        return $this->languageColumns;
269
    }
270
271
    public function getGrid(): Grid
272
    {
273
        $grid = GeneralUtility::makeInstance(Grid::class, $this);
274
        foreach ($this->structure['__config']['backend_layout.']['rows.'] ?? [] as $row) {
275
            $rowObject = GeneralUtility::makeInstance(GridRow::class, $this);
276
            foreach ($row['columns.'] as $column) {
277
                $columnObject = GeneralUtility::makeInstance(GridColumn::class, $this, $column);
278
                $rowObject->addColumn($columnObject);
279
            }
280
            $grid->addRow($rowObject);
281
        }
282
        $pageId = $this->drawingConfiguration->getPageId();
283
        $allowInconsistentLanguageHandling = (bool)(BackendUtility::getPagesTSconfig($pageId)['mod.']['web_layout.']['allowInconsistentLanguageHandling'] ?? false);
284
        if (!$allowInconsistentLanguageHandling && $this->getLanguageModeIdentifier() === 'connected') {
285
            $grid->setAllowNewContent(false);
286
        }
287
        return $grid;
288
    }
289
290
    public function getColumnPositionNumbers(): array
291
    {
292
        return $this->structure['__colPosList'];
293
    }
294
295
    public function getContentFetcher(): ContentFetcher
296
    {
297
        return $this->contentFetcher;
298
    }
299
300
    public function setContentFetcher(ContentFetcher $contentFetcher): void
301
    {
302
        $this->contentFetcher = $contentFetcher;
303
    }
304
305
    public function getDrawingConfiguration(): DrawingConfiguration
306
    {
307
        return $this->drawingConfiguration;
308
    }
309
310
    public function setDrawingConfiguration(DrawingConfiguration $drawingConfiguration): void
311
    {
312
        $this->drawingConfiguration = $drawingConfiguration;
313
    }
314
315
    public function getBackendLayoutRenderer(): BackendLayoutRenderer
316
    {
317
        return GeneralUtility::makeInstance(BackendLayoutRenderer::class, $this);
318
    }
319
320
    public function getRecordRememberer(): RecordRememberer
321
    {
322
        return $this->recordRememberer;
323
    }
324
325
    public function getLanguageModeIdentifier(): string
326
    {
327
        $contentRecordsPerColumn = $this->contentFetcher->getContentRecordsPerColumn(null, $this->drawingConfiguration->getLanguageColumnsPointer());
328
        $contentRecords = empty($contentRecordsPerColumn) ? [] : array_merge(...$contentRecordsPerColumn);
329
        $translationData = $this->contentFetcher->getTranslationData($contentRecords, $this->drawingConfiguration->getLanguageColumnsPointer());
330
        return $translationData['mode'] ?? '';
331
    }
332
333
    public function __clone()
334
    {
335
        $this->drawingConfiguration = clone $this->drawingConfiguration;
336
        $this->contentFetcher->setBackendLayout($this);
337
    }
338
}
339