added @var phpDoc comments to various variables
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_flexformtools.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2006 Kasper Skaarhoj (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 * Contains functions for manipulating flex form data
29 *
30 * $Id$
31 *
32 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
33 */
34 /**
35 * [CLASS/FUNCTION INDEX of SCRIPT]
36 *
37 *
38 *
39 * 71: class t3lib_flexformtools
40 * 105: function traverseFlexFormXMLData($table,$field,$row,&$callBackObj,$callBackMethod_value)
41 * 203: function traverseFlexFormXMLData_recurse($dataStruct,$editData,&$PA,$path='')
42 * 274: function getAvailableLanguages()
43 *
44 * SECTION: Processing functions
45 * 323: function cleanFlexFormXML($table,$field,$row)
46 * 347: function cleanFlexFormXML_callBackFunction($dsArr, $data, $PA, $path, &$pObj)
47 *
48 * SECTION: Multi purpose functions
49 * 374: function &getArrayValueByPath($pathArray,&$array)
50 * 403: function setArrayValueByPath($pathArray,&$array,$value)
51 * 433: function flexArray2Xml($array, $addPrologue=FALSE)
52 *
53 * TOTAL FUNCTIONS: 8
54 * (This index is automatically created/updated by the extension "extdeveval")
55 *
56 */
57
58
59
60
61
62
63
64 /**
65 * Contains functions for manipulating flex form data
66 *
67 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
68 * @package TYPO3
69 * @subpackage t3lib
70 */
71 class t3lib_flexformtools {
72
73 var $convertCharset = FALSE; // If set, the charset of data XML is converted to system charset.
74 var $reNumberIndexesOfSectionData = FALSE; // If set, section indexes are re-numbered before processing
75
76 var $traverseFlexFormXMLData_DS = array(); // Contains data structure when traversing flexform
77 var $traverseFlexFormXMLData_Data = array(); // Contains data array when traversing flexform
78
79 // Options for array2xml() for flexform. This will map the weird keys from the internal array to tags that could potentially be checked with a DTD/schema
80 var $flexArray2Xml_options = array(
81 'parentTagMap' => array(
82 'data' => 'sheet',
83 'sheet' => 'language',
84 'language' => 'field',
85 'el' => 'field',
86 'field' => 'value',
87 'field:el' => 'el',
88 'el:_IS_NUM' => 'section',
89 'section' => 'itemType'
90 ),
91 'disableTypeAttrib' => 2
92 );
93
94 // Internal:
95 /**
96 * Reference to object called
97 */
98 var $callBackObj = NULL;
99 var $cleanFlexFormXML = array(); // Used for accumulation of clean XML
100
101 /**
102 * Handler for Flex Forms
103 *
104 * @param string The table name of the record
105 * @param string The field name of the flexform field to work on
106 * @param array The record data array
107 * @param object Object (passed by reference) in which the call back function is located
108 * @param string Method name of call back function in object for values
109 * @return boolean If true, error happened (error string returned)
110 */
111 function traverseFlexFormXMLData($table,$field,$row,&$callBackObj,$callBackMethod_value) {
112
113 if (!is_array($GLOBALS['TCA'][$table]) || !is_array($GLOBALS['TCA'][$table]['columns'][$field])) {
114 return 'TCA table/field was not defined.';
115 }
116
117 $this->callBackObj = &$callBackObj;
118
119 // Get Data Structure:
120 $dataStructArray = t3lib_BEfunc::getFlexFormDS($GLOBALS['TCA'][$table]['columns'][$field]['config'],$row,$table);
121
122 // If data structure was ok, proceed:
123 if (is_array($dataStructArray)) {
124
125 // Get flexform XML data:
126 $xmlData = $row[$field];
127
128 // Convert charset:
129 if ($this->convertCharset) {
130 $xmlHeaderAttributes = t3lib_div::xmlGetHeaderAttribs($xmlData);
131 $storeInCharset = strtolower($xmlHeaderAttributes['encoding']);
132 if ($storeInCharset) {
133 $currentCharset = $GLOBALS['LANG']->charSet;
134 $xmlData = $GLOBALS['LANG']->csConvObj->conv($xmlData,$storeInCharset,$currentCharset,1);
135 }
136 }
137
138 $editData = t3lib_div::xml2array($xmlData);
139 if (!is_array($editData)) {
140 return 'Parsing error: '.$editData;
141 }
142
143 // Language settings:
144 $langChildren = $dataStructArray['meta']['langChildren'] ? 1 : 0;
145 $langDisabled = $dataStructArray['meta']['langDisable'] ? 1 : 0;
146
147 // empty or invalid <meta>
148 if (!is_array($editData['meta'])) {
149 $editData['meta'] = array();
150 }
151 $editData['meta']['currentLangId'] = array();
152 $languages = $this->getAvailableLanguages();
153
154 foreach ($languages as $lInfo) {
155 $editData['meta']['currentLangId'][] = $lInfo['ISOcode'];
156 }
157 if (!count($editData['meta']['currentLangId'])) {
158 $editData['meta']['currentLangId'] = array('DEF');
159 }
160 $editData['meta']['currentLangId'] = array_unique($editData['meta']['currentLangId']);
161
162 if ($langChildren || $langDisabled) {
163 $lKeys = array('DEF');
164 } else {
165 $lKeys = $editData['meta']['currentLangId'];
166 }
167
168 // Tabs sheets
169 if (is_array($dataStructArray['sheets'])) {
170 $sKeys = array_keys($dataStructArray['sheets']);
171 } else {
172 $sKeys = array('sDEF');
173 }
174
175 // Traverse languages:
176 foreach($lKeys as $lKey) {
177 foreach($sKeys as $sheet) {
178 $sheetCfg = $dataStructArray['sheets'][$sheet];
179 list ($dataStruct, $sheet) = t3lib_div::resolveSheetDefInDS($dataStructArray,$sheet);
180
181 // Render sheet:
182 if (is_array($dataStruct['ROOT']) && is_array($dataStruct['ROOT']['el'])) {
183 $lang = 'l'.$lKey; // Separate language key
184 $PA['vKeys'] = $langChildren && !$langDisabled ? $editData['meta']['currentLangId'] : array('DEF');
185 $PA['lKey'] = $lang;
186 $PA['callBackMethod_value'] = $callBackMethod_value;
187 $PA['table'] = $table;
188 $PA['field'] = $field;
189 $PA['uid'] = $row['uid'];
190
191 $this->traverseFlexFormXMLData_DS = &$dataStruct;
192 $this->traverseFlexFormXMLData_Data = &$editData;
193
194 // Render flexform:
195 $this->traverseFlexFormXMLData_recurse(
196 $dataStruct['ROOT']['el'],
197 $editData['data'][$sheet][$lang],
198 $PA,
199 'data/'.$sheet.'/'.$lang
200 );
201 } else return 'Data Structure ERROR: No ROOT element found for sheet "'.$sheet.'".';
202 }
203 }
204 } else return 'Data Structure ERROR: '.$dataStructArray;
205 }
206
207 /**
208 * Recursively traversing flexform data according to data structure and element data
209 *
210 * @param array (Part of) data structure array that applies to the sub section of the flexform data we are processing
211 * @param array (Part of) edit data array, reflecting current part of data structure
212 * @param array Additional parameters passed.
213 * @param string Telling the "path" to the element in the flexform XML
214 * @return array
215 */
216 function traverseFlexFormXMLData_recurse($dataStruct,$editData,&$PA,$path='') {
217
218 if (is_array($dataStruct)) {
219 foreach($dataStruct as $key => $value) {
220 if (is_array($value)) { // The value of each entry must be an array.
221
222 if ($value['type']=='array') {
223 if ($value['section']) { // Array (Section) traversal:
224
225 $cc = 0;
226 if (is_array($editData[$key]['el'])) {
227
228 if ($this->reNumberIndexesOfSectionData) {
229 $temp = array();
230 $c3=0;
231 foreach($editData[$key]['el'] as $v3) {
232 $temp[++$c3] = $v3;
233 }
234 $editData[$key]['el'] = $temp;
235 }
236
237 foreach($editData[$key]['el'] as $k3 => $v3) {
238 $cc=$k3;
239 $theType = key($v3);
240 $theDat = $v3[$theType];
241 $newSectionEl = $value['el'][$theType];
242 if (is_array($newSectionEl)) {
243 $this->traverseFlexFormXMLData_recurse(
244 array($theType => $newSectionEl),
245 array($theType => $theDat),
246 $PA,
247 $path.'/'.$key.'/el/'.$cc
248 );
249 }
250 }
251 }
252 } else { // Array traversal:
253 $this->traverseFlexFormXMLData_recurse(
254 $value['el'],
255 $editData[$key]['el'],
256 $PA,
257 $path.'/'.$key.'/el'
258 );
259 }
260 } elseif (is_array($value['TCEforms']['config'])) { // Processing a field value:
261
262 foreach($PA['vKeys'] as $vKey) {
263 $vKey = 'v'.$vKey;
264
265 // Call back:
266 if ($PA['callBackMethod_value']) {
267 $this->callBackObj->$PA['callBackMethod_value'](
268 $value,
269 $editData[$key][$vKey],
270 $PA,
271 $path.'/'.$key.'/'.$vKey,
272 $this
273 );
274 }
275 }
276 }
277 }
278 }
279 }
280 }
281
282 /**
283 * Returns an array of available languages to use for FlexForm operations
284 *
285 * @return array
286 */
287 function getAvailableLanguages() {
288 $isL = t3lib_extMgm::isLoaded('static_info_tables');
289
290 // Find all language records in the system:
291 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('static_lang_isocode,title,uid', 'sys_language', 'pid=0'.t3lib_BEfunc::deleteClause('sys_language'), '', 'title');
292
293 // Traverse them:
294 $output = array();
295 $output[0]=array(
296 'uid' => 0,
297 'title' => 'Default language',
298 'ISOcode' => 'DEF'
299 );
300
301 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
302 $output[$row['uid']] = $row;
303
304 if ($isL && $row['static_lang_isocode']) {
305 $rr = t3lib_BEfunc::getRecord('static_languages',$row['static_lang_isocode'],'lg_iso_2');
306 if ($rr['lg_iso_2']) $output[$row['uid']]['ISOcode']=$rr['lg_iso_2'];
307 }
308
309 if (!$output[$row['uid']]['ISOcode']) unset($output[$row['uid']]);
310 }
311 return $output;
312 }
313
314
315
316
317
318
319
320
321
322 /***********************************
323 *
324 * Processing functions
325 *
326 ***********************************/
327
328 /**
329 * Cleaning up FlexForm XML to hold only the values it may according to its Data Structure. Also the order of tags will follow that of the data structure.
330 * BE CAREFUL: DO not clean records in workspaces unless IN the workspace! The Data Structure might resolve falsely on a workspace record when cleaned from Live workspace.
331 *
332 * @param string Table name
333 * @param string Field name of the flex form field in which the XML is found that should be cleaned.
334 * @param array The record
335 * @return string Clean XML from FlexForm field
336 */
337 function cleanFlexFormXML($table,$field,$row) {
338
339 // New structure:
340 $this->cleanFlexFormXML = array();
341
342 // Create and call iterator object:
343 $flexObj = t3lib_div::makeInstance('t3lib_flexformtools');
344 $flexObj->reNumberIndexesOfSectionData = TRUE;
345 $flexObj->traverseFlexFormXMLData($table,$field,$row,$this,'cleanFlexFormXML_callBackFunction');
346
347 return $this->flexArray2Xml($this->cleanFlexFormXML, TRUE);
348 }
349
350 /**
351 * Call back function for t3lib_flexformtools class
352 * Basically just setting the value in a new array (thus cleaning because only values that are valid are visited!)
353 *
354 * @param array Data structure for the current value
355 * @param mixed Current value
356 * @param array Additional configuration used in calling function
357 * @param string Path of value in DS structure
358 * @param object Object reference to caller
359 * @return void
360 */
361 function cleanFlexFormXML_callBackFunction($dsArr, $data, $PA, $path, &$pObj) {
362 #debug(array($dsArr, $data, $PA),$path);
363 // Just setting value in our own result array, basically replicating the structure:
364 $pObj->setArrayValueByPath($path,$this->cleanFlexFormXML,$data);
365 }
366
367
368
369
370
371
372
373
374
375 /***********************************
376 *
377 * Multi purpose functions
378 *
379 ***********************************/
380
381 /**
382 * Get a value from a multi-dimensional array by giving a path "../../.." pointing to the element
383 *
384 * @param string The path pointing to the value field, eg. test/2/title to access $array['test'][2]['title']
385 * @param array Array to get value from. Passed by reference so the value returned can be used to change the value in the array!
386 * @return mixed Value returned
387 */
388 function &getArrayValueByPath($pathArray,&$array) {
389 if (!is_array($pathArray)) {
390 $pathArray = explode('/',$pathArray);
391 }
392 if (is_array($array)) {
393 if (count($pathArray)) {
394 $key = array_shift($pathArray);
395
396 if (isset($array[$key])) {
397 if (!count($pathArray)) {
398 return $array[$key];
399 } else {
400 return $this->getArrayValueByPath($pathArray,$array[$key]);
401 }
402 } else {
403 return NULL;
404 }
405 }
406 }
407 }
408
409 /**
410 * Set a value in a multi-dimensional array by giving a path "../../.." pointing to the element
411 *
412 * @param string The path pointing to the value field, eg. test/2/title to access $array['test'][2]['title']
413 * @param array Array to set value in. Passed by reference so the value returned can be used to change the value in the array!
414 * @param mixed Value to set
415 * @return mixed Value returned
416 */
417 function setArrayValueByPath($pathArray,&$array,$value) {
418 if (isset($value)) {
419 if (!is_array($pathArray)) {
420 $pathArray = explode('/',$pathArray);
421 }
422 if (is_array($array)) {
423 if (count($pathArray)) {
424 $key = array_shift($pathArray);
425
426 if (!count($pathArray)) {
427 $array[$key] = $value;
428 return TRUE;
429 } else {
430 if (!isset($array[$key])) {
431 $array[$key] = array();
432 }
433 return $this->setArrayValueByPath($pathArray,$array[$key],$value);
434 }
435 }
436 }
437 }
438 }
439
440 /**
441 * Convert FlexForm data array to XML
442 *
443 * @param array Array to output in <T3FlexForms> XML
444 * @param boolean If set, the XML prologue is returned as well.
445 * @return string XML content.
446 */
447 function flexArray2Xml($array, $addPrologue=FALSE) {
448
449 $options = $GLOBALS['TYPO3_CONF_VARS']['BE']['niceFlexFormXMLtags'] ? $this->flexArray2Xml_options : array();
450 $spaceInd = ($GLOBALS['TYPO3_CONF_VARS']['BE']['compactFlexFormXML'] ? -1 : 4);
451 $output = t3lib_div::array2xml($array,'',0,'T3FlexForms', $spaceInd, $options);
452
453 if ($addPrologue) {
454 $output = '<?xml version="1.0" encoding="'.$GLOBALS['LANG']->charSet.'" standalone="yes" ?>'.chr(10).$output;
455 }
456
457 return $output;
458 }
459 }
460
461 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flexformtools.php']) {
462 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flexformtools.php']);
463 }
464 ?>