aboutsummaryrefslogtreecommitdiff
path: root/taxonomy_node_tree.module
blob: 088c02a38cc51c74afab180cee36bdc8f3f31190 (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
<?php
// $Id$

/**
 * Return the parent terms of a given vocabulary.
 *
 * This function takes a vocabulary id and returns a
 * list of it's parent terms.
 */
function taxonomy_node_tree_parents($vid) {
  // Get all terms from a given vocabulary
  $terms = taxonomy_node_tree_taxonomy_get_tree($vid);

  foreach ($terms as $term) {
    // Just show parent terms
    if (taxonomy_get_parents($term->tid) == array()) {
      $menu[] = $term;
    }
  }

  return $menu;
}

/**
 * Implementation of hook_theme();
 *
 * @TODO: update
 */
function taxonomy_node_tree_theme() {
  return array(
      'taxonomy_node_tree_menu_parents' => array(
        'arguments'                     => array(
          'menu'                        => NULL,
          'parent'                      => NULL,
          ),
        ),
      'taxonomy_node_tree_link'       => array(
        'arguments'                   => array(),
        ),
      );
}

/**
 * Render the parent items of a menu.
 *
 * @ingroup themeable
 */
function theme_taxonomy_node_tree_menu_parents($menu, $class = 'menu', $id = NULL, $base = NULL) {

  if ($id != NULL) {
    $id = ' id="'. $id .'"';
  }

  $output  = '<ul class="'. $class .'"'.  $id .'>';
  $output .= implode((array) module_invoke_all('taxonomy_node_tree_menu_parents'));

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

  $output .= '</ul>';
  $output .= '<div id="drw">';
  $output .= '</div>';
  return $output;

}

/**
 * Link theme function.
 *
 * @ingroup themeable
 */
function theme_taxonomy_node_tree_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;
}

/**
 * Setup an index of terms associated with it's children nodes.
 *
 * This function accept a list of nodes and terms and build a
 * tree with the corresponding association between terms and
 * nodes.
 *
 * @TODO: It is assumed that nodes are just associated with
 *        a single term.
 */
function taxonomy_node_tree_index($nodes, $terms) {  
  while ($node = db_fetch_object($nodes)) {
    foreach ($terms as $term) {
      // add nodes into the term
      if ($node->tid == $term->tid) {
        $term->nodes[] = $node;
      }
      // update an index of terms
      $tree[$term->tid] = $term;
    }
  }
  return $tree;
}

/**
 * Add children relationship for terms present in the tree.
 */
function taxonomy_node_tree_relation($tree) {
  foreach ($tree as $term) {
    if ($term->parents[0] != 0 && isset($tree[$term->parents[0]])) {
      $tree[$term->parents[0]]->children[] = $term->tid;
    }
  }
  return $tree;
}

/**
 * Sort terms in the tree.
 *
 * This function add terms on it's right place in the taxonomy tree.
 */
function taxonomy_node_tree_sort(&$tree, $term) {
  if ($term->parents[0] != 0 && isset($tree[$term->parents[0]])) {
    // is child
    if (isset($term->children)) {
      // is also parent, so go down one level
      foreach($term->children as $child) {
        taxonomy_node_tree_sort($tree, $tree[$child]);
      }
    }
    $tree[$term->parents[0]]->below[] = drupal_clone($term);
    unset($tree[$term->tid]);
  }
}

/**
 * Sort a taxonomy tree to the right hierarchy.
 */
function taxonomy_node_tree_hierarchy($tree) {
  foreach ($tree as $term) {
    taxonomy_node_tree_sort($tree, $term);
  }
  return $tree;
}

function taxonomy_node_tree_build($nodes, $terms) {
  $tree = taxonomy_node_tree_index($nodes, $terms);
  $tree = taxonomy_node_tree_relation($tree);
  $tree = taxonomy_node_tree_hierarchy($tree);
  return $tree;
}

/**
 * Menu callback.
 *
 * @TODO: revamp
 */
function taxonomy_node_tree($tid = 0, $vid = 1, $type = NULL) {
  if ($type) {
     // TODO: validate $type before sql query
     $type = ' AND node.type = "'. $type .'"';
  }

  // Select nodes and terms
  if ($tid != 0) {

    // First render all nodes whose parent is $term
    $output = '';
    $nodes = taxonomy_select_nodes(array($tid));

    while ($node = db_fetch_object($nodes)) {
      $link['title']  = $node->title;
      $link['href']   = 'node/' . $node->nid;
      $output        .= theme('taxonomy_node_tree_link', $link);
    }

    // Render other child nodes

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

    $query  = 'SELECT node.nid, node.title, term_node.tid FROM {node} LEFT JOIN 
               {term_node} ON term_node.nid = node.nid WHERE term_node.tid IN (%s)
               AND node.status = "1"';
    $query .= $type;
    $result = db_query(db_rewrite_sql($query), implode(',', $filter)); 
  }
  else {
    $query  = 'SELECT node.nid, node.title, term_node.tid FROM {node} LEFT JOIN 
             {term_node} ON term_node.nid = node.nid WHERE term_node.tid IN
             (SELECT tid FROM {term_data} WHERE vid = "%d" AND node.status = "1")';
    $query .= $type;
    $result = db_query(db_rewrite_sql($query)); 
  }


  $tree = taxonomy_node_tree_build($result, $terms);

  // format output
  $output .= '<ul id="drw_item" class="hidden">';
  foreach ($tree as $term) {
    $output .= taxonomy_node_tree_menu_build($term);
  }
  $output .= '</ul></li>';

  // Display output
  echo($output);

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

/**
 * Recursively build the menu.
 *
 * @ingroup themeable
 */
function taxonomy_node_tree_menu_build($term) {

  global $taxonomy_node_tree_id;

  if ($taxonomy_node_tree_id == null) {
    $taxonomy_node_tree_id = 0;
  }

  $taxonomy_node_tree_id++;

  if (isset($term->tid)) {
    $output .= '<li>';
    $output .= $term->name;
    if (isset($term->below)) {
      $output .= '<ul id="taxonomy_node_tree[ul][' . $taxonomy_node_tree_id . ']" class="hidden">';
      foreach($term->below as $child) {
        $output .= taxonomy_node_tree_menu_build($child);
      }
      $output .= '</ul>';
    } else if (isset($term->nodes)) {
      $output .= '<ul id="taxonomy_node_tree[ul][' . $taxonomy_node_tree_id . ']" class="hidden">';
      foreach ($term->nodes as $node) {
        $output .= '<li>';
        $output .= '<a href="' . $GLOBALS['base_url'] . '/sitio/' . $node->nid . '">' . $node->title . '</a>';
      }
      $output .= '</ul>';
      $output .= '</li>';
    }
    $output .= '</li>';
  }

  return $output;

}

/**
 * Version of taxonomy_get_tree() without caching.
 */
function taxonomy_node_tree_taxonomy_get_tree($vid, $parent = 0, $depth = -1, $max_depth = NULL, $cache = FALSE) {
  static $children, $parents, $terms;

  $depth++;

  // We can cache trees, so it's not CPU-intensive to call get_tree() on a term
  // and its children, too.
  if (!$cache && !isset($children[$vid])) {
    $children[$vid] = array();

    $result = db_query(db_rewrite_sql('SELECT t.tid, t.*, parent FROM {term_data} t INNER JOIN {term_hierarchy} h ON t.tid = h.tid WHERE t.vid = %d ORDER BY weight, name', 't', 'tid'), $vid);
    while ($term = db_fetch_object($result)) {
      $children[$vid][$term->parent][] = $term->tid;
      $parents[$vid][$term->tid][] = $term->parent;
      $terms[$vid][$term->tid] = $term;
    }
  }

  $max_depth = (is_null($max_depth)) ? count($children[$vid]) : $max_depth;
  $tree = array();
  if ($max_depth > $depth && !empty($children[$vid][$parent])) {
    foreach ($children[$vid][$parent] as $child) {
      $term = drupal_clone($terms[$vid][$child]);
      $term->depth = $depth;
      // The "parent" attribute is not useful, as it would show one parent only.
      unset($term->parent);
      $term->parents = $parents[$vid][$child];
      $tree[] = $term;
      if (!empty($children[$vid][$child])) {
        $tree = array_merge($tree, taxonomy_get_tree($vid, $child, $depth, $max_depth));
      }
    }
  }

  return $tree;
}