TYPO3 API  SVNRelease
class.t3lib_tree_tca_dataproviderfactory.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2010-2011 Steffen Ritter <info@steffen-ritter.net>
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  * Builds a t3lib_tree_Tca_DatabaseTreeDataProvider object based on some TCA configuration
00030  *
00031  * @author Steffen Ritter <info@steffen-ritter.net>
00032  * @package TYPO3
00033  * @subpackage t3lib_tree
00034  */
00035 class t3lib_tree_Tca_DataProviderFactory {
00036 
00037     /**
00038      * Gets the data provider, depending on TCA configuration
00039      *
00040      * @static
00041      * @param array $tcaConfiguration
00042      * @return t3lib_tree_Tca_DatabaseTreeDataProvider
00043      * @throws InvalidArgumentException
00044      */
00045     public static function getDataProvider(array $tcaConfiguration, $table, $field, $currentValue) {
00046         $dataProvider = NULL;
00047 
00048         if (!isset($tcaConfiguration['internal_type'])) {
00049             $tcaConfiguration['internal_type'] = 'db';
00050         }
00051 
00052         if ($tcaConfiguration['internal_type'] == 'db') {
00053             /**
00054              * @var $dataProvider t3lib_tree_Tca_DatabaseTreeDataProvider
00055              */
00056             $dataProvider = t3lib_div::makeInstance('t3lib_tree_Tca_DatabaseTreeDataProvider');
00057 
00058             if (isset($tcaConfiguration['foreign_table'])) {
00059                 $tableName = $tcaConfiguration['foreign_table'];
00060                 $dataProvider->setTableName($tableName);
00061 
00062                 t3lib_div::loadTCA($tableName);
00063             } else {
00064                 throw new InvalidArgumentException(
00065                     'TCA Tree configuration is invalid: "foreign_table" not set',
00066                     '1288215888'
00067                 );
00068             }
00069 
00070             if (isset($tcaConfiguration['foreign_label'])) {
00071                 $dataProvider->setLabelField($tcaConfiguration['foreign_label']);
00072             } else {
00073                 $dataProvider->setLabelField($GLOBALS['TCA'][$tableName]['ctrl']['label']);
00074             }
00075             $dataProvider->setTreeId(md5($table . '|' . $field));
00076             $dataProvider->setSelectedList($currentValue);
00077             if (isset($tcaConfiguration['treeConfig']) && is_array($tcaConfiguration['treeConfig'])) {
00078                 $treeConfiguration = $tcaConfiguration['treeConfig'];
00079 
00080                 if (isset($treeConfiguration['rootUid'])) {
00081                     $dataProvider->setRootUid(intval($treeConfiguration['rootUid']));
00082                 }
00083 
00084                 if (isset($treeConfiguration['appearance']['expandAll'])) {
00085                     $dataProvider->setExpandAll((boolean) $treeConfiguration['appearance']['expandAll']);
00086                 }
00087 
00088                 if (isset($treeConfiguration['appearance']['maxLevels'])) {
00089                     $dataProvider->setLevelMaximum(intval($treeConfiguration['appearance']['maxLevels']));
00090                 }
00091 
00092                 if (isset($treeConfiguration['appearance']['nonSelectableLevels'])) {
00093                     $dataProvider->setNonSelectableLevelList($treeConfiguration['appearance']['nonSelectableLevels']);
00094                 } elseif (isset($treeConfiguration['rootUid'])) {
00095                     $dataProvider->setNonSelectableLevelList('');
00096                 }
00097 
00098                 if (isset($treeConfiguration['childrenField'])) {
00099                     $dataProvider->setLookupMode(t3lib_tree_tca_DatabaseTreeDataProvider::MODE_CHILDREN);
00100                     $dataProvider->setLookupField($treeConfiguration['childrenField']);
00101                 } elseif (isset($treeConfiguration['parentField'])) {
00102                     $dataProvider->setLookupMode(t3lib_tree_tca_DatabaseTreeDataProvider::MODE_PARENT);
00103                     $dataProvider->setLookupField($treeConfiguration['parentField']);
00104                 } else {
00105                     throw new InvalidArgumentException(
00106                         'TCA Tree configuration is invalid: neither "childrenField" nor "parentField" is set',
00107                         '1288215889'
00108                     );
00109                 }
00110             } else {
00111                 throw new InvalidArgumentException(
00112                     'TCA Tree configuration is invalid: "treeConfig" array is missing',
00113                     '1288215890'
00114                 );
00115             }
00116 
00117         } elseif ($tcaConfiguration['internal_type'] == 'file') {
00118             // Not implemented yet
00119             throw new InvalidArgumentException(
00120                 'TCA Tree configuration is invalid: tree for "internal_type=file" not implemented yet',
00121                 '1288215891'
00122             );
00123         } else {
00124             throw new InvalidArgumentException(
00125                 'TCA Tree configuration is invalid: tree for "internal_type=' .
00126                 $tcaConfiguration['internal_type'] .
00127                 '" not implemented yet',
00128                 '1288215892'
00129             );
00130         }
00131 
00132         return $dataProvider;
00133     }
00134 }
00135 
00136 ?>