3acd9a17e94ee21bb91fe225f2cc069190f9a9a0
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Provider / PageTsBackendLayoutDataProvider.php
1 <?php
2 namespace TYPO3\CMS\Backend\Provider;
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\BackendLayout;
19 use TYPO3\CMS\Backend\View\BackendLayout\BackendLayoutCollection;
20 use TYPO3\CMS\Backend\View\BackendLayout\DataProviderContext;
21 use TYPO3\CMS\Backend\View\BackendLayout\DataProviderInterface;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24 /**
25 * This Provider adds Backend Layouts based on PageTsConfig
26 *
27 * = Example =
28 * mod {
29 * web_layout {
30 * BackendLayouts {
31 * example {
32 * title = Example
33 * config {
34 * backend_layout {
35 * colCount = 1
36 * rowCount = 2
37 * rows {
38 * 1 {
39 * columns {
40 * 1 {
41 * name = LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:colPos.I.3
42 * colPos = 3
43 * colspan = 1
44 * }
45 * }
46 * }
47 * 2 {
48 * columns {
49 * 1 {
50 * name = Main
51 * colPos = 0
52 * colspan = 1
53 * }
54 * }
55 * }
56 * }
57 * }
58 * }
59 * icon = EXT:example_extension/Resources/Public/Images/BackendLayouts/default.gif
60 * }
61 * }
62 * }
63 * }
64 *
65 */
66 class PageTsBackendLayoutDataProvider implements DataProviderInterface
67 {
68 /**
69 * Internal Backend Layout stack
70 *
71 * @var array
72 */
73 protected $backendLayouts = array();
74
75 /**
76 * PageTs Config
77 *
78 * @var array
79 */
80 protected $pageTsConfig = array();
81
82 /**
83 * PageId
84 *
85 * @var int
86 */
87 protected $pageId = 0;
88
89 /**
90 * Set PageTsConfig
91 *
92 * @param array $pageTsConfig
93 * @return void
94 */
95 protected function setPageTsConfig(array $pageTsConfig)
96 {
97 $this->pageTsConfig = $pageTsConfig;
98 }
99
100 /**
101 * Get PageTsConfig
102 *
103 * @return array
104 */
105 protected function getPageTsConfig()
106 {
107 return $this->pageTsConfig;
108 }
109
110 /**
111 * Set PageId
112 *
113 * @param int $pageId
114 * @return void
115 */
116 protected function setPageId($pageId)
117 {
118 $this->pageId = (int)$pageId;
119 }
120
121 /**
122 * Get PageId
123 *
124 * @return int
125 */
126 protected function getPageId()
127 {
128 return (int)$this->pageId;
129 }
130
131 /**
132 * Gets PageTsConfig from DataProviderContext if available,
133 * if not it will be generated for the current Page.
134 *
135 * @param DataProviderContext $dataProviderContext
136 * @return void
137 */
138 protected function generatePageTsConfig($dataProviderContext = null)
139 {
140 if ($dataProviderContext === null) {
141 $pageId = $this->getPageId();
142 $pageId = $pageId > 0 ? $pageId : (int)GeneralUtility::_GP('id');
143 $pageTsConfig = BackendUtility::getPagesTSconfig($pageId);
144 } else {
145 $pageTsConfig = $dataProviderContext->getPageTsConfig();
146 }
147 $this->setPageTsConfig($pageTsConfig);
148 }
149
150 /**
151 * Generate the Backend Layout configs
152 *
153 * @param DataProviderContext $dataProviderContext
154 * @return void
155 */
156 protected function generateBackendLayouts($dataProviderContext = null)
157 {
158 $this->generatePageTsConfig($dataProviderContext);
159 $pageTsConfig = $this->getPageTsConfig();
160 if (!empty($pageTsConfig['mod.']['web_layout.']['BackendLayouts.'])) {
161 $backendLayouts = (array)$pageTsConfig['mod.']['web_layout.']['BackendLayouts.'];
162 foreach ($backendLayouts as $identifier => $data) {
163 $backendLayout = $this->generateBackendLayoutFromTsConfig($identifier, $data);
164 $this->attachBackendLayout($backendLayout);
165 }
166 }
167 }
168
169 /**
170 * Generates a Backend Layout from PageTsConfig array
171 *
172 * @return mixed
173 */
174 protected function generateBackendLayoutFromTsConfig($identifier, $data)
175 {
176 if (!empty($data['config.']['backend_layout.']) && is_array($data['config.']['backend_layout.'])) {
177 $backendLayout['uid'] = substr($identifier, 0, -1);
178 $backendLayout['title'] = ($data['title']) ? $data['title'] : $backendLayout['uid'];
179 $backendLayout['icon'] = ($data['icon']) ? $data['icon'] : '';
180 // Convert PHP array back to plain TypoScript so it can be procecced
181 $config = \TYPO3\CMS\Core\Utility\ArrayUtility::flatten($data['config.']);
182 $backendLayout['config'] = '';
183 foreach ($config as $row => $value) {
184 $backendLayout['config'] .= $row . " = " . $value . "\r\n";
185 }
186 return $backendLayout;
187 }
188 return null;
189 }
190
191 /**
192 * Attach Backend Layout to internal Stack
193 *
194 * @param mixed $backendLayout
195 */
196 protected function attachBackendLayout($backendLayout = null)
197 {
198 if ($backendLayout) {
199 $this->backendLayouts[$backendLayout['uid']] = $backendLayout;
200 }
201 }
202
203 /**
204 * @param DataProviderContext $dataProviderContext
205 * @param BackendLayoutCollection $backendLayoutCollection
206 * @return void
207 */
208 public function addBackendLayouts(DataProviderContext $dataProviderContext, BackendLayoutCollection $backendLayoutCollection)
209 {
210 $this->generateBackendLayouts($dataProviderContext);
211 foreach ($this->backendLayouts as $backendLayoutConfig) {
212 $backendLayout = $this->createBackendLayout($backendLayoutConfig);
213 $backendLayoutCollection->add($backendLayout);
214 }
215 }
216
217 /**
218 * Gets a backend layout by (regular) identifier.
219 *
220 * @param string $identifier
221 * @param int $pageId
222 * @return NULL|BackendLayout
223 */
224 public function getBackendLayout($identifier, $pageId)
225 {
226 $this->setPageId($pageId);
227 $this->generateBackendLayouts();
228 $backendLayout = null;
229 if (array_key_exists($identifier, $this->backendLayouts)) {
230 return $this->createBackendLayout($this->backendLayouts[$identifier]);
231 }
232 return $backendLayout;
233 }
234
235 /**
236 * Creates a new backend layout using the given record data.
237 *
238 * @param array $data
239 * @return BackendLayout
240 */
241 protected function createBackendLayout(array $data)
242 {
243 $backendLayout = BackendLayout::create($data['uid'], $data['title'], $data['config']);
244 $backendLayout->setIconPath($this->getIconPath($data['icon']));
245 $backendLayout->setData($data);
246 return $backendLayout;
247 }
248
249 /**
250 * Gets and sanitizes the icon path.
251 *
252 * @param string $icon Name of the icon file
253 * @return string
254 */
255 protected function getIconPath($icon)
256 {
257 $iconPath = '';
258 if (!empty($icon)) {
259 $iconPath = $icon;
260 }
261 return $iconPath;
262 }
263 }