[TASK] Use ModuleTemplateAPI for Controller\Wizard\EditController
[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\ServerRequestInterface;
18 use Psr\Http\Message\ResponseInterface;
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 parent::__construct();
57 $this->getLanguageService()->includeLLFile('EXT:lang/locallang_wizards.xlf');
58 $GLOBALS['SOBE'] = $this;
59
60 $this->init();
61 }
62
63 /**
64 * Initialization of the script
65 *
66 * @return void
67 */
68 protected function init()
69 {
70 $this->P = GeneralUtility::_GP('P');
71 // Used for the return URL to FormEngine so that we can close the window.
72 $this->doClose = GeneralUtility::_GP('doClose');
73 }
74
75 /**
76 * Injects the request object for the current request or subrequest
77 * As this controller goes only through the main() method, it is rather simple for now
78 *
79 * @param ServerRequestInterface $request
80 * @param ResponseInterface $response
81 * @return ResponseInterface
82 */
83 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
84 {
85 $content = $this->main();
86 $response->getBody()->write($content);
87 return $response;
88 }
89
90 /**
91 * Main function
92 * Makes a header-location redirect to an edit form IF POSSIBLE from the passed data - otherwise the window will
93 * just close.
94 *
95 * @return string
96 */
97 public function main()
98 {
99 if ($this->doClose) {
100 return $this->closeWindow;
101 }
102 // Initialize:
103 $table = $this->P['table'];
104 $field = $this->P['field'];
105 $config = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
106 $fTable = $config['foreign_table'];
107
108 $urlParameters = array(
109 'returnUrl' => BackendUtility::getModuleUrl('wizard_edit', array('doClose' => 1))
110 );
111
112 // Detecting the various allowed field type setups and acting accordingly.
113 if (is_array($config)
114 && $config['type'] === 'select'
115 && !$config['MM']
116 && $config['maxitems'] <= 1 && MathUtility::canBeInterpretedAsInteger($this->P['currentValue'])
117 && $this->P['currentValue'] && $fTable
118 ) {
119 // SINGLE value
120 $urlParameters['edit[' . $fTable . '][' . $this->P['currentValue'] . ']'] = 'edit';
121 // Redirect to FormEngine
122 $url = BackendUtility::getModuleUrl('record_edit', $urlParameters);
123 HttpUtility::redirect($url);
124 } elseif (is_array($config)
125 && $this->P['currentSelectedValues']
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 = BackendUtility::getModuleUrl('record_edit', $urlParameters);
148 HttpUtility::redirect($url);
149 } else {
150 return $this->closeWindow;
151 }
152 }
153
154 /**
155 * Printing a little JavaScript to close the open window.
156 *
157 * @return void
158 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8
159 */
160 public function closeWindow()
161 {
162 GeneralUtility::logDeprecatedFunction();
163 echo $this->closeWindow;
164 die;
165 }
166 }