[TASK] Introduce Enum constants for t3ver_state
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Service / IntegrityService.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Oliver Hader <oliver.hader@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Backend\Utility\BackendUtility;
31 use TYPO3\CMS\Core\Versioning\VersionState;
32
33 /**
34 * Service for integrity
35 *
36 * @author Oliver Hader <oliver.hader@typo3.org>
37 */
38 class IntegrityService {
39
40 /**
41 * Succes status - everything is fine
42 *
43 * @var integer
44 */
45 const STATUS_Succes = 100;
46 /**
47 * Info status - nothing is wrong, but a notice is shown
48 *
49 * @var integer
50 */
51 const STATUS_Info = 101;
52 /**
53 * Warning status - user interaction might be required
54 *
55 * @var integer
56 */
57 const STATUS_Warning = 102;
58 /**
59 * Error status - user interaction is required
60 *
61 * @var integer
62 */
63 const STATUS_Error = 103;
64 /**
65 * @var array
66 */
67 protected $statusRepresentation = array(
68 self::STATUS_Succes => 'success',
69 self::STATUS_Info => 'info',
70 self::STATUS_Warning => 'warning',
71 self::STATUS_Error => 'error'
72 );
73
74 /**
75 * @var \TYPO3\CMS\Workspaces\Domain\Model\CombinedRecord[]
76 */
77 protected $affectedElements;
78
79 /**
80 * Array storing all issues that have been checked and
81 * found during runtime in this object. The array keys
82 * are identifiers of table and the version-id.
83 *
84 * 'tx_table:123' => array(
85 * array(
86 * 'status' => 'warning',
87 * 'message' => 'Element cannot be...',
88 * )
89 * )
90 *
91 * @var array
92 */
93 protected $issues = array();
94
95 /**
96 * Sets the affected elements.
97 *
98 * @param \TYPO3\CMS\Workspaces\Domain\Model\CombinedRecord[] $affectedElements
99 * @return void
100 */
101 public function setAffectedElements(array $affectedElements) {
102 $this->affectedElements = $affectedElements;
103 }
104
105 /**
106 * Checks integrity of affected records.
107 *
108 * @return void
109 */
110 public function check() {
111 foreach ($this->affectedElements as $affectedElement) {
112 $this->checkElement($affectedElement);
113 }
114 }
115
116 /**
117 * Checks a single element.
118 *
119 * @param \TYPO3\CMS\Workspaces\Domain\Model\CombinedRecord $element
120 * @return void
121 */
122 public function checkElement(\TYPO3\CMS\Workspaces\Domain\Model\CombinedRecord $element) {
123 $this->checkLocalization($element);
124 }
125
126 /**
127 * Checks workspace localization integrity of a single elements.
128 * If current record is a localization and its localization parent
129 * is new in this workspace (has only a placeholder record in live),
130 * then boths (localization and localization parent) should be published.
131 *
132 * @param \TYPO3\CMS\Workspaces\Domain\Model\CombinedRecord $element
133 * @return void
134 */
135 protected function checkLocalization(\TYPO3\CMS\Workspaces\Domain\Model\CombinedRecord $element) {
136 $table = $element->getTable();
137 if (BackendUtility::isTableLocalizable($table)) {
138 $languageField = $GLOBALS['TCA'][$table]['ctrl']['languageField'];
139 $languageParentField = $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'];
140 $versionRow = $element->getVersionRecord()->getRow();
141 // If element is a localization:
142 if ($versionRow[$languageField] > 0) {
143 // Get localization parent from live workspace:
144 $languageParentRecord = BackendUtility::getRecord($table, $versionRow[$languageParentField], 'uid,t3ver_state');
145 // If localization parent is a "new placeholder" record:
146 if (VersionState::cast($languageParentRecord['t3ver_state'])->equals(\TYPO3\CMS\Core\Type\VersionState::NEW_PLACEHOLDER)) {
147 $title = BackendUtility::getRecordTitle($table, $versionRow);
148 // Add warning for current versionized record:
149 $this->addIssue($element->getLiveRecord()->getIdentifier(), self::STATUS_Warning, sprintf(\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('integrity.dependsOnDefaultLanguageRecord', 'workspaces'), $title));
150 // Add info for related localization parent record:
151 $this->addIssue($table . ':' . $languageParentRecord['uid'], self::STATUS_Info, sprintf(\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('integrity.isDefaultLanguageRecord', 'workspaces'), $title));
152 }
153 }
154 }
155 }
156
157 /**
158 * Gets the status of the most important severity.
159 * (low << success, info, warning, error >> high)
160 *
161 * @param string $identifier Record identifier (table:id) for look-ups
162 * @return string
163 */
164 public function getStatus($identifier = NULL) {
165 $status = self::STATUS_Succes;
166 if ($identifier === NULL) {
167 foreach ($this->issues as $idenfieriferIssues) {
168 foreach ($idenfieriferIssues as $issue) {
169 if ($status < $issue['status']) {
170 $status = $issue['status'];
171 }
172 }
173 }
174 } else {
175 foreach ($this->getIssues($identifier) as $issue) {
176 if ($status < $issue['status']) {
177 $status = $issue['status'];
178 }
179 }
180 }
181 return $status;
182 }
183
184 /**
185 * Gets the (human readable) represetation of the status with the most
186 * important severity (wraps $this->getStatus() and translates the result).
187 *
188 * @param string $identifier Record identifier (table:id) for look-ups
189 * @return string One out of success, info, warning, error
190 */
191 public function getStatusRepresentation($identifier = NULL) {
192 return $this->statusRepresentation[$this->getStatus($identifier)];
193 }
194
195 /**
196 * Gets issues, all or specific for one identifier.
197 *
198 * @param string $identifier Record identifier (table:id) for look-ups
199 * @return array
200 */
201 public function getIssues($identifier = NULL) {
202 if ($identifier === NULL) {
203 return $this->issues;
204 } elseif (isset($this->issues[$identifier])) {
205 return $this->issues[$identifier];
206 }
207 return array();
208 }
209
210 /**
211 * Gets the message of all issues.
212 *
213 * @param string $identifier Record identifier (table:id) for look-ups
214 * @param boolean $asString Return results as string instead of array
215 * @return array|string
216 */
217 public function getIssueMessages($identifier = NULL, $asString = FALSE) {
218 $messages = array();
219 if ($identifier === NULL) {
220 foreach ($this->issues as $idenfieriferIssues) {
221 foreach ($idenfieriferIssues as $issue) {
222 $messages[] = $issue['message'];
223 }
224 }
225 } else {
226 foreach ($this->getIssues($identifier) as $issue) {
227 $messages[] = $issue['message'];
228 }
229 }
230 if ($asString) {
231 $messages = implode('<br/>', $messages);
232 }
233 return $messages;
234 }
235
236 /**
237 * Adds an issue.
238 *
239 * @param string $identifier Record identifier (table:id)
240 * @param integer $status Status code (see constants)
241 * @param string $message Message/description of the issue
242 * @return void
243 */
244 protected function addIssue($identifier, $status, $message) {
245 if (!isset($this->issues[$identifier])) {
246 $this->issues[$identifier] = array();
247 }
248 $this->issues[$identifier][] = array(
249 'status' => $status,
250 'message' => $message
251 );
252 }
253
254 }