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