00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00071 class t3lib_flexformtools {
00072
00073 var $convertCharset = FALSE;
00074 var $reNumberIndexesOfSectionData = FALSE;
00075
00076 var $traverseFlexFormXMLData_DS = array();
00077 var $traverseFlexFormXMLData_Data = array();
00078
00079
00080 var $flexArray2Xml_options = array(
00081 'parentTagMap' => array(
00082 'data' => 'sheet',
00083 'sheet' => 'language',
00084 'language' => 'field',
00085 'el' => 'field',
00086 'field' => 'value',
00087 'field:el' => 'el',
00088 'el:_IS_NUM' => 'section',
00089 'section' => 'itemType'
00090 ),
00091 'disableTypeAttrib' => 2
00092 );
00093
00094
00098 var $callBackObj = NULL;
00099 var $cleanFlexFormXML = array();
00100
00111 function traverseFlexFormXMLData($table,$field,$row,&$callBackObj,$callBackMethod_value) {
00112
00113 if (!is_array($GLOBALS['TCA'][$table]) || !is_array($GLOBALS['TCA'][$table]['columns'][$field])) {
00114 return 'TCA table/field was not defined.';
00115 }
00116
00117 $this->callBackObj = &$callBackObj;
00118
00119
00120 $dataStructArray = t3lib_BEfunc::getFlexFormDS($GLOBALS['TCA'][$table]['columns'][$field]['config'],$row,$table);
00121
00122
00123 if (is_array($dataStructArray)) {
00124
00125
00126 $xmlData = $row[$field];
00127
00128
00129 if ($this->convertCharset) {
00130 $xmlHeaderAttributes = t3lib_div::xmlGetHeaderAttribs($xmlData);
00131 $storeInCharset = strtolower($xmlHeaderAttributes['encoding']);
00132 if ($storeInCharset) {
00133 $currentCharset = $GLOBALS['LANG']->charSet;
00134 $xmlData = $GLOBALS['LANG']->csConvObj->conv($xmlData,$storeInCharset,$currentCharset,1);
00135 }
00136 }
00137
00138 $editData = t3lib_div::xml2array($xmlData);
00139 if (!is_array($editData)) {
00140 return 'Parsing error: '.$editData;
00141 }
00142
00143
00144 $langChildren = $dataStructArray['meta']['langChildren'] ? 1 : 0;
00145 $langDisabled = $dataStructArray['meta']['langDisable'] ? 1 : 0;
00146
00147
00148 if (!is_array($editData['meta'])) {
00149 $editData['meta'] = array();
00150 }
00151 $editData['meta']['currentLangId'] = array();
00152 $languages = $this->getAvailableLanguages();
00153
00154 foreach ($languages as $lInfo) {
00155 $editData['meta']['currentLangId'][] = $lInfo['ISOcode'];
00156 }
00157 if (!count($editData['meta']['currentLangId'])) {
00158 $editData['meta']['currentLangId'] = array('DEF');
00159 }
00160 $editData['meta']['currentLangId'] = array_unique($editData['meta']['currentLangId']);
00161
00162 if ($langChildren || $langDisabled) {
00163 $lKeys = array('DEF');
00164 } else {
00165 $lKeys = $editData['meta']['currentLangId'];
00166 }
00167
00168
00169 if (is_array($dataStructArray['sheets'])) {
00170 $sKeys = array_keys($dataStructArray['sheets']);
00171 } else {
00172 $sKeys = array('sDEF');
00173 }
00174
00175
00176 foreach($lKeys as $lKey) {
00177 foreach($sKeys as $sheet) {
00178 $sheetCfg = $dataStructArray['sheets'][$sheet];
00179 list ($dataStruct, $sheet) = t3lib_div::resolveSheetDefInDS($dataStructArray,$sheet);
00180
00181
00182 if (is_array($dataStruct['ROOT']) && is_array($dataStruct['ROOT']['el'])) {
00183 $lang = 'l'.$lKey;
00184 $PA['vKeys'] = $langChildren && !$langDisabled ? $editData['meta']['currentLangId'] : array('DEF');
00185 $PA['lKey'] = $lang;
00186 $PA['callBackMethod_value'] = $callBackMethod_value;
00187 $PA['table'] = $table;
00188 $PA['field'] = $field;
00189 $PA['uid'] = $row['uid'];
00190
00191 $this->traverseFlexFormXMLData_DS = &$dataStruct;
00192 $this->traverseFlexFormXMLData_Data = &$editData;
00193
00194
00195 $this->traverseFlexFormXMLData_recurse(
00196 $dataStruct['ROOT']['el'],
00197 $editData['data'][$sheet][$lang],
00198 $PA,
00199 'data/'.$sheet.'/'.$lang
00200 );
00201 } else return 'Data Structure ERROR: No ROOT element found for sheet "'.$sheet.'".';
00202 }
00203 }
00204 } else return 'Data Structure ERROR: '.$dataStructArray;
00205 }
00206
00216 function traverseFlexFormXMLData_recurse($dataStruct,$editData,&$PA,$path='') {
00217
00218 if (is_array($dataStruct)) {
00219 foreach($dataStruct as $key => $value) {
00220 if (is_array($value)) {
00221
00222 if ($value['type']=='array') {
00223 if ($value['section']) {
00224
00225 $cc = 0;
00226 if (is_array($editData[$key]['el'])) {
00227
00228 if ($this->reNumberIndexesOfSectionData) {
00229 $temp = array();
00230 $c3=0;
00231 foreach($editData[$key]['el'] as $v3) {
00232 $temp[++$c3] = $v3;
00233 }
00234 $editData[$key]['el'] = $temp;
00235 }
00236
00237 foreach($editData[$key]['el'] as $k3 => $v3) {
00238 if (is_array($v3)) {
00239 $cc=$k3;
00240 $theType = key($v3);
00241 $theDat = $v3[$theType];
00242 $newSectionEl = $value['el'][$theType];
00243 if (is_array($newSectionEl)) {
00244 $this->traverseFlexFormXMLData_recurse(
00245 array($theType => $newSectionEl),
00246 array($theType => $theDat),
00247 $PA,
00248 $path.'/'.$key.'/el/'.$cc
00249 );
00250 }
00251 }
00252 }
00253 }
00254 } else {
00255 $this->traverseFlexFormXMLData_recurse(
00256 $value['el'],
00257 $editData[$key]['el'],
00258 $PA,
00259 $path.'/'.$key.'/el'
00260 );
00261 }
00262 } elseif (is_array($value['TCEforms']['config'])) {
00263
00264 foreach($PA['vKeys'] as $vKey) {
00265 $vKey = 'v'.$vKey;
00266
00267
00268 if ($PA['callBackMethod_value']) {
00269 $this->callBackObj->$PA['callBackMethod_value'](
00270 $value,
00271 $editData[$key][$vKey],
00272 $PA,
00273 $path.'/'.$key.'/'.$vKey,
00274 $this
00275 );
00276 }
00277 }
00278 }
00279 }
00280 }
00281 }
00282 }
00283
00289 function getAvailableLanguages() {
00290 $isL = t3lib_extMgm::isLoaded('static_info_tables');
00291
00292
00293 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('static_lang_isocode,title,uid', 'sys_language', 'pid=0'.t3lib_BEfunc::deleteClause('sys_language'), '', 'title');
00294
00295
00296 $output = array();
00297 $output[0]=array(
00298 'uid' => 0,
00299 'title' => 'Default language',
00300 'ISOcode' => 'DEF'
00301 );
00302
00303 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
00304 $output[$row['uid']] = $row;
00305
00306 if ($isL && $row['static_lang_isocode']) {
00307 $rr = t3lib_BEfunc::getRecord('static_languages',$row['static_lang_isocode'],'lg_iso_2');
00308 if ($rr['lg_iso_2']) $output[$row['uid']]['ISOcode']=$rr['lg_iso_2'];
00309 }
00310
00311 if (!$output[$row['uid']]['ISOcode']) unset($output[$row['uid']]);
00312 }
00313 return $output;
00314 }
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00339 function cleanFlexFormXML($table,$field,$row) {
00340
00341
00342 $this->cleanFlexFormXML = array();
00343
00344
00345 $flexObj = t3lib_div::makeInstance('t3lib_flexformtools');
00346 $flexObj->reNumberIndexesOfSectionData = TRUE;
00347 $flexObj->traverseFlexFormXMLData($table,$field,$row,$this,'cleanFlexFormXML_callBackFunction');
00348
00349 return $this->flexArray2Xml($this->cleanFlexFormXML, TRUE);
00350 }
00351
00363 function cleanFlexFormXML_callBackFunction($dsArr, $data, $PA, $path, &$pObj) {
00364 #debug(array($dsArr, $data, $PA),$path);
00365
00366 $pObj->setArrayValueByPath($path,$this->cleanFlexFormXML,$data);
00367 }
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00390 function &getArrayValueByPath($pathArray,&$array) {
00391 if (!is_array($pathArray)) {
00392 $pathArray = explode('/',$pathArray);
00393 }
00394 if (is_array($array)) {
00395 if (count($pathArray)) {
00396 $key = array_shift($pathArray);
00397
00398 if (isset($array[$key])) {
00399 if (!count($pathArray)) {
00400 return $array[$key];
00401 } else {
00402 return $this->getArrayValueByPath($pathArray,$array[$key]);
00403 }
00404 } else {
00405 return NULL;
00406 }
00407 }
00408 }
00409 }
00410
00419 function setArrayValueByPath($pathArray,&$array,$value) {
00420 if (isset($value)) {
00421 if (!is_array($pathArray)) {
00422 $pathArray = explode('/',$pathArray);
00423 }
00424 if (is_array($array)) {
00425 if (count($pathArray)) {
00426 $key = array_shift($pathArray);
00427
00428 if (!count($pathArray)) {
00429 $array[$key] = $value;
00430 return TRUE;
00431 } else {
00432 if (!isset($array[$key])) {
00433 $array[$key] = array();
00434 }
00435 return $this->setArrayValueByPath($pathArray,$array[$key],$value);
00436 }
00437 }
00438 }
00439 }
00440 }
00441
00449 function flexArray2Xml($array, $addPrologue=FALSE) {
00450
00451 $options = $GLOBALS['TYPO3_CONF_VARS']['BE']['niceFlexFormXMLtags'] ? $this->flexArray2Xml_options : array();
00452 $spaceInd = ($GLOBALS['TYPO3_CONF_VARS']['BE']['compactFlexFormXML'] ? -1 : 4);
00453 $output = t3lib_div::array2xml($array,'',0,'T3FlexForms', $spaceInd, $options);
00454
00455 if ($addPrologue) {
00456 $output = '<?xml version="1.0" encoding="'.$GLOBALS['LANG']->charSet.'" standalone="yes" ?>'.chr(10).$output;
00457 }
00458
00459 return $output;
00460 }
00461 }
00462
00463 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flexformtools.php']) {
00464 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flexformtools.php']);
00465 }
00466 ?>