[CLEANUP] Alwas put null at the last position
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Domain / Record / WorkspaceRecord.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\Core\Database\ConnectionPool;
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Workspaces\Service\StagesService;
19
20 /**
21 * Combined record class
22 */
23 class WorkspaceRecord extends AbstractRecord
24 {
25 /**
26 * @var array
27 */
28 protected $internalStages = [
29 StagesService::STAGE_EDIT_ID => [
30 'name' => 'edit',
31 'label' => 'LLL:EXT:lang/Resources/Private/Language/locallang_mod_user_ws.xlf:stage_editing'
32 ],
33 StagesService::STAGE_PUBLISH_ID => [
34 'name' => 'publish',
35 'label' => 'LLL:EXT:workspaces/Resources/Private/Language/locallang_mod.xlf:stage_ready_to_publish'
36 ],
37 StagesService::STAGE_PUBLISH_EXECUTE_ID => [
38 'name' => 'execute',
39 'label' => 'LLL:EXT:lang/Resources/Private/Language/locallang_mod_user_ws.xlf:stage_publish'
40 ],
41 ];
42
43 /**
44 * @var array
45 */
46 protected $internalStageFieldNames = [
47 'notification_defaults',
48 'notification_preselection',
49 'allow_notificaton_settings'
50 ];
51
52 /**
53 * @var array
54 */
55 protected $owners;
56
57 /**
58 * @var array
59 */
60 protected $members;
61
62 /**
63 * @var StageRecord[]
64 */
65 protected $stages;
66
67 /**
68 * @param int $uid
69 * @param array $record
70 * @return WorkspaceRecord
71 */
72 public static function get($uid, array $record = null)
73 {
74 if (empty($uid)) {
75 $record = [];
76 } elseif (empty($record)) {
77 $record = static::fetch('sys_workspace', $uid);
78 }
79 return new static($record);
80 }
81
82 /**
83 * @return array
84 */
85 public function getOwners()
86 {
87 if (!isset($this->owners)) {
88 $this->owners = $this->getStagesService()->resolveBackendUserIds($this->record['adminusers']);
89 }
90 return $this->owners;
91 }
92
93 /**
94 * @return array
95 */
96 public function getMembers()
97 {
98 if (!isset($this->members)) {
99 $this->members = $this->getStagesService()->resolveBackendUserIds($this->record['members']);
100 }
101 return $this->members;
102 }
103
104 /**
105 * @return StageRecord[]
106 */
107 public function getStages()
108 {
109 if (!isset($this->stages)) {
110 $this->stages = [];
111 $this->addStage($this->createInternalStage(StagesService::STAGE_EDIT_ID));
112
113 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
114 ->getQueryBuilderForTable('sys_workspace_stage');
115
116 $result = $queryBuilder
117 ->select('*')
118 ->from('sys_workspace_stage')
119 ->where(
120 $queryBuilder->expr()->eq(
121 'parentid',
122 $queryBuilder->createNamedParameter($this->getUid(), \PDO::PARAM_INT)
123 ),
124 $queryBuilder->expr()->eq(
125 'parenttable',
126 $queryBuilder->createNamedParameter('sys_workspace', \PDO::PARAM_STR)
127 )
128 )
129 ->orderBy('sorting')
130 ->execute();
131
132 while ($record = $result->fetch()) {
133 $this->addStage(StageRecord::build($this, $record['uid'], $record));
134 }
135
136 $this->addStage($this->createInternalStage(StagesService::STAGE_PUBLISH_ID));
137 $this->addStage($this->createInternalStage(StagesService::STAGE_PUBLISH_EXECUTE_ID));
138 }
139
140 return $this->stages;
141 }
142
143 /**
144 * @param int $stageId
145 * @return StageRecord|null
146 */
147 public function getStage($stageId)
148 {
149 $stageId = (int)$stageId;
150 $this->getStages();
151 if (!isset($this->stages[$stageId])) {
152 return null;
153 }
154 return $this->stages[$stageId];
155 }
156
157 /**
158 * @param int $stageId
159 * @return StageRecord|null
160 */
161 public function getPreviousStage($stageId)
162 {
163 $stageId = (int)$stageId;
164 $stageIds = array_keys($this->getStages());
165 $stageIndex = array_search($stageId, $stageIds);
166
167 // catches "0" (edit stage) as well
168 if (empty($stageIndex)) {
169 return null;
170 }
171
172 $previousStageId = $stageIds[$stageIndex - 1];
173 return $this->stages[$previousStageId];
174 }
175
176 /**
177 * @param int $stageId
178 * @return StageRecord|null
179 */
180 public function getNextStage($stageId)
181 {
182 $stageId = (int)$stageId;
183 $stageIds = array_keys($this->getStages());
184 $stageIndex = array_search($stageId, $stageIds);
185
186 if ($stageIndex === false || !isset($stageIds[$stageIndex + 1])) {
187 return null;
188 }
189
190 $nextStageId = $stageIds[$stageIndex + 1];
191 return $this->stages[$nextStageId];
192 }
193
194 /**
195 * @param StageRecord $stage
196 */
197 protected function addStage(StageRecord $stage)
198 {
199 $this->stages[$stage->getUid()] = $stage;
200 }
201
202 /**
203 * @param int $stageId
204 * @return StageRecord
205 * @throws \RuntimeException
206 */
207 protected function createInternalStage($stageId)
208 {
209 $stageId = (int)$stageId;
210
211 if (!isset($this->internalStages[$stageId])) {
212 throw new \RuntimeException('Invalid internal stage "' . $stageId . '"', 1476048246);
213 }
214
215 $record = [
216 'uid' => $stageId,
217 'title' => static::getLanguageService()->sL($this->internalStages[$stageId]['label'])
218 ];
219
220 $fieldNamePrefix = $this->internalStages[$stageId]['name'] . '_';
221 foreach ($this->internalStageFieldNames as $fieldName) {
222 $record[$fieldName] = $this->record[$fieldNamePrefix . $fieldName];
223 }
224
225 $stage = StageRecord::build($this, $stageId, $record);
226 $stage->setInternal(true);
227 return $stage;
228 }
229 }