[BUGFIX] Consider default recipients in preselection check
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Domain / Record / StageRecord.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\Domain\Record;
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 use TYPO3\CMS\Workspaces\Service\StagesService;
17
18 /**
19 * Combined record class
20 */
21 class StageRecord extends AbstractRecord
22 {
23 /**
24 * @var WorkspaceRecord
25 */
26 protected $workspace;
27
28 /**
29 * @var bool
30 */
31 protected $internal = false;
32
33 /**
34 * @var array
35 */
36 protected $responsiblePersons;
37
38 /**
39 * @var array
40 */
41 protected $defaultRecipients;
42
43 /**
44 * @var array
45 */
46 protected $preselectedRecipients;
47
48 /**
49 * @var array
50 */
51 protected $allRecipients;
52
53 /**
54 * @param int $uid
55 * @param array $record
56 * @return StageRecord
57 */
58 public static function get($uid, array $record = null)
59 {
60 if (empty($record)) {
61 $record = static::fetch('sys_workspace_stage', $uid);
62 }
63 return WorkspaceRecord::get($record['parentid'])->getStage($uid);
64 }
65
66 /**
67 * @param WorkspaceRecord $workspace
68 * @param int $uid
69 * @param array $record
70 * @return StageRecord
71 */
72 public static function build(WorkspaceRecord $workspace, $uid, array $record = null)
73 {
74 if (empty($record)) {
75 $record = static::fetch('sys_workspace_stage', $uid);
76 }
77 return new self($workspace, $record);
78 }
79
80 /**
81 * @param WorkspaceRecord $workspace
82 * @param array $record
83 */
84 public function __construct(WorkspaceRecord $workspace, array $record)
85 {
86 parent::__construct($record);
87 $this->workspace = $workspace;
88 }
89
90 /**
91 * @return WorkspaceRecord
92 */
93 public function getWorkspace()
94 {
95 return $this->workspace;
96 }
97
98 /**
99 * @return StageRecord|null
100 */
101 public function getPrevious()
102 {
103 return $this->getWorkspace()->getPreviousStage($this->getUid());
104 }
105
106 /**
107 * @return StageRecord|null
108 */
109 public function getNext()
110 {
111 return $this->getWorkspace()->getNextStage($this->getUid());
112 }
113
114 /**
115 * @param StageRecord $stageRecord
116 * @return int
117 */
118 public function determineOrder(StageRecord $stageRecord)
119 {
120 if ($this->getUid() === $stageRecord->getUid()) {
121 return 0;
122 }
123 if ($this->isEditStage() || $stageRecord->isExecuteStage() || $this->isPreviousTo($stageRecord)) {
124 return -1;
125 }
126 if ($this->isExecuteStage() || $stageRecord->isEditStage() || $this->isNextTo($stageRecord)) {
127 return 1;
128 }
129 return 0;
130 }
131
132 /**
133 * Determines whether $this is in a previous stage compared to $stageRecord.
134 *
135 * @param StageRecord $stageRecord
136 * @return bool
137 */
138 public function isPreviousTo(StageRecord $stageRecord)
139 {
140 $current = $stageRecord;
141 while ($previous = $current->getPrevious()) {
142 if ($this->getUid() === $previous->getUid()) {
143 return true;
144 }
145 $current = $previous;
146 }
147 return false;
148 }
149
150 /**
151 * Determines whether $this is in a later stage compared to $stageRecord.
152 *
153 * @param StageRecord $stageRecord
154 * @return bool
155 */
156 public function isNextTo(StageRecord $stageRecord)
157 {
158 $current = $stageRecord;
159 while ($next = $current->getNext()) {
160 if ($this->getUid() === $next->getUid()) {
161 return true;
162 }
163 $current = $next;
164 }
165 return false;
166 }
167
168 /**
169 * @return string
170 */
171 public function getDefaultComment()
172 {
173 $defaultComment = '';
174 if (isset($this->record['default_mailcomment'])) {
175 $defaultComment = $this->record['default_mailcomment'];
176 }
177 return $defaultComment;
178 }
179
180 /**
181 * @param bool $internal
182 */
183 public function setInternal($internal = true)
184 {
185 $this->internal = (bool)$internal;
186 }
187
188 /**
189 * @return bool
190 */
191 public function isInternal()
192 {
193 return $this->internal;
194 }
195
196 /**
197 * @return bool
198 */
199 public function isEditStage()
200 {
201 return $this->getUid() === StagesService::STAGE_EDIT_ID;
202 }
203
204 /**
205 * @return bool
206 */
207 public function isPublishStage()
208 {
209 return $this->getUid() === StagesService::STAGE_PUBLISH_ID;
210 }
211
212 /**
213 * @return bool
214 */
215 public function isExecuteStage()
216 {
217 return $this->getUid() === StagesService::STAGE_PUBLISH_EXECUTE_ID;
218 }
219
220 /**
221 * @return bool
222 */
223 public function isDialogEnabled()
224 {
225 return ((int)$this->record['allow_notificaton_settings'] & 1) > 0;
226 }
227
228 /**
229 * @return bool
230 */
231 public function isPreselectionChangeable()
232 {
233 return ((int)$this->record['allow_notificaton_settings'] & 2) > 0;
234 }
235
236 /**
237 * @return bool
238 */
239 public function areOwnersPreselected()
240 {
241 return ((int)$this->record['notification_preselection'] & 1) > 0;
242 }
243
244 /**
245 * @return bool
246 */
247 public function areMembersPreselected()
248 {
249 return ((int)$this->record['notification_preselection'] & 2) > 0;
250 }
251
252 /**
253 * @return bool
254 */
255 public function areEditorsPreselected()
256 {
257 return ((int)$this->record['notification_preselection'] & 4) > 0;
258 }
259
260 /**
261 * @return bool
262 */
263 public function areResponsiblePersonsPreselected()
264 {
265 return ((int)$this->record['notification_preselection'] & 8) > 0;
266 }
267
268 /**
269 * @return bool
270 */
271 public function hasDefaultRecipients(): bool
272 {
273 return $this->record['notification_defaults'] !== '';
274 }
275
276 /**
277 * @return bool
278 */
279 public function hasPreselection()
280 {
281 return
282 $this->areOwnersPreselected()
283 || $this->areMembersPreselected()
284 || $this->areEditorsPreselected()
285 || $this->areResponsiblePersonsPreselected()
286 || $this->hasDefaultRecipients()
287 ;
288 }
289
290 /**
291 * @return array
292 */
293 public function getResponsiblePersons()
294 {
295 if (!isset($this->responsiblePersons)) {
296 $this->responsiblePersons = [];
297 if (!empty($this->record['responsible_persons'])) {
298 $this->responsiblePersons = $this->getStagesService()->resolveBackendUserIds($this->record['responsible_persons']);
299 }
300 }
301 return $this->responsiblePersons;
302 }
303
304 /**
305 * @return array
306 */
307 public function getDefaultRecipients()
308 {
309 if (!isset($this->defaultRecipients)) {
310 $this->defaultRecipients = $this->getStagesService()->resolveBackendUserIds($this->record['notification_defaults']);
311 }
312 return $this->defaultRecipients;
313 }
314
315 /**
316 * Gets all recipients (backend user ids).
317 *
318 * @return array
319 */
320 public function getAllRecipients()
321 {
322 if (!isset($this->allRecipients)) {
323 $allRecipients = $this->getDefaultRecipients();
324
325 if ($this->isInternal() || $this->areOwnersPreselected()) {
326 $allRecipients = array_merge($allRecipients, $this->getWorkspace()->getOwners());
327 }
328 if ($this->isInternal() || $this->areMembersPreselected()) {
329 $allRecipients = array_merge($allRecipients, $this->getWorkspace()->getMembers());
330 }
331 if (!$this->isInternal()) {
332 $allRecipients = array_merge($allRecipients, $this->getResponsiblePersons());
333 }
334
335 $this->allRecipients = array_unique($allRecipients);
336 }
337
338 return $this->allRecipients;
339 }
340
341 /**
342 * @return int[]
343 */
344 public function getPreselectedRecipients()
345 {
346 if (!isset($this->preselectedRecipients)) {
347 $preselectedRecipients = $this->getDefaultRecipients();
348
349 if ($this->areOwnersPreselected()) {
350 $preselectedRecipients = array_merge($preselectedRecipients, $this->getWorkspace()->getOwners());
351 }
352 if ($this->areMembersPreselected()) {
353 $preselectedRecipients = array_merge($preselectedRecipients, $this->getWorkspace()->getMembers());
354 }
355 if ($this->areResponsiblePersonsPreselected()) {
356 $preselectedRecipients = array_merge($preselectedRecipients, $this->getResponsiblePersons());
357 }
358
359 $this->preselectedRecipients = array_unique($preselectedRecipients);
360 }
361
362 return $this->preselectedRecipients;
363 }
364
365 /**
366 * @return bool
367 */
368 public function isAllowed()
369 {
370 return
371 $this->isEditStage()
372 || static::getBackendUser()->workspaceCheckStageForCurrent($this->getUid())
373 || $this->isExecuteStage() && static::getBackendUser()->workspacePublishAccess($this->workspace->getUid())
374 ;
375 }
376 }