Project

General

Profile

Feature #17822 » class.t3lib_tabmenus.php.txt

Administrator Admin, 2007-11-21 08:25

 
<?php
/***************************************************************
* Copyright notice
*
* (c) 1999-2006 Kasper Skaarhoj (kasperYYYY@typo3.com)
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* A copy is found in the textfile GPL.txt and important notices to the license
* from the author is found in LICENSE.txt distributed with these scripts.
*
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Contains class with tabbed menus related functions
* @author Kasper Skaarhoj <kasperYYYY@typo3.com>
*/
require_once(PATH_t3lib.'class.t3lib_iconworks.php');
class t3lib_tabMenus {
var $modSharedTSconfig=array(); // mod.SHARED related configurations
var $PageTSConfig=array(); // TS Config for pages
/**
* Creates a tab menu from an array definition
*
* Returns a tab menu for a module
* Requires the JS function jumpToUrl() to be available
*
* @param mixed $id is the "&id=" parameter value to be sent to the module, but it can be also a parameter array which will be passed instead of the &id=...
* @param string $elementName it the form elements name, probably something like "SET[...]"
* @param string $currentValue is the value to be selected currently.
* @param array $menuItems is an array with the menu items for the selector box
* @param string $script is the script to send the &id to, if empty it's automatically found
* @param string $addParams is additional parameters to pass to the script.
* @return string HTML code for tab menu
* @author Rene Fritz <r.fritz@colorcube.de>
*/
function getTabMenu($mainParams,$elementName,$currentValue,$menuItems,$script='',$addparams='') {
$content='';

if (is_array($menuItems)) {
if (!is_array($mainParams)) {
$mainParams = array('id' => $mainParams);
}
$mainParams = t3lib_div::implodeArrayForUrl('',$mainParams);

if (!$script) {$script=basename(PATH_thisScript);}

$menuDef = array();
foreach($menuItems as $value => $label) {
$menuDef[$value]['isActive'] = !strcmp($currentValue,$value);
$menuDef[$value]['label'] = t3lib_div::deHSCentities(htmlspecialchars($label));
$menuDef[$value]['url'] = htmlspecialchars($script.'?'.$mainParams.$addparams.'&'.$elementName.'='.$value);
}
$content = t3lib_tabMenus::getTabMenuRaw($menuDef);

}
return $content;
}

/**
* Creates the HTML content for the tab menu
*
* @param array Menu items for tabs
* @return string Table HTML
* @access private
*/
function getTabMenuRaw($menuItems) {
$content='';

if (is_array($menuItems)) {
$options='';

$count = count($menuItems);
$widthLeft = 1;
$addToAct = 5;

$widthRight = max (1,floor(30-pow($count,1.72)));
$widthTabs = 100 - $widthRight - $widthLeft;
$widthNo = floor(($widthTabs - $addToAct)/$count);
$addToAct = max ($addToAct,$widthTabs-($widthNo*$count));
$widthAct = $widthNo + $addToAct;
$widthRight = 100 - ($widthLeft + ($count*$widthNo) + $addToAct);

$first=true;
foreach($menuItems as $id => $def) {
$isActive = $def['isActive'];
$class = $isActive ? 'tabact' : 'tab';
$width = $isActive ? $widthAct : $widthNo;

// @rene: Here you should probably wrap $label and $url in htmlspecialchars() in order to make sure its XHTML compatible! I did it for $url already since that is VERY likely to break.
$label = $def['label'];
$url = htmlspecialchars($def['url']);
$params = $def['addParams'];

if($first) {
$options.= '
<td width="'.$width.'%" class="'.$class.'" style="border-left: solid #000 1px;"><a href="'.$url.'" style="padding-left:5px;padding-right:2px;" '.$params.'>'.$label.'</a></td>';
} else {
$options.='
<td width="'.$width.'%" class="'.$class.'"><a href="'.$url.'" '.$params.'>'.$label.'</a></td>';
}
$first=false;
}

if ($options) {
$content .= '
<!-- Tab menu -->
<table cellpadding="0" cellspacing="0" border="0" width="100%" id="typo3-tabmenu">
<tr>
<td width="'.$widthLeft.'%">&nbsp;</td>
'.$options.'
<td width="'.$widthRight.'%">&nbsp;</td>
</tr>
</table>
<div class="hr" style="margin:0px"></div>';
}

}
return $content;
}
/**
* Creates a DYNAMIC tab-menu where the tabs are switched between with DHTML.
* Should work in MSIE, Mozilla, Opera and Konqueror. On Konqueror I did find a serious problem: <textarea> fields loose their content when you switch tabs!
*
* @param array Numeric array where each entry is an array in itself with associative keys: "label" contains the label for the TAB, "content" contains the HTML content that goes into the div-layer of the tabs content. "description" contains description text to be shown in the layer. "linkTitle" is short text for the title attribute of the tab-menu link (mouse-over text of tab). "stateIcon" indicates a standard status icon (see ->icon(), values: -1, 1, 2, 3). "icon" is an image tag placed before the text.
* @param string Identification string. This should be unique for every instance of a dynamic menu!
* @param integer If "1", then enabling one tab does not hide the others - they simply toggles each sheet on/off. This makes most sense together with the $foldout option. If "-1" then it acts normally where only one tab can be active at a time BUT you can click a tab and it will close so you have no active tabs.
* @param boolean If set, the tabs are rendered as headers instead over each sheet. Effectively this means there is no tab menu, but rather a foldout/foldin menu. Make sure to set $toggle as well for this option.
* @param integer Character limit for a new row.
* @param boolean If set, tab table cells are not allowed to wrap their content
* @param boolean If set, the tabs will span the full width of their position
* @param integer Default tab to open (for toggle <=0). Value corresponds to integer-array index + 1 (index zero is "1", index "1" is 2 etc.). A value of zero (or something non-existing) will result in no default tab open.
* @param boolean If set to '1' empty tabs will be remove, If set to '2' empty tabs will be disabled
* @return string JavaScript section for the HTML header.
*/
function getDynTabMenu($menuItems,$identString,$toggle=0,$foldout=FALSE,$newRowCharLimit=50,$noWrap=1,$fullWidth=FALSE,$defaultTabIndex=1,$dividers2tabs=1,$conf=array()) {
global $BACK_PATH;
// trying to set correct path for images, which relates with the 'icons' function.
if(!isset($this->backPath) && $BACK_PATH)
$backPath=$BACK_PATH;
elseif(isset($this->backPath))
$backPath=$this->backPath;
else $backPath=t3lib_div::getIndpEnv('TYPO3_SITE_URL').TYPO3_mainDir;
// id for configurations
if($this->id) // typical way to use id in backend modules
$id=$this->id;
elseif($GLOBALS['TSFE']->id) // typical way to use id in frontend editing and frontend plugins
$id=$GLOBALS['TSFE']->id;
elseif(is_numeric(t3lib_div::_GET('id')))
$id=t3lib_div::_GET('id');
$this->modSharedTSconfig = t3lib_BEfunc::getModTSconfig($id,'mod.SHARED');
$this->PageTSConfig=t3lib_BEfunc::getPagesTSconfig($id);
// control layout - table wrappers or without wrappers - wrappers make possible to create rounded background images for tabs
if(isset($conf['useWrappertable'])) // this is for frontend plugins - so must have priority
$useWrappertable=$conf['useWrappertable'];
elseif(isset($this->modSharedTSconfig['properties']['useWrappertable'])
$useWrappertable=$this->modSharedTSconfig['properties']['useWrappertable'];
elseif($this->PageTSConfig['mod.']['SHARED.']['useWrappertable'])
$useWrappertable=$this->PageTSConfig['mod.']['SHARED.']['useWrappertable'];
$content = '';
if (is_array($menuItems)) {

// Init:
$options = array(array());
$divs = array();
$JSinit = array();
$id = t3lib_tabMenus::getDynTabMenuId($identString);
$noWrap = $noWrap ? ' nowrap="nowrap"' : '';

// Traverse menu items
$c=0;
$tabRows=0;
$titleLenCount = 0;
foreach($menuItems as $index => $def) {
$index+=1; // Need to add one so checking for first index in JavaScript is different than if it is not set at all.

// Switch to next tab row if needed
if (!$foldout && ($titleLenCount>$newRowCharLimit | ($def['newline'] === true && $titleLenCount > 0))) { // 50 characters is probably a reasonable count of characters before switching to next row of tabs.
$titleLenCount=0;
$tabRows++;
$options[$tabRows] = array();
}

if ($toggle==1) {
$onclick = 'this.blur(); DTM_toggle("'.$id.'","'.$index.'"); return false;';
} else {
$onclick = 'this.blur(); DTM_activate("'.$id.'","'.$index.'", '.($toggle<0?1:0).'); return false;';
}
$isNonEmpty = strcmp(trim($def['content']),'');
// "Removes" empty tabs
if (!$isNonEmpty && $dividers2tabs == 1) {
continue;
}
// possible to set Windows XP style tabs
if($useWrappertable) {
$startTable='<table class="tabTable" cellspacing="0" cellpadding="0" border="0"><tr><td class="left"><div style="width:8px">&nbsp;</div></td><td class="middle">';
$endTable = '</td><td class="right"><div style="width:8px">&nbsp;</div></td></tr></table>';
}
else $mouseOverOut = 'onmouseover="DTM_mouseOver(this);" onmouseout="DTM_mouseOut(this);"'; //'onmouseover="DTM_mouseOver(this);" onmouseout="DTM_mouseOut(this);"';
if (!$foldout) {
// Create TAB cell:
$options[$tabRows][] = '
<td class="'.($isNonEmpty ? 'tab' : 'disabled').'" id="'.$id.'-'.$index.'-MENU"'.$noWrap.$mouseOverOut.'>'.
($isNonEmpty ? $startTable.'<a href="#" onclick="'.htmlspecialchars($onclick).'"'.($def['linkTitle'] ? ' title="'.htmlspecialchars($def['linkTitle']).'"':'').'>' : $startTable.'<span class="disabled">').
$def['icon'].
($def['label'] ? htmlspecialchars($def['label']) : '<span class="space">&nbsp;</span>').
t3lib_iconworks::icons($def['stateIcon'],'margin-left: 10px;',$backPath).
($isNonEmpty ? '</a>'.$endTable :'</span>'.$endTable).
'</td>';
$titleLenCount+= strlen($def['label']);
} else {
// Create DIV layer for content:
$divs[] = '
<div class="'.($isNonEmpty ? 'tab' : 'disabled').'" id="'.$id.'-'.$index.'-MENU"'.$mouseOverOut.'>'.
($isNonEmpty ? $startTable.'<a href="#" onclick="'.htmlspecialchars($onclick).'"'.($def['linkTitle'] ? ' title="'.htmlspecialchars($def['linkTitle']).'"':'').'>' : $startTable.'<span class="disabled">').
$def['icon'].
($def['label'] ? htmlspecialchars($def['label']) : '').
($isNonEmpty ? '</a>'.$endTable :'</span>'.$endTable).'</div>';
}

// Create DIV layer for content:
$divs[] = '
<div style="display: none;" id="'.$id.'-'.$index.'-DIV" class="c-tablayer'.$extraClass.'">'.
($def['description'] ? '<p class="c-descr">'.nl2br(htmlspecialchars($def['description'])).'</p>' : '').
$def['content'].
'</div>';
// Create initialization string:
$JSinit[] = '
DTM_array["'.$id.'"]['.$c.'] = "'.$id.'-'.$index.'";
';
if ($toggle==1) {
$JSinit[] = '
if (top.DTM_currentTabs["'.$id.'-'.$index.'"]) { DTM_toggle("'.$id.'","'.$index.'",1); }
';
}
$c++;
}

// Render menu:
if (count($options)) {

// Tab menu is compiled:
if (!$foldout) {
$tabContent = '';
for($a=0;$a<=$tabRows;$a++) {
$tabContent.= '

<!-- Tab menu -->
<table cellpadding="0" cellspacing="0" border="0"'.($fullWidth ? ' width="100%"' : '').' class="typo3-dyntabmenu">
<tr>
'.implode('',$options[$a]).'
</tr>
</table>';
}
$content.= '<div class="typo3-dyntabmenu-tabs">'.$tabContent.'</div>';
}

// Div layers are added:
$content.= '
<!-- Div layers for tab menu: -->
<div class="typo3-dyntabmenu-divs'.($foldout?'-foldout':'').'">
'.implode('',$divs).'</div>';

// Java Script section added:
$content.= '
<!-- Initialization JavaScript for the menu -->
<script type="text/javascript">
DTM_array["'.$id.'"] = new Array();
'.implode('',$JSinit).'
'.($toggle<=0 ? 'DTM_activate("'.$id.'", top.DTM_currentTabs["'.$id.'"]?top.DTM_currentTabs["'.$id.'"]:'.intval($defaultTabIndex).', 0);' : '').'
</script>

';
}
}
return $content;
}
/**
* Creates the id for dynTabMenus.
*
* @param string $identString: Identification string. This should be unique for every instance of a dynamic menu!
* @return string The id with a short MD5 of $identString and prefixed "DTM-", like "DTM-2e8791854a"
*/
function getDynTabMenuId($identString) {
$id = 'DTM-'.t3lib_div::shortMD5($identString);
return $id;
}
}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_tabmenus.php']) {
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_tabmenus.php']);
}
?>
(3-3/4)