1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16: class
17: {
18: 19: 20:
21: const MASK_NONE = 0;
22:
23: 24: 25:
26: const MASK_HELP = 1;
27:
28: 29: 30:
31: const MASK_LOGIN = 2;
32:
33: 34: 35:
36: const MASK_PREFS = 4;
37:
38: 39: 40:
41: const MASK_PROBLEM = 8;
42:
43: 44: 45:
46: const MASK_BASE = 16;
47:
48: 49: 50:
51: const MASK_ALL = 31;
52:
53:
54: const POS_LAST = 999;
55:
56: 57: 58: 59: 60:
61: protected $_mask;
62:
63: 64: 65: 66: 67:
68: protected = array();
69:
70: 71: 72: 73: 74:
75: public function __construct($mask = self::MASK_ALL)
76: {
77: $this->setMask($mask);
78: }
79:
80: 81: 82: 83: 84: 85: 86:
87: public function setMask($mask)
88: {
89: $this->_mask = $mask;
90: }
91:
92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109:
110: public function add($url, $text, $icon = '', $icon_path = null,
111: $target = '', $onclick = null, $class = null)
112: {
113: $pos = count($this->_menu);
114: if (!$pos || ($pos - 1 != max(array_keys($this->_menu)))) {
115: $pos = count($this->_menu);
116: }
117:
118: $this->_menu[$pos] = array(
119: 'url' => ($url instanceof Horde_Url) ? $url : new Horde_Url($url),
120: 'text' => $text,
121: 'icon' => $icon,
122: 'icon_path' => $icon_path,
123: 'target' => $target,
124: 'onclick' => $onclick,
125: 'class' => $class
126: );
127:
128: return $pos;
129: }
130:
131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147:
148: public function addArray($item)
149: {
150: $pos = count($this->_menu);
151: if (!$pos || ($pos - 1 != max(array_keys($this->_menu)))) {
152: $pos = count($this->_menu);
153: }
154:
155: if (!isset($item['url'])) {
156: $item['url'] = new Horde_Url();
157: } elseif (!($item['url'] instanceof Horde_Url)) {
158: $item['url'] = new Horde_Url($item['url']);
159: }
160:
161: $this->_menu[$pos] = array_merge(array(
162: 'class' => '',
163: 'icon' => '',
164: 'icon_path' => null,
165: 'onclick' => null,
166: 'target' => '',
167: 'text' => ''
168: ), $item);
169:
170: return $pos;
171: }
172:
173: 174: 175:
176: public function setPosition($id, $pos)
177: {
178: if (!isset($this->_menu[$id]) || isset($this->_menu[$pos])) {
179: return false;
180: }
181:
182: $item = $this->_menu[$id];
183: unset($this->_menu[$id]);
184: $this->_menu[$pos] = $item;
185:
186: return true;
187: }
188:
189: 190: 191: 192: 193:
194: public function render()
195: {
196: global $conf, $registry, $prefs;
197:
198: $app = $registry->getApp();
199:
200: if ($this->_mask !== self::MASK_NONE) {
201:
202: $this->addSiteLinks();
203:
204:
205: $this->addAppLinks();
206: }
207:
208:
209: if (($this->_mask & self::MASK_PREFS) &&
210: $this->showService('prefs') &&
211: ($url = Horde::getServiceLink('prefs', $app))) {
212: $this->add($url, Horde_Core_Translation::t("_Preferences"), 'prefs.png');
213: }
214:
215:
216: if (($this->_mask & self::MASK_PROBLEM) &&
217: $this->showService('problem') &&
218: ($problem_link = Horde::getServiceLink('problem', $app))) {
219: $this->add($problem_link, Horde_Core_Translation::t("Problem"), 'problem.png');
220: }
221:
222:
223: if (($this->_mask & self::MASK_HELP) &&
224: $this->showService('help') &&
225: ($help_link = Horde::getServiceLink('help', $app))) {
226: Horde::
227: $this->add($help_link, Horde_Core_Translation::t("Help"), 'help_index.png', null, 'help', Horde::popupJs($help_link, array('urlencode' => true)) . 'return false;', 'helplink');
228: }
229:
230:
231: if ($this->_mask & self::MASK_LOGIN) {
232: 233: 234:
235: $auth_target = null;
236: if ($conf['menu']['always'] || $prefs->getValue('show_sidebar')) {
237: $auth_target = '_parent';
238: }
239:
240: if ($registry->getAuth()) {
241: if ((!$prefs->getValue('show_sidebar') || $this->showService('logout')) &&
242: ($logout_link = Horde::getServiceLink('logout', $app))) {
243: $this->add($logout_link, Horde_Core_Translation::t("_Log out"), 'logout.png', null, $auth_target, null, '__noselection');
244: }
245: } else {
246: if ($this->showService('login') &&
247: ($login_link = Horde::getServiceLink('login', $app))) {
248: $this->add($login_link->add('url', Horde::selfUrl(true, true, true)), Horde_Core_Translation::t("_Log in"), 'login.png', null, $auth_target, null, '__noselection');
249: }
250: }
251: }
252:
253: 254:
255: if (!count($this->_menu)) {
256: return '';
257: }
258:
259:
260: ksort($this->_menu);
261: if ($registry->nlsconfig->curr_rtl) {
262: $this->_menu = array_reverse($this->_menu);
263: }
264:
265: return $this->_render();
266: }
267:
268: 269: 270: 271: 272: 273: 274:
275: protected function _render()
276: {
277: $menu_view = $GLOBALS['prefs']->getValue('menu_view');
278: $output = '<ul>';
279:
280: foreach ($this->_menu as $m) {
281:
282: if ($m == 'separator') {
283: $output .= "\n<li class=\"separator\"> </li>";
284: continue;
285: }
286:
287:
288: if (!isset($m['class'])) {
289: 290: 291:
292: if ($this->isSelected($m['url'])) {
293: $m['class'] = 'current';
294: }
295: } elseif ($m['class'] === '__noselection') {
296: unset($m['class']);
297: }
298:
299:
300: $icon = '';
301: if ($menu_view == 'icon' || $menu_view == 'both') {
302: if (empty($m['icon_path'])) {
303: $m['icon_path'] = null;
304: }
305: $icon = Horde::img($m['icon'], Horde::stripAccessKey($m['text']), '', $m['icon_path']) . '<br />';
306: }
307:
308:
309: $accesskey = Horde::getAccessKey($m['text']);
310: $link = $m['url']->setRaw(false)->link(
311: array('title' => $menu_view == 'icon' ? Horde::stripAccessKey($m['text']) : '',
312: 'class' => isset($m['class']) ? $m['class'] : '',
313: 'target' => $m['target'],
314: 'onclick' => $m['onclick'],
315: 'accesskey' => $accesskey));
316:
317: $output .= sprintf("\n<li>%s%s%s</a></li>",
318: $link, $icon, ($menu_view != 'icon') ? Horde::highlightAccessKey($m['text'], $accesskey) : '');
319: }
320:
321: return $output . '</ul>';
322: }
323:
324: 325: 326: 327:
328: public function addAppLinks()
329: {
330: global $registry;
331:
332: foreach ($this->getAppLinks() as $app) {
333: try {
334: $this->add(Horde::url($registry->getInitialPage($app)), $registry->get('name', $app), $registry->get('icon', $app), '');
335: } catch (Horde_Exception $e) {}
336: }
337: }
338:
339: 340: 341: 342: 343: 344:
345: public function getAppLinks()
346: {
347: global $conf, $registry;
348:
349: $out = array();
350:
351: if (isset($conf['menu']['apps']) && is_array($conf['menu']['apps'])) {
352: foreach ($conf['menu']['apps'] as $app) {
353: if (!$registry->isInactive($app) &&
354: $registry->hasPermission($app, Horde_Perms::SHOW)) {
355: $out[] = $app;
356: }
357: }
358: }
359:
360: return $out;
361: }
362:
363: 364: 365:
366: public function addSiteLinks()
367: {
368: foreach ($this->getSiteLinks() as $item) {
369: $this->addArray($item);
370: }
371: }
372:
373: 374: 375: 376: 377:
378: public function getSiteLinks()
379: {
380: $menufile = $GLOBALS['registry']->get('fileroot') . '/config/menu.php';
381:
382: if (is_readable($menufile)) {
383: include $menufile;
384: if (isset($_menu) && is_array($_menu)) {
385: return $_menu;
386: }
387: }
388:
389: return array();
390: }
391:
392: 393: 394: 395: 396:
397: static public function isSelected($url)
398: {
399: $server_url = parse_url($_SERVER['PHP_SELF']);
400: $check_url = parse_url($url);
401:
402: 403:
404: return isset($check_url['path']) &&
405: (($check_url['path'] == $server_url['path']) ||
406: ($check_url['path'] . 'index.php' == $server_url['path']) ||
407: ($check_url['path'] . '/index.php' == $server_url['path']));
408: }
409:
410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421:
422: static public function showService($type)
423: {
424: global $conf;
425:
426: if (!in_array($type, array('help', 'problem', 'logout', 'login', 'prefs'))) {
427: return true;
428: }
429:
430: if (empty($conf['menu']['links'][$type])) {
431: return false;
432: }
433:
434: switch ($conf['menu']['links'][$type]) {
435: case 'all':
436: return true;
437:
438: case 'authenticated':
439: return (bool)$GLOBALS['registry']->getAuth();
440:
441: default:
442: case 'never':
443: return false;
444: }
445: }
446:
447: }
448: