9cad7ce5b5dcac0fe37326f12b7a6b0a4767af58
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Controller / Remote / AbstractHandler.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\Controller\Remote;
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 /**
18 * Class AbstractHandler
19 */
20 abstract class AbstractHandler
21 {
22 /**
23 * Gets the current workspace ID.
24 *
25 * @return int The current workspace ID
26 */
27 protected function getCurrentWorkspace()
28 {
29 return $this->getWorkspaceService()->getCurrentWorkspace();
30 }
31
32 /**
33 * Gets an error response to be shown in the grid component.
34 *
35 * @param string $errorLabel Name of the label in the locallang.xlf file
36 * @param int $errorCode The error code to be used
37 * @param bool $successFlagValue Value of the success flag to be delivered back (might be FALSE in most cases)
38 * @return array
39 */
40 protected function getErrorResponse($errorLabel, $errorCode = 0, $successFlagValue = false)
41 {
42 $localLangFile = 'LLL:EXT:workspaces/Resources/Private/Language/locallang.xlf';
43 $response = [
44 'error' => [
45 'code' => $errorCode,
46 'message' => $GLOBALS['LANG']->sL($localLangFile . ':' . $errorLabel)
47 ],
48 'success' => $successFlagValue
49 ];
50 return $response;
51 }
52
53 /**
54 * Gets an instance of the workspaces service.
55 *
56 * @return \TYPO3\CMS\Workspaces\Service\WorkspaceService
57 */
58 protected function getWorkspaceService()
59 {
60 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Workspaces\Service\WorkspaceService::class);
61 }
62
63 /**
64 * Validates whether the submitted language parameter can be
65 * interpreted as integer value.
66 *
67 * @param stdClass $parameters
68 * @return int|NULL
69 */
70 protected function validateLanguageParameter(\stdClass $parameters)
71 {
72 $language = null;
73 if (isset($parameters->language) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($parameters->language)) {
74 $language = $parameters->language;
75 }
76 return $language;
77 }
78
79 /**
80 * Gets affected elements on publishing/swapping actions.
81 * Affected elements have a dependency, e.g. translation overlay
82 * and the default origin record - thus, the default record would be
83 * affected if the translation overlay shall be published.
84 *
85 * @param stdClass $parameters
86 * @return array
87 */
88 protected function getAffectedElements(\stdClass $parameters)
89 {
90 $affectedElements = [];
91 if ($parameters->type === 'selection') {
92 foreach ((array)$parameters->selection as $element) {
93 $affectedElements[] = \TYPO3\CMS\Workspaces\Domain\Model\CombinedRecord::create($element->table, $element->liveId, $element->versionId);
94 }
95 } elseif ($parameters->type === 'all') {
96 $versions = $this->getWorkspaceService()->selectVersionsInWorkspace($this->getCurrentWorkspace(), 0, -99, -1, 0, 'tables_select', $this->validateLanguageParameter($parameters));
97 foreach ($versions as $table => $tableElements) {
98 foreach ($tableElements as $element) {
99 $affectedElement = \TYPO3\CMS\Workspaces\Domain\Model\CombinedRecord::create($table, $element['t3ver_oid'], $element['uid']);
100 $affectedElement->getVersionRecord()->setRow($element);
101 $affectedElements[] = $affectedElement;
102 }
103 }
104 }
105 return $affectedElements;
106 }
107
108 /**
109 * Creates a new instance of the integrity service for the
110 * given set of affected elements.
111 *
112 * @param \TYPO3\CMS\Workspaces\Domain\Model\CombinedRecord[] $affectedElements
113 * @return \TYPO3\CMS\Workspaces\Service\IntegrityService
114 * @see getAffectedElements
115 */
116 protected function createIntegrityService(array $affectedElements)
117 {
118 /** @var $integrityService \TYPO3\CMS\Workspaces\Service\IntegrityService */
119 $integrityService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Workspaces\Service\IntegrityService::class);
120 $integrityService->setAffectedElements($affectedElements);
121 return $integrityService;
122 }
123 }