"TYPO3 4.0.1: typo3_src-4.0.1/t3lib/class.t3lib_flexformtools.php Source File", "datetime" => "Sat Dec 2 19:22:18 2006", "date" => "2 Dec 2006", "doxygenversion" => "1.4.6", "projectname" => "TYPO3 4.0.1", "projectnumber" => "4.0.1" ); get_header($doxygen_vars); ?>
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 2006 Kasper Skaarhoj (kasperYYYY@typo3.com) 00006 * All rights reserved 00007 * 00008 * This script is part of the TYPO3 project. The TYPO3 project is 00009 * free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * The GNU General Public License can be found at 00015 * http://www.gnu.org/copyleft/gpl.html. 00016 * A copy is found in the textfile GPL.txt and important notices to the license 00017 * from the author is found in LICENSE.txt distributed with these scripts. 00018 * 00019 * 00020 * This script is distributed in the hope that it will be useful, 00021 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 * GNU General Public License for more details. 00024 * 00025 * This copyright notice MUST APPEAR in all copies of the script! 00026 ***************************************************************/ 00071 class t3lib_flexformtools { 00072 00073 var $convertCharset = FALSE; // If set, the charset of data XML is converted to system charset. 00074 var $reNumberIndexesOfSectionData = FALSE; // If set, section indexes are re-numbered before processing 00075 00076 // 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 00077 var $flexArray2Xml_options = array( 00078 'parentTagMap' => array( 00079 'data' => 'sheet', 00080 'sheet' => 'language', 00081 'language' => 'field', 00082 'el' => 'field', 00083 'field' => 'value', 00084 'field:el' => 'el', 00085 'el:_IS_NUM' => 'section', 00086 'section' => 'itemType' 00087 ), 00088 'disableTypeAttrib' => 2 00089 ); 00090 00091 // Internal: 00092 var $callBackObj = NULL; // Reference to object called 00093 var $cleanFlexFormXML = array(); // Used for accumulation of clean XML 00094 00105 function traverseFlexFormXMLData($table,$field,$row,&$callBackObj,$callBackMethod_value) { 00106 00107 if (!is_array($GLOBALS['TCA'][$table]) || !is_array($GLOBALS['TCA'][$table]['columns'][$field])) { 00108 return 'TCA table/field was not defined.'; 00109 } 00110 00111 $this->callBackObj = &$callBackObj; 00112 00113 // Get Data Structure: 00114 $dataStructArray = t3lib_BEfunc::getFlexFormDS($GLOBALS['TCA'][$table]['columns'][$field]['config'],$row,$table); 00115 00116 // If data structure was ok, proceed: 00117 if (is_array($dataStructArray)) { 00118 00119 // Get flexform XML data: 00120 $xmlData = $row[$field]; 00121 00122 // Convert charset: 00123 if ($this->convertCharset) { 00124 $xmlHeaderAttributes = t3lib_div::xmlGetHeaderAttribs($xmlData); 00125 $storeInCharset = strtolower($xmlHeaderAttributes['encoding']); 00126 if ($storeInCharset) { 00127 $currentCharset = $GLOBALS['LANG']->charSet; 00128 $xmlData = $GLOBALS['LANG']->csConvObj->conv($xmlData,$storeInCharset,$currentCharset,1); 00129 } 00130 } 00131 00132 $editData = t3lib_div::xml2array($xmlData); 00133 if (!is_array($editData)) { 00134 return 'Parsing error: '.$editData; 00135 } 00136 00137 // Language settings: 00138 $langChildren = $dataStructArray['meta']['langChildren'] ? 1 : 0; 00139 $langDisabled = $dataStructArray['meta']['langDisable'] ? 1 : 0; 00140 00141 // empty or invalid <meta> 00142 if (!is_array($editData['meta'])) { 00143 $editData['meta'] = array(); 00144 } 00145 $editData['meta']['currentLangId'] = array(); 00146 $languages = $this->getAvailableLanguages(); 00147 00148 foreach ($languages as $lInfo) { 00149 $editData['meta']['currentLangId'][] = $lInfo['ISOcode']; 00150 } 00151 if (!count($editData['meta']['currentLangId'])) { 00152 $editData['meta']['currentLangId'] = array('DEF'); 00153 } 00154 $editData['meta']['currentLangId'] = array_unique($editData['meta']['currentLangId']); 00155 00156 if ($langChildren || $langDisabled) { 00157 $lKeys = array('DEF'); 00158 } else { 00159 $lKeys = $editData['meta']['currentLangId']; 00160 } 00161 00162 // Tabs sheets 00163 if (is_array($dataStructArray['sheets'])) { 00164 $sKeys = array_keys($dataStructArray['sheets']); 00165 } else { 00166 $sKeys = array('sDEF'); 00167 } 00168 00169 // Traverse languages: 00170 foreach($lKeys as $lKey) { 00171 foreach($sKeys as $sheet) { 00172 $sheetCfg = $dataStructArray['sheets'][$sheet]; 00173 list ($dataStruct, $sheet) = t3lib_div::resolveSheetDefInDS($dataStructArray,$sheet); 00174 00175 // Render sheet: 00176 if (is_array($dataStruct['ROOT']) && is_array($dataStruct['ROOT']['el'])) { 00177 $lang = 'l'.$lKey; // Separate language key 00178 $PA['vKeys'] = $langChildren && !$langDisabled ? $editData['meta']['currentLangId'] : array('DEF'); 00179 $PA['lKey'] = $lang; 00180 $PA['callBackMethod_value'] = $callBackMethod_value; 00181 $PA['table'] = $table; 00182 $PA['field'] = $field; 00183 $PA['uid'] = $row['uid']; 00184 00185 // Render flexform: 00186 $this->traverseFlexFormXMLData_recurse( 00187 $dataStruct['ROOT']['el'], 00188 $editData['data'][$sheet][$lang], 00189 $PA, 00190 'data/'.$sheet.'/'.$lang 00191 ); 00192 } else return 'Data Structure ERROR: No ROOT element found for sheet "'.$sheet.'".'; 00193 } 00194 } 00195 } else return 'Data Structure ERROR: '.$dataStructArray; 00196 } 00197 00207 function traverseFlexFormXMLData_recurse($dataStruct,$editData,&$PA,$path='') { 00208 00209 if (is_array($dataStruct)) { 00210 foreach($dataStruct as $key => $value) { 00211 if (is_array($value)) { // The value of each entry must be an array. 00212 00213 if ($value['type']=='array') { 00214 if ($value['section']) { // Array (Section) traversal: 00215 00216 $cc = 0; 00217 if (is_array($editData[$key]['el'])) { 00218 00219 if ($this->reNumberIndexesOfSectionData) { 00220 $temp = array(); 00221 $c3=0; 00222 foreach($editData[$key]['el'] as $v3) { 00223 $temp[++$c3] = $v3; 00224 } 00225 $editData[$key]['el'] = $temp; 00226 } 00227 00228 foreach($editData[$key]['el'] as $k3 => $v3) { 00229 $cc=$k3; 00230 $theType = key($v3); 00231 $theDat = $v3[$theType]; 00232 $newSectionEl = $value['el'][$theType]; 00233 if (is_array($newSectionEl)) { 00234 $this->traverseFlexFormXMLData_recurse( 00235 array($theType => $newSectionEl), 00236 array($theType => $theDat), 00237 $PA, 00238 $path.'/'.$key.'/el/'.$cc 00239 ); 00240 } 00241 } 00242 } 00243 } else { // Array traversal: 00244 $this->traverseFlexFormXMLData_recurse( 00245 $value['el'], 00246 $editData[$key]['el'], 00247 $PA, 00248 $path.'/'.$key.'/el' 00249 ); 00250 } 00251 } elseif (is_array($value['TCEforms']['config'])) { // Processing a field value: 00252 00253 foreach($PA['vKeys'] as $vKey) { 00254 $vKey = 'v'.$vKey; 00255 00256 // Call back: 00257 if ($PA['callBackMethod_value']) { 00258 $this->callBackObj->$PA['callBackMethod_value']( 00259 $value, 00260 $editData[$key][$vKey], 00261 $PA, 00262 $path.'/'.$key.'/'.$vKey, 00263 $this 00264 ); 00265 } 00266 } 00267 } 00268 } 00269 } 00270 } 00271 } 00272 00278 function getAvailableLanguages() { 00279 $isL = t3lib_extMgm::isLoaded('static_info_tables'); 00280 00281 // Find all language records in the system: 00282 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('static_lang_isocode,title,uid', 'sys_language', 'pid=0'.t3lib_BEfunc::deleteClause('sys_language'), '', 'title'); 00283 00284 // Traverse them: 00285 $output = array(); 00286 $output[0]=array( 00287 'uid' => 0, 00288 'title' => 'Default language', 00289 'ISOcode' => 'DEF' 00290 ); 00291 00292 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { 00293 $output[$row['uid']] = $row; 00294 00295 if ($isL && $row['static_lang_isocode']) { 00296 $rr = t3lib_BEfunc::getRecord('static_languages',$row['static_lang_isocode'],'lg_iso_2'); 00297 if ($rr['lg_iso_2']) $output[$row['uid']]['ISOcode']=$rr['lg_iso_2']; 00298 } 00299 00300 if (!$output[$row['uid']]['ISOcode']) unset($output[$row['uid']]); 00301 } 00302 return $output; 00303 } 00304 00305 00306 00307 00308 00309 00310 00311 00312 00313 /*********************************** 00314 * 00315 * Processing functions 00316 * 00317 ***********************************/ 00318 00328 function cleanFlexFormXML($table,$field,$row) { 00329 00330 // New structure: 00331 $this->cleanFlexFormXML = array(); 00332 00333 // Create and call iterator object: 00334 $flexObj = t3lib_div::makeInstance('t3lib_flexformtools'); 00335 $flexObj->reNumberIndexesOfSectionData = TRUE; 00336 $flexObj->traverseFlexFormXMLData($table,$field,$row,$this,'cleanFlexFormXML_callBackFunction'); 00337 00338 return $this->flexArray2Xml($this->cleanFlexFormXML, TRUE); 00339 } 00340 00352 function cleanFlexFormXML_callBackFunction($dsArr, $data, $PA, $path, &$pObj) { 00353 #debug(array($dsArr, $data, $PA),$path); 00354 // Just setting value in our own result array, basically replicating the structure: 00355 $pObj->setArrayValueByPath($path,$this->cleanFlexFormXML,$data); 00356 } 00357 00358 00359 00360 00361 00362 00363 00364 00365 00366 /*********************************** 00367 * 00368 * Multi purpose functions 00369 * 00370 ***********************************/ 00371 00379 function &getArrayValueByPath($pathArray,&$array) { 00380 if (!is_array($pathArray)) { 00381 $pathArray = explode('/',$pathArray); 00382 } 00383 if (is_array($array)) { 00384 if (count($pathArray)) { 00385 $key = array_shift($pathArray); 00386 00387 if (isset($array[$key])) { 00388 if (!count($pathArray)) { 00389 return $array[$key]; 00390 } else { 00391 return $this->getArrayValueByPath($pathArray,$array[$key]); 00392 } 00393 } else { 00394 return NULL; 00395 } 00396 } 00397 } 00398 } 00399 00408 function setArrayValueByPath($pathArray,&$array,$value) { 00409 if (isset($value)) { 00410 if (!is_array($pathArray)) { 00411 $pathArray = explode('/',$pathArray); 00412 } 00413 if (is_array($array)) { 00414 if (count($pathArray)) { 00415 $key = array_shift($pathArray); 00416 00417 if (!count($pathArray)) { 00418 $array[$key] = $value; 00419 return TRUE; 00420 } else { 00421 if (!isset($array[$key])) { 00422 $array[$key] = array(); 00423 } 00424 return $this->setArrayValueByPath($pathArray,$array[$key],$value); 00425 } 00426 } 00427 } 00428 } 00429 } 00430 00438 function flexArray2Xml($array, $addPrologue=FALSE) { 00439 00440 $options = $GLOBALS['TYPO3_CONF_VARS']['BE']['niceFlexFormXMLtags'] ? $this->flexArray2Xml_options : array(); 00441 $output = t3lib_div::array2xml($array,'',0,'T3FlexForms',4, $options); 00442 00443 if ($addPrologue) { 00444 $output = '<?xml version="1.0" encoding="'.$GLOBALS['LANG']->charSet.'" standalone="yes" ?>'.chr(10).$output; 00445 } 00446 00447 return $output; 00448 } 00449 } 00450 00451 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flexformtools.php']) { 00452 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flexformtools.php']); 00453 } 00454 ?>