[CLEANUP] EXT:backend/Classes/Wizard/*.php
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / Wizard / AddController.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\Form\DataPreprocessor;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
20 use TYPO3\CMS\Core\DataHandling\DataHandler;
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 adding new items to a group/select field. Performs proper redirection as needed.
27 *
28 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
29 */
30 class AddController extends AbstractWizardController {
31
32 /**
33 * Content accumulation for the module.
34 *
35 * @var string
36 */
37 public $content;
38
39 /**
40 * If set, the TCEmain class is loaded and used to add the returning ID to the parent record.
41 *
42 * @var int
43 */
44 public $processDataFlag = 0;
45
46 /**
47 * Create new record -pid (pos/neg). If blank, return immediately
48 *
49 * @var int
50 */
51 public $pid;
52
53 /**
54 * The parent table we are working on.
55 *
56 * @var string
57 */
58 public $table;
59
60 /**
61 * Loaded with the created id of a record FormEngine returns ...
62 *
63 * @var int
64 */
65 public $id;
66
67 /**
68 * Wizard parameters, coming from TCEforms linking to the wizard.
69 *
70 * @var array
71 */
72 public $P;
73
74 /**
75 * Information coming back from the FormEngine script, telling what the table/id was of the newly created record.
76 *
77 * @var array
78 */
79 public $returnEditConf;
80
81 /**
82 * Constructor
83 */
84 public function __construct() {
85 $this->getLanguageService()->includeLLFile('EXT:lang/locallang_wizards.xlf');
86 $GLOBALS['SOBE'] = $this;
87
88 $this->init();
89 }
90
91 /**
92 * Initialization of the class.
93 *
94 * @return void
95 */
96 protected function init() {
97 // Init GPvars:
98 $this->P = GeneralUtility::_GP('P');
99 $this->returnEditConf = GeneralUtility::_GP('returnEditConf');
100 // Get this record
101 $record = BackendUtility::getRecord($this->P['table'], $this->P['uid']);
102 // Set table:
103 $this->table = $this->P['params']['table'];
104 // Get TSconfig for it.
105 $TSconfig = BackendUtility::getTCEFORM_TSconfig($this->P['table'], is_array($record) ? $record : array('pid' => $this->P['pid']));
106 // Set [params][pid]
107 if (substr($this->P['params']['pid'], 0, 3) === '###' && substr($this->P['params']['pid'], -3) === '###') {
108 $keyword = substr($this->P['params']['pid'], 3, -3);
109 if (strpos($keyword, 'PAGE_TSCONFIG_') === 0) {
110 $this->pid = (int)$TSconfig[$this->P['field']][$keyword];
111 } else {
112 $this->pid = (int)$TSconfig['_' . $keyword];
113 }
114 } else {
115 $this->pid = (int)$this->P['params']['pid'];
116 }
117 // Return if new record as parent (not possibly/allowed)
118 if ($this->pid === '') {
119 HttpUtility::redirect(GeneralUtility::sanitizeLocalUrl($this->P['returnUrl']));
120 }
121 // Else proceed:
122 // If a new id has returned from a newly created record...
123 if ($this->returnEditConf) {
124 $editConfiguration = json_decode($this->returnEditConf, TRUE);
125 if (is_array($editConfiguration[$this->table]) && MathUtility::canBeInterpretedAsInteger($this->P['uid'])) {
126 // Getting id and cmd from returning editConf array.
127 reset($editConfiguration[$this->table]);
128 $this->id = (int)key($editConfiguration[$this->table]);
129 $cmd = current($editConfiguration[$this->table]);
130 // ... and if everything seems OK we will register some classes for inclusion and instruct the object to perform processing later.
131 if ($this->P['params']['setValue'] && $cmd === 'edit' && $this->id && $this->P['table'] && $this->P['field'] && $this->P['uid']) {
132 $liveRecord = BackendUtility::getLiveVersionOfRecord($this->table, $this->id, 'uid');
133 if ($liveRecord) {
134 $this->id = $liveRecord['uid'];
135 }
136 $this->processDataFlag = 1;
137 }
138 }
139 }
140 }
141
142 /**
143 * Main function
144 * Will issue a location-header, redirecting either BACK or to a new FormEngine instance...
145 *
146 * @return void
147 */
148 public function main() {
149 if ($this->returnEditConf) {
150 if ($this->processDataFlag) {
151 // Preparing the data of the parent record...:
152 /** @var DataPreprocessor $dataPreprocessor */
153 $dataPreprocessor = GeneralUtility::makeInstance(DataPreprocessor::class);
154 // 'new'
155 $dataPreprocessor->fetchRecord($this->P['table'], $this->P['uid'], '');
156 $current = reset($dataPreprocessor->regTableItems_data);
157 // If that record was found (should absolutely be...), then init DataHandler and set, prepend or append the record
158 if (is_array($current)) {
159 /** @var DataHandler $dataHandler */
160 $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
161 $dataHandler->stripslashes_values = FALSE;
162 $data = array();
163 $recordId = $this->table . '_' . $this->id;
164 // Setting the new field data:
165 // If the field is a flexForm field, work with the XML structure instead:
166 if ($this->P['flexFormPath']) {
167 // Current value of flexForm path:
168 $currentFlexFormData = GeneralUtility::xml2array($current[$this->P['field']]);
169 /** @var FlexFormTools $flexFormTools */
170 $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
171 $currentFlexFormValue = $flexFormTools->getArrayValueByPath($this->P['flexFormPath'], $currentFlexFormData);
172 $insertValue = '';
173 switch ((string)$this->P['params']['setValue']) {
174 case 'set':
175 $insertValue = $recordId;
176 break;
177 case 'prepend':
178 $insertValue = $currentFlexFormValue . ',' . $recordId;
179 break;
180 case 'append':
181 $insertValue = $recordId . ',' . $currentFlexFormValue;
182 break;
183 }
184 $insertValue = implode(',', GeneralUtility::trimExplode(',', $insertValue, TRUE));
185 $data[$this->P['table']][$this->P['uid']][$this->P['field']] = array();
186 $flexFormTools->setArrayValueByPath($this->P['flexFormPath'], $data[$this->P['table']][$this->P['uid']][$this->P['field']], $insertValue);
187 } else {
188 switch ((string)$this->P['params']['setValue']) {
189 case 'set':
190 $data[$this->P['table']][$this->P['uid']][$this->P['field']] = $recordId;
191 break;
192 case 'prepend':
193 $data[$this->P['table']][$this->P['uid']][$this->P['field']] = $current[$this->P['field']] . ',' . $recordId;
194 break;
195 case 'append':
196 $data[$this->P['table']][$this->P['uid']][$this->P['field']] = $recordId . ',' . $current[$this->P['field']];
197 break;
198 }
199 $data[$this->P['table']][$this->P['uid']][$this->P['field']] = implode(',', GeneralUtility::trimExplode(',', $data[$this->P['table']][$this->P['uid']][$this->P['field']], TRUE));
200 }
201 // Submit the data:
202 $dataHandler->start($data, array());
203 $dataHandler->process_datamap();
204 }
205 }
206 // Return to the parent FormEngine record editing session:
207 HttpUtility::redirect(GeneralUtility::sanitizeLocalUrl($this->P['returnUrl']));
208 } else {
209 // Redirecting to FormEngine with instructions to create a new record
210 // AND when closing to return back with information about that records ID etc.
211 $redirectUrl = BackendUtility::getModuleUrl('record_edit', array(
212 'returnEditConf' => 1,
213 'edit[' . $this->P['params']['table'] . '][' . $this->pid . ']' => 'new',
214 'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
215 ));
216 HttpUtility::redirect($redirectUrl);
217 }
218 }
219
220 }