[TASK] Streamline PHPDoc comment matches function/method signature
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / Wizard / EditController.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Controller\Wizard;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
20 use TYPO3\CMS\Core\Compatibility\PublicPropertyDeprecationTrait;
21 use TYPO3\CMS\Core\Database\RelationHandler;
22 use TYPO3\CMS\Core\Http\HtmlResponse;
23 use TYPO3\CMS\Core\Http\RedirectResponse;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Core\Utility\HttpUtility;
26 use TYPO3\CMS\Core\Utility\MathUtility;
27
28 /**
29 * Script Class for redirecting a backend user to the editing form when an "Edit wizard" link was clicked in FormEngine somewhere
30 */
31 class EditController extends AbstractWizardController
32 {
33 use PublicPropertyDeprecationTrait;
34
35 /**
36 * Properties which have been moved to protected status from public
37 *
38 * @var array
39 */
40 protected $deprecatedPublicProperties = [
41 'P' => 'Using $P of class EditController from the outside is discouraged, as this variable is only used for internal storage.',
42 'doClose' => 'Using $doClose of class EditController from the outside is discouraged, as this variable is only used for internal storage.',
43 ];
44
45 /**
46 * Wizard parameters, coming from FormEngine linking to the wizard.
47 *
48 * Contains the following parts:
49 * - table
50 * - field
51 * - formName
52 * - hmac
53 * - fieldChangeFunc
54 * - fieldChangeFuncHash
55 * - currentValue
56 * - currentSelectedValues
57 *
58 * @var array
59 */
60 protected $P;
61
62 /**
63 * Boolean; if set, the window will be closed by JavaScript
64 *
65 * @var int
66 */
67 protected $doClose;
68
69 /**
70 * A little JavaScript to close the open window.
71 *
72 * @var string
73 */
74 protected $closeWindow = '<script language="javascript" type="text/javascript">close();</script>';
75
76 /**
77 * Constructor
78 */
79 public function __construct()
80 {
81 $this->getLanguageService()->includeLLFile('EXT:core/Resources/Private/Language/locallang_wizards.xlf');
82
83 // @deprecated since v9, will be moved out of __construct() in v10
84 $this->init($GLOBALS['TYPO3_REQUEST']);
85 }
86
87 /**
88 * Initialization of the script
89 *
90 * @param ServerRequestInterface $request
91 */
92 protected function init(ServerRequestInterface $request)
93 {
94 $parsedBody = $request->getParsedBody();
95 $queryParams = $request->getQueryParams();
96
97 $this->P = $parsedBody['P'] ?? $queryParams['P'] ?? [];
98
99 // Used for the return URL to FormEngine so that we can close the window.
100 $this->doClose = $parsedBody['doClose'] ?? $queryParams['doClose'] ?? 0;
101 }
102
103 /**
104 * Injects the request object for the current request or subrequest
105 * As this controller goes only through the main() method, it is rather simple for now
106 *
107 * @param ServerRequestInterface $request
108 * @return ResponseInterface
109 */
110 public function mainAction(ServerRequestInterface $request): ResponseInterface
111 {
112 $content = $this->processRequest($request);
113 return $content;
114 }
115
116 /**
117 * Main function
118 * Makes a header-location redirect to an edit form IF POSSIBLE from the passed data - otherwise the window will
119 * just close.
120 *
121 * @deprecated since v9, will be removed in v10
122 * @return string
123 */
124 public function main()
125 {
126 trigger_error('Method main() will be set to protected in v10. Do not call from other extension', E_USER_DEPRECATED);
127 $request = $GLOBALS['TYPO3_REQUEST'];
128
129 $response = $this->processRequest($request);
130
131 if ($response instanceof RedirectResponse) {
132 HttpUtility::redirect($response->getHeaders()['location'][0]);
133 } else {
134 return $response->getBody()->getContents();
135 }
136 }
137
138 /**
139 * Process request function
140 * Makes a header-location redirect to an edit form IF POSSIBLE from the passed data - otherwise the window will
141 * just close.
142 *
143 * @param ServerRequestInterface $request
144 * @return ResponseInterface
145 */
146 protected function processRequest(ServerRequestInterface $request): ResponseInterface
147 {
148 if ($this->doClose) {
149 return new HtmlResponse($this->closeWindow);
150 }
151 // Initialize:
152 $table = $this->P['table'];
153 $field = $this->P['field'];
154 $config = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
155 $fTable = $config['foreign_table'];
156
157 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
158
159 $urlParameters = [
160 'returnUrl' => (string)$uriBuilder->buildUriFromRoute('wizard_edit', ['doClose' => 1])
161 ];
162
163 // Detecting the various allowed field type setups and acting accordingly.
164 if (is_array($config)
165 && $config['type'] === 'select'
166 && !$config['MM']
167 && $config['maxitems'] <= 1 && MathUtility::canBeInterpretedAsInteger($this->P['currentValue'])
168 && $this->P['currentValue'] && $fTable
169 ) {
170 // SINGLE value
171 $urlParameters['edit[' . $fTable . '][' . $this->P['currentValue'] . ']'] = 'edit';
172 // Redirect to FormEngine
173 $url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
174 return new RedirectResponse($url);
175 }
176
177 if (is_array($config)
178 && $this->P['currentSelectedValues']
179 && (
180 $config['type'] === 'select'
181 && $config['foreign_table']
182 || $config['type'] === 'group'
183 && $config['internal_type'] === 'db'
184 )
185 ) {
186 // MULTIPLE VALUES:
187 // Init settings:
188 $allowedTables = $config['type'] === 'group' ? $config['allowed'] : $config['foreign_table'];
189 $prependName = 1;
190 // Selecting selected values into an array:
191 $relationHandler = GeneralUtility::makeInstance(RelationHandler::class);
192 $relationHandler->start($this->P['currentSelectedValues'], $allowedTables);
193 $value = $relationHandler->getValueArray($prependName);
194 // Traverse that array and make parameters for FormEngine
195 foreach ($value as $rec) {
196 $recTableUidParts = GeneralUtility::revExplode('_', $rec, 2);
197 $urlParameters['edit[' . $recTableUidParts[0] . '][' . $recTableUidParts[1] . ']'] = 'edit';
198 }
199 // Redirect to FormEngine
200 $url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
201
202 return new RedirectResponse($url);
203 }
204 return new HtmlResponse($this->closeWindow);
205 }
206 }