aboutsummaryrefslogtreecommitdiff
path: root/finder_menu.module
blob: a372112e1141c12887e6bb3cf856afdbf84519f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
// $Id$

/**
 * @file
 * Implementation of a Finder Dropdown Menu.
 *
 * This module implements the Finder Dropdown menu described
 * at http://www.alistapart.com/articles/complexdynamiclists.
 */

/**
 * Implementation of hook_uninstall();
 */
function finder_menu_uninstall() {
  variable_del('finder_menu');
}

/**
 * Implementation of hook_init();
 */
function finder_menu_init() {
  theme('finder_menu_css');
  theme('finder_menu_javascript');
}

/**
 * Implementation of hook_block();
 */
function finder_menu_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks[0]['info']  = t('Finder Menu');
      $blocks[0]['cache'] = BLOCK_NO_CACHE;
      return $blocks;

    case 'configure':
      $form['finder_menu'] = array(
        '#type'          => 'select',
        '#title'         => t('Select the menu to list'),
        '#default_value' => variable_get('finder_menu', 'navigation:0'),
        '#options'       => menu_parent_options(menu_get_menus(), 0),
      );
      return $form;

    case 'save':
      variable_set('finder_menu', $edit['finder_menu']);
      break;

    case 'view':
      $menu             = explode(':', variable_get('finder_menu', 'navigation:0'));
      $block['content'] = theme('finder_menu', $menu[0], $menu[1]);
      return $block;
  }
}

/**
 * Implementation of hook_theme();
 */
function finder_menu_theme() {
  return array(
      'finder_menu' => array(
        'arguments' => array(
          'menu'    => NULL,
          'parent'  => NULL,
          ),
        ),
      'finder_menu_javascript' => array(
        'arguments'            => array(),
        ),
      'finder_menu_css'        => array(
        'arguments'            => array(),
        ),
      );
}

/**
 * Generate the menu.
 */
function theme_finder_menu($menu_name, $mlid) {
  // Find menu item in the menu tree

  $menu_tree = menu_tree_all_data($menu_name);
  $menu_link = menu_link_load($mlid);

  if ($mlid != 0) {
    for ($i=1; $i<10; $i++) {
      foreach ($menu_tree as $menu_item) {
        if ($menu_item["link"]['mlid'] == $mlid) {
          $menu = $menu_item['below'];
          break 2;
        }
        else {
          if ($menu_item["link"]['mlid'] == $menu_link['p'. $i]) {
            $menu_tree = $menu_item['below'];
            break;
          }
        }
      }
    }
  }
  else {
    $menu = $menu_tree;
  }

  // Don't display anything if the selected menu has no children

  if (!$menu) {
    return;
  }

  // Backup active menu trail and set a new one

  $active_menu_name = menu_get_active_menu_name();
  menu_set_active_menu_name($menu_name);

  // Build table of mlid in the active trail

  foreach (menu_set_active_trail() as $value) {
    if ($value['mlid']) {
      $trail[] = $value['mlid'];
    }
  }

  // Restore active menu trail

  menu_set_active_menu_name($active_menu_name);

  // Build the menus

  $output  = '<div id="finderparent">';
  $output .= finder_menu_build($menu, 'finder', 'hidden');
  $output .= '</div>';

  return $output;
}

/**
 * Finder Menu Javascript theme function.
 *
 * @ingroup themeable
 */
function theme_finder_menu_javascript() {
  drupal_add_js(drupal_get_path('module', 'finder_menu') .'/finder_menu.js');
}

/**
 * Finder Menu CSS theme function.
 *
 * @ingroup themeable
 */
function theme_finder_menu_css() {
  drupal_add_css(drupal_get_path('module', 'finder_menu') .'/finder_menu.css');
}

/**
 * Recursively build the menu.
 *
 * @ingroup themeable
 * @TODO: usage with hook_theme();
 */
function finder_menu_build($menu, $id, $class, &$level = NULL) {

  if ($level == NULL) {
    $output  = '<ul id="' . $id .'" class="'. $class .'">';
    $output .= implode((array) module_invoke_all('finder_menu_build'));
    $level = 0;
  }
  else {
    $output = '<ul id="'. $id .'-ul-'. $level .'" class="'. $class .'">';
  }

  foreach ($menu as $menu_item) {
    if ($menu_item['link']['hidden'] == 0) {
      $output .= '<li>';
      if ($menu_item['below'] !== FALSE && finder_menu_has_unhidden_submenu($menu_item['below'])) {
        $level++;
        $output .= $menu_item['link']['title'];
        $output .= finder_menu_build($menu_item['below'], $id, $class, $level);
      }
      else {
        $output .= theme('menu_item_link', $menu_item['link']);
      }
      $output .= '</li>';
    }
  }

  $output .= '</ul>';
  return $output;

}

/**
 * Check whether a menu has at least one unhidden submenu.
 */
function finder_menu_has_unhidden_submenu($menu = FALSE) {

  if ($menu == FALSE) {
    return FALSE;
  }

  foreach ($menu as $menu_item) {
    if ($menu_item['link']['hidden'] == 0) {
      return TRUE;
    }
  }

  return FALSE;
}