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