[FEATURE] Conditions support in backend layouts
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / View / BackendLayoutView.php
1 <?php
2 namespace TYPO3\CMS\Backend\View;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 GridView Team
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 use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33 /**
34 * Backend layout for CMS
35 *
36 * @author GridView Team
37 */
38 class BackendLayoutView {
39
40 /**
41 * ItemProcFunc for colpos items
42 *
43 * @param array $params
44 * @return void
45 */
46 public function colPosListItemProcFunc(&$params) {
47 if ($params['row']['pid'] > 0) {
48 $params['items'] = $this->addColPosListLayoutItems($params['row']['pid'], $params['items']);
49 } else {
50 // Negative uid_pid values indicate that the element has been inserted after an existing element
51 // so there is no pid to get the backendLayout for and we have to get that first
52 $existingElement = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('pid', 'tt_content', 'uid=' . -intval($params['row']['pid']));
53 if ($existingElement['pid'] > 0) {
54 $params['items'] = $this->addColPosListLayoutItems($existingElement['pid'], $params['items']);
55 }
56 }
57 }
58
59 /**
60 * Adds items to a colpos list
61 *
62 * @param integer $pageId
63 * @param array $items
64 * @return array
65 */
66 protected function addColPosListLayoutItems($pageId, $items) {
67 $layout = $this->getSelectedBackendLayout($pageId);
68 if ($layout && $layout['__items']) {
69 $items = $layout['__items'];
70 }
71 return $items;
72 }
73
74 /**
75 * Gets the list of available columns for a given page id
76 *
77 * @param integer $id
78 * @return array $tcaItems
79 */
80 public function getColPosListItemsParsed($id) {
81 $tsConfig = BackendUtility::getModTSconfig($id, 'TCEFORM.tt_content.colPos');
82 $tcaConfig = $GLOBALS['TCA']['tt_content']['columns']['colPos']['config'];
83 /** @var $tceForms \TYPO3\CMS\Backend\Form\FormEngine */
84 $tceForms = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Form\\FormEngine');
85 $tcaItems = $tcaConfig['items'];
86 $tcaItems = $tceForms->addItems($tcaItems, $tsConfig['properties']['addItems.']);
87 if (isset($tcaConfig['itemsProcFunc']) && $tcaConfig['itemsProcFunc']) {
88 $tcaItems = $this->addColPosListLayoutItems($id, $tcaItems);
89 }
90 foreach (GeneralUtility::trimExplode(',', $tsConfig['properties']['removeItems'], TRUE) as $removeId) {
91 foreach ($tcaItems as $key => $item) {
92 if ($item[1] == $removeId) {
93 unset($tcaItems[$key]);
94 }
95 }
96 }
97 return $tcaItems;
98 }
99
100 /**
101 * Gets the selected backend layout
102 *
103 * @param integer $id
104 * @return array|NULL $backendLayout
105 */
106 public function getSelectedBackendLayout($id) {
107 $rootline = BackendUtility::BEgetRootLine($id);
108 $backendLayoutUid = NULL;
109 for ($i = count($rootline); $i > 0; $i--) {
110 $page = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('uid, pid, backend_layout, backend_layout_next_level', 'pages', 'uid=' . intval($rootline[$i]['uid']));
111 BackendUtility::workspaceOL('pages', $page);
112 $selectedBackendLayout = intval($page['backend_layout']);
113 $selectedBackendLayoutNextLevel = intval($page['backend_layout_next_level']);
114 if ($selectedBackendLayout != 0 && $page['uid'] == $id) {
115 if ($selectedBackendLayout > 0) {
116 // Backend layout for current page is set
117 $backendLayoutUid = $selectedBackendLayout;
118 }
119 break;
120 } elseif ($selectedBackendLayoutNextLevel == -1 && $page['uid'] != $id) {
121 // Some previous page in our rootline sets layout_next to "None"
122 break;
123 } elseif ($selectedBackendLayoutNextLevel > 0 && $page['uid'] != $id) {
124 // Some previous page in our rootline sets some backend_layout, use it
125 $backendLayoutUid = $selectedBackendLayoutNextLevel;
126 break;
127 }
128 }
129 $backendLayout = NULL;
130 if ($backendLayoutUid) {
131 $backendLayout = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('*', 'backend_layout', 'uid=' . $backendLayoutUid);
132 } else {
133 $backendLayout['config'] = self::getDefaultColumnLayout();
134 }
135 if ($backendLayout) {
136 /** @var $parser \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser */
137 $parser = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\Parser\\TypoScriptParser');
138 /** @var \TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher $conditionMatcher */
139 $conditionMatcher = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Configuration\\TypoScript\\ConditionMatching\\ConditionMatcher');
140 $parser->parse($parser->checkIncludeLines($backendLayout['config']), $conditionMatcher);
141 $backendLayout['__config'] = $parser->setup;
142 $backendLayout['__items'] = array();
143 $backendLayout['__colPosList'] = array();
144 // create items and colPosList
145 if ($backendLayout['__config']['backend_layout.'] && $backendLayout['__config']['backend_layout.']['rows.']) {
146 foreach ($backendLayout['__config']['backend_layout.']['rows.'] as $row) {
147 if (isset($row['columns.']) && is_array($row['columns.'])) {
148 foreach ($row['columns.'] as $column) {
149 $backendLayout['__items'][] = array(
150 GeneralUtility::isFirstPartOfStr($column['name'], 'LLL:') ? $GLOBALS['LANG']->sL($column['name']) : $column['name'],
151 $column['colPos'],
152 NULL
153 );
154 $backendLayout['__colPosList'][] = $column['colPos'];
155 }
156 }
157 }
158 }
159 }
160 return $backendLayout;
161 }
162
163 /**
164 * Get default columns layout
165 *
166 * @return string Default four column layout
167 * @static
168 */
169 static public function getDefaultColumnLayout() {
170 return '
171 backend_layout {
172 colCount = 4
173 rowCount = 1
174 rows {
175 1 {
176 columns {
177 1 {
178 name = LLL:EXT:cms/locallang_ttc.xlf:colPos.I.0
179 colPos = 1
180 }
181 2 {
182 name = LLL:EXT:cms/locallang_ttc.xlf:colPos.I.1
183 colPos = 0
184 }
185 3 {
186 name = LLL:EXT:cms/locallang_ttc.xlf:colPos.I.2
187 colPos = 2
188 }
189 4 {
190 name = LLL:EXT:cms/locallang_ttc.xlf:colPos.I.3
191 colPos = 3
192 }
193 }
194 }
195 }
196 }
197 ';
198 }
199
200 }
201
202
203 ?>