[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[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\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
19 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * Migrate the workspaces notification settings to the enhanced schema.
24 */
25 class WorkspacesNotificationSettingsUpdate extends AbstractUpdate
26 {
27 /**
28 * @var string
29 */
30 protected $title = 'Migrate the workspaces notification settings to the enhanced schema';
31
32 /**
33 * Checks if an update is needed
34 *
35 * @param string &$description The description for the update
36 * @return bool Whether an update is needed (TRUE) or not (FALSE)
37 */
38 public function checkForUpdate(&$description)
39 {
40 if (!ExtensionManagementUtility::isLoaded('workspaces') || $this->isWizardDone()) {
41 return false;
42 }
43
44 $workspacesCount = GeneralUtility::makeInstance(ConnectionPool::class)
45 ->getConnectionForTable('sys_workspace')
46 ->count(
47 'uid',
48 'sys_workspace',
49 ['deleted' => 0]
50 );
51 $stagesCount = GeneralUtility::makeInstance(ConnectionPool::class)
52 ->getConnectionForTable('sys_workspace_stage')
53 ->count(
54 'uid',
55 'sys_workspace_stage',
56 ['deleted' => 0]
57 );
58 if ($workspacesCount + $stagesCount > 0) {
59 $description = 'The workspaces notification settings have been extended'
60 . ' and need to be migrated to the new definitions. This update wizard'
61 . ' upgrades the accordant settings in the available workspaces and stages.';
62 return true;
63 }
64 $this->markWizardAsDone();
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 string &$customMessage Custom message
74 * @return bool
75 */
76 public function performUpdate(array &$databaseQueries, &$customMessage)
77 {
78 $workspaceConnection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_workspace');
79 $queryBuilder = $workspaceConnection->createQueryBuilder();
80 $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
81 $statement = $queryBuilder->select('*')->from('sys_workspace')->execute();
82 while ($workspaceRecord = $statement->fetch()) {
83 $update = $this->prepareWorkspaceUpdate($workspaceRecord);
84 if ($update !== null) {
85 $queryBuilder = $workspaceConnection->createQueryBuilder();
86 $queryBuilder->update('sys_workspace')
87 ->where(
88 $queryBuilder->expr()->eq(
89 'uid',
90 $queryBuilder->createNamedParameter($workspaceRecord['uid'], \PDO::PARAM_INT)
91 )
92 );
93 foreach ($update as $field => $value) {
94 $queryBuilder->set($field, $value);
95 }
96 $databaseQueries[] = $queryBuilder->getSQL();
97 $queryBuilder->execute();
98 }
99 }
100
101 $stageConnection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_workspace_stage');
102 $queryBuilder = $stageConnection->createQueryBuilder();
103 $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
104 $statement = $queryBuilder->select('*')->from('sys_workspace_stage')->execute();
105 while ($stageRecord = $statement->fetch()) {
106 $update = $this->prepareStageUpdate($stageRecord);
107 if ($update !== null) {
108 $queryBuilder = $workspaceConnection->createQueryBuilder();
109 $queryBuilder->update('sys_workspace_stage')
110 ->where(
111 $queryBuilder->expr()->eq(
112 'uid',
113 $queryBuilder->createNamedParameter($stageRecord['uid'], \PDO::PARAM_INT)
114 )
115 );
116 foreach ($update as $field => $value) {
117 $queryBuilder->set($field, $value);
118 }
119 $databaseQueries[] = $queryBuilder->getSQL();
120 $queryBuilder->execute();
121 }
122 }
123
124 $this->markWizardAsDone();
125 return true;
126 }
127
128 /**
129 * Prepares SQL updates for workspace records.
130 *
131 * @param array $workspaceRecord
132 * @return array|null
133 */
134 protected function prepareWorkspaceUpdate(array $workspaceRecord)
135 {
136 if (empty($workspaceRecord['uid'])) {
137 return null;
138 }
139
140 $update = [];
141 $update = $this->mapSettings($workspaceRecord, $update, 'edit', 'edit');
142 $update = $this->mapSettings($workspaceRecord, $update, 'publish', 'publish');
143 $update = $this->mapSettings($workspaceRecord, $update, 'publish', 'execute');
144 return $update;
145 }
146
147 /**
148 * Prepares SQL update for stage records.
149 *
150 * @param array $stageRecord
151 * @return array|null
152 */
153 protected function prepareStageUpdate(array $stageRecord)
154 {
155 if (empty($stageRecord['uid'])) {
156 return null;
157 }
158
159 $update = [];
160 $update = $this->mapSettings($stageRecord, $update);
161 return $update;
162 }
163
164 /**
165 * Maps settings to new meaning.
166 *
167 * @param array $record
168 * @param array $update
169 * @param string $from
170 * @param string $to
171 * @return array
172 */
173 protected function mapSettings(array $record, array $update, $from = '', $to = '')
174 {
175 $fromPrefix = ($from ? $from . '_' : '');
176 $toPrefix = ($to ? $to . '_' : '');
177
178 $settings = 0;
179 // Previous setting: "Allow notification settings during stage change"
180 if ($record[$fromPrefix . 'allow_notificaton_settings']) {
181 $settings += 1;
182 }
183 // Previous setting: "All are selected per default (can be changed)"
184 if ((int)$record[$fromPrefix . 'notification_mode'] === 0) {
185 $settings += 2;
186 }
187
188 if (isset($record['responsible_persons'])) {
189 // Custom stages: preselect responsible persons (8)
190 $preselection = 8;
191 } elseif ($to === 'edit') {
192 // Workspace "edit" stage: preselect members (2)
193 $preselection = 2;
194 } elseif ($to === 'publish') {
195 // Workspace "publish" stage: preselect owners (1)
196 $preselection = 1;
197 } else {
198 // Workspace "execute" stage: preselect owners (1) and members (2) as default
199 $preselection = 1 + 2;
200 }
201
202 $update[$toPrefix . 'allow_notificaton_settings'] = $settings;
203 $update[$toPrefix . 'notification_preselection'] = $preselection;
204
205 return $update;
206 }
207 }