[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 3 define('HORDE_MENU_POS_LAST', 999); 4 5 define('HORDE_MENU_MASK_NONE', 0); 6 define('HORDE_MENU_MASK_HELP', 1); 7 define('HORDE_MENU_MASK_LOGIN', 2); 8 define('HORDE_MENU_MASK_PREFS', 4); 9 define('HORDE_MENU_MASK_PROBLEM', 8); 10 define('HORDE_MENU_MASK_ALL', 15); 11 12 /** 13 * The Menu:: class provides standardized methods for creating menus in 14 * Horde applications. 15 * 16 * $Horde: framework/Horde/Horde/Menu.php,v 1.35.2.11 2006/07/20 04:31:27 slusarz Exp $ 17 * 18 * Copyright 1999-2006 Chuck Hagenbuch <chuck@horde.org> 19 * Copyright 1999-2006 Jon Parise <jon@horde.org> 20 * 21 * See the enclosed file COPYING for license information (LGPL). If you 22 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 23 * 24 * @author Chuck Hagenbuch <chuck@horde.org> 25 * @author Jon Parise <jon@horde.org> 26 * @since Horde 1.3 27 * @package Horde_Framework 28 */ 29 class Menu { 30 31 /** 32 * Menu array. 33 * 34 * @var array 35 */ 36 var $_menu = array(); 37 38 /** 39 * Mask defining what general Horde links are shown in this Menu. 40 * 41 * @var integer 42 */ 43 var $_mask; 44 45 /** 46 * Constructor 47 */ 48 function Menu($mask = HORDE_MENU_MASK_ALL) 49 { 50 /* Menuitem mask. */ 51 $this->_mask = $mask; 52 53 /* Location of the menufile. */ 54 $this->_menufile = $GLOBALS['registry']->get('fileroot') . '/config/menu.php'; 55 } 56 57 /** 58 * Add an item to the menu array. 59 * 60 * @param string $url String containing the value for the hyperlink. 61 * @param string $text String containing the label for this menu 62 * item. 63 * @param string $icon String containing the filename of the image 64 * icon to display for this menu item. 65 * @param string $icon_path If the icon lives in a non-default directory, 66 * where is it? 67 * @param string $target If the link needs to open in another frame or 68 * window, what is its name? 69 * @param string $onclick Onclick javascript, if desired. 70 * @param string $class CSS class for the menu item. 71 * 72 * @return integer The id (NOT guaranteed to be an array index) of the 73 * item just added to the menu. 74 */ 75 function add($url, $text, $icon = '', $icon_path = null, 76 $target = '', $onclick = null, $class = null) 77 { 78 $pos = count($this->_menu); 79 if (!$pos || ($pos - 1 != max(array_keys($this->_menu)))) { 80 $pos = count($this->_menu); 81 } 82 83 $this->_menu[$pos] = 84 array( 85 'url' => $url, 86 'text' => $text, 87 'icon' => $icon, 88 'icon_path' => $icon_path, 89 'target' => $target, 90 'onclick' => $onclick, 91 'class' => $class 92 ); 93 94 return $pos; 95 } 96 97 /** 98 * Add an item to the menu array. 99 * 100 * @param string $url String containing the value for the hyperlink. 101 * @param string $text String containing the label for this menu 102 * item. 103 * @param string $icon String containing the filename of the image 104 * icon to display for this menu item. 105 * @param string $icon_path If the icon lives in a non-default directory, 106 * where is it? 107 * @param string $target If the link needs to open in another frame or 108 * window, what is its name? 109 * @param string $onclick Onclick javascript, if desired. 110 * @param string $class CSS class for the menu item. 111 * 112 * @return integer The id (NOT guaranteed to be an array index) of the item 113 * just added to the menu. 114 */ 115 function addArray($item) 116 { 117 $pos = count($this->_menu); 118 if (!$pos || ($pos - 1 != max(array_keys($this->_menu)))) { 119 $pos = count($this->_menu); 120 } 121 122 $this->_menu[$pos] = $item; 123 124 return $pos; 125 } 126 127 function setPosition($id, $pos) 128 { 129 if (!isset($this->_menu[$id]) || isset($this->_menu[$pos])) { 130 return false; 131 } 132 133 $item = $this->_menu[$id]; 134 unset($this->_menu[$id]); 135 $this->_menu[$pos] = $item; 136 137 return true; 138 } 139 140 /** 141 * Return the unordered list representing the list of menu items. Styling 142 * is done through CSS. 143 * 144 * @return string An unordered list of menu elements that can be entirely 145 * styled with CSS. 146 */ 147 function render() 148 { 149 global $conf, $registry, $prefs; 150 151 $graphics = $registry->getImageDir('horde'); 152 $app = $registry->getApp(); 153 154 if ($this->_mask !== HORDE_MENU_MASK_NONE) { 155 /* Add any custom menu items. */ 156 $this->addSiteLinks(); 157 158 /* Add any app menu items. */ 159 $this->addAppLinks(); 160 } 161 162 /* Add settings link. */ 163 if ($this->_mask & HORDE_MENU_MASK_PREFS && $url = Horde::getServiceLink('options', $app)) { 164 $this->add($url, _("_Options"), 'prefs.png', $graphics); 165 } 166 167 /* Add problem link. */ 168 if ($this->_mask & HORDE_MENU_MASK_PROBLEM && $problem_link = Horde::getServiceLink('problem', $app)) { 169 $this->add($problem_link, _("Problem"), 'problem.png', $graphics); 170 } 171 172 /* Add help link. */ 173 require_once 'Horde/Help.php'; 174 if ($this->_mask & HORDE_MENU_MASK_HELP && $help_link = Horde::getServiceLink('help', $app)) { 175 $this->add($help_link, _("Help"), 'help_index.png', $graphics, 'help', 'popup(this.href); return false;'); 176 } 177 178 /* Login/Logout. */ 179 if ($this->_mask & HORDE_MENU_MASK_LOGIN) { 180 /* If the sidebar isn't always shown, but is sometimes 181 * shown, then logout links should be to the parent 182 * frame. */ 183 $auth_target = null; 184 if ($conf['menu']['always'] || $prefs->getValue('show_sidebar')) { 185 $auth_target = '_parent'; 186 } 187 188 if (Auth::getAuth()) { 189 if ($logout_link = Horde::getServiceLink('logout', $app, !$prefs->getValue('show_sidebar'))) { 190 $this->add($logout_link, _("_Log out"), 'logout.png', $graphics, $auth_target, null, '__noselection'); 191 } 192 } else { 193 if ($login_link = Horde::getServiceLink('login', $app)) { 194 $this->add($login_link, _("_Log in"), 'login.png', $graphics, $auth_target, null, '__noselection'); 195 } 196 } 197 } 198 199 /* No need to return an empty list if there are no menu 200 * items. */ 201 if (!count($this->_menu)) { 202 return ''; 203 } 204 205 /* Sort to match explicitly set positions. */ 206 ksort($this->_menu); 207 if (!empty($GLOBALS['nls']['rtl'][$GLOBALS['language']])) { 208 $this->_menu = array_reverse($this->_menu) ; 209 } 210 211 $menu_view = $prefs->getValue('menu_view'); 212 $output = '<ul>'; 213 foreach ($this->_menu as $m) { 214 /* Item class and selected indication. */ 215 if (!isset($m['class'])) { 216 /* Try to match the item's path against the current 217 * script filename as well as other possible URLs to 218 * this script. */ 219 if (Menu::isSelected($m['url'])) { 220 $m['class'] = 'current'; 221 } 222 } elseif ($m['class'] === '__noselection') { 223 unset($m['class']); 224 } 225 226 /* Icon. */ 227 $icon = ''; 228 if ($menu_view == 'icon' || $menu_view == 'both') { 229 if (!isset($m['icon_path'])) { 230 $m['icon_path'] = null; 231 } 232 $icon = Horde::img($m['icon'], Horde::stripAccessKey($m['text']), '', $m['icon_path']); 233 } 234 235 /* Link. */ 236 $accesskey = Horde::getAccessKey($m['text']); 237 $link = Horde::link($m['url'], $menu_view == 'icon' ? $m['text'] : '', 238 isset($m['class']) ? $m['class'] : '', 239 isset($m['target']) ? $m['target'] : '', 240 isset($m['onclick']) ? $m['onclick'] : '', 241 '', $accesskey); 242 243 $output .= sprintf("\n<li>%s%s<br />%s</a></li>", 244 $link, $icon, ($menu_view != 'icon') ? Horde::highlightAccessKey($m['text'], $accesskey) : ''); 245 } 246 247 return $output . '</ul>'; 248 } 249 250 /** 251 * Any links to other Horde applications defined in an application's config 252 * file by the $conf['menu']['apps'] array are added to the menu array. 253 */ 254 function addAppLinks() 255 { 256 global $conf, $registry; 257 258 if (isset($conf['menu']['apps']) && is_array($conf['menu']['apps'])) { 259 foreach ($conf['menu']['apps'] as $app) { 260 if ($registry->get('status', $app) != 'inactive' && $registry->hasPermission($app, PERMS_SHOW)) { 261 $url = $registry->getInitialPage($app); 262 if (!is_a($url, 'PEAR_Error')) { 263 $this->add(Horde::url($url), $registry->get('name', $app), $registry->get('icon', $app), ''); 264 } 265 } 266 } 267 } 268 } 269 270 /** 271 * Add any other links found in $this->_menufile to be included in the 272 * menu. 273 */ 274 function addSiteLinks() 275 { 276 if (@is_readable($this->_menufile)) { 277 include $this->_menufile; 278 if (isset($_menu) && is_array($_menu)) { 279 foreach ($_menu as $menuitem) { 280 $this->addArray($menuitem); 281 } 282 } 283 } 284 } 285 286 /** 287 * Checks to see if the current url matches the given url. 288 * 289 * @return boolean Whether the given URL is the current location. 290 */ 291 function isSelected($url) 292 { 293 $server_url = parse_url($_SERVER['PHP_SELF']); 294 $check_url = parse_url($url); 295 296 /* Try to match the item's path against the current script 297 filename as well as other possible URLs to this script. */ 298 if (isset($check_url['path']) && 299 (($check_url['path'] == $server_url['path']) || 300 ($check_url['path'] . 'index.php' == $server_url['path']) || 301 ($check_url['path'] . '/index.php' == $server_url['path']))) { 302 return true; 303 } 304 305 return false; 306 } 307 308 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |