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