[TASK] Create own response instance in controller actions
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / Wizard / EditController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Controller\Wizard;
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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Core\Database\RelationHandler;
20 use TYPO3\CMS\Core\Http\HtmlResponse;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Core\Utility\HttpUtility;
23 use TYPO3\CMS\Core\Utility\MathUtility;
24
25 /**
26 * Script Class for redirecting a backend user to the editing form when an "Edit wizard" link was clicked in FormEngine somewhere
27 */
28 class EditController extends AbstractWizardController
29 {
30 /**
31 * Wizard parameters, coming from FormEngine linking to the wizard.
32 *
33 * @var array
34 */
35 public $P;
36
37 /**
38 * Boolean; if set, the window will be closed by JavaScript
39 *
40 * @var int
41 */
42 public $doClose;
43
44 /**
45 * A little JavaScript to close the open window.
46 *
47 * @var string
48 */
49 protected $closeWindow = '<script language="javascript" type="text/javascript">close();</script>';
50
51 /**
52 * Constructor
53 */
54 public function __construct()
55 {
56 $this->getLanguageService()->includeLLFile('EXT:lang/Resources/Private/Language/locallang_wizards.xlf');
57 $GLOBALS['SOBE'] = $this;
58
59 $this->init();
60 }
61
62 /**
63 * Initialization of the script
64 */
65 protected function init()
66 {
67 $this->P = GeneralUtility::_GP('P');
68 // Used for the return URL to FormEngine so that we can close the window.
69 $this->doClose = GeneralUtility::_GP('doClose');
70 }
71
72 /**
73 * Injects the request object for the current request or subrequest
74 * As this controller goes only through the main() method, it is rather simple for now
75 *
76 * @param ServerRequestInterface $request
77 * @return ResponseInterface
78 */
79 public function mainAction(ServerRequestInterface $request): ResponseInterface
80 {
81 $content = $this->main();
82 return new HtmlResponse($content);
83 }
84
85 /**
86 * Main function
87 * Makes a header-location redirect to an edit form IF POSSIBLE from the passed data - otherwise the window will
88 * just close.
89 *
90 * @return string
91 */
92 public function main()
93 {
94 if ($this->doClose) {
95 return $this->closeWindow;
96 }
97 // Initialize:
98 $table = $this->P['table'];
99 $field = $this->P['field'];
100 $config = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
101 $fTable = $config['foreign_table'];
102
103 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
104 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
105
106 $urlParameters = [
107 'returnUrl' => (string)$uriBuilder->buildUriFromRoute('wizard_edit', ['doClose' => 1])
108 ];
109
110 // Detecting the various allowed field type setups and acting accordingly.
111 if (is_array($config)
112 && $config['type'] === 'select'
113 && !$config['MM']
114 && $config['maxitems'] <= 1 && MathUtility::canBeInterpretedAsInteger($this->P['currentValue'])
115 && $this->P['currentValue'] && $fTable
116 ) {
117 // SINGLE value
118 $urlParameters['edit[' . $fTable . '][' . $this->P['currentValue'] . ']'] = 'edit';
119 // Redirect to FormEngine
120 $url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
121 HttpUtility::redirect($url);
122 } elseif (is_array($config)
123 && $this->P['currentSelectedValues']
124 && (
125 $config['type'] === 'select'
126 && $config['foreign_table']
127 || $config['type'] === 'group'
128 && $config['internal_type'] === 'db'
129 )
130 ) {
131 // MULTIPLE VALUES:
132 // Init settings:
133 $allowedTables = $config['type'] === 'group' ? $config['allowed'] : $config['foreign_table'];
134 $prependName = 1;
135 // Selecting selected values into an array:
136 /** @var RelationHandler $relationHandler */
137 $relationHandler = GeneralUtility::makeInstance(RelationHandler::class);
138 $relationHandler->start($this->P['currentSelectedValues'], $allowedTables);
139 $value = $relationHandler->getValueArray($prependName);
140 // Traverse that array and make parameters for FormEngine
141 foreach ($value as $rec) {
142 $recTableUidParts = GeneralUtility::revExplode('_', $rec, 2);
143 $urlParameters['edit[' . $recTableUidParts[0] . '][' . $recTableUidParts[1] . ']'] = 'edit';
144 }
145 // Redirect to FormEngine
146 $url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
147 HttpUtility::redirect($url);
148 } else {
149 return $this->closeWindow;
150 }
151 }
152 }