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