45e6d4b2525185f503a1d25c96d71399810c3bd6
[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 Doctrine\Common\Collections\Expr\Comparison;
18 use TYPO3\CMS\Core\Database\ConnectionPool;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Backend layout data provider class
23 */
24 class DefaultDataProvider implements DataProviderInterface
25 {
26
27 /**
28 * @var string
29 * Table name for backend_layouts
30 */
31 protected $tableName = 'backend_layout';
32
33 /**
34 * Adds backend layouts to the given backend layout collection.
35 * The default backend layout ('default_default') is not added
36 * since it's the default fallback if nothing is specified.
37 *
38 * @param DataProviderContext $dataProviderContext
39 * @param BackendLayoutCollection $backendLayoutCollection
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', $queryBuilder->createNamedParameter($identifier, \PDO::PARAM_INT)))
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(
157 $queryBuilder->createNamedParameter($pageTsConfigId[$fieldName], \PDO::PARAM_INT),
158 Comparison::EQ,
159 $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
160 ),
161 $queryBuilder->expr()->comparison(
162 $queryBuilder->createNamedParameter($storagePid, \PDO::PARAM_INT),
163 Comparison::EQ,
164 $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
165 )
166 ),
167 $queryBuilder->expr()->orX(
168 $queryBuilder->expr()->eq(
169 'backend_layout.pid',
170 $queryBuilder->createNamedParameter($pageTsConfigId[$fieldName], \PDO::PARAM_INT)
171 ),
172 $queryBuilder->expr()->eq(
173 'backend_layout.pid',
174 $queryBuilder->createNamedParameter($storagePid, \PDO::PARAM_INT)
175 )
176 ),
177 $queryBuilder->expr()->andX(
178 $queryBuilder->expr()->comparison(
179 $queryBuilder->createNamedParameter($pageTsConfigId[$fieldName], \PDO::PARAM_INT),
180 Comparison::EQ,
181 $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
182 ),
183 $queryBuilder->expr()->eq(
184 'backend_layout.pid',
185 $queryBuilder->createNamedParameter($pageUid, \PDO::PARAM_INT)
186 )
187 )
188 )
189 );
190
191 if (!empty($GLOBALS['TCA'][$this->tableName]['ctrl']['sortby'])) {
192 $queryBuilder->orderBy($GLOBALS['TCA'][$this->tableName]['ctrl']['sortby']);
193 }
194
195 $results = $queryBuilder
196 ->execute()
197 ->fetchAll();
198
199 return $results;
200 }
201
202 /**
203 * Returns the storage PID from TCEFORM.
204 *
205 * @param array $pageTsConfig
206 * @return int
207 */
208 protected function getStoragePid(array $pageTsConfig)
209 {
210 $storagePid = 0;
211
212 if (!empty($pageTsConfig['TCEFORM.']['pages.']['_STORAGE_PID'])) {
213 $storagePid = (int)$pageTsConfig['TCEFORM.']['pages.']['_STORAGE_PID'];
214 }
215
216 return $storagePid;
217 }
218
219 /**
220 * Returns the page TSconfig from TCEFORM.
221 *
222 * @param array $pageTsConfig
223 * @return array
224 */
225 protected function getPageTSconfigIds(array $pageTsConfig)
226 {
227 $pageTsConfigIds = [
228 'backend_layout' => 0,
229 'backend_layout_next_level' => 0,
230 ];
231
232 if (!empty($pageTsConfig['TCEFORM.']['pages.']['backend_layout.']['PAGE_TSCONFIG_ID'])) {
233 $pageTsConfigIds['backend_layout'] = (int)$pageTsConfig['TCEFORM.']['pages.']['backend_layout.']['PAGE_TSCONFIG_ID'];
234 }
235
236 if (!empty($pageTsConfig['TCEFORM.']['pages.']['backend_layout_next_level.']['PAGE_TSCONFIG_ID'])) {
237 $pageTsConfigIds['backend_layout_next_level'] = (int)$pageTsConfig['TCEFORM.']['pages.']['backend_layout_next_level.']['PAGE_TSCONFIG_ID'];
238 }
239
240 return $pageTsConfigIds;
241 }
242 }