aboutsummaryrefslogtreecommitdiff
path: root/jquery_drawer.module
blob: 169a74bd28b469bf267c1ccec6bdc19fb161c4eb (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
// $Id$

/**
 * @file
 * Implementation of a jQuery Drawer.
 *
 * This module implements the jQuery.drawer navigation menu from
 * http://lib.metatype.jp/jquery_drawer/sample.html
 */

/**
 * Implementation of hook_uninstall().
 */
function jquery_drawer_uninstall() {
  variable_del('jquery_drawer');
  variable_del('jquery_drawer_drw');
  variable_del('jquery_drawer_link');
}

/**
 * Implementation of hook_init().
 */
function jquery_drawer_init() {
  theme('jquery_drawer_css');
  theme('jquery_drawer_javascript');
}

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

    case 'configure':
      $query  = 'SELECT vid, name from {vocabulary}';
      $result = db_query(db_rewrite_sql($query));
      while ($vocabulary = db_fetch_object($result)) {
        $vocabularies[$vocabulary->vid] = $vocabulary->name;
      }
      $form['jquery_drawer'] = array(
        '#type'          => 'select',
        '#title'         => t('Select the vocabulary to list'),
        '#default_value' => variable_get('jquery_drawer', '1'),
        '#options'       => $vocabularies,
      );
      $form['jquery_drawer_drw'] = array(
        '#type'          => 'checkbox',
        '#title'         => t('Do not show drawer box.'),
        '#description'   => t('Select this option if you plan to manually provide the drawer box.'),
        '#default_value' => variable_get('jquery_drawer_drw', '0'),
      );
      $form['jquery_drawer_link'] = array(
        '#type'          => 'textfield',
        '#title'         => t('Base link path (useful for views).'),
        '#description'   => t('Set the base link for nodes listed in drawer.'),
        '#default_value' => variable_get('jquery_drawer_link', 'node'),
      );
      return $form;

    case 'save':
      variable_set('jquery_drawer',      $edit['jquery_drawer']);
      variable_set('jquery_drawer_drw',  $edit['jquery_drawer_drw']);
      variable_set('jquery_drawer_link', $edit['jquery_drawer_link']);
      break;

    case 'view':
      // Get all terms from a given vocabulary
      $vid  = variable_get('jquery_drawer', '1');
      $menu = taxonomy_node_tree_parents($vid);

      // Remove empty menus
      foreach ($menu as $key => $term) {
        if (jquery_drawer_check_empty($term->tid)) {
          unset($menu[$key]);
        }
      }

      $block['content'] = theme('jquery_drawer', $menu);
      return $block;
  }
}

/**
 * Check empty menus.
 */
function jquery_drawer_check_empty($tid) {
  $parents  = array();
  $nodes    = jquery_drawer_select_nodes($tid);
  $children = jquery_drawer_get_children($tid);

  while ($node = db_fetch_object($nodes)) {
    $parents[] = $node;
  }

  if (empty($parents) && empty($children)) {
    return TRUE;
  }
}

/**
 * Static version of taxonomy_select_nodes().
 */
function jquery_drawer_select_nodes($tid = NULL) {
  static $cache = array();

  if ($tid == NULL) {
    return;
  }

  if (!isset($cache[$tid])) {
    $cache[$tid] = taxonomy_select_nodes(array($tid));
  }

  return $cache[$tid];
}

/**
 * Implementation of hook_menu().
 */
function jquery_drawer_menu() {
  $items['jquery_drawer'] = array(
    'title'            => 'jQuery Drawer',
    'description'      => 'Displays content inside a jQuery Drawer.',
    'page callback'    => 'jquery_drawer_page',
    'access arguments' => array('access content'),
    'type'             => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function jquery_drawer_theme() {
  return array(
      'jquery_drawer' => array(
        'arguments'   => array(
          'menu'      => NULL,
          ),
        ),
      'jquery_drawer_javascript' => array(
        'arguments'              => array(),
        ),
      'jquery_drawer_css'        => array(
        'arguments'              => array(),
        ),
      'jquery_drawer_link'       => array(
        'arguments'              => array(),
        ),
      );
}

/**
 * Drawer logic.
 *
 * @ingroup themeable
 */
function theme_jquery_drawer($menu) {
  $output  = '<ul id="drw_tabs">';
  $output .= implode((array) module_invoke_all('jquery_drawer'));

  if (module_exists('i18n')) {
    $menu = i18ntaxonomy_localize_terms($menu);
  }

  foreach ($menu as $item) {
    $link['title'] = $item->name;
    $link['href']  = 'jquery_drawer/'. $item->tid;
    $output       .= theme('jquery_drawer_link', $link);
  }

  $output .= '</ul>';

  if (variable_get('jquery_drawer_drw', '0') == 0) {
    $output .= '<div id="drw">';
    $output .= '</div>';
  }

  return $output;
}

/**
 * Render all nodes whose terms are children of $tid.
 */
function jquery_drawer_get_children($tid = NULL) {
  static $cache = array();

  if (isset($cache[$tid])) {
    return $cache[$tid];
  }

  $vid   = variable_get('jquery_drawer', '1');
  $terms = taxonomy_node_tree_taxonomy_get_tree($vid, $tid);

  if (module_exists('i18n')) {
    $terms = i18ntaxonomy_localize_terms($terms);
  }

  foreach ($terms as $term) {
    $filter[] = $term->tid;
  }

  if (isset($filter) && !empty($filter)) {
    // Then render all nodes whose terms are children of $term
    $query  = 'SELECT DISTINCT n.nid, n.title, term_node.tid FROM {node} n LEFT JOIN
               {term_node} ON term_node.nid = n.nid WHERE term_node.tid IN (%s)
               AND n.status = "1" ORDER BY term_node.weight_in_tid ASC';

    //$query = db_rewrite_sql($query);
    $nodes = db_query(db_rewrite_sql($query), implode(',', $filter));
    $tree  = taxonomy_node_tree_build($nodes, $terms);

    $cache[$tid] = $tree;
    return $tree;
  }

  $cache[$tid] = NULL;
}

/**
 * Menu callback.
 */
function jquery_drawer_page($tid = NULL) {
  static $cache = array();

  if (!isset($cache[$tid])) {
    $output  = '';
    $nodes   = jquery_drawer_select_nodes($tid);
    $base    = variable_get('jquery_drawer_link', 'node');
    $tree    = jquery_drawer_get_children($tid);
    $output .= '<ul id="drw_item" class="hidden">';

    // First render all nodes whose parent is $term
    while ($node = db_fetch_object($nodes)) {
      path_nodeapi($node,'load','');
      $link['title']  = $node->title;
      $link['href']   = ($node->path?$node->path:"$base/". $node->nid);
      $output        .= theme('jquery_drawer_link', $link);
    }

    // Add other nodes
    if (is_array($tree) && !empty($tree)) {
      foreach ($tree as $term) {
        $output .= theme('taxonomy_node_tree_list', $term, 'jquery_drawer', 'hidden', $base);
      }
    }
    else {
      $output .= t('No content exists for this topic yet.');
    }

    $output      .= '</ul></li>';
    $cache[$tid]  = $output;
  }

  // Display output
  echo($output);

  // We need to exit here to avoid themeable output
  exit();
}

/**
 * jQuery Drawer Javascript theme function.
 *
 * @ingroup themeable
 */
function theme_jquery_drawer_javascript() {
  drupal_add_js(drupal_get_path('module', 'jquery_drawer') .'/jquery_drawer/drw/scripts.js');
}

/**
 * jQuery Drawer CSS theme function.
 *
 * @ingroup themeable
 */
function theme_jquery_drawer_css() {
  drupal_add_css(drupal_get_path('module', 'jquery_drawer') .'/jquery_drawer/drw/styles.css');
  drupal_add_css(drupal_get_path('module', 'jquery_drawer') .'/jquery_drawer.css');
}

/**
 * jQuery Drawer Link theme function.
 *
 * @ingroup themeable
 */
function theme_jquery_drawer_link($link) {
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }

  $link['attributes']['rel'] = 'drw';

  $output  = '<li class="drw_li">';
  $output .= l($link['title'], $link['href'], array('attributes' => array('rel' => 'drw')));
  $output .= '</li>';

  return $output;
}