3b5309d6935a34ee17309a3b8fcb15edca2cc9e6
[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
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/locallang_wizards.xlf');
56 $GLOBALS['SOBE'] = $this;
57
58 $this->init();
59 }
60
61 /**
62 * Initialization of the script
63 *
64 * @return void
65 */
66 protected function init()
67 {
68 $this->P = GeneralUtility::_GP('P');
69 // Used for the return URL to FormEngine so that we can close the window.
70 $this->doClose = GeneralUtility::_GP('doClose');
71 }
72
73 /**
74 * Injects the request object for the current request or subrequest
75 * As this controller goes only through the main() method, it is rather simple for now
76 *
77 * @param ServerRequestInterface $request
78 * @param ResponseInterface $response
79 * @return ResponseInterface
80 */
81 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
82 {
83 $content = $this->main();
84 $response->getBody()->write($content);
85 return $response;
86 }
87
88 /**
89 * Main function
90 * Makes a header-location redirect to an edit form IF POSSIBLE from the passed data - otherwise the window will 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 = array(
106 'returnUrl' => BackendUtility::getModuleUrl('wizard_edit', array('doClose' => 1))
107 );
108
109 // Detecting the various allowed field type setups and acting accordingly.
110 if (is_array($config) && $config['type'] === 'select' && !$config['MM'] && $config['maxitems'] <= 1 && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->P['currentValue']) && $this->P['currentValue'] && $fTable) {
111 // SINGLE value
112 $urlParameters['edit[' . $fTable . '][' . $this->P['currentValue'] . ']'] = 'edit';
113 // Redirect to FormEngine
114 $url = BackendUtility::getModuleUrl('record_edit', $urlParameters);
115 HttpUtility::redirect($url);
116 } elseif (is_array($config) && $this->P['currentSelectedValues'] && ($config['type'] === 'select' && $config['foreign_table'] || $config['type'] === 'group' && $config['internal_type'] === 'db')) {
117 // MULTIPLE VALUES:
118 // Init settings:
119 $allowedTables = $config['type'] === 'group' ? $config['allowed'] : $config['foreign_table'];
120 $prependName = 1;
121 // Selecting selected values into an array:
122 /** @var RelationHandler $relationHandler */
123 $relationHandler = GeneralUtility::makeInstance(RelationHandler::class);
124 $relationHandler->start($this->P['currentSelectedValues'], $allowedTables);
125 $value = $relationHandler->getValueArray($prependName);
126 // Traverse that array and make parameters for FormEngine
127 foreach ($value as $rec) {
128 $recTableUidParts = GeneralUtility::revExplode('_', $rec, 2);
129 $urlParameters['edit[' . $recTableUidParts[0] . '][' . $recTableUidParts[1] . ']'] = 'edit';
130 }
131 // Redirect to FormEngine
132 $url = BackendUtility::getModuleUrl('record_edit', $urlParameters);
133 HttpUtility::redirect($url);
134 } else {
135 return $this->closeWindow;
136 }
137 }
138
139 /**
140 * Printing a little JavaScript to close the open window.
141 *
142 * @return void
143 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8
144 */
145 public function closeWindow()
146 {
147 GeneralUtility::logDeprecatedFunction();
148 echo $this->closeWindow;
149 die;
150 }
151 }