[TASK] Add more fixers for php-cs-fixer
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / View / BackendLayout / DefaultDataProvider.php
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\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Backend layout data provider class
22 */
23 class DefaultDataProvider implements DataProviderInterface
24 {
25
26 /**
27 * @var string
28 * Table name for backend_layouts
29 */
30 protected $tableName = 'backend_layout';
31
32 /**
33 * Adds backend layouts to the given backend layout collection.
34 * The default backend layout ('default_default') is not added
35 * since it's the default fallback if nothing is specified.
36 *
37 * @param DataProviderContext $dataProviderContext
38 * @param BackendLayoutCollection $backendLayoutCollection
39 * @return void
40 */
41 public function addBackendLayouts(
42 DataProviderContext $dataProviderContext,
43 BackendLayoutCollection $backendLayoutCollection
44 ) {
45 $layoutData = $this->getLayoutData(
46 $dataProviderContext->getFieldName(),
47 $dataProviderContext->getPageTsConfig(),
48 $dataProviderContext->getPageId()
49 );
50
51 foreach ($layoutData as $data) {
52 $backendLayout = $this->createBackendLayout($data);
53 $backendLayoutCollection->add($backendLayout);
54 }
55 }
56
57 /**
58 * Gets a backend layout by (regular) identifier.
59 *
60 * @param string $identifier
61 * @param int $pageId
62 * @return NULL|BackendLayout
63 */
64 public function getBackendLayout($identifier, $pageId)
65 {
66 $backendLayout = null;
67
68 if ((string)$identifier === 'default') {
69 return $this->createDefaultBackendLayout();
70 }
71
72 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
73 ->getQueryBuilderForTable($this->tableName);
74 $data = $queryBuilder
75 ->select('*')
76 ->from($this->tableName)
77 ->where($queryBuilder->expr()->eq('uid', (int)$identifier))
78 ->execute()
79 ->fetch();
80
81 if (is_array($data)) {
82 $backendLayout = $this->createBackendLayout($data);
83 }
84
85 return $backendLayout;
86 }
87
88 /**
89 * Creates a backend layout with the default configuration.
90 *
91 * @return BackendLayout
92 */
93 protected function createDefaultBackendLayout()
94 {
95 return BackendLayout::create(
96 'default',
97 'LLL:EXT:frontend/Resources/Private/Language/locallang_tca.xlf:pages.backend_layout.default',
98 \TYPO3\CMS\Backend\View\BackendLayoutView::getDefaultColumnLayout()
99 );
100 }
101
102 /**
103 * Creates a new backend layout using the given record data.
104 *
105 * @param array $data
106 * @return BackendLayout
107 */
108 protected function createBackendLayout(array $data)
109 {
110 $backendLayout = BackendLayout::create($data['uid'], $data['title'], $data['config']);
111 $backendLayout->setIconPath($this->getIconPath($data['icon']));
112 $backendLayout->setData($data);
113 return $backendLayout;
114 }
115
116 /**
117 * Gets and sanitizes the icon path.
118 *
119 * @param string $icon Name of the icon file
120 * @return string
121 */
122 protected function getIconPath($icon)
123 {
124 $iconPath = '';
125
126 if (!empty($icon)) {
127 $path = rtrim($GLOBALS['TCA']['backend_layout']['ctrl']['selicon_field_path'], '/') . '/';
128 $iconPath = $path . $icon;
129 }
130
131 return $iconPath;
132 }
133
134 /**
135 * Get all layouts from the core's default data provider.
136 *
137 * @param string $fieldName the name of the field the layouts are provided for (either backend_layout or backend_layout_next_level)
138 * @param array $pageTsConfig PageTSconfig of the given page
139 * @param int $pageUid the ID of the page wea re getting the layouts for
140 * @return array $layouts A collection of layout data of the registered provider
141 */
142 protected function getLayoutData($fieldName, array $pageTsConfig, $pageUid)
143 {
144 $storagePid = $this->getStoragePid($pageTsConfig);
145 $pageTsConfigId = $this->getPageTSconfigIds($pageTsConfig);
146
147 // Add layout records
148 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
149 ->getQueryBuilderForTable($this->tableName);
150 $queryBuilder
151 ->select('*')
152 ->from($this->tableName)
153 ->where(
154 $queryBuilder->expr()->orX(
155 $queryBuilder->expr()->andX(
156 $queryBuilder->expr()->comparison((int)$pageTsConfigId[$fieldName], '=', 0),
157 $queryBuilder->expr()->comparison((int)$storagePid, '=', 0)
158 ),
159 $queryBuilder->expr()->orX(
160 $queryBuilder->expr()->eq('backend_layout.pid', (int)$pageTsConfigId[$fieldName]),
161 $queryBuilder->expr()->eq('backend_layout.pid', (int)$storagePid)
162 ),
163 $queryBuilder->expr()->andX(
164 $queryBuilder->expr()->comparison((int)$pageTsConfigId[$fieldName], '=', 0),
165 $queryBuilder->expr()->eq('backend_layout.pid', (int)$pageUid)
166 )
167 )
168 );
169
170 if (!empty($GLOBALS['TCA'][$this->tableName]['ctrl']['sortby'])) {
171 $queryBuilder->orderBy($GLOBALS['TCA'][$this->tableName]['ctrl']['sortby']);
172 }
173
174 $results = $queryBuilder
175 ->execute()
176 ->fetchAll();
177
178 return $results;
179 }
180
181 /**
182 * Returns the storage PID from TCEFORM.
183 *
184 * @param array $pageTsConfig
185 * @return int
186 */
187 protected function getStoragePid(array $pageTsConfig)
188 {
189 $storagePid = 0;
190
191 if (!empty($pageTsConfig['TCEFORM.']['pages.']['_STORAGE_PID'])) {
192 $storagePid = (int)$pageTsConfig['TCEFORM.']['pages.']['_STORAGE_PID'];
193 }
194
195 return $storagePid;
196 }
197
198 /**
199 * Returns the page TSconfig from TCEFORM.
200 *
201 * @param array $pageTsConfig
202 * @return array
203 */
204 protected function getPageTSconfigIds(array $pageTsConfig)
205 {
206 $pageTsConfigIds = [
207 'backend_layout' => 0,
208 'backend_layout_next_level' => 0,
209 ];
210
211 if (!empty($pageTsConfig['TCEFORM.']['pages.']['backend_layout.']['PAGE_TSCONFIG_ID'])) {
212 $pageTsConfigIds['backend_layout'] = (int)$pageTsConfig['TCEFORM.']['pages.']['backend_layout.']['PAGE_TSCONFIG_ID'];
213 }
214
215 if (!empty($pageTsConfig['TCEFORM.']['pages.']['backend_layout_next_level.']['PAGE_TSCONFIG_ID'])) {
216 $pageTsConfigIds['backend_layout_next_level'] = (int)$pageTsConfig['TCEFORM.']['pages.']['backend_layout_next_level.']['PAGE_TSCONFIG_ID'];
217 }
218
219 return $pageTsConfigIds;
220 }
221 }