TYPO3 CMS  TYPO3_6-2
AddController.php
Go to the documentation of this file.
1 <?php
3 
20 
27 
28  // Internal, dynamic:
29  // Content accumulation for the module.
33  public $content;
34 
35  // If set, the TCEmain class is loaded and used to add the returning ID to the parent record.
39  public $processDataFlag = 0;
40 
41  // Internal, static:
42  // Create new record -pid (pos/neg). If blank, return immediately
46  public $pid;
47 
48  // The parent table we are working on.
52  public $table;
53 
54  // Loaded with the created id of a record when TCEforms (alt_doc.php) returns ...
58  public $id;
59 
60  // Internal, static: GPvars
61  // Wizard parameters, coming from TCEforms linking to the wizard.
65  public $P;
66 
67  // Information coming back from alt_doc.php script, telling what the table/id was of the newly created record.
72 
76  public function __construct() {
77  $GLOBALS['LANG']->includeLLFile('EXT:lang/locallang_wizards.xlf');
78  $GLOBALS['SOBE'] = $this;
79 
80  $this->init();
81  }
82 
88  protected function init() {
89  // Init GPvars:
90  $this->P = GeneralUtility::_GP('P');
91  $this->returnEditConf = GeneralUtility::_GP('returnEditConf');
92  // Get this record
93  $origRow = BackendUtility::getRecord($this->P['table'], $this->P['uid']);
94  // Set table:
95  $this->table = $this->P['params']['table'];
96  // Get TSconfig for it.
97  $TSconfig = BackendUtility::getTCEFORM_TSconfig($this->P['table'], is_array($origRow) ? $origRow : array('pid' => $this->P['pid']));
98  // Set [params][pid]
99  if (substr($this->P['params']['pid'], 0, 3) === '###' && substr($this->P['params']['pid'], -3) === '###') {
100  $keyword = substr($this->P['params']['pid'], 3, -3);
101  if (strpos($keyword, 'PAGE_TSCONFIG_') === 0) {
102  $this->pid = (int)$TSconfig[$this->P['field']][$keyword];
103  } else {
104  $this->pid = (int)$TSconfig['_' . $keyword];
105  }
106  } else {
107  $this->pid = (int)$this->P['params']['pid'];
108  }
109  // Return if new record as parent (not possibly/allowed)
110  if ($this->pid === '') {
112  }
113  // Else proceed:
114  // If a new id has returned from a newly created record...
115  if ($this->returnEditConf) {
116  $eC = json_decode($this->returnEditConf, TRUE);
117  if (is_array($eC[$this->table]) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->P['uid'])) {
118  // Getting id and cmd from returning editConf array.
119  reset($eC[$this->table]);
120  $this->id = (int)key($eC[$this->table]);
121  $cmd = current($eC[$this->table]);
122  // ... and if everything seems OK we will register some classes for inclusion and instruct the object to perform processing later.
123  if ($this->P['params']['setValue'] && $cmd == 'edit' && $this->id && $this->P['table'] && $this->P['field'] && $this->P['uid']) {
124  if ($LiveRec = BackendUtility::getLiveVersionOfRecord($this->table, $this->id, 'uid')) {
125  $this->id = $LiveRec['uid'];
126  }
127  $this->processDataFlag = 1;
128  }
129  }
130  }
131  }
132 
139  public function main() {
140  if ($this->returnEditConf) {
141  if ($this->processDataFlag) {
142  // Preparing the data of the parent record...:
143  $trData = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Form\\DataPreprocessor');
144  // 'new'
145  $trData->fetchRecord($this->P['table'], $this->P['uid'], '');
146  $current = reset($trData->regTableItems_data);
147  // If that record was found (should absolutely be...), then init TCEmain and set, prepend or append the record
148  if (is_array($current)) {
149  $tce = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
150  $tce->stripslashes_values = 0;
151  $data = array();
152  $addEl = $this->table . '_' . $this->id;
153  // Setting the new field data:
154  // If the field is a flexform field, work with the XML structure instead:
155  if ($this->P['flexFormPath']) {
156  // Current value of flexform path:
157  $currentFlexFormData = GeneralUtility::xml2array($current[$this->P['field']]);
158  $flexToolObj = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Configuration\\FlexForm\\FlexFormTools');
159  $curValueOfFlexform = $flexToolObj->getArrayValueByPath($this->P['flexFormPath'], $currentFlexFormData);
160  $insertValue = '';
161  switch ((string) $this->P['params']['setValue']) {
162  case 'set':
163  $insertValue = $addEl;
164  break;
165  case 'prepend':
166  $insertValue = $curValueOfFlexform . ',' . $addEl;
167  break;
168  case 'append':
169  $insertValue = $addEl . ',' . $curValueOfFlexform;
170  break;
171  }
172  $insertValue = implode(',', GeneralUtility::trimExplode(',', $insertValue, TRUE));
173  $data[$this->P['table']][$this->P['uid']][$this->P['field']] = array();
174  $flexToolObj->setArrayValueByPath($this->P['flexFormPath'], $data[$this->P['table']][$this->P['uid']][$this->P['field']], $insertValue);
175  } else {
176  switch ((string) $this->P['params']['setValue']) {
177  case 'set':
178  $data[$this->P['table']][$this->P['uid']][$this->P['field']] = $addEl;
179  break;
180  case 'prepend':
181  $data[$this->P['table']][$this->P['uid']][$this->P['field']] = $current[$this->P['field']] . ',' . $addEl;
182  break;
183  case 'append':
184  $data[$this->P['table']][$this->P['uid']][$this->P['field']] = $addEl . ',' . $current[$this->P['field']];
185  break;
186  }
187  $data[$this->P['table']][$this->P['uid']][$this->P['field']] = implode(',', GeneralUtility::trimExplode(',', $data[$this->P['table']][$this->P['uid']][$this->P['field']], TRUE));
188  }
189  // Submit the data:
190  $tce->start($data, array());
191  $tce->process_datamap();
192  }
193  }
194  // Return to the parent alt_doc.php record editing session:
196  } else {
197  // Redirecting to alt_doc.php with instructions to create a new record
198  // AND when closing to return back with information about that records ID etc.
199  $redirectUrl = 'alt_doc.php?returnUrl=' . rawurlencode(GeneralUtility::getIndpEnv('REQUEST_URI')) . '&returnEditConf=1&edit[' . $this->P['params']['table'] . '][' . $this->pid . ']=new';
200  HttpUtility::redirect($redirectUrl);
201  }
202  }
203 
204 }
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)
static redirect($url, $httpStatus=self::HTTP_STATUS_303)
Definition: HttpUtility.php:76
static getLiveVersionOfRecord($table, $uid, $fields=' *')
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
static xml2array($string, $NSprefix='', $reportDocTag=FALSE)