[BUGFIX] Show icons of backend layouts
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / View / BackendLayout / DefaultDataProvider.php
1 <?php
2 namespace TYPO3\CMS\Backend\View\BackendLayout;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2008-2013 Jo Hasenau <info@cybercraft.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Backend\Utility\BackendUtility;
31
32 /**
33 * Backend layout data provider class
34 *
35 * @author Jo Hasenau <info@cybercraft.de>
36 * @author Oliver Hader <oliver.hader@typo3.org>
37 */
38 class DefaultDataProvider implements DataProviderInterface {
39
40 /**
41 * Adds backend layouts to the given backend layout collection.
42 * The default backend layout ('default_default') is not added
43 * since it's the default fallback if nothing is specified.
44 *
45 * @param DataProviderContext $dataProviderContext
46 * @param BackendLayoutCollection $backendLayoutCollection
47 * @return void
48 */
49 public function addBackendLayouts(DataProviderContext $dataProviderContext, BackendLayoutCollection $backendLayoutCollection) {
50 $layoutData = $this->getLayoutData(
51 $dataProviderContext->getFieldName(),
52 $dataProviderContext->getPageTsConfig(),
53 $dataProviderContext->getPageId()
54 );
55
56 foreach ($layoutData as $data) {
57 $backendLayout = $this->createBackendLayout($data);
58 $backendLayoutCollection->add($backendLayout);
59 }
60 }
61
62 /**
63 * Gets a backend layout by (regular) identifier.
64 *
65 * @param string $identifier
66 * @return NULL|BackendLayout
67 */
68 public function getBackendLayout($identifier) {
69 $backendLayout = NULL;
70
71 if ((string) $identifier === 'default') {
72 return $this->createDefaultBackendLayout();
73 }
74
75 $data = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
76 '*',
77 'backend_layout',
78 'uid=' . intval($identifier) . BackendUtility::BEenableFields('backend_layout') . BackendUtility::deleteClause('backend_layout')
79 );
80 if (is_array($data)) {
81 $backendLayout = $this->createBackendLayout($data);
82 }
83
84 return $backendLayout;
85 }
86
87 /**
88 * Creates a backend layout with the default configuration.
89 *
90 * @return BackendLayout
91 */
92 protected function createDefaultBackendLayout() {
93 return BackendLayout::create(
94 'default',
95 'LLL:EXT:cms/locallang_tca.xlf:pages.backend_layout.default',
96 \TYPO3\CMS\Backend\View\BackendLayoutView::getDefaultColumnLayout()
97 );
98 }
99
100 /**
101 * Creates a new backend layout using the given record data.
102 *
103 * @param array $data
104 * @return BackendLayout
105 */
106 protected function createBackendLayout(array $data) {
107 $backendLayout = BackendLayout::create($data['uid'], $data['title'], $data['config']);
108 $backendLayout->setIconPath($this->getIconPath($data['icon']));
109 $backendLayout->setData($data);
110 return $backendLayout;
111 }
112
113 /**
114 * Gets and sanitizes the icon path.
115 *
116 * @param string $icon Name of the icon file
117 * @return string
118 */
119 protected function getIconPath($icon) {
120 $iconPath = '';
121
122 if (!empty($icon)) {
123 $path = rtrim($GLOBALS['TCA']['backend_layout']['ctrl']['selicon_field_path'], '/') . '/';
124 $iconPath = '../' . $path . $icon;
125 }
126
127 return $iconPath;
128 }
129
130 /**
131 * Get all layouts from the core's default data provider.
132 *
133 * @param string $fieldName the name of the field the layouts are provided for (either backend_layout or backend_layout_next_level)
134 * @param array $pageTsConfig PageTSconfig of the given page
135 * @param integer $pageUid the ID of the page wea re getting the layouts for
136 * @return array $layouts A collection of layout data of the registered provider
137 */
138 protected function getLayoutData($fieldName, array $pageTsConfig, $pageUid) {
139 $storagePid = $this->getStoragePid($pageTsConfig);
140 $pageTsConfigId = $this->getPageTSconfigIds($pageTsConfig);
141
142 // Add layout records
143 $results = $this->getDatabaseConnection()->exec_SELECTgetRows(
144 '*',
145 'backend_layout',
146 '(
147 ( ' . intval($pageTsConfigId[$fieldName]) . ' = 0 AND ' . intval($storagePid) . ' = 0 )
148 OR ( backend_layout.pid = ' . intval($pageTsConfigId[$fieldName]) . ' OR backend_layout.pid = ' . intval($storagePid) . ' )
149 OR ( ' . intval($pageTsConfigId[$fieldName]) . ' = 0 AND backend_layout.pid = ' . intval($pageUid) . ' )
150 ) ' . BackendUtility::BEenableFields('backend_layout') . BackendUtility::deleteClause('backend_layout'),
151 '',
152 'sorting ASC'
153 );
154
155 if (!is_array($results)) {
156 $results = array();
157 }
158
159 return $results;
160 }
161
162 /**
163 * Returns the storage PID from TCEFORM.
164 *
165 * @param array $pageTsConfig
166 * @return integer
167 */
168 protected function getStoragePid(array $pageTsConfig) {
169 $storagePid = 0;
170
171 if (!empty($pageTsConfig['TCEFORM.']['pages.']['_STORAGE_PID'])) {
172 $storagePid = (int) $pageTsConfig['TCEFORM.']['pages.']['_STORAGE_PID'];
173 }
174
175 return $storagePid;
176 }
177
178 /**
179 * Returns the page TSconfig from TCEFORM.
180 *
181 * @param array $pageTsConfig
182 * @return array
183 */
184 protected function getPageTSconfigIds(array $pageTsConfig) {
185 $pageTsConfigIds = array(
186 'backend_layout' => 0,
187 'backend_layout_next_level' => 0,
188 );
189
190 if (!empty($pageTsConfig['TCEFORM.']['pages.']['backend_layout.']['PAGE_TSCONFIG_ID'])) {
191 $pageTsConfigIds['backend_layout'] = (int) $pageTsConfig['TCEFORM.']['pages.']['backend_layout.']['PAGE_TSCONFIG_ID'];
192 }
193
194 if (!empty($pageTsConfig['TCEFORM.']['pages.']['backend_layout_next_level.']['PAGE_TSCONFIG_ID'])) {
195 $pageTsConfigIds['backend_layout_next_level'] = (int) $pageTsConfig['TCEFORM.']['pages.']['backend_layout_next_level.']['PAGE_TSCONFIG_ID'];
196 }
197
198 return $pageTsConfigIds;
199 }
200
201 /**
202 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
203 */
204 protected function getDatabaseConnection() {
205 return $GLOBALS['TYPO3_DB'];
206 }
207
208 }