cc764005e340df6669206555b1e8eb51044c68ae
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / WorkspacesNotificationSettingsUpdate.php
1 <?php
2 namespace TYPO3\CMS\Install\Updates;
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\Core\Utility\ExtensionManagementUtility;
18
19 /**
20 * Migrate the workspaces notification settings to the enhanced schema.
21 */
22 class WorkspacesNotificationSettingsUpdate extends AbstractUpdate
23 {
24 /**
25 * @var string
26 */
27 protected $title = 'Migrate the workspaces notification settings to the enhanced schema';
28
29 /**
30 * Checks if an update is needed
31 *
32 * @param string &$description The description for the update
33 * @return bool Whether an update is needed (TRUE) or not (FALSE)
34 */
35 public function checkForUpdate(&$description)
36 {
37 if (!ExtensionManagementUtility::isLoaded('workspaces')) {
38 return false;
39 }
40
41 if ($this->isWizardDone()) {
42 return false;
43 }
44
45 $workspacesCount = $this->getDatabaseConnection()->exec_SELECTcountRows(
46 'uid',
47 'sys_workspace',
48 'deleted=0'
49 );
50
51 $stagesCount = $this->getDatabaseConnection()->exec_SELECTcountRows(
52 'uid',
53 'sys_workspace_stage',
54 'deleted=0'
55 );
56
57 if ($workspacesCount + $stagesCount > 0) {
58 $description = 'The workspaces notification settings have been extended'
59 . ' and need to be migrated to the new definitions. This update wizard'
60 . ' upgrades the accordant settings in the availble workspaces and stages.';
61 return true;
62 } else {
63 $this->markWizardAsDone();
64 }
65
66 return false;
67 }
68
69 /**
70 * Perform the database updates for workspace records
71 *
72 * @param array &$databaseQueries Queries done in this update
73 * @param mixed &$customMessages Custom messages
74 * @return bool
75 */
76 public function performUpdate(array &$databaseQueries, &$customMessages)
77 {
78 $databaseConnection = $this->getDatabaseConnection();
79
80 $workspaceRecords = $databaseConnection->exec_SELECTgetRows('*', 'sys_workspace', 'deleted=0');
81 foreach ($workspaceRecords as $workspaceRecord) {
82 $update = $this->prepareWorkspaceUpdate($workspaceRecord);
83 if ($update !== null) {
84 $databaseConnection->exec_UPDATEquery('sys_workspace', 'uid=' . (int)$workspaceRecord['uid'], $update);
85 $databaseQueries[] = $databaseConnection->debug_lastBuiltQuery;
86 }
87 }
88
89 $stageRecords = $databaseConnection->exec_SELECTgetRows('*', 'sys_workspace_stage', 'deleted=0');
90 foreach ($stageRecords as $stageRecord) {
91 $update = $this->prepareStageUpdate($stageRecord);
92 if ($update !== null) {
93 $databaseConnection->exec_UPDATEquery('sys_workspace_stage', 'uid=' . (int)$stageRecord['uid'], $update);
94 $databaseQueries[] = $databaseConnection->debug_lastBuiltQuery;
95 }
96 }
97
98 $this->markWizardAsDone();
99 return true;
100 }
101
102 /**
103 * Prepares SQL updates for workspace records.
104 *
105 * @param array $workspaceRecord
106 * @return array|NULL
107 */
108 protected function prepareWorkspaceUpdate(array $workspaceRecord)
109 {
110 if (empty($workspaceRecord['uid'])) {
111 return null;
112 }
113
114 $update = array();
115 $update = $this->mapSettings($workspaceRecord, $update, 'edit', 'edit');
116 $update = $this->mapSettings($workspaceRecord, $update, 'publish', 'publish');
117 $update = $this->mapSettings($workspaceRecord, $update, 'publish', 'execute');
118 return $update;
119 }
120
121 /**
122 * Prepares SQL update for stage records.
123 *
124 * @param array $stageRecord
125 * @return array|null
126 */
127 protected function prepareStageUpdate(array $stageRecord)
128 {
129 if (empty($stageRecord['uid'])) {
130 return null;
131 }
132
133 $update = array();
134 $update = $this->mapSettings($stageRecord, $update);
135 return $update;
136 }
137
138 /**
139 * Maps settings to new meaning.
140 *
141 * @param array $record
142 * @param array $update
143 * @param string $from
144 * @param string $to
145 * @return array
146 */
147 protected function mapSettings(array $record, array $update, $from = '', $to = '')
148 {
149 $fromPrefix = ($from ? $from . '_' : '');
150 $toPrefix = ($to ? $to . '_' : '');
151
152 $settings = 0;
153 // Previous setting: "Allow notification settings during stage change"
154 if ($record[$fromPrefix . 'allow_notificaton_settings']) {
155 $settings += 1;
156 }
157 // Previous setting: "All are selected per default (can be changed)"
158 if ((int)$record[$fromPrefix . 'notification_mode'] === 0) {
159 $settings += 2;
160 }
161
162 // Custom stages: preselect responsible persons (8)
163 if (isset($record['responsible_persons'])) {
164 $preselection = 8;
165 // Workspace "edit" stage: preselect members (2)
166 } elseif ($to === 'edit') {
167 $preselection = 2;
168 // Workspace "publish" stage: preselect owners (1)
169 } elseif ($to === 'publish') {
170 $preselection = 1;
171 // Workspace "execute" stage: preselect owners (1) and members (2) as default
172 } else {
173 $preselection = 1 + 2;
174 }
175
176 $update[$toPrefix . 'allow_notificaton_settings'] = $settings;
177 $update[$toPrefix . 'notification_preselection'] = $preselection;
178
179 return $update;
180 }
181 }