Cleanup: Updated copyright comments
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_flexformtools.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2006-2010 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 if (is_array($v3)) {
239 $cc=$k3;
240 $theType = key($v3);
241 $theDat = $v3[$theType];
242 $newSectionEl = $value['el'][$theType];
243 if (is_array($newSectionEl)) {
244 $this->traverseFlexFormXMLData_recurse(
245 array($theType => $newSectionEl),
246 array($theType => $theDat),
247 $PA,
248 $path.'/'.$key.'/el/'.$cc
249 );
250 }
251 }
252 }
253 }
254 } else { // Array traversal:
255 $this->traverseFlexFormXMLData_recurse(
256 $value['el'],
257 $editData[$key]['el'],
258 $PA,
259 $path.'/'.$key.'/el'
260 );
261 }
262 } elseif (is_array($value['TCEforms']['config'])) { // Processing a field value:
263
264 foreach($PA['vKeys'] as $vKey) {
265 $vKey = 'v'.$vKey;
266
267 // Call back:
268 if ($PA['callBackMethod_value']) {
269 $this->callBackObj->$PA['callBackMethod_value'](
270 $value,
271 $editData[$key][$vKey],
272 $PA,
273 $path.'/'.$key.'/'.$vKey,
274 $this
275 );
276 }
277 }
278 }
279 }
280 }
281 }
282 }
283
284 /**
285 * Returns an array of available languages to use for FlexForm operations
286 *
287 * @return array
288 */
289 function getAvailableLanguages() {
290 $isL = t3lib_extMgm::isLoaded('static_info_tables');
291
292 // Find all language records in the system:
293 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('static_lang_isocode,title,uid', 'sys_language', 'pid=0'.t3lib_BEfunc::deleteClause('sys_language'), '', 'title');
294
295 // Traverse them:
296 $output = array();
297 $output[0]=array(
298 'uid' => 0,
299 'title' => 'Default language',
300 'ISOcode' => 'DEF'
301 );
302
303 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
304 $output[$row['uid']] = $row;
305
306 if ($isL && $row['static_lang_isocode']) {
307 $rr = t3lib_BEfunc::getRecord('static_languages',$row['static_lang_isocode'],'lg_iso_2');
308 if ($rr['lg_iso_2']) $output[$row['uid']]['ISOcode']=$rr['lg_iso_2'];
309 }
310
311 if (!$output[$row['uid']]['ISOcode']) unset($output[$row['uid']]);
312 }
313 return $output;
314 }
315
316
317
318
319
320
321
322
323
324 /***********************************
325 *
326 * Processing functions
327 *
328 ***********************************/
329
330 /**
331 * 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.
332 * 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.
333 *
334 * @param string Table name
335 * @param string Field name of the flex form field in which the XML is found that should be cleaned.
336 * @param array The record
337 * @return string Clean XML from FlexForm field
338 */
339 function cleanFlexFormXML($table,$field,$row) {
340
341 // New structure:
342 $this->cleanFlexFormXML = array();
343
344 // Create and call iterator object:
345 $flexObj = t3lib_div::makeInstance('t3lib_flexformtools');
346 $flexObj->reNumberIndexesOfSectionData = TRUE;
347 $flexObj->traverseFlexFormXMLData($table,$field,$row,$this,'cleanFlexFormXML_callBackFunction');
348
349 return $this->flexArray2Xml($this->cleanFlexFormXML, TRUE);
350 }
351
352 /**
353 * Call back function for t3lib_flexformtools class
354 * Basically just setting the value in a new array (thus cleaning because only values that are valid are visited!)
355 *
356 * @param array Data structure for the current value
357 * @param mixed Current value
358 * @param array Additional configuration used in calling function
359 * @param string Path of value in DS structure
360 * @param object Object reference to caller
361 * @return void
362 */
363 function cleanFlexFormXML_callBackFunction($dsArr, $data, $PA, $path, $pObj) {
364 #debug(array($dsArr, $data, $PA),$path);
365 // Just setting value in our own result array, basically replicating the structure:
366 $pObj->setArrayValueByPath($path,$this->cleanFlexFormXML,$data);
367
368 // Looking if an "extension" called ".vDEFbase" is found and if so, accept that too:
369 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['flexFormXMLincludeDiffBase']) {
370 $vDEFbase = $pObj->getArrayValueByPath($path.'.vDEFbase',$pObj->traverseFlexFormXMLData_Data);
371 if (isset($vDEFbase)) {
372 $pObj->setArrayValueByPath($path.'.vDEFbase',$this->cleanFlexFormXML,$vDEFbase);
373 }
374 }
375 }
376
377
378
379
380
381
382
383
384
385 /***********************************
386 *
387 * Multi purpose functions
388 *
389 ***********************************/
390
391 /**
392 * Get a value from a multi-dimensional array by giving a path "../../.." pointing to the element
393 *
394 * @param string The path pointing to the value field, eg. test/2/title to access $array['test'][2]['title']
395 * @param array Array to get value from. Passed by reference so the value returned can be used to change the value in the array!
396 * @return mixed Value returned
397 */
398 function &getArrayValueByPath($pathArray,&$array) {
399 if (!is_array($pathArray)) {
400 $pathArray = explode('/',$pathArray);
401 }
402 if (is_array($array)) {
403 if (count($pathArray)) {
404 $key = array_shift($pathArray);
405
406 if (isset($array[$key])) {
407 if (!count($pathArray)) {
408 return $array[$key];
409 } else {
410 return $this->getArrayValueByPath($pathArray,$array[$key]);
411 }
412 } else {
413 return NULL;
414 }
415 }
416 }
417 }
418
419 /**
420 * Set a value in a multi-dimensional array by giving a path "../../.." pointing to the element
421 *
422 * @param string The path pointing to the value field, eg. test/2/title to access $array['test'][2]['title']
423 * @param array Array to set value in. Passed by reference so the value returned can be used to change the value in the array!
424 * @param mixed Value to set
425 * @return mixed Value returned
426 */
427 function setArrayValueByPath($pathArray,&$array,$value) {
428 if (isset($value)) {
429 if (!is_array($pathArray)) {
430 $pathArray = explode('/',$pathArray);
431 }
432 if (is_array($array)) {
433 if (count($pathArray)) {
434 $key = array_shift($pathArray);
435
436 if (!count($pathArray)) {
437 $array[$key] = $value;
438 return TRUE;
439 } else {
440 if (!isset($array[$key])) {
441 $array[$key] = array();
442 }
443 return $this->setArrayValueByPath($pathArray,$array[$key],$value);
444 }
445 }
446 }
447 }
448 }
449
450 /**
451 * Convert FlexForm data array to XML
452 *
453 * @param array Array to output in <T3FlexForms> XML
454 * @param boolean If set, the XML prologue is returned as well.
455 * @return string XML content.
456 */
457 function flexArray2Xml($array, $addPrologue=FALSE) {
458 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['flexformForceCDATA']) {
459 $this->flexArray2Xml_options['useCDATA'] = 1;
460 }
461
462 $options = $GLOBALS['TYPO3_CONF_VARS']['BE']['niceFlexFormXMLtags'] ? $this->flexArray2Xml_options : array();
463 $spaceInd = ($GLOBALS['TYPO3_CONF_VARS']['BE']['compactFlexFormXML'] ? -1 : 4);
464 $output = t3lib_div::array2xml($array,'',0,'T3FlexForms', $spaceInd, $options);
465
466 if ($addPrologue) {
467 $output = '<?xml version="1.0" encoding="'.$GLOBALS['LANG']->charSet.'" standalone="yes" ?>'.LF.$output;
468 }
469
470 return $output;
471 }
472 }
473
474 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flexformtools.php']) {
475 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flexformtools.php']);
476 }
477 ?>