[BUGFIX] Fix PHP warning in EXT:beuser
[Packages/TYPO3.CMS.git] / typo3 / wizard_add.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Wizard to add new records to a group/select TCEform formfield
30 *
31 * Revised for TYPO3 3.6 November/2003 by Kasper Skårhøj
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 */
35
36 $BACK_PATH = '';
37 require('init.php');
38 $LANG->includeLLFile('EXT:lang/locallang_wizards.xml');
39
40 /**
41 * Script Class for adding new items to a group/select field. Performs proper redirection as needed.
42 *
43 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
44 * @package TYPO3
45 * @subpackage core
46 */
47 class SC_wizard_add {
48
49 // Internal, dynamic:
50 // Content accumulation for the module.
51 var $content;
52 // List of files to include.
53 var $include_once = array();
54 // If set, the TCEmain class is loaded and used to add the returning ID to the parent record.
55 var $processDataFlag = 0;
56
57 // Internal, static:
58 // Create new record -pid (pos/neg). If blank, return immediately
59 var $pid;
60 // The parent table we are working on.
61 var $table;
62 // Loaded with the created id of a record when TCEforms (alt_doc.php) returns ...
63 var $id;
64
65 // Internal, static: GPvars
66 // Wizard parameters, coming from TCEforms linking to the wizard.
67 var $P;
68 // Information coming back from alt_doc.php script, telling what the table/id was of the newly created record.
69 var $returnEditConf;
70
71 /**
72 * Initialization of the class.
73 *
74 * @return void
75 */
76 function init() {
77
78 // Init GPvars:
79 $this->P = t3lib_div::_GP('P');
80 $this->returnEditConf = t3lib_div::_GP('returnEditConf');
81
82 // Get this record
83 $origRow = t3lib_BEfunc::getRecord($this->P['table'], $this->P['uid']);
84
85 // Set table:
86 $this->table = $this->P['params']['table'];
87
88 // Get TSconfig for it.
89 $TSconfig = t3lib_BEfunc::getTCEFORM_TSconfig($this->P['table'], is_array($origRow)?$origRow:array('pid'=>$this->P['pid']));
90
91 // Set [params][pid]
92 if (substr($this->P['params']['pid'], 0, 3) == '###' && substr($this->P['params']['pid'], -3)=='###') {
93 $this->pid = intval($TSconfig['_'.substr($this->P['params']['pid'], 3, -3)]);
94 } else {
95 $this->pid = intval($this->P['params']['pid']);
96 }
97
98 // Return if new record as parent (not possibly/allowed)
99 if (!strcmp($this->pid, '')) {
100 t3lib_utility_Http::redirect(t3lib_div::sanitizeLocalUrl($this->P['returnUrl']));
101 }
102
103 // Else proceed:
104 // If a new id has returned from a newly created record...
105 if ($this->returnEditConf) {
106 $eC = unserialize($this->returnEditConf);
107 if (is_array($eC[$this->table]) && t3lib_utility_Math::canBeInterpretedAsInteger($this->P['uid'])) {
108
109 // Getting id and cmd from returning editConf array.
110 reset($eC[$this->table]);
111 $this->id = intval(key($eC[$this->table]));
112 $cmd = current($eC[$this->table]);
113
114 // ... and if everything seems OK we will register some classes for inclusion and instruct the object to perform processing later.
115 if ($this->P['params']['setValue'] && $cmd=='edit' && $this->id && $this->P['table'] && $this->P['field'] && $this->P['uid']) {
116
117 if ($LiveRec = t3lib_BEfunc::getLiveVersionOfRecord($this->table, $this->id, 'uid')) {
118 $this->id=$LiveRec['uid'];
119 }
120 $this->processDataFlag = 1;
121 }
122 }
123 }
124 }
125
126 /**
127 * Main function
128 * Will issue a location-header, redirecting either BACK or to a new alt_doc.php instance...
129 *
130 * @return void
131 */
132 function main() {
133
134 if ($this->returnEditConf) {
135 if ($this->processDataFlag) {
136
137 // Preparing the data of the parent record...:
138 $trData = t3lib_div::makeInstance('t3lib_transferData');
139 // 'new'
140 $trData->fetchRecord($this->P['table'], $this->P['uid'], '');
141 $current = reset($trData->regTableItems_data);
142
143 // If that record was found (should absolutely be...), then init TCEmain and set, prepend or append the record
144 if (is_array($current)) {
145 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
146 $tce->stripslashes_values = 0;
147 $data = array();
148 $addEl = $this->table.'_'.$this->id;
149
150 // Setting the new field data:
151 // If the field is a flexform field, work with the XML structure instead:
152 if ($this->P['flexFormPath']) {
153 // Current value of flexform path:
154 $currentFlexFormData = t3lib_div::xml2array($current[$this->P['field']]);
155 $flexToolObj = t3lib_div::makeInstance('t3lib_flexformtools');
156 $curValueOfFlexform = $flexToolObj->getArrayValueByPath($this->P['flexFormPath'], $currentFlexFormData);
157 $insertValue = '';
158
159 switch((string)$this->P['params']['setValue']) {
160 case 'set':
161 $insertValue = $addEl;
162 break;
163 case 'prepend':
164 $insertValue = $curValueOfFlexform.','.$addEl;
165 break;
166 case 'append':
167 $insertValue = $addEl.','.$curValueOfFlexform;
168 break;
169 }
170 $insertValue = implode(',', t3lib_div::trimExplode(',', $insertValue, 1));
171
172 $data[$this->P['table']][$this->P['uid']][$this->P['field']] = array();
173 $flexToolObj->setArrayValueByPath($this->P['flexFormPath'], $data[$this->P['table']][$this->P['uid']][$this->P['field']], $insertValue);
174 } else {
175 switch((string)$this->P['params']['setValue']) {
176 case 'set':
177 $data[$this->P['table']][$this->P['uid']][$this->P['field']] = $addEl;
178 break;
179 case 'prepend':
180 $data[$this->P['table']][$this->P['uid']][$this->P['field']] = $current[$this->P['field']].','.$addEl;
181 break;
182 case 'append':
183 $data[$this->P['table']][$this->P['uid']][$this->P['field']] = $addEl.','.$current[$this->P['field']];
184 break;
185 }
186 $data[$this->P['table']][$this->P['uid']][$this->P['field']] = implode(',', t3lib_div::trimExplode(',', $data[$this->P['table']][$this->P['uid']][$this->P['field']], 1));
187 }
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:
195 t3lib_utility_Http::redirect(t3lib_div::sanitizeLocalUrl($this->P['returnUrl']));
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(t3lib_div::getIndpEnv('REQUEST_URI')) . '&returnEditConf=1&edit[' . $this->P['params']['table'] . '][' . $this->pid . ']=new';
200 t3lib_utility_Http::redirect($redirectUrl);
201 }
202 }
203 }
204
205 // Make instance:
206 $SOBE = t3lib_div::makeInstance('SC_wizard_add');
207 $SOBE->init();
208
209 // Include files?
210 foreach ($SOBE->include_once as $INC_FILE) {
211 include_once($INC_FILE);
212 }
213
214 $SOBE->main();
215
216 ?>