TYPO3 API  SVNRelease
class.t3lib_tceforms_tree.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  *  (c) 2010-2011 Steffen Kamper <steffen@typo3.org>
00007  *  All rights reserved
00008  *
00009  *  This script is part of the TYPO3 project. The TYPO3 project is
00010  *  free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  The GNU General Public License can be found at
00016  *  http://www.gnu.org/copyleft/gpl.html.
00017  *  A copy is found in the textfile GPL.txt and important notices to the license
00018  *  from the author is found in LICENSE.txt distributed with these scripts.
00019  *
00020  *
00021  *  This script is distributed in the hope that it will be useful,
00022  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  *  GNU General Public License for more details.
00025  *
00026  *  This copyright notice MUST APPEAR in all copies of the script!
00027  ***************************************************************/
00028 /**
00029  * TCEforms wizard for rendering an AJAX selector for records
00030  *
00031  * $Id: class.t3lib_tceforms_suggest.php 7905 2010-06-13 14:42:33Z ohader $
00032  *
00033  * @author Steffen Ritter <info@steffen-ritter.net>
00034  * @author Steffen Kamper <steffen@typo3.org>
00035  */
00036 
00037 class t3lib_TCEforms_Tree {
00038     /**
00039      * Stores a reference to the original tceForms object
00040      *
00041      * @var t3lib_TCEforms
00042      */
00043     protected $tceForms = NULL;
00044 
00045     /**
00046      * Constructor which sets the tceForms.
00047      *
00048      * @param t3lib_TCEforms $tceForms
00049      *
00050      */
00051     public function __construct(t3lib_TCEforms &$tceForms) {
00052         $this->tceForms = $tceForms;
00053     }
00054 
00055     /**
00056      * renders the tree as replacement for the selector
00057      *
00058      * @param string The table name of the record
00059      * @param string The field name which this element is supposed to edit
00060      * @param array The record data array where the value(s) for the field can be found
00061      * @param array An array with additional configuration options.
00062      * @param array (Redundant) content of $PA['fieldConf']['config'] (for convenience)
00063      * @param array Items available for selection
00064      * @param string Label for no-matching-value
00065      * @return string The HTML code for the TCEform field
00066      */
00067     public function renderField($table, $field, $row, &$PA, $config, $possibleSelectboxItems, $noMatchLabel) {
00068         $valueArray = explode(',', $PA['itemFormElValue']);
00069         $selectedNodes = array();
00070         if (count($valueArray)) {
00071             foreach ($valueArray as $selectedValue) {
00072                 $temp = explode('|', $selectedValue);
00073                 $selectedNodes[] = $temp[0];
00074             }
00075         }
00076         $allowedUids = array();
00077         foreach ($possibleSelectboxItems as $item) {
00078             if (intval($item[1]) > 0) {
00079                 $allowedUids[] = $item[1];
00080             }
00081         }
00082         $treeDataProvider = t3lib_tree_Tca_DataProviderFactory::getDataProvider(
00083             $config,
00084             $table,
00085             $field,
00086             $row
00087         );
00088         $treeDataProvider->setSelectedList(implode(',', $selectedNodes));
00089         $treeDataProvider->setItemWhiteList($allowedUids);
00090         $treeDataProvider->initializeTreeData();
00091 
00092         $treeRenderer = t3lib_div::makeInstance('t3lib_tree_Tca_ExtJsArrayRenderer');
00093         $tree = t3lib_div::makeInstance('t3lib_tree_Tca_TcaTree');
00094         $tree->setDataProvider($treeDataProvider);
00095         $tree->setNodeRenderer($treeRenderer);
00096 
00097         $treeData = $tree->render();
00098 
00099         $itemArray = array();
00100         if (is_array($PA['fieldConf']['config']['items'])) {
00101             foreach ($PA['fieldConf']['config']['items'] as $additionalItem) {
00102                 if ($additionalItem[1] !== '--div--') {
00103                     $item = new stdClass();
00104                     $item->uid = $additionalItem[1];
00105                     $item->text = $GLOBALS['LANG']->sL($additionalItem[0]);
00106                     $item->selectable = TRUE;
00107                     $item->leaf = TRUE;
00108                     $item->checked = in_array($additionalItem[1], $selectedNodes);
00109                     if (file_exists(PATH_typo3 . $additionalItem[3])) {
00110                         $item->icon = $additionalItem[3];
00111                     } elseif (strlen(trim($additionalItem[3]))) {
00112                         $item->iconCls = t3lib_iconWorks::getSpriteIconClasses($additionalItem[3]);
00113                     }
00114 
00115                     $itemArray[] = $item;
00116                 }
00117             }
00118         }
00119         $itemArray[] = $treeData;
00120         $treeData = json_encode($itemArray);
00121 
00122         $id = md5($PA['itemFormElName']);
00123 
00124         if (isset($PA['fieldConf']['config']['size']) && intval($PA['fieldConf']['config']['size']) > 0) {
00125             $height = intval($PA['fieldConf']['config']['size']) * 20;
00126         } else {
00127             $height = 280;
00128         }
00129         if (isset($PA['fieldConf']['config']['autoSizeMax']) && intval($PA['fieldConf']['config']['autoSizeMax']) > 0) {
00130             $autoSizeMax = intval($PA['fieldConf']['config']['autoSizeMax']) * 20;
00131         }
00132 
00133 
00134         $header = FALSE;
00135         $expanded = FALSE;
00136         $appearance = $PA['fieldConf']['config']['treeConfig']['appearance'];
00137         if (is_array($appearance)) {
00138             $header = $appearance['showHeader'] ? TRUE : FALSE;
00139             $expanded = ($appearance['expandAll'] === TRUE);
00140         }
00141 
00142         $onChange = '';
00143         if ($PA['fieldChangeFunc']['TBE_EDITOR_fieldChanged']) {
00144             $onChange = substr($PA['fieldChangeFunc']['TBE_EDITOR_fieldChanged'], 0, -1);
00145         }
00146 
00147         /** @var $pageRenderer t3lib_PageRenderer */
00148         $pageRenderer = $GLOBALS['SOBE']->doc->getPageRenderer();
00149         $pageRenderer->loadExtJs();
00150         $pageRenderer->addJsFile('../t3lib/js/extjs/tree/tree.js');
00151         $pageRenderer->addExtOnReadyCode('
00152             TYPO3.Components.Tree.StandardTreeItemData["' . $id . '"] = ' . $treeData . ';
00153             var tree' . $id . ' = new TYPO3.Components.Tree.StandardTree({
00154                 id: "' . $id . '",
00155                 showHeader: ' . intval($header) . ',
00156                 onChange: "' . $onChange . '",
00157                 countSelectedNodes: ' . count ($selectedNodes) . ',
00158                 listeners: {
00159                     click: function(node, event) {
00160                         if (typeof(node.attributes.checked) == "boolean") {
00161                             node.attributes.checked = ! node.attributes.checked;
00162                             node.getUI().toggleCheck(node.attributes.checked);
00163                         }
00164                     },
00165                     dblclick: function(node, event) {
00166                         if (typeof(node.attributes.checked) == "boolean") {
00167                             node.attributes.checked = ! node.attributes.checked;
00168                             node.getUI().toggleCheck(node.attributes.checked);
00169                         }
00170                     },
00171                     checkchange: TYPO3.Components.Tree.TcaCheckChangeHandler,
00172                     collapsenode: function(node) {
00173                         top.TYPO3.BackendUserSettings.ExtDirect.removeFromList("tcaTrees." + this.ucId, node.attributes.uid);
00174                     },
00175                     expandnode: function(node) {
00176                         top.TYPO3.BackendUserSettings.ExtDirect.addToList("tcaTrees." + this.ucId, node.attributes.uid);
00177                     }
00178                 },
00179                 tcaMaxItems: ' . ($PA['fieldConf']['config']['maxitems'] ? intval($PA['fieldConf']['config']['maxitems']) : 99999) . ',
00180                 tcaExclusiveKeys: "' . (
00181         $PA['fieldConf']['config']['exclusiveKeys']
00182                 ? $PA['fieldConf']['config']['exclusiveKeys'] : '') . '",
00183                 ucId: "' . md5($table . '|' . $field) . '",
00184                 selModel: TYPO3.Components.Tree.EmptySelectionModel
00185             });' . LF .
00186             ($autoSizeMax
00187                 ? 'tree' . $id . '.bodyStyle = "max-height: ' . $autoSizeMax . 'px;min-height: ' . $height . 'px;";'
00188                 : 'tree' . $id . '.height = ' . $height . ';'
00189             ) . LF .
00190             'tree' . $id . '.render("tree_' . $id . '");' .
00191             ($expanded ? 'tree' . $id . '.expandAll();' : '') . '
00192         ');
00193 
00194         $formField = '
00195             <div class="typo3-tceforms-tree">
00196                 <input type="hidden" name="' . htmlspecialchars($PA['itemFormElName']) . '" id="treeinput' . $id . '" value="' . htmlspecialchars($PA['itemFormElValue']) . '" />
00197             </div>
00198             <div id="tree_' . $id . '">
00199 
00200             </div>';
00201 
00202         return $formField;
00203     }
00204 }
00205 
00206 
00207 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['classes/t3lib/tceforms/class.t3lib_tceforms_tree.php'])) {
00208     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['classes/t3lib/tceforms/class.t3lib_tceforms_tree.php']);
00209 }
00210 
00211 ?>