See changelog.
[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 var $callBackObj = NULL; // Reference to object called
96 var $cleanFlexFormXML = array(); // Used for accumulation of clean XML
97
98 /**
99 * Handler for Flex Forms
100 *
101 * @param string The table name of the record
102 * @param string The field name of the flexform field to work on
103 * @param array The record data array
104 * @param object Object (passed by reference) in which the call back function is located
105 * @param string Method name of call back function in object for values
106 * @return boolean If true, error happened (error string returned)
107 */
108 function traverseFlexFormXMLData($table,$field,$row,&$callBackObj,$callBackMethod_value) {
109
110 if (!is_array($GLOBALS['TCA'][$table]) || !is_array($GLOBALS['TCA'][$table]['columns'][$field])) {
111 return 'TCA table/field was not defined.';
112 }
113
114 $this->callBackObj = &$callBackObj;
115
116 // Get Data Structure:
117 $dataStructArray = t3lib_BEfunc::getFlexFormDS($GLOBALS['TCA'][$table]['columns'][$field]['config'],$row,$table);
118
119 // If data structure was ok, proceed:
120 if (is_array($dataStructArray)) {
121
122 // Get flexform XML data:
123 $xmlData = $row[$field];
124
125 // Convert charset:
126 if ($this->convertCharset) {
127 $xmlHeaderAttributes = t3lib_div::xmlGetHeaderAttribs($xmlData);
128 $storeInCharset = strtolower($xmlHeaderAttributes['encoding']);
129 if ($storeInCharset) {
130 $currentCharset = $GLOBALS['LANG']->charSet;
131 $xmlData = $GLOBALS['LANG']->csConvObj->conv($xmlData,$storeInCharset,$currentCharset,1);
132 }
133 }
134
135 $editData = t3lib_div::xml2array($xmlData);
136 if (!is_array($editData)) {
137 return 'Parsing error: '.$editData;
138 }
139
140 // Language settings:
141 $langChildren = $dataStructArray['meta']['langChildren'] ? 1 : 0;
142 $langDisabled = $dataStructArray['meta']['langDisable'] ? 1 : 0;
143
144 // empty or invalid <meta>
145 if (!is_array($editData['meta'])) {
146 $editData['meta'] = array();
147 }
148 $editData['meta']['currentLangId'] = array();
149 $languages = $this->getAvailableLanguages();
150
151 foreach ($languages as $lInfo) {
152 $editData['meta']['currentLangId'][] = $lInfo['ISOcode'];
153 }
154 if (!count($editData['meta']['currentLangId'])) {
155 $editData['meta']['currentLangId'] = array('DEF');
156 }
157 $editData['meta']['currentLangId'] = array_unique($editData['meta']['currentLangId']);
158
159 if ($langChildren || $langDisabled) {
160 $lKeys = array('DEF');
161 } else {
162 $lKeys = $editData['meta']['currentLangId'];
163 }
164
165 // Tabs sheets
166 if (is_array($dataStructArray['sheets'])) {
167 $sKeys = array_keys($dataStructArray['sheets']);
168 } else {
169 $sKeys = array('sDEF');
170 }
171
172 // Traverse languages:
173 foreach($lKeys as $lKey) {
174 foreach($sKeys as $sheet) {
175 $sheetCfg = $dataStructArray['sheets'][$sheet];
176 list ($dataStruct, $sheet) = t3lib_div::resolveSheetDefInDS($dataStructArray,$sheet);
177
178 // Render sheet:
179 if (is_array($dataStruct['ROOT']) && is_array($dataStruct['ROOT']['el'])) {
180 $lang = 'l'.$lKey; // Separate language key
181 $PA['vKeys'] = $langChildren && !$langDisabled ? $editData['meta']['currentLangId'] : array('DEF');
182 $PA['lKey'] = $lang;
183 $PA['callBackMethod_value'] = $callBackMethod_value;
184 $PA['table'] = $table;
185 $PA['field'] = $field;
186 $PA['uid'] = $row['uid'];
187
188 $this->traverseFlexFormXMLData_DS = &$dataStruct;
189 $this->traverseFlexFormXMLData_Data = &$editData;
190
191 // Render flexform:
192 $this->traverseFlexFormXMLData_recurse(
193 $dataStruct['ROOT']['el'],
194 $editData['data'][$sheet][$lang],
195 $PA,
196 'data/'.$sheet.'/'.$lang
197 );
198 } else return 'Data Structure ERROR: No ROOT element found for sheet "'.$sheet.'".';
199 }
200 }
201 } else return 'Data Structure ERROR: '.$dataStructArray;
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 $cc=$k3;
236 $theType = key($v3);
237 $theDat = $v3[$theType];
238 $newSectionEl = $value['el'][$theType];
239 if (is_array($newSectionEl)) {
240 $this->traverseFlexFormXMLData_recurse(
241 array($theType => $newSectionEl),
242 array($theType => $theDat),
243 $PA,
244 $path.'/'.$key.'/el/'.$cc
245 );
246 }
247 }
248 }
249 } else { // Array traversal:
250 $this->traverseFlexFormXMLData_recurse(
251 $value['el'],
252 $editData[$key]['el'],
253 $PA,
254 $path.'/'.$key.'/el'
255 );
256 }
257 } elseif (is_array($value['TCEforms']['config'])) { // Processing a field value:
258
259 foreach($PA['vKeys'] as $vKey) {
260 $vKey = 'v'.$vKey;
261
262 // Call back:
263 if ($PA['callBackMethod_value']) {
264 $this->callBackObj->$PA['callBackMethod_value'](
265 $value,
266 $editData[$key][$vKey],
267 $PA,
268 $path.'/'.$key.'/'.$vKey,
269 $this
270 );
271 }
272 }
273 }
274 }
275 }
276 }
277 }
278
279 /**
280 * Returns an array of available languages to use for FlexForm operations
281 *
282 * @return array
283 */
284 function getAvailableLanguages() {
285 $isL = t3lib_extMgm::isLoaded('static_info_tables');
286
287 // Find all language records in the system:
288 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('static_lang_isocode,title,uid', 'sys_language', 'pid=0'.t3lib_BEfunc::deleteClause('sys_language'), '', 'title');
289
290 // Traverse them:
291 $output = array();
292 $output[0]=array(
293 'uid' => 0,
294 'title' => 'Default language',
295 'ISOcode' => 'DEF'
296 );
297
298 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
299 $output[$row['uid']] = $row;
300
301 if ($isL && $row['static_lang_isocode']) {
302 $rr = t3lib_BEfunc::getRecord('static_languages',$row['static_lang_isocode'],'lg_iso_2');
303 if ($rr['lg_iso_2']) $output[$row['uid']]['ISOcode']=$rr['lg_iso_2'];
304 }
305
306 if (!$output[$row['uid']]['ISOcode']) unset($output[$row['uid']]);
307 }
308 return $output;
309 }
310
311
312
313
314
315
316
317
318
319 /***********************************
320 *
321 * Processing functions
322 *
323 ***********************************/
324
325 /**
326 * 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.
327 * 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.
328 *
329 * @param string Table name
330 * @param string Field name of the flex form field in which the XML is found that should be cleaned.
331 * @param array The record
332 * @return string Clean XML from FlexForm field
333 */
334 function cleanFlexFormXML($table,$field,$row) {
335
336 // New structure:
337 $this->cleanFlexFormXML = array();
338
339 // Create and call iterator object:
340 $flexObj = t3lib_div::makeInstance('t3lib_flexformtools');
341 $flexObj->reNumberIndexesOfSectionData = TRUE;
342 $flexObj->traverseFlexFormXMLData($table,$field,$row,$this,'cleanFlexFormXML_callBackFunction');
343
344 return $this->flexArray2Xml($this->cleanFlexFormXML, TRUE);
345 }
346
347 /**
348 * Call back function for t3lib_flexformtools class
349 * Basically just setting the value in a new array (thus cleaning because only values that are valid are visited!)
350 *
351 * @param array Data structure for the current value
352 * @param mixed Current value
353 * @param array Additional configuration used in calling function
354 * @param string Path of value in DS structure
355 * @param object Object reference to caller
356 * @return void
357 */
358 function cleanFlexFormXML_callBackFunction($dsArr, $data, $PA, $path, &$pObj) {
359 #debug(array($dsArr, $data, $PA),$path);
360 // Just setting value in our own result array, basically replicating the structure:
361 $pObj->setArrayValueByPath($path,$this->cleanFlexFormXML,$data);
362 }
363
364
365
366
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
446 $options = $GLOBALS['TYPO3_CONF_VARS']['BE']['niceFlexFormXMLtags'] ? $this->flexArray2Xml_options : array();
447 $spaceInd = ($GLOBALS['TYPO3_CONF_VARS']['BE']['compactFlexFormXML'] ? -1 : 4);
448 $output = t3lib_div::array2xml($array,'',0,'T3FlexForms', $spaceInd, $options);
449
450 if ($addPrologue) {
451 $output = '<?xml version="1.0" encoding="'.$GLOBALS['LANG']->charSet.'" standalone="yes" ?>'.chr(10).$output;
452 }
453
454 return $output;
455 }
456 }
457
458 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flexformtools.php']) {
459 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flexformtools.php']);
460 }
461 ?>