[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[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 null|StageRecord
100 */
101 public function getPrevious()
102 {
103 return $this->getWorkspace()->getPreviousStage($this->getUid());
104 }
105
106 /**
107 * @return null|StageRecord
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 hasPreselection()
272 {
273 return
274 $this->areOwnersPreselected()
275 || $this->areMembersPreselected()
276 || $this->areEditorsPreselected()
277 || $this->areResponsiblePersonsPreselected()
278 ;
279 }
280
281 /**
282 * @return array
283 */
284 public function getResponsiblePersons()
285 {
286 if (!isset($this->responsiblePersons)) {
287 $this->responsiblePersons = [];
288 if (!empty($this->record['responsible_persons'])) {
289 $this->responsiblePersons = $this->getStagesService()->resolveBackendUserIds($this->record['responsible_persons']);
290 }
291 }
292 return $this->responsiblePersons;
293 }
294
295 /**
296 * @return array
297 */
298 public function getDefaultRecipients()
299 {
300 if (!isset($this->defaultRecipients)) {
301 $this->defaultRecipients = $this->getStagesService()->resolveBackendUserIds($this->record['notification_defaults']);
302 }
303 return $this->defaultRecipients;
304 }
305
306 /**
307 * Gets all recipients (backend user ids).
308 *
309 * @return array
310 */
311 public function getAllRecipients()
312 {
313 if (!isset($this->allRecipients)) {
314 $allRecipients = $this->getDefaultRecipients();
315
316 if ($this->isInternal() || $this->areOwnersPreselected()) {
317 $allRecipients = array_merge($allRecipients, $this->getWorkspace()->getOwners());
318 }
319 if ($this->isInternal() || $this->areMembersPreselected()) {
320 $allRecipients = array_merge($allRecipients, $this->getWorkspace()->getMembers());
321 }
322 if (!$this->isInternal()) {
323 $allRecipients = array_merge($allRecipients, $this->getResponsiblePersons());
324 }
325
326 $this->allRecipients = array_unique($allRecipients);
327 }
328
329 return $this->allRecipients;
330 }
331
332 /**
333 * @return int[]
334 */
335 public function getPreselectedRecipients()
336 {
337 if (!isset($this->preselectedRecipients)) {
338 $preselectedRecipients = $this->getDefaultRecipients();
339
340 if ($this->areOwnersPreselected()) {
341 $preselectedRecipients = array_merge($preselectedRecipients, $this->getWorkspace()->getOwners());
342 }
343 if ($this->areMembersPreselected()) {
344 $preselectedRecipients = array_merge($preselectedRecipients, $this->getWorkspace()->getMembers());
345 }
346 if ($this->areResponsiblePersonsPreselected()) {
347 $preselectedRecipients = array_merge($preselectedRecipients, $this->getResponsiblePersons());
348 }
349
350 $this->preselectedRecipients = array_unique($preselectedRecipients);
351 }
352
353 return $this->preselectedRecipients;
354 }
355
356 /**
357 * @return bool
358 */
359 public function isAllowed()
360 {
361 return
362 $this->isEditStage()
363 || static::getBackendUser()->workspaceCheckStageForCurrent($this->getUid())
364 || $this->isExecuteStage() && static::getBackendUser()->workspacePublishAccess($this->workspace->getUid())
365 ;
366 }
367 }