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