TYPO3  7.6
AddController.php
Go to the documentation of this file.
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 
28 
34 {
40  public $content;
41 
47  public $processDataFlag = 0;
48 
54  public $pid;
55 
61  public $table;
62 
68  public $id;
69 
75  public $P;
76 
83 
87  public function __construct()
88  {
89  parent::__construct();
90  $this->getLanguageService()->includeLLFile('EXT:lang/locallang_wizards.xlf');
91  $GLOBALS['SOBE'] = $this;
92 
93  $this->init();
94  }
95 
101  protected function init()
102  {
103  // Init GPvars:
104  $this->P = GeneralUtility::_GP('P');
105  $this->returnEditConf = GeneralUtility::_GP('returnEditConf');
106  // Get this record
107  $record = BackendUtility::getRecord($this->P['table'], $this->P['uid']);
108  // Set table:
109  $this->table = $this->P['params']['table'];
110  // Get TSconfig for it.
112  $this->P['table'],
113  is_array($record) ? $record : array('pid' => $this->P['pid'])
114  );
115  // Set [params][pid]
116  if (substr($this->P['params']['pid'], 0, 3) === '###' && substr($this->P['params']['pid'], -3) === '###') {
117  $keyword = substr($this->P['params']['pid'], 3, -3);
118  if (strpos($keyword, 'PAGE_TSCONFIG_') === 0) {
119  $this->pid = (int)$TSconfig[$this->P['field']][$keyword];
120  } else {
121  $this->pid = (int)$TSconfig['_' . $keyword];
122  }
123  } else {
124  $this->pid = (int)$this->P['params']['pid'];
125  }
126  // Return if new record as parent (not possibly/allowed)
127  if ($this->pid === '') {
129  }
130  // Else proceed:
131  // If a new id has returned from a newly created record...
132  if ($this->returnEditConf) {
133  $editConfiguration = json_decode($this->returnEditConf, true);
134  if (is_array($editConfiguration[$this->table]) && MathUtility::canBeInterpretedAsInteger($this->P['uid'])) {
135  // Getting id and cmd from returning editConf array.
136  reset($editConfiguration[$this->table]);
137  $this->id = (int)key($editConfiguration[$this->table]);
138  $cmd = current($editConfiguration[$this->table]);
139  // ... and if everything seems OK we will register some classes for inclusion and instruct the object
140  // to perform processing later.
141  if ($this->P['params']['setValue']
142  && $cmd === 'edit'
143  && $this->id
144  && $this->P['table']
145  && $this->P['field'] && $this->P['uid']
146  ) {
147  $liveRecord = BackendUtility::getLiveVersionOfRecord($this->table, $this->id, 'uid');
148  if ($liveRecord) {
149  $this->id = $liveRecord['uid'];
150  }
151  $this->processDataFlag = 1;
152  }
153  }
154  }
155  }
156 
166  {
167  $this->main();
168  return $response;
169  }
170 
177  public function main()
178  {
179  if ($this->returnEditConf) {
180  if ($this->processDataFlag) {
181  // This data processing is done here to basically just get the current record. It can be discussed
182  // if this isn't overkill here. In case this construct does not work out well, it would be less
183  // overhead to just BackendUtility::fetchRecord the current parent here.
185  $formDataGroup = GeneralUtility::makeInstance(OnTheFly::class);
186  $formDataGroup->setProviderList([ DatabaseEditRow::class ]);
188  $formDataCompiler = GeneralUtility::makeInstance(FormDataCompiler::class, $formDataGroup);
189  $input = [
190  'tableName' => $this->P['table'],
191  'vanillaUid' => (int)$this->P['uid'],
192  'command' => 'edit',
193  ];
194  $result = $formDataCompiler->compile($input);
195  $currentParentRow = $result['databaseRow'];
196 
197  // If that record was found (should absolutely be...), then init DataHandler and set, prepend or append
198  // the record
199  if (is_array($currentParentRow)) {
201  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
202  $dataHandler->stripslashes_values = false;
203  $data = array();
204  $recordId = $this->table . '_' . $this->id;
205  // Setting the new field data:
206  // If the field is a flexForm field, work with the XML structure instead:
207  if ($this->P['flexFormPath']) {
208  // Current value of flexForm path:
209  $currentFlexFormData = GeneralUtility::xml2array($currentParentRow[$this->P['field']]);
211  $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
212  $currentFlexFormValue = $flexFormTools->getArrayValueByPath(
213  $this->P['flexFormPath'],
214  $currentFlexFormData
215  );
216  $insertValue = '';
217  switch ((string)$this->P['params']['setValue']) {
218  case 'set':
219  $insertValue = $recordId;
220  break;
221  case 'prepend':
222  $insertValue = $currentFlexFormValue . ',' . $recordId;
223  break;
224  case 'append':
225  $insertValue = $recordId . ',' . $currentFlexFormValue;
226  break;
227  }
228  $insertValue = implode(',', GeneralUtility::trimExplode(',', $insertValue, true));
229  $data[$this->P['table']][$this->P['uid']][$this->P['field']] = array();
230  $flexFormTools->setArrayValueByPath(
231  $this->P['flexFormPath'],
232  $data[$this->P['table']][$this->P['uid']][$this->P['field']],
233  $insertValue
234  );
235  } else {
236  switch ((string)$this->P['params']['setValue']) {
237  case 'set':
238  $data[$this->P['table']][$this->P['uid']][$this->P['field']] = $recordId;
239  break;
240  case 'prepend':
241  $data[$this->P['table']][$this->P['uid']][$this->P['field']] = $currentParentRow[$this->P['field']] . ',' . $recordId;
242  break;
243  case 'append':
244  $data[$this->P['table']][$this->P['uid']][$this->P['field']] = $recordId . ',' . $currentParentRow[$this->P['field']];
245  break;
246  }
247  $data[$this->P['table']][$this->P['uid']][$this->P['field']] = implode(
248  ',',
250  ',',
251  $data[$this->P['table']][$this->P['uid']][$this->P['field']],
252  true
253  )
254  );
255  }
256  // Submit the data:
257  $dataHandler->start($data, array());
258  $dataHandler->process_datamap();
259  }
260  }
261  // Return to the parent FormEngine record editing session:
263  } else {
264  // Redirecting to FormEngine with instructions to create a new record
265  // AND when closing to return back with information about that records ID etc.
266  $redirectUrl = BackendUtility::getModuleUrl('record_edit', array(
267  'returnEditConf' => 1,
268  'edit[' . $this->P['params']['table'] . '][' . $this->pid . ']' => 'new',
269  'returnUrl' => GeneralUtility::removeXSS(GeneralUtility::getIndpEnv('REQUEST_URI'))
270  ));
271  HttpUtility::redirect($redirectUrl);
272  }
273  }
274 }