TYPO3 API  SVNRelease
class.t3lib_tceforms_flexforms.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2010-2011 Kai Vogel (kai.vogel(at)speedprogs.de)
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  ***************************************************************/
00027 
00028 
00029 /**
00030  * Contains FlexForm manipulation methods as part of the TCEforms
00031  *
00032  * @author Kai Vogel <kai.vogel(at)speedprogs.de>
00033  */
00034 class t3lib_TCEforms_Flexforms extends t3lib_TCEforms {
00035 
00036     /**
00037      * Options that will be removed from config after creating items for a select to prevent double parsing
00038      *
00039      * @var array
00040      */
00041     protected $removeSelectConfig = array(
00042         'itemsProcFunc',
00043         'foreign_table',
00044         'foreign_table_where',
00045         'foreign_table_prefix',
00046         'foreign_table_loadIcons',
00047         'neg_foreign_table',
00048         'neg_foreign_table_where',
00049         'neg_foreign_table_prefix',
00050         'neg_foreign_table_loadIcons',
00051         'neg_foreign_table_imposeValueField',
00052         'fileFolder',
00053         'fileFolder_extList',
00054         'fileFolder_recursions',
00055         'MM',
00056         'MM_opposite_field',
00057         'MM_match_fields',
00058         'MM_insert_fields',
00059         'MM_table_where',
00060         'MM_hasUidField',
00061         'special',
00062     );
00063 
00064     /**
00065      * Modify the Data Structure of a FlexForm field via TSconfig and group access lists
00066      *
00067      * @param array $dataStructure The data structure of the FlexForm field
00068      * @param string $table The table name of the record
00069      * @param string $tableField The field name
00070      * @param array $tableRow The record data
00071      * @param array $tableConf Additional configuration options
00072      * @return array Modified FlexForm DS
00073      * @see t3lib_TCEforms::getSingleField_typeFlex()
00074      */
00075     public function modifyFlexFormDS(array $dataStructure, $table, $tableField, array $tableRow, array $tableConf) {
00076         $singleSheet = (!isset($dataStructure['sheets']) || !is_array($dataStructure['sheets']));
00077         $metaConf = (!empty($dataStructure['meta']) ? $dataStructure['meta'] : array());
00078         $sheetConf = array();
00079 
00080             // Get extension identifier (uses second pointer field if it's value is not empty, "list" or "*", else it must be a plugin and first one will be used)
00081         $pointerFields = (!empty($tableConf['config']['ds_pointerField']) ? $tableConf['config']['ds_pointerField'] : 'list_type,CType');
00082         $pointerFields = t3lib_div::trimExplode(',', $pointerFields);
00083         $flexformIdentifier = (!empty($tableRow[$pointerFields[0]]) ? $tableRow[$pointerFields[0]] : '');
00084         if (!empty($tableRow[$pointerFields[1]]) && $tableRow[$pointerFields[1]] != 'list' && $tableRow[$pointerFields[1]] != '*') {
00085             $flexformIdentifier = $tableRow[$pointerFields[1]];
00086         }
00087         if (empty($flexformIdentifier)) {
00088             return $dataStructure;
00089         }
00090 
00091             // Get field configuration from page TSConfig
00092         $TSconfig = $this->setTSconfig($table, $tableRow);
00093         if (!empty($TSconfig[$tableField][$flexformIdentifier . '.'])) {
00094             $sheetConf = t3lib_div::removeDotsFromTS($TSconfig[$tableField][$flexformIdentifier . '.']);
00095         }
00096         ;
00097 
00098             // Get non-exclude-fields from group access lists
00099         $nonExcludeFields = $this->getFlexFormNonExcludeFields($table, $tableField, $flexformIdentifier);
00100 
00101             // Load complete DS, including external file references
00102         $dataStructure = t3lib_div::resolveAllSheetsInDS($dataStructure);
00103 
00104             // Modify flexform sheets
00105         foreach ($dataStructure['sheets'] as $sheetName => $sheet) {
00106             if (empty($sheet['ROOT']['el']) || !is_array($sheet['ROOT']['el'])) {
00107                 continue;
00108             }
00109 
00110                 // Remove whole sheet (tab) if disabled
00111             if (!empty($sheetConf[$sheetName]['disabled'])) {
00112                 unset($dataStructure['sheets'][$sheetName]);
00113                 continue;
00114             }
00115 
00116                 // Rename sheet (tab)
00117             if (!empty($sheetConf[$sheetName]['title'])) {
00118                 $dataStructure['sheets'][$sheetName]['ROOT']['TCEforms']['sheetTitle'] = $sheetConf[$sheetName]['title'];
00119             }
00120 
00121                 // Modify all configured fields in sheet
00122             $dataStructure['sheets'][$sheetName]['ROOT']['el'] = $this->modifySingleFlexFormSheet(
00123                 $sheet['ROOT']['el'],
00124                 $table,
00125                 $tableField,
00126                 $tableRow,
00127                 (!empty($sheetConf[$sheetName]) ? $sheetConf[$sheetName] : array()),
00128                 (!empty($nonExcludeFields[$sheetName]) ? $nonExcludeFields[$sheetName] : array())
00129             );
00130 
00131                 // Remove empty sheet (tab)
00132             if (empty($dataStructure['sheets'][$sheetName]['ROOT']['el'])) {
00133                 unset($dataStructure['sheets'][$sheetName]);
00134             }
00135         }
00136 
00137             // Recover single flexform structure
00138         if ($singleSheet && isset($dataStructure['sheets']['sDEF'])) {
00139             $dataStructure = $dataStructure['sheets']['sDEF'];
00140         }
00141 
00142             // Recover meta configuration
00143         if (!empty($metaConf)) {
00144             $dataStructure['meta'] = $metaConf;
00145         }
00146 
00147         return $dataStructure;
00148     }
00149 
00150     /**
00151      * Modify a single FlexForm sheet according to given configuration
00152      *
00153      * @param array $sheet Flexform sheet to manipulate
00154      * @param string $table The table name
00155      * @param string $tableField The field name
00156      * @param array $tableRow The record data
00157      * @param array $sheetConf Sheet configuration
00158      * @param array $nonExcludeFields Non-exclude-fields for this sheet
00159      * @return array Modified sheet
00160      * @see t3lib_TCEforms_flex::modifyFlexFormDS()
00161      */
00162     public function modifySingleFlexFormSheet(array $sheet, $table, $tableField, array $tableRow, array $sheetConf, array $nonExcludeFields) {
00163         if (empty($sheet) || empty($table) || empty($tableField) || empty($tableRow)) {
00164             return $sheet;
00165         }
00166 
00167             // Modify fields
00168         foreach ($sheet as $fieldName => $field) {
00169 
00170                 // Remove excluded fields
00171             if (!$GLOBALS['BE_USER']->isAdmin() && !empty($field['TCEforms']['exclude']) && empty($nonExcludeFields[$fieldName])) {
00172                 unset($sheet[$fieldName]);
00173                 continue;
00174             }
00175 
00176                 // Stop here if no TSConfig was found for this field
00177             if (empty($sheetConf[$fieldName]) || !is_array($sheetConf[$fieldName])) {
00178                 continue;
00179             }
00180 
00181                 // Remove disabled fields
00182             if (!empty($sheetConf[$fieldName]['disabled'])) {
00183                 unset($sheet[$fieldName]);
00184                 continue;
00185             }
00186 
00187             $fieldConf = $sheetConf[$fieldName];
00188             $removeItems = (!empty($fieldConf['removeItems']) ? t3lib_div::trimExplode(',', $fieldConf['removeItems'], TRUE) : array());
00189             $keepItems = (!empty($fieldConf['keepItems']) ? t3lib_div::trimExplode(',', $fieldConf['keepItems'], TRUE) : array());
00190             $renameItems = (!empty($fieldConf['altLabels']) && is_array($fieldConf['altLabels']) ? $fieldConf['altLabels'] : array());
00191             $addItems = (!empty($fieldConf['addItems']) && is_array($fieldConf['addItems']) ? $fieldConf['addItems'] : array());
00192 
00193             unset($fieldConf['removeItems']);
00194             unset($fieldConf['keepItems']);
00195             unset($fieldConf['altLabels']);
00196             unset($fieldConf['addItems']);
00197 
00198                 // Manipulate field
00199             if (!empty($field['TCEforms']) && is_array($field['TCEforms'])) {
00200                 $sheet[$fieldName]['TCEforms'] = t3lib_div::array_merge_recursive_overrule($field['TCEforms'], $fieldConf);
00201             }
00202 
00203                 // Manipulate only select fields, other field types will stop here
00204             if (empty($field['TCEforms']['config']['type']) || $field['TCEforms']['config']['type'] != 'select') {
00205                 continue;
00206             }
00207 
00208                 // Getting the selector box items from system
00209             $selItems = $this->addSelectOptionsToItemArray(
00210                 $this->initItemArray($field['TCEforms']),
00211                 $field['TCEforms'],
00212                 $this->setTSconfig($table, $tableRow),
00213                 $tableField
00214             );
00215 
00216                 // Possibly filter some items
00217             $keepItemsFunc = create_function('$value', 'return $value[1];');
00218             $selItems = t3lib_div::keepItemsInArray($selItems, $keepItems, $keepItemsFunc);
00219 
00220                 // Possibly add some items
00221             $selItems = $this->addItems($selItems, $addItems);
00222 
00223                 // Process items by a user function
00224             if (!empty($field['TCEforms']['config']['itemsProcFunc'])) {
00225                 $selItems = $this->procItems(
00226                     $selItems,
00227                     $fieldConf['config'],
00228                     $field['TCEforms']['config'],
00229                     $table,
00230                     $tableRow,
00231                     $tableField
00232                 );
00233             }
00234 
00235                 // Remove special configuration options after creating items to prevent double parsing
00236             foreach ($this->removeSelectConfig as $option) {
00237                 unset($sheet[$fieldName]['TCEforms']['config'][$option]);
00238             }
00239 
00240                 // Rename and remove items in select
00241             if ((!empty($removeItems) || !empty($renameItems)) && !empty($selItems) && is_array($selItems)) {
00242                 foreach ($selItems as $itemKey => $itemConf) {
00243                         // Option has no key, no manipulation possible
00244                     if (!isset($itemConf[1])) {
00245                         continue;
00246                     }
00247 
00248                         // Remove
00249                     foreach ($removeItems as $removeKey => $removeValue) {
00250                         if (strcasecmp($removeValue, $itemConf[1]) == 0) {
00251                             unset($selItems[$itemKey]);
00252                             unset($removeItems[$removeKey]);
00253                         }
00254                     }
00255 
00256                         // Rename
00257                     foreach ($renameItems as $renameKey => $renameValue) {
00258                         if (strcasecmp($renameKey, $itemConf[1]) == 0) {
00259                             $selItems[$itemKey][0] = $renameValue;
00260                             unset($renameItems[$renameKey]);
00261                         }
00262                     }
00263                 }
00264             }
00265 
00266             $sheet[$fieldName]['TCEforms']['config']['items'] = $selItems;
00267 
00268         }
00269 
00270         return $sheet;
00271     }
00272 
00273     /**
00274      * Get FlexForm non-exclude-fields for current backend user
00275      *
00276      * @param string $table The table name
00277      * @param string $tableField The field name
00278      * @param string $extIdent The extension identifier
00279      * @return array All non_exclude_fields from FlexForms
00280      * @see t3lib_TCEforms::getSingleField_typeFlex()
00281      */
00282     protected function getFlexFormNonExcludeFields($table, $tableField, $extIdent) {
00283         if (empty($GLOBALS['BE_USER']->groupData['non_exclude_fields']) || empty($table) || empty($tableField) || empty($extIdent)) {
00284             return array();
00285         }
00286 
00287         $accessListFields = t3lib_div::trimExplode(',', $GLOBALS['BE_USER']->groupData['non_exclude_fields']);
00288         $identPrefix = $table . ':' . $tableField . ';' . $extIdent . ';';
00289         $nonExcludeFields = array();
00290 
00291             // Collect only FlexForm fields
00292         foreach ($accessListFields as $field) {
00293             if (strpos($field, $identPrefix) !== FALSE) {
00294                 list(, , $sheetName, $fieldName) = explode(';', $field);
00295                 $nonExcludeFields[$sheetName][$fieldName] = TRUE;
00296             }
00297         }
00298 
00299         return $nonExcludeFields;
00300     }
00301 
00302     /**
00303      * Compare two arrays by their first value
00304      *
00305      * @param array $array1 First array
00306      * @param array $array2 Second array
00307      * @return integer Negative int if first array is lower, zero if both are identical, and positive if second is higher
00308      */
00309     public static function compareArraysByFirstValue(array $array1, array $array2) {
00310         $array1 = reset($array1);
00311         $array2 = reset($array2);
00312 
00313         if (is_string($array1) && is_string($array2)) {
00314             return strcasecmp($array1, $array2);
00315         }
00316 
00317         return 0;
00318     }
00319 
00320 }
00321 
00322 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['classes/t3lib/tceforms/class.t3lib_tceforms_flexforms.php']) {
00323     include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['classes/t3lib/tceforms/class.t3lib_tceforms_flexforms.php']);
00324 }
00325 
00326 ?>