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         var $traverseFlexFormXMLData_DS = array();      // Contains data structure when traversing flexform
00077         var $traverseFlexFormXMLData_Data = array();    // Contains data array when traversing flexform
00078 
00079                 // 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
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                 // Internal:
00095         var $callBackObj = NULL;                // Reference to object called
00096         var $cleanFlexFormXML = array();                // Used for accumulation of clean XML
00097 
00108         function traverseFlexFormXMLData($table,$field,$row,&$callBackObj,$callBackMethod_value)        {
00109 
00110                 if (!is_array($GLOBALS['TCA'][$table]) || !is_array($GLOBALS['TCA'][$table]['columns'][$field]))        {
00111                         return 'TCA table/field was not defined.';
00112                 }
00113 
00114                 $this->callBackObj = &$callBackObj;
00115 
00116                         // Get Data Structure:
00117                 $dataStructArray = t3lib_BEfunc::getFlexFormDS($GLOBALS['TCA'][$table]['columns'][$field]['config'],$row,$table);
00118 
00119                         // If data structure was ok, proceed:
00120                 if (is_array($dataStructArray)) {
00121 
00122                                 // Get flexform XML data:
00123                         $xmlData = $row[$field];
00124 
00125                                 // Convert charset:
00126                         if ($this->convertCharset)      {
00127                                 $xmlHeaderAttributes = t3lib_div::xmlGetHeaderAttribs($xmlData);
00128                                 $storeInCharset = strtolower($xmlHeaderAttributes['encoding']);
00129                                 if ($storeInCharset)    {
00130                                         $currentCharset = $GLOBALS['LANG']->charSet;
00131                                         $xmlData = $GLOBALS['LANG']->csConvObj->conv($xmlData,$storeInCharset,$currentCharset,1);
00132                                 }
00133                         }
00134 
00135                         $editData = t3lib_div::xml2array($xmlData);
00136                         if (!is_array($editData))       {
00137                                 return 'Parsing error: '.$editData;
00138                         }
00139 
00140                                 // Language settings:
00141                         $langChildren = $dataStructArray['meta']['langChildren'] ? 1 : 0;
00142                         $langDisabled = $dataStructArray['meta']['langDisable'] ? 1 : 0;
00143 
00144                                 // empty or invalid <meta>
00145                         if (!is_array($editData['meta']))       {
00146                                 $editData['meta'] = array();
00147                         }
00148                         $editData['meta']['currentLangId'] = array();
00149                         $languages = $this->getAvailableLanguages();
00150 
00151                         foreach ($languages as $lInfo)  {
00152                                 $editData['meta']['currentLangId'][] = $lInfo['ISOcode'];
00153                         }
00154                         if (!count($editData['meta']['currentLangId'])) {
00155                                 $editData['meta']['currentLangId'] = array('DEF');
00156                         }
00157                         $editData['meta']['currentLangId'] = array_unique($editData['meta']['currentLangId']);
00158 
00159                         if ($langChildren || $langDisabled)     {
00160                                 $lKeys = array('DEF');
00161                         } else {
00162                                 $lKeys = $editData['meta']['currentLangId'];
00163                         }
00164 
00165                                 // Tabs sheets
00166                         if (is_array($dataStructArray['sheets']))       {
00167                                 $sKeys = array_keys($dataStructArray['sheets']);
00168                         } else {
00169                                 $sKeys = array('sDEF');
00170                         }
00171 
00172                                 // Traverse languages:
00173                         foreach($lKeys as $lKey)        {
00174                                 foreach($sKeys as $sheet)       {
00175                                         $sheetCfg = $dataStructArray['sheets'][$sheet];
00176                                         list ($dataStruct, $sheet) = t3lib_div::resolveSheetDefInDS($dataStructArray,$sheet);
00177 
00178                                                 // Render sheet:
00179                                         if (is_array($dataStruct['ROOT']) && is_array($dataStruct['ROOT']['el']))               {
00180                                                 $lang = 'l'.$lKey;      // Separate language key
00181                                                 $PA['vKeys'] = $langChildren && !$langDisabled ? $editData['meta']['currentLangId'] : array('DEF');
00182                                                 $PA['lKey'] = $lang;
00183                                                 $PA['callBackMethod_value'] = $callBackMethod_value;
00184                                                 $PA['table'] = $table;
00185                                                 $PA['field'] = $field;
00186                                                 $PA['uid'] = $row['uid'];
00187 
00188                                                 $this->traverseFlexFormXMLData_DS = &$dataStruct;
00189                                                 $this->traverseFlexFormXMLData_Data = &$editData;
00190 
00191                                                         // Render flexform:
00192                                                 $this->traverseFlexFormXMLData_recurse(
00193                                                         $dataStruct['ROOT']['el'],
00194                                                         $editData['data'][$sheet][$lang],
00195                                                         $PA,
00196                                                         'data/'.$sheet.'/'.$lang
00197                                                 );
00198                                         } else return 'Data Structure ERROR: No ROOT element found for sheet "'.$sheet.'".';
00199                                 }
00200                         }
00201                 } else return 'Data Structure ERROR: '.$dataStructArray;
00202         }
00203 
00213         function traverseFlexFormXMLData_recurse($dataStruct,$editData,&$PA,$path='')   {
00214 
00215                 if (is_array($dataStruct))      {
00216                         foreach($dataStruct as $key => $value)  {
00217                                 if (is_array($value))   {       // The value of each entry must be an array.
00218 
00219                                         if ($value['type']=='array')    {
00220                                                 if ($value['section'])  {               // Array (Section) traversal:
00221 
00222                                                         $cc = 0;
00223                                                         if (is_array($editData[$key]['el']))    {
00224 
00225                                                                 if ($this->reNumberIndexesOfSectionData)        {
00226                                                                         $temp = array();
00227                                                                         $c3=0;
00228                                                                         foreach($editData[$key]['el'] as $v3)   {
00229                                                                                 $temp[++$c3] = $v3;
00230                                                                         }
00231                                                                         $editData[$key]['el'] = $temp;
00232                                                                 }
00233 
00234                                                                 foreach($editData[$key]['el'] as $k3 => $v3)    {
00235                                                                         $cc=$k3;
00236                                                                         $theType = key($v3);
00237                                                                         $theDat = $v3[$theType];
00238                                                                         $newSectionEl = $value['el'][$theType];
00239                                                                         if (is_array($newSectionEl))    {
00240                                                                                 $this->traverseFlexFormXMLData_recurse(
00241                                                                                         array($theType => $newSectionEl),
00242                                                                                         array($theType => $theDat),
00243                                                                                         $PA,
00244                                                                                         $path.'/'.$key.'/el/'.$cc
00245                                                                                 );
00246                                                                         }
00247                                                                 }
00248                                                         }
00249                                                 } else {        // Array traversal:
00250                                                         $this->traverseFlexFormXMLData_recurse(
00251                                                                 $value['el'],
00252                                                                 $editData[$key]['el'],
00253                                                                 $PA,
00254                                                                 $path.'/'.$key.'/el'
00255                                                         );
00256                                                 }
00257                                         } elseif (is_array($value['TCEforms']['config'])) {     // Processing a field value:
00258 
00259                                                 foreach($PA['vKeys'] as $vKey)  {
00260                                                         $vKey = 'v'.$vKey;
00261 
00262                                                                 // Call back:
00263                                                         if ($PA['callBackMethod_value'])        {
00264                                                                 $this->callBackObj->$PA['callBackMethod_value'](
00265                                                                         $value,
00266                                                                         $editData[$key][$vKey],
00267                                                                         $PA,
00268                                                                         $path.'/'.$key.'/'.$vKey,
00269                                                                         $this
00270                                                                 );
00271                                                         }
00272                                                 }
00273                                         }
00274                                 }
00275                         }
00276                 }
00277         }
00278 
00284         function getAvailableLanguages()        {
00285                 $isL = t3lib_extMgm::isLoaded('static_info_tables');
00286 
00287                         // Find all language records in the system:
00288                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('static_lang_isocode,title,uid', 'sys_language', 'pid=0'.t3lib_BEfunc::deleteClause('sys_language'), '', 'title');
00289 
00290                         // Traverse them:
00291                 $output = array();
00292                 $output[0]=array(
00293                         'uid' => 0,
00294                         'title' => 'Default language',
00295                         'ISOcode' => 'DEF'
00296                 );
00297 
00298                 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))       {
00299                         $output[$row['uid']] = $row;
00300 
00301                         if ($isL && $row['static_lang_isocode'])        {
00302                                 $rr = t3lib_BEfunc::getRecord('static_languages',$row['static_lang_isocode'],'lg_iso_2');
00303                                 if ($rr['lg_iso_2'])    $output[$row['uid']]['ISOcode']=$rr['lg_iso_2'];
00304                         }
00305 
00306                         if (!$output[$row['uid']]['ISOcode'])   unset($output[$row['uid']]);
00307                 }
00308                 return $output;
00309         }
00310 
00311 
00312 
00313 
00314 
00315 
00316 
00317 
00318 
00319         /***********************************
00320          *
00321          * Processing functions
00322          *
00323          ***********************************/
00324 
00334         function cleanFlexFormXML($table,$field,$row)   {
00335 
00336                         // New structure:
00337                 $this->cleanFlexFormXML = array();
00338 
00339                         // Create and call iterator object:
00340                 $flexObj = t3lib_div::makeInstance('t3lib_flexformtools');
00341                 $flexObj->reNumberIndexesOfSectionData = TRUE;
00342                 $flexObj->traverseFlexFormXMLData($table,$field,$row,$this,'cleanFlexFormXML_callBackFunction');
00343 
00344                 return $this->flexArray2Xml($this->cleanFlexFormXML, TRUE);
00345         }
00346 
00358         function cleanFlexFormXML_callBackFunction($dsArr, $data, $PA, $path, &$pObj)   {
00359                 #debug(array($dsArr, $data, $PA),$path);
00360                         // Just setting value in our own result array, basically replicating the structure:
00361                 $pObj->setArrayValueByPath($path,$this->cleanFlexFormXML,$data);
00362         }
00363 
00364 
00365 
00366 
00367 
00368 
00369 
00370 
00371 
00372         /***********************************
00373          *
00374          * Multi purpose functions
00375          *
00376          ***********************************/
00377 
00385         function &getArrayValueByPath($pathArray,&$array)       {
00386                 if (!is_array($pathArray))      {
00387                         $pathArray = explode('/',$pathArray);
00388                 }
00389                 if (is_array($array))   {
00390                         if (count($pathArray))  {
00391                                 $key = array_shift($pathArray);
00392 
00393                                 if (isset($array[$key]))        {
00394                                         if (!count($pathArray)) {
00395                                                 return $array[$key];
00396                                         } else {
00397                                                 return $this->getArrayValueByPath($pathArray,$array[$key]);
00398                                         }
00399                                 } else {
00400                                         return NULL;
00401                                 }
00402                         }
00403                 }
00404         }
00405 
00414         function setArrayValueByPath($pathArray,&$array,$value) {
00415                 if (isset($value))       {
00416                         if (!is_array($pathArray))      {
00417                                 $pathArray = explode('/',$pathArray);
00418                         }
00419                         if (is_array($array))   {
00420                                 if (count($pathArray))  {
00421                                         $key = array_shift($pathArray);
00422 
00423                                         if (!count($pathArray)) {
00424                                                 $array[$key] = $value;
00425                                                 return TRUE;
00426                                         } else {
00427                                                 if (!isset($array[$key]))       {
00428                                                         $array[$key] = array();
00429                                                 }
00430                                                 return $this->setArrayValueByPath($pathArray,$array[$key],$value);
00431                                         }
00432                                 }
00433                         }
00434                 }
00435         }
00436 
00444         function flexArray2Xml($array, $addPrologue=FALSE)      {
00445 
00446                 $options = $GLOBALS['TYPO3_CONF_VARS']['BE']['niceFlexFormXMLtags'] ? $this->flexArray2Xml_options : array();
00447                 $spaceInd = ($GLOBALS['TYPO3_CONF_VARS']['BE']['compactFlexFormXML'] ? -1 : 4);
00448                 $output = t3lib_div::array2xml($array,'',0,'T3FlexForms', $spaceInd, $options);
00449 
00450                 if ($addPrologue)       {
00451                         $output = '<?xml version="1.0" encoding="'.$GLOBALS['LANG']->charSet.'" standalone="yes" ?>'.chr(10).$output;
00452                 }
00453 
00454                 return $output;
00455         }
00456 }
00457 
00458 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flexformtools.php'])     {
00459         include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flexformtools.php']);
00460 }
00461 ?>


Généré par Les spécialistes TYPO3 avec  doxygen 1.4.6