b9f1e9e7d5f4ac25e3ec08ea20aaa1aa04a626ce
[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\Backend\Utility\BackendUtility;
20 use TYPO3\CMS\Core\Database\RelationHandler;
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 * @param ResponseInterface $response
78 * @return ResponseInterface
79 */
80 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
81 {
82 $content = $this->main();
83 $response->getBody()->write($content);
84 return $response;
85 }
86
87 /**
88 * Main function
89 * Makes a header-location redirect to an edit form IF POSSIBLE from the passed data - otherwise the window will
90 * just close.
91 *
92 * @return string
93 */
94 public function main()
95 {
96 if ($this->doClose) {
97 return $this->closeWindow;
98 }
99 // Initialize:
100 $table = $this->P['table'];
101 $field = $this->P['field'];
102 $config = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
103 $fTable = $config['foreign_table'];
104
105 $urlParameters = [
106 'returnUrl' => BackendUtility::getModuleUrl('wizard_edit', ['doClose' => 1])
107 ];
108
109 // Detecting the various allowed field type setups and acting accordingly.
110 if (is_array($config)
111 && $config['type'] === 'select'
112 && !$config['MM']
113 && $config['maxitems'] <= 1 && MathUtility::canBeInterpretedAsInteger($this->P['currentValue'])
114 && $this->P['currentValue'] && $fTable
115 ) {
116 // SINGLE value
117 $urlParameters['edit[' . $fTable . '][' . $this->P['currentValue'] . ']'] = 'edit';
118 // Redirect to FormEngine
119 $url = BackendUtility::getModuleUrl('record_edit', $urlParameters);
120 HttpUtility::redirect($url);
121 } elseif (is_array($config)
122 && $this->P['currentSelectedValues']
123 && (
124 $config['type'] === 'select'
125 && $config['foreign_table']
126 || $config['type'] === 'group'
127 && $config['internal_type'] === 'db'
128 )
129 ) {
130 // MULTIPLE VALUES:
131 // Init settings:
132 $allowedTables = $config['type'] === 'group' ? $config['allowed'] : $config['foreign_table'];
133 $prependName = 1;
134 // Selecting selected values into an array:
135 /** @var RelationHandler $relationHandler */
136 $relationHandler = GeneralUtility::makeInstance(RelationHandler::class);
137 $relationHandler->start($this->P['currentSelectedValues'], $allowedTables);
138 $value = $relationHandler->getValueArray($prependName);
139 // Traverse that array and make parameters for FormEngine
140 foreach ($value as $rec) {
141 $recTableUidParts = GeneralUtility::revExplode('_', $rec, 2);
142 $urlParameters['edit[' . $recTableUidParts[0] . '][' . $recTableUidParts[1] . ']'] = 'edit';
143 }
144 // Redirect to FormEngine
145 $url = BackendUtility::getModuleUrl('record_edit', $urlParameters);
146 HttpUtility::redirect($url);
147 } else {
148 return $this->closeWindow;
149 }
150 }
151 }