Cleanup: Updated copyright comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / updates / class.tx_coreupdates_addflexformstoacl.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 Kai Vogel (kai.vogel(at)speedprogs.de)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28
29 /**
30 * Contains the update for group access lists, adds all excludeable FlexForm fields. Used by the update wizard in the install tool.
31 *
32 * @author Kai Vogel <kai.vogel(at)speedprogs.de>
33 */
34 class tx_coreupdates_addflexformstoacl extends Tx_Install_Updates_Base {
35 protected $title = 'Add Excludable FlexForm Fields to Group Access Lists';
36
37 /**
38 * Checks if FlexForm fields are missing in group access lists.
39 *
40 * @param string &$description The description for the update
41 * @return boolean Whether an update is required (true) or not (false)
42 */
43 public function checkForUpdate(&$description) {
44 $description = '
45 <br />TYPO3 4.5 introduced the possibility to exclude FlexForm fields like normal fields in group access control lists (ACL).
46 All excludeable fields will be hidden for non-admins if you do not add them to the ACL of each user group manually or with
47 this update wizard.
48 ';
49
50 // Check access lists
51 if (!$this->getGroupAddFields()) {
52 return FALSE;
53 }
54
55 return TRUE;
56 }
57
58 /**
59 * Get user confirmation
60 *
61 * @param string $inputPrefix The input prefix, all names of form fields have to start with this
62 * @return string HTML output
63 */
64 public function getUserInput($inputPrefix) {
65 $description = '
66 <br />You are about to update group access control lists to include excludable FlexForm fields. Each backend group will be checked
67 and only those that already have entries in the access control lists will be updated.
68 ';
69
70 return $description;
71 }
72
73 /**
74 * Performs the action of the UpdateManager
75 *
76 * @param array &$dbQueries Queries done in this update
77 * @param mixed &$customMessages Custom messages
78 * @return boolean Whether update was successful or not
79 */
80 public function performUpdate(array &$dbQueries, &$customMessages) {
81 // Get additional FlexForm fields for group access lists
82 $addFields = $this->getGroupAddFields();
83 if (empty($addFields)) {
84 $customMessages = 'No missing FlexForm fields found!';
85 return FALSE;
86 }
87
88 return $this->updateGroupAccessLists($addFields, $dbQueries, $customMessages);
89 }
90
91 /**
92 * Get all FlexForm fields which must be added to group access lists
93 *
94 * @return array Additional FlexForm fields for ACL
95 */
96 protected function getGroupAddFields() {
97 $addFields = array();
98 $contentTable = (!empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['contentTable']) ? $GLOBALS['TYPO3_CONF_VARS']['SYS']['contentTable'] : 'tt_content');
99
100 // Initialize TCA if not loaded yet
101 if (empty($GLOBALS['TCA'])) {
102 $this->pObj->includeTCA();
103 }
104
105 // Get all access lists from groups which are allowed to select or modify the content-table
106 $search = $GLOBALS['TYPO3_DB']->escapeStrForLike($contentTable, 'be_groups');
107 $where = 'deleted = 0 AND non_exclude_fields IS NOT NULL AND non_exclude_fields != ""';
108 $where .= ' AND (tables_select LIKE "%' . $search . '%" OR tables_modify LIKE "%' . $search . '%")';
109 $accessLists = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid, non_exclude_fields', 'be_groups', $where);
110 if(empty($accessLists)) {
111 return array();
112 }
113
114 // Get all excludeable FlexForm fields from content-table
115 $flexExcludeFields = array();
116 $flexFormArray = t3lib_BEfunc::getRegisteredFlexForms($contentTable);
117 if (!empty($flexFormArray) && is_array($flexFormArray)) {
118 foreach ($flexFormArray as $tableField => $flexForms) {
119 // Get all sheets
120 foreach ($flexForms as $flexFormIdentifier => $flexFormConfig) {
121 // Get all excludeable fields in sheet
122 foreach ($flexFormConfig['ds']['sheets'] as $sheetName => $sheet) {
123 if (empty($sheet['ROOT']['el']) || !is_array($sheet['ROOT']['el'])) {
124 continue;
125 }
126 foreach ($sheet['ROOT']['el'] as $fieldName => $field) {
127 if (empty($field['TCEforms']['exclude'])) {
128 continue;
129 }
130 $flexExcludeFields[] = $contentTable . ':' . $tableField . ';' . $flexFormIdentifier . ';' . $sheetName . ';' . $fieldName;
131 }
132 }
133 }
134 }
135 }
136 if (empty($flexExcludeFields)) {
137 return array();
138 }
139
140 // Get FlexForm fields from access lists
141 foreach ($accessLists as $accessList) {
142 $nonExcludeFields = t3lib_div::trimExplode(',', $accessList['non_exclude_fields']);
143 if (empty($nonExcludeFields)) {
144 continue;
145 }
146 // Add FlexForm fields only if the field was not already selected by a user
147 $nonExcludeFields = array_diff($flexExcludeFields, $nonExcludeFields);
148 if (!empty($nonExcludeFields) && $nonExcludeFields == $flexExcludeFields) {
149 $addFields[$accessList['uid']] = $nonExcludeFields;
150 }
151 }
152
153 return $addFields;
154 }
155
156 /**
157 * Update Backend user groups, add FlexForm fields to access list
158 *
159 * @param array $addFields All missing FlexForm fields by groups
160 * @param array &$dbQueries Queries done in this update
161 * @param mixed &$customMessages Custom messages
162 * @return boolean Whether update was successful or not
163 */
164 protected function updateGroupAccessLists(array $addFields, array &$dbQueries, &$customMessages) {
165 foreach ($addFields as $groupUID => $flexExcludeFields) {
166 // First get current fields
167 $result = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('non_exclude_fields', 'be_groups', 'uid=' . (int) $groupUID);
168 if (!isset($result['non_exclude_fields'])) {
169 continue;
170 }
171 $nonExcludeFields = $result['non_exclude_fields'];
172
173 // Now add new ones
174 $flexExcludeFields = implode(',', $flexExcludeFields);
175 $nonExcludeFields = trim($nonExcludeFields . ',' . $flexExcludeFields, ', ');
176
177 // Finally override with new fields
178 $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
179 'be_groups',
180 'uid=' . (int) $groupUID,
181 array('non_exclude_fields' => $nonExcludeFields)
182 );
183
184 // Get last executed query
185 $dbQueries[] = str_replace(chr(10), ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
186
187 // Check for errors
188 if ($GLOBALS['TYPO3_DB']->sql_error()) {
189 $customMessages = 'SQL-ERROR: ' . htmlspecialchars($GLOBALS['TYPO3_DB']->sql_error());
190 return FALSE;
191 }
192 }
193
194 return TRUE;
195 }
196
197 }
198
199 ?>