~ubuntu-branches/ubuntu/saucy/horde3/saucy

« back to all changes in this revision

Viewing changes to lib/Horde/Menu.php

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-05-04 23:08:08 UTC
  • Revision ID: james.westby@ubuntu.com-20050504230808-p4hf3hk28o3v7wir
Tags: upstream-3.0.4
ImportĀ upstreamĀ versionĀ 3.0.4

Show diffs side-by-side

added added

removed removed

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