Documentation TYPO3 par Ameos

class.t3lib_flexformtools.php

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                         $editData['meta']['currentLangId'] = array();
00142                         $languages = $this->getAvailableLanguages();
00143 
00144                         foreach($languages as $lInfo)   {
00145                                 $editData['meta']['currentLangId'][] = $lInfo['ISOcode'];
00146                         }
00147                         if (!count($editData['meta']['currentLangId'])) {
00148                                 $editData['meta']['currentLangId'] = array('DEF');
00149                         }
00150                         $editData['meta']['currentLangId'] = array_unique($editData['meta']['currentLangId']);
00151 
00152                         if ($langChildren || $langDisabled)     {
00153                                 $lKeys = array('DEF');
00154                         } else {
00155                                 $lKeys = $editData['meta']['currentLangId'];
00156                         }
00157 
00158                                 // Tabs sheets
00159                         if (is_array($dataStructArray['sheets']))       {
00160                                 $sKeys = array_keys($dataStructArray['sheets']);
00161                         } else {
00162                                 $sKeys = array('sDEF');
00163                         }
00164 
00165                                 // Traverse languages:
00166                         foreach($lKeys as $lKey)        {
00167                                 foreach($sKeys as $sheet)       {
00168                                         $sheetCfg = $dataStructArray['sheets'][$sheet];
00169                                         list ($dataStruct, $sheet) = t3lib_div::resolveSheetDefInDS($dataStructArray,$sheet);
00170 
00171                                                 // Render sheet:
00172                                         if (is_array($dataStruct['ROOT']) && is_array($dataStruct['ROOT']['el']))               {
00173                                                 $lang = 'l'.$lKey;      // Separate language key
00174                                                 $PA['vKeys'] = $langChildren && !$langDisabled ? $editData['meta']['currentLangId'] : array('DEF');
00175                                                 $PA['lKey'] = $lang;
00176                                                 $PA['callBackMethod_value'] = $callBackMethod_value;
00177                                                 $PA['table'] = $table;
00178                                                 $PA['field'] = $field;
00179                                                 $PA['uid'] = $row['uid'];
00180 
00181                                                         // Render flexform:
00182                                                 $this->traverseFlexFormXMLData_recurse(
00183                                                         $dataStruct['ROOT']['el'],
00184                                                         $editData['data'][$sheet][$lang],
00185                                                         $PA,
00186                                                         'data/'.$sheet.'/'.$lang
00187                                                 );
00188                                         } else return 'Data Structure ERROR: No ROOT element found for sheet "'.$sheet.'".';
00189                                 }
00190                         }
00191                 } else return 'Data Structure ERROR: '.$dataStructArray;
00192         }
00193 
00203         function traverseFlexFormXMLData_recurse($dataStruct,$editData,&$PA,$path='')   {
00204 
00205                 if (is_array($dataStruct))      {
00206                         foreach($dataStruct as $key => $value)  {
00207                                 if (is_array($value))   {       // The value of each entry must be an array.
00208 
00209                                         if ($value['type']=='array')    {
00210                                                 if ($value['section'])  {               // Array (Section) traversal:
00211 
00212                                                         $cc = 0;
00213                                                         if (is_array($editData[$key]['el']))    {
00214 
00215                                                                 if ($this->reNumberIndexesOfSectionData)        {
00216                                                                         $temp = array();
00217                                                                         $c3=0;
00218                                                                         foreach($editData[$key]['el'] as $v3)   {
00219                                                                                 $temp[++$c3] = $v3;
00220                                                                         }
00221                                                                         $editData[$key]['el'] = $temp;
00222                                                                 }
00223 
00224                                                                 foreach($editData[$key]['el'] as $k3 => $v3)    {
00225                                                                         $cc=$k3;
00226                                                                         $theType = key($v3);
00227                                                                         $theDat = $v3[$theType];
00228                                                                         $newSectionEl = $value['el'][$theType];
00229                                                                         if (is_array($newSectionEl))    {
00230                                                                                 $this->traverseFlexFormXMLData_recurse(
00231                                                                                         array($theType => $newSectionEl),
00232                                                                                         array($theType => $theDat),
00233                                                                                         $PA,
00234                                                                                         $path.'/'.$key.'/el/'.$cc
00235                                                                                 );
00236                                                                         }
00237                                                                 }
00238                                                         }
00239                                                 } else {        // Array traversal:
00240                                                         $this->traverseFlexFormXMLData_recurse(
00241                                                                 $value['el'],
00242                                                                 $editData[$key]['el'],
00243                                                                 $PA,
00244                                                                 $path.'/'.$key.'/el'
00245                                                         );
00246                                                 }
00247                                         } elseif (is_array($value['TCEforms']['config'])) {     // Processing a field value:
00248 
00249                                                 foreach($PA['vKeys'] as $vKey)  {
00250                                                         $vKey = 'v'.$vKey;
00251 
00252                                                                 // Call back:
00253                                                         if ($PA['callBackMethod_value'])        {
00254                                                                 $this->callBackObj->$PA['callBackMethod_value'](
00255                                                                         $value,
00256                                                                         $editData[$key][$vKey],
00257                                                                         $PA,
00258                                                                         $path.'/'.$key.'/'.$vKey,
00259                                                                         $this
00260                                                                 );
00261                                                         }
00262                                                 }
00263                                         }
00264                                 }
00265                         }
00266                 }
00267         }
00268 
00274         function getAvailableLanguages()        {
00275                 $isL = t3lib_extMgm::isLoaded('static_info_tables');
00276 
00277                         // Find all language records in the system:
00278                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('static_lang_isocode,title,uid', 'sys_language', 'pid=0'.t3lib_BEfunc::deleteClause('sys_language'), '', 'title');
00279 
00280                         // Traverse them:
00281                 $output = array();
00282                 $output[0]=array(
00283                         'uid' => 0,
00284                         'title' => 'Default language',
00285                         'ISOcode' => 'DEF'
00286                 );
00287 
00288                 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))       {
00289                         $output[$row['uid']] = $row;
00290 
00291                         if ($isL && $row['static_lang_isocode'])        {
00292                                 $rr = t3lib_BEfunc::getRecord('static_languages',$row['static_lang_isocode'],'lg_iso_2');
00293                                 if ($rr['lg_iso_2'])    $output[$row['uid']]['ISOcode']=$rr['lg_iso_2'];
00294                         }
00295 
00296                         if (!$output[$row['uid']]['ISOcode'])   unset($output[$row['uid']]);
00297                 }
00298                 return $output;
00299         }
00300 
00301 
00302 
00303 
00304 
00305 
00306 
00307 
00308 
00309         /***********************************
00310          *
00311          * Processing functions
00312          *
00313          ***********************************/
00314 
00324         function cleanFlexFormXML($table,$field,$row)   {
00325 
00326                         // New structure:
00327                 $this->cleanFlexFormXML = array();
00328 
00329                         // Create and call iterator object:
00330                 $flexObj = t3lib_div::makeInstance('t3lib_flexformtools');
00331                 $flexObj->reNumberIndexesOfSectionData = TRUE;
00332                 $flexObj->traverseFlexFormXMLData($table,$field,$row,$this,'cleanFlexFormXML_callBackFunction');
00333 
00334                 return $this->flexArray2Xml($this->cleanFlexFormXML, TRUE);
00335         }
00336 
00348         function cleanFlexFormXML_callBackFunction($dsArr, $data, $PA, $path, &$pObj)   {
00349                 #debug(array($dsArr, $data, $PA),$path);
00350                         // Just setting value in our own result array, basically replicating the structure:
00351                 $pObj->setArrayValueByPath($path,$this->cleanFlexFormXML,$data);
00352         }
00353 
00354 
00355 
00356 
00357 
00358 
00359 
00360 
00361 
00362         /***********************************
00363          *
00364          * Multi purpose functions
00365          *
00366          ***********************************/
00367 
00375         function &getArrayValueByPath($pathArray,&$array)       {
00376                 if (!is_array($pathArray))      {
00377                         $pathArray = explode('/',$pathArray);
00378                 }
00379                 if (is_array($array))   {
00380                         if (count($pathArray))  {
00381                                 $key = array_shift($pathArray);
00382 
00383                                 if (isset($array[$key]))        {
00384                                         if (!count($pathArray)) {
00385                                                 return $array[$key];
00386                                         } else {
00387                                                 return $this->getArrayValueByPath($pathArray,$array[$key]);
00388                                         }
00389                                 } else {
00390                                         return NULL;
00391                                 }
00392                         }
00393                 }
00394         }
00395 
00404         function setArrayValueByPath($pathArray,&$array,$value) {
00405                 if (isset($value))       {
00406                         if (!is_array($pathArray))      {
00407                                 $pathArray = explode('/',$pathArray);
00408                         }
00409                         if (is_array($array))   {
00410                                 if (count($pathArray))  {
00411                                         $key = array_shift($pathArray);
00412 
00413                                         if (!count($pathArray)) {
00414                                                 $array[$key] = $value;
00415                                                 return TRUE;
00416                                         } else {
00417                                                 if (!isset($array[$key]))       {
00418                                                         $array[$key] = array();
00419                                                 }
00420                                                 return $this->setArrayValueByPath($pathArray,$array[$key],$value);
00421                                         }
00422                                 }
00423                         }
00424                 }
00425         }
00426 
00434         function flexArray2Xml($array, $addPrologue=FALSE)      {
00435 
00436                 $options = $GLOBALS['TYPO3_CONF_VARS']['BE']['niceFlexFormXMLtags'] ? $this->flexArray2Xml_options : array();
00437                 $output = t3lib_div::array2xml($array,'',0,'T3FlexForms',4, $options);
00438 
00439                 if ($addPrologue)       {
00440                         $output = '<?xml version="1.0" encoding="'.$GLOBALS['LANG']->charSet.'" standalone="yes" ?>'.chr(10).$output;
00441                 }
00442 
00443                 return $output;
00444         }
00445 }
00446 
00447 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flexformtools.php'])     {
00448         include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flexformtools.php']);
00449 }
00450 ?>


Généré par Le spécialiste TYPO3 avec  doxygen 1.4.6