[TASK] Update php-cs-fixer to 2.5.0
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Service / IntegrityService.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\Service;
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\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Versioning\VersionState;
19
20 /**
21 * Service for integrity
22 */
23 class IntegrityService
24 {
25 /**
26 * Succes status - everything is fine
27 *
28 * @var int
29 */
30 const STATUS_Succes = 100;
31 /**
32 * Info status - nothing is wrong, but a notice is shown
33 *
34 * @var int
35 */
36 const STATUS_Info = 101;
37 /**
38 * Warning status - user interaction might be required
39 *
40 * @var int
41 */
42 const STATUS_Warning = 102;
43 /**
44 * Error status - user interaction is required
45 *
46 * @var int
47 */
48 const STATUS_Error = 103;
49 /**
50 * @var array
51 */
52 protected $statusRepresentation = [
53 self::STATUS_Succes => 'success',
54 self::STATUS_Info => 'info',
55 self::STATUS_Warning => 'warning',
56 self::STATUS_Error => 'error'
57 ];
58
59 /**
60 * @var \TYPO3\CMS\Workspaces\Domain\Model\CombinedRecord[]
61 */
62 protected $affectedElements;
63
64 /**
65 * Array storing all issues that have been checked and
66 * found during runtime in this object. The array keys
67 * are identifiers of table and the version-id.
68 *
69 * 'tx_table:123' => array(
70 * array(
71 * 'status' => 'warning',
72 * 'message' => 'Element cannot be...',
73 * )
74 * )
75 *
76 * @var array
77 */
78 protected $issues = [];
79
80 /**
81 * Sets the affected elements.
82 *
83 * @param \TYPO3\CMS\Workspaces\Domain\Model\CombinedRecord[] $affectedElements
84 */
85 public function setAffectedElements(array $affectedElements)
86 {
87 $this->affectedElements = $affectedElements;
88 }
89
90 /**
91 * Checks integrity of affected records.
92 */
93 public function check()
94 {
95 foreach ($this->affectedElements as $affectedElement) {
96 $this->checkElement($affectedElement);
97 }
98 }
99
100 /**
101 * Checks a single element.
102 *
103 * @param \TYPO3\CMS\Workspaces\Domain\Model\CombinedRecord $element
104 */
105 public function checkElement(\TYPO3\CMS\Workspaces\Domain\Model\CombinedRecord $element)
106 {
107 $this->checkLocalization($element);
108 }
109
110 /**
111 * Checks workspace localization integrity of a single elements.
112 * If current record is a localization and its localization parent
113 * is new in this workspace (has only a placeholder record in live),
114 * then boths (localization and localization parent) should be published.
115 *
116 * @param \TYPO3\CMS\Workspaces\Domain\Model\CombinedRecord $element
117 */
118 protected function checkLocalization(\TYPO3\CMS\Workspaces\Domain\Model\CombinedRecord $element)
119 {
120 $table = $element->getTable();
121 if (BackendUtility::isTableLocalizable($table)) {
122 $languageField = $GLOBALS['TCA'][$table]['ctrl']['languageField'];
123 $languageParentField = $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'];
124 $versionRow = $element->getVersionRecord()->getRow();
125 // If element is a localization:
126 if ($versionRow[$languageField] > 0) {
127 // Get localization parent from live workspace:
128 $languageParentRecord = BackendUtility::getRecord($table, $versionRow[$languageParentField], 'uid,t3ver_state');
129 // If localization parent is a "new placeholder" record:
130 if (VersionState::cast($languageParentRecord['t3ver_state'])->equals(VersionState::NEW_PLACEHOLDER)) {
131 $title = BackendUtility::getRecordTitle($table, $versionRow);
132 // Add warning for current versionized record:
133 $this->addIssue($element->getLiveRecord()->getIdentifier(), self::STATUS_Warning, sprintf(\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('integrity.dependsOnDefaultLanguageRecord', 'workspaces'), $title));
134 // Add info for related localization parent record:
135 $this->addIssue($table . ':' . $languageParentRecord['uid'], self::STATUS_Info, sprintf(\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('integrity.isDefaultLanguageRecord', 'workspaces'), $title));
136 }
137 }
138 }
139 }
140
141 /**
142 * Gets the status of the most important severity.
143 * (low << success, info, warning, error >> high)
144 *
145 * @param string $identifier Record identifier (table:id) for look-ups
146 * @return string
147 */
148 public function getStatus($identifier = null)
149 {
150 $status = self::STATUS_Succes;
151 if ($identifier === null) {
152 foreach ($this->issues as $idenfieriferIssues) {
153 foreach ($idenfieriferIssues as $issue) {
154 if ($status < $issue['status']) {
155 $status = $issue['status'];
156 }
157 }
158 }
159 } else {
160 foreach ($this->getIssues($identifier) as $issue) {
161 if ($status < $issue['status']) {
162 $status = $issue['status'];
163 }
164 }
165 }
166 return $status;
167 }
168
169 /**
170 * Gets the (human readable) represetation of the status with the most
171 * important severity (wraps $this->getStatus() and translates the result).
172 *
173 * @param string $identifier Record identifier (table:id) for look-ups
174 * @return string One out of success, info, warning, error
175 */
176 public function getStatusRepresentation($identifier = null)
177 {
178 return $this->statusRepresentation[$this->getStatus($identifier)];
179 }
180
181 /**
182 * Gets issues, all or specific for one identifier.
183 *
184 * @param string $identifier Record identifier (table:id) for look-ups
185 * @return array
186 */
187 public function getIssues($identifier = null)
188 {
189 if ($identifier === null) {
190 return $this->issues;
191 }
192 if (isset($this->issues[$identifier])) {
193 return $this->issues[$identifier];
194 }
195 return [];
196 }
197
198 /**
199 * Gets the message of all issues.
200 *
201 * @param string $identifier Record identifier (table:id) for look-ups
202 * @param bool $asString Return results as string instead of array
203 * @return array|string
204 */
205 public function getIssueMessages($identifier = null, $asString = false)
206 {
207 $messages = [];
208 if ($identifier === null) {
209 foreach ($this->issues as $idenfieriferIssues) {
210 foreach ($idenfieriferIssues as $issue) {
211 $messages[] = $issue['message'];
212 }
213 }
214 } else {
215 foreach ($this->getIssues($identifier) as $issue) {
216 $messages[] = $issue['message'];
217 }
218 }
219 if ($asString) {
220 $messages = implode('<br/>', $messages);
221 }
222 return $messages;
223 }
224
225 /**
226 * Adds an issue.
227 *
228 * @param string $identifier Record identifier (table:id)
229 * @param int $status Status code (see constants)
230 * @param string $message Message/description of the issue
231 */
232 protected function addIssue($identifier, $status, $message)
233 {
234 if (!isset($this->issues[$identifier])) {
235 $this->issues[$identifier] = [];
236 }
237 $this->issues[$identifier][] = [
238 'status' => $status,
239 'message' => $message
240 ];
241 }
242 }