ac2a7a58b085223b749ccd5865b4293861d63aaa
[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 TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Utility\HttpUtility;
20
21 /**
22 * Script Class for redirecting a backend user to the editing form when an "Edit wizard" link was clicked in TCEforms somewhere
23 *
24 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
25 */
26 class EditController extends AbstractWizardController {
27
28 /**
29 * Wizard parameters, coming from TCEforms linking to the wizard.
30 *
31 * @var array
32 */
33 public $P;
34
35 /**
36 * Boolean; if set, the window will be closed by JavaScript
37 *
38 * @var int
39 */
40 public $doClose;
41
42 /**
43 * Constructor
44 */
45 public function __construct() {
46 $this->getLanguageService()->includeLLFile('EXT:lang/locallang_wizards.xlf');
47 $GLOBALS['SOBE'] = $this;
48
49 $this->init();
50 }
51
52 /**
53 * Initialization of the script
54 *
55 * @return void
56 */
57 protected function init() {
58 $this->P = GeneralUtility::_GP('P');
59 // Used for the return URL to FormEngine so that we can close the window.
60 $this->doClose = GeneralUtility::_GP('doClose');
61 }
62
63 /**
64 * Main function
65 * Makes a header-location redirect to an edit form IF POSSIBLE from the passed data - otherwise the window will just close.
66 *
67 * @return void
68 */
69 public function main() {
70 if ($this->doClose) {
71 $this->closeWindow();
72 } else {
73 // Initialize:
74 $table = $this->P['table'];
75 $field = $this->P['field'];
76 $config = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
77 $fTable = $this->P['currentValue'] < 0 ? $config['neg_foreign_table'] : $config['foreign_table'];
78
79 $urlParameters = array(
80 'returnUrl' => rawurlencode(BackendUtility::getModuleUrl('wizard_edit', array('doClose' => 1)))
81 );
82
83 // Detecting the various allowed field type setups and acting accordingly.
84 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) {
85 // SINGLE value
86 $urlParameters['edit[' . $fTable . '][' . $this->P['currentValue'] . ']'] = 'edit';
87 // Redirect to FormEngine
88 $url = BackendUtility::getModuleUrl('record_edit', $urlParameters);
89 HttpUtility::redirect($url);
90 } elseif (is_array($config) && $this->P['currentSelectedValues'] && ($config['type'] == 'select' && $config['foreign_table'] || $config['type'] == 'group' && $config['internal_type'] == 'db')) {
91 // MULTIPLE VALUES:
92 // Init settings:
93 $allowedTables = $config['type'] == 'group' ? $config['allowed'] : $config['foreign_table'] . ',' . $config['neg_foreign_table'];
94 $prependName = 1;
95 // Selecting selected values into an array:
96 $dbAnalysis = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\RelationHandler::class);
97 $dbAnalysis->start($this->P['currentSelectedValues'], $allowedTables);
98 $value = $dbAnalysis->getValueArray($prependName);
99 // Traverse that array and make parameters for FormEngine
100 foreach ($value as $rec) {
101 $recTableUidParts = GeneralUtility::revExplode('_', $rec, 2);
102 $urlParameters['edit[' . $recTableUidParts[0] . '][' . $recTableUidParts[1] . ']'] = 'edit';
103 }
104 // Redirect to FormEngine
105 $url = BackendUtility::getModuleUrl('record_edit', $urlParameters);
106 HttpUtility::redirect($url);
107 } else {
108 $this->closeWindow();
109 }
110 }
111 }
112
113 /**
114 * Printing a little JavaScript to close the open window.
115 *
116 * @return void
117 */
118 public function closeWindow() {
119 echo '<script language="javascript" type="text/javascript">close();</script>';
120 die;
121 }
122
123 }