File: class.t3lib_flexformtools.php

package info (click to toggle)
typo3-src 4.0.2%2Bdebian-3
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 29,856 kB
  • ctags: 33,382
  • sloc: php: 134,523; xml: 6,976; sql: 1,084; sh: 168; makefile: 45
file content (454 lines) | stat: -rw-r--r-- 14,360 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2006 Kasper Skaarhoj (kasperYYYY@typo3.com)
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*  A copy is found in the textfile GPL.txt and important notices to the license
*  from the author is found in LICENSE.txt distributed with these scripts.
*
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * Contains functions for manipulating flex form data
 *
 * $Id: class.t3lib_flexformtools.php 1608 2006-07-07 19:10:26Z mundaun $
 *
 * @author	Kasper Skaarhoj <kasperYYYY@typo3.com>
 */
/**
 * [CLASS/FUNCTION INDEX of SCRIPT]
 *
 *
 *
 *   71: class t3lib_flexformtools
 *  105:     function traverseFlexFormXMLData($table,$field,$row,&$callBackObj,$callBackMethod_value)
 *  203:     function traverseFlexFormXMLData_recurse($dataStruct,$editData,&$PA,$path='')
 *  274:     function getAvailableLanguages()
 *
 *              SECTION: Processing functions
 *  323:     function cleanFlexFormXML($table,$field,$row)
 *  347:     function cleanFlexFormXML_callBackFunction($dsArr, $data, $PA, $path, &$pObj)
 *
 *              SECTION: Multi purpose functions
 *  374:     function &getArrayValueByPath($pathArray,&$array)
 *  403:     function setArrayValueByPath($pathArray,&$array,$value)
 *  433:     function flexArray2Xml($array, $addPrologue=FALSE)
 *
 * TOTAL FUNCTIONS: 8
 * (This index is automatically created/updated by the extension "extdeveval")
 *
 */







/**
 * Contains functions for manipulating flex form data
 *
 * @author	Kasper Skaarhoj <kasperYYYY@typo3.com>
 * @package TYPO3
 * @subpackage t3lib
 */
class t3lib_flexformtools {

	var $convertCharset = FALSE;		// If set, the charset of data XML is converted to system charset.
	var $reNumberIndexesOfSectionData = FALSE;	// If set, section indexes are re-numbered before processing

		// 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
	var $flexArray2Xml_options = array(
			'parentTagMap' => array(
				'data' => 'sheet',
				'sheet' => 'language',
				'language' => 'field',
				'el' => 'field',
				'field' => 'value',
				'field:el' => 'el',
				'el:_IS_NUM' => 'section',
				'section' => 'itemType'
			),
			'disableTypeAttrib' => 2
		);

		// Internal:
	var $callBackObj = NULL;		// Reference to object called
	var $cleanFlexFormXML = array();		// Used for accumulation of clean XML

	/**
	 * Handler for Flex Forms
	 *
	 * @param	string		The table name of the record
	 * @param	string		The field name of the flexform field to work on
	 * @param	array		The record data array
	 * @param	object		Object (passed by reference) in which the call back function is located
	 * @param	string		Method name of call back function in object for values
	 * @return	boolean		If true, error happened (error string returned)
	 */
	function traverseFlexFormXMLData($table,$field,$row,&$callBackObj,$callBackMethod_value)	{

		if (!is_array($GLOBALS['TCA'][$table]) || !is_array($GLOBALS['TCA'][$table]['columns'][$field])) 	{
			return 'TCA table/field was not defined.';
		}

		$this->callBackObj = &$callBackObj;

			// Get Data Structure:
		$dataStructArray = t3lib_BEfunc::getFlexFormDS($GLOBALS['TCA'][$table]['columns'][$field]['config'],$row,$table);

			// If data structure was ok, proceed:
		if (is_array($dataStructArray))	{

				// Get flexform XML data:
			$xmlData = $row[$field];

				// Convert charset:
			if ($this->convertCharset)	{
				$xmlHeaderAttributes = t3lib_div::xmlGetHeaderAttribs($xmlData);
				$storeInCharset = strtolower($xmlHeaderAttributes['encoding']);
				if ($storeInCharset)	{
					$currentCharset = $GLOBALS['LANG']->charSet;
					$xmlData = $GLOBALS['LANG']->csConvObj->conv($xmlData,$storeInCharset,$currentCharset,1);
				}
			}

			$editData = t3lib_div::xml2array($xmlData);
			if (!is_array($editData))	{
				return 'Parsing error: '.$editData;
			}

				// Language settings:
			$langChildren = $dataStructArray['meta']['langChildren'] ? 1 : 0;
			$langDisabled = $dataStructArray['meta']['langDisable'] ? 1 : 0;

				// empty or invalid <meta>
			if (!is_array($editData['meta']))	{
				$editData['meta'] = array();
			}
			$editData['meta']['currentLangId'] = array();
			$languages = $this->getAvailableLanguages();

			foreach ($languages as $lInfo)	{
				$editData['meta']['currentLangId'][] = $lInfo['ISOcode'];
			}
			if (!count($editData['meta']['currentLangId']))	{
				$editData['meta']['currentLangId'] = array('DEF');
			}
			$editData['meta']['currentLangId'] = array_unique($editData['meta']['currentLangId']);

			if ($langChildren || $langDisabled)	{
				$lKeys = array('DEF');
			} else {
				$lKeys = $editData['meta']['currentLangId'];
			}

				// Tabs sheets
			if (is_array($dataStructArray['sheets']))	{
				$sKeys = array_keys($dataStructArray['sheets']);
			} else {
				$sKeys = array('sDEF');
			}

				// Traverse languages:
			foreach($lKeys as $lKey)	{
				foreach($sKeys as $sheet)	{
					$sheetCfg = $dataStructArray['sheets'][$sheet];
					list ($dataStruct, $sheet) = t3lib_div::resolveSheetDefInDS($dataStructArray,$sheet);

						// Render sheet:
					if (is_array($dataStruct['ROOT']) && is_array($dataStruct['ROOT']['el']))		{
						$lang = 'l'.$lKey;	// Separate language key
						$PA['vKeys'] = $langChildren && !$langDisabled ? $editData['meta']['currentLangId'] : array('DEF');
						$PA['lKey'] = $lang;
						$PA['callBackMethod_value'] = $callBackMethod_value;
						$PA['table'] = $table;
						$PA['field'] = $field;
						$PA['uid'] = $row['uid'];

							// Render flexform:
						$this->traverseFlexFormXMLData_recurse(
							$dataStruct['ROOT']['el'],
							$editData['data'][$sheet][$lang],
							$PA,
							'data/'.$sheet.'/'.$lang
						);
					} else return 'Data Structure ERROR: No ROOT element found for sheet "'.$sheet.'".';
				}
			}
		} else return 'Data Structure ERROR: '.$dataStructArray;
	}

	/**
	 * Recursively traversing flexform data according to data structure and element data
	 *
	 * @param	array		(Part of) data structure array that applies to the sub section of the flexform data we are processing
	 * @param	array		(Part of) edit data array, reflecting current part of data structure
	 * @param	array		Additional parameters passed.
	 * @param	string		Telling the "path" to the element in the flexform XML
	 * @return	array
	 */
	function traverseFlexFormXMLData_recurse($dataStruct,$editData,&$PA,$path='')	{

		if (is_array($dataStruct))	{
			foreach($dataStruct as $key => $value)	{
				if (is_array($value))	{	// The value of each entry must be an array.

					if ($value['type']=='array')	{
						if ($value['section'])	{		// Array (Section) traversal:

							$cc = 0;
							if (is_array($editData[$key]['el']))	{

								if ($this->reNumberIndexesOfSectionData)	{
									$temp = array();
									$c3=0;
									foreach($editData[$key]['el'] as $v3)	{
										$temp[++$c3] = $v3;
									}
									$editData[$key]['el'] = $temp;
								}

								foreach($editData[$key]['el'] as $k3 => $v3)	{
									$cc=$k3;
									$theType = key($v3);
									$theDat = $v3[$theType];
									$newSectionEl = $value['el'][$theType];
									if (is_array($newSectionEl))	{
										$this->traverseFlexFormXMLData_recurse(
											array($theType => $newSectionEl),
											array($theType => $theDat),
											$PA,
											$path.'/'.$key.'/el/'.$cc
										);
									}
								}
							}
						} else {	// Array traversal:
							$this->traverseFlexFormXMLData_recurse(
								$value['el'],
								$editData[$key]['el'],
								$PA,
								$path.'/'.$key.'/el'
							);
						}
					} elseif (is_array($value['TCEforms']['config'])) {	// Processing a field value:

						foreach($PA['vKeys'] as $vKey)	{
							$vKey = 'v'.$vKey;

								// Call back:
							if ($PA['callBackMethod_value'])	{
								$this->callBackObj->$PA['callBackMethod_value'](
									$value,
									$editData[$key][$vKey],
									$PA,
									$path.'/'.$key.'/'.$vKey,
									$this
								);
							}
						}
					}
				}
			}
		}
	}

	/**
	 * Returns an array of available languages to use for FlexForm operations
	 *
	 * @return	array
	 */
	function getAvailableLanguages()	{
		$isL = t3lib_extMgm::isLoaded('static_info_tables');

			// Find all language records in the system:
		$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('static_lang_isocode,title,uid', 'sys_language', 'pid=0'.t3lib_BEfunc::deleteClause('sys_language'), '', 'title');

			// Traverse them:
		$output = array();
		$output[0]=array(
			'uid' => 0,
			'title' => 'Default language',
			'ISOcode' => 'DEF'
		);

		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))	{
			$output[$row['uid']] = $row;

			if ($isL && $row['static_lang_isocode'])	{
				$rr = t3lib_BEfunc::getRecord('static_languages',$row['static_lang_isocode'],'lg_iso_2');
				if ($rr['lg_iso_2'])	$output[$row['uid']]['ISOcode']=$rr['lg_iso_2'];
			}

			if (!$output[$row['uid']]['ISOcode'])	unset($output[$row['uid']]);
		}
		return $output;
	}









	/***********************************
	 *
	 * Processing functions
	 *
	 ***********************************/

	/**
	 * 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.
	 * 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.
	 *
	 * @param	string		Table name
	 * @param	string		Field name of the flex form field in which the XML is found that should be cleaned.
	 * @param	array		The record
	 * @return	string		Clean XML from FlexForm field
	 */
	function cleanFlexFormXML($table,$field,$row)	{

			// New structure:
		$this->cleanFlexFormXML = array();

			// Create and call iterator object:
		$flexObj = t3lib_div::makeInstance('t3lib_flexformtools');
		$flexObj->reNumberIndexesOfSectionData = TRUE;
		$flexObj->traverseFlexFormXMLData($table,$field,$row,$this,'cleanFlexFormXML_callBackFunction');

		return $this->flexArray2Xml($this->cleanFlexFormXML, TRUE);
	}

	/**
	 * Call back function for t3lib_flexformtools class
	 * Basically just setting the value in a new array (thus cleaning because only values that are valid are visited!)
	 *
	 * @param	array		Data structure for the current value
	 * @param	mixed		Current value
	 * @param	array		Additional configuration used in calling function
	 * @param	string		Path of value in DS structure
	 * @param	object		Object reference to caller
	 * @return	void
	 */
	function cleanFlexFormXML_callBackFunction($dsArr, $data, $PA, $path, &$pObj)	{
		#debug(array($dsArr, $data, $PA),$path);
			// Just setting value in our own result array, basically replicating the structure:
		$pObj->setArrayValueByPath($path,$this->cleanFlexFormXML,$data);
	}









	/***********************************
	 *
	 * Multi purpose functions
	 *
	 ***********************************/

	/**
	 * Get a value from a multi-dimensional array by giving a path "../../.." pointing to the element
	 *
	 * @param	string		The path pointing to the value field, eg. test/2/title to access $array['test'][2]['title']
	 * @param	array		Array to get value from. Passed by reference so the value returned can be used to change the value in the array!
	 * @return	mixed		Value returned
	 */
	function &getArrayValueByPath($pathArray,&$array)	{
		if (!is_array($pathArray))	{
			$pathArray = explode('/',$pathArray);
		}
		if (is_array($array))	{
			if (count($pathArray))	{
				$key = array_shift($pathArray);

				if (isset($array[$key]))	{
					if (!count($pathArray))	{
						return $array[$key];
					} else {
						return $this->getArrayValueByPath($pathArray,$array[$key]);
					}
				} else {
					return NULL;
				}
			}
		}
	}

	/**
	 * Set a value in a multi-dimensional array by giving a path "../../.." pointing to the element
	 *
	 * @param	string		The path pointing to the value field, eg. test/2/title to access $array['test'][2]['title']
	 * @param	array		Array to set value in. Passed by reference so the value returned can be used to change the value in the array!
	 * @param	mixed		Value to set
	 * @return	mixed		Value returned
	 */
	function setArrayValueByPath($pathArray,&$array,$value)	{
		if (isset($value))	 {
			if (!is_array($pathArray))	{
				$pathArray = explode('/',$pathArray);
			}
			if (is_array($array))	{
				if (count($pathArray))	{
					$key = array_shift($pathArray);

					if (!count($pathArray))	{
						$array[$key] = $value;
						return TRUE;
					} else {
						if (!isset($array[$key]))	{
							$array[$key] = array();
						}
						return $this->setArrayValueByPath($pathArray,$array[$key],$value);
					}
				}
			}
		}
	}

	/**
	 * Convert FlexForm data array to XML
	 *
	 * @param	array		Array to output in <T3FlexForms> XML
	 * @param	boolean		If set, the XML prologue is returned as well.
	 * @return	string		XML content.
	 */
	function flexArray2Xml($array, $addPrologue=FALSE)	{

		$options = $GLOBALS['TYPO3_CONF_VARS']['BE']['niceFlexFormXMLtags'] ? $this->flexArray2Xml_options : array();
		$output = t3lib_div::array2xml($array,'',0,'T3FlexForms',4, $options);

		if ($addPrologue)	{
			$output = '<?xml version="1.0" encoding="'.$GLOBALS['LANG']->charSet.'" standalone="yes" ?>'.chr(10).$output;
		}

		return $output;
	}
}

if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flexformtools.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flexformtools.php']);
}
?>