aboutsummaryrefslogtreecommitdiff
path: root/taxonomy_node_tree.module
blob: 85a60e956b52932a2e166a44b60905bdcc7ed1f2 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
// $Id$

/**
 * @file
 * Taxonomy Node Tree.
 *
 * This module implements functions to build hierarchical term trees including
 * children nodes.
 */

/**
 * Return the parent terms of a given vocabulary.
 *
 * This function takes a vocabulary id and returns a list of it's parent terms.
 *
 * @param $vid
 *   Taxonomy id.
 *
 * @return
 *   Array with parent terms of the vocabulary.
 */
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;
}

/**
 * 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.
 *
 * @param $nodes
 *   Array with node objects.
 *
 * @param $terms
 *   Array with term objects.
 *
 * @return
 *  Term tree with nodes at their parent terms.
 *
 * @todo
 *   It is assumed that nodes are just associated with a single term.
 *   This could be changed to support multiple relationships.
 *
 *   Also, current result rewind method should support postgresql
 *   data structures.
 */
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;
    }
  }

  // Rewind result so it can be used again.
  if (is_object($nodes)) {
    $nodes->data_seek(0);
  }
  else if (mysql_num_rows($nodes) > 0) {
    mysql_data_seek($nodes, 0);
  }

  return $tree;
}

/**
 * Add children relationship for terms present in the tree.
 *
 * @param
 *   Tree with term objects.
 *
 * @return
 *   Term tree with parent/children relation.
 */
function taxonomy_node_tree_relation($tree) {
  if (is_array($tree) && $tree != NULL) {
    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.
 *
 * @param $tree
 *   Tree with term objects to be changed (by reference).
 *
 * @param $term
 *   Term object.
 */
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.
 *
 * @param $tree
 *   Tree with term objects.
 *
 * @return
 *   Hierarchical term tree.
 */
function taxonomy_node_tree_hierarchy($tree) {
  if (is_array($tree) && $tree != NULL) {
    foreach ($tree as $term) {
      taxonomy_node_tree_sort($tree, $term);
    }
  }

  return $tree;
}

/**
 * Count descendant nodes.
 *
 * @param $term
 *   Term object.
 *
 * @param $count
 *   Optional initial count offset. Also used for recursion.
 *
 * @return
 *   Number of descendant nodes.
 */
function taxonomy_node_tree_count($term, &$count = 0) {
  if (isset($term->below)) {
    foreach ($term->below as &$below) {
      taxonomy_node_tree_count($below, $count);
    }
  }

  if (isset($term->nodes)) {
    $count += count($term->nodes);
  }

  return $count;
}

/**
 * Build a list of empty terms.
 *
 * @param $term
 *   Term object.
 *
 * @param $clean
 *   Set to true to cleanup term before adding to list.
 *
 * @param $parents
 *   Set to FALSE to exclude parent items even if they
 *   don't have descendant nodes.
 *
 * @param $list
 *   Array to store term list.
 *
 * @return
 *   Array with terms without descendant nodes.
 */
function taxonomy_node_tree_get_empty($term, $clean = FALSE, $parents = TRUE, &$list = array()) {
  if (taxonomy_node_tree_count($term) == 0) {
    // Skip parents.
    if ($term->parents[0] != 0 || ($term->parents[0] == 0 && $parents)) {
      $save = drupal_clone($term);

      if ($clean == TRUE) {
        unset($save->nodes);
        unset($save->below);
        unset($save->children);
      }

      $list[] = $save;
    }
  }
  else if (isset($term->below)) {
    foreach ($term->below as $below) {
      taxonomy_node_tree_get_empty($below, $clean, $parents, $list);
    }
  }

  return $list;
}

/**
 * Build a list of non-nempty terms.
 *
 * @param $term
 *   Term object.
 *
 * @param $clean
 *   Set to true to cleanup term before adding to list.
 *
 * @param $parents
 *   Set to TRUE to keep parent items even if they don't have
 *   descendant nodes.
 *
 * @param $list
 *   Array to store term list.
 *
 * @return
 *   Array with terms with descendant nodes.
 */
function taxonomy_node_tree_get_non_empty($term, $clean = FALSE, $parents = FALSE, &$list = array()) {
  // Add parents and terms with descendant nodes.
  if (($term->parents[0] == 0 && $parents) || taxonomy_node_tree_count($term) > 0) {
    $save = drupal_clone($term);

    if ($clean == TRUE) {
      unset($save->nodes);
      unset($save->below);
      unset($save->children);
    }

    $list[] = $save;
  }

  if (is_array($term->below)) {
    foreach ($term->below as $below) {
      taxonomy_node_tree_get_non_empty($below, $clean, $parents, $list);
    }
  }

  return $list;
}

/**
 * Setup a full taxonomy node hierarchical tree.
 *
 * Build a tree with taxonomy terms with full dept and add child node
 * information at each level.
 *
 * @param $nodes
 *   Array with node objects.
 *
 * @param $terms
 *   Array with term objects.
 *
 * @param $clean
 *   Set to TRUE to remove branches without descendant nodes.
 *
 * @param $parents
 *   Set to TRUE to keep parent items even if they don't have
 *   descendant nodes. Used just when $clean is also set to TRUE.
 *
 * @return
 *  Hierarchical tree.
 */
function taxonomy_node_tree_build($nodes, $terms, $clean = FALSE, $parents = FALSE) {
  $tree = taxonomy_node_tree_index($nodes, $terms);
  $tree = taxonomy_node_tree_relation($tree);
  $tree = taxonomy_node_tree_hierarchy($tree);

  if ($clean == TRUE && is_array($tree)) {
    $terms = array();
    foreach ($tree as $term) {
      $list  = taxonomy_node_tree_get_non_empty($term, TRUE, $parents);
      $terms = array_merge($terms, $list);
    }

    $tree = taxonomy_node_tree_build($nodes, $terms, FALSE);
  }

  return $tree;
}

/**
 * Create a hierarchical representation of a vocabulary.
 *
 * Version of taxonomy_get_tree() without caching.
 *
 * @param $vid
 *   Which vocabulary to generate the tree for.
 *
 * @param $parent
 *   The term ID under which to generate the tree. If 0, generate the tree
 *   for the entire vocabulary.
 *
 * @param $depth
 *   Internal use only.
 *
 * @param $max_depth
 *   The number of levels of the tree to return. Leave NULL to return all levels.
 *
 * @param $cache
 *   Whether to use cache results.
 *
 * @return
 *   An array of all term objects in the tree. Each term object is extended
 *   to have "depth" and "parents" attributes in addition to its normal ones.
 */
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;
}

/**
 * Implementation of hook_theme().
 */
function taxonomy_node_tree_theme() {
  return array(
      'taxonomy_node_tree_list' => array(
        'arguments'             => array(
          'term'                => NULL,
          'id'                  => NULL,
          'class'               => NULL,
          'baselink'            => NULL,
          'level'               => NULL,
          'active_link'         => NULL,
          ),
        'function'              => 'theme_taxonomy_node_tree_list',
        ),
      );
}

/**
 * Recursively build an HTML taxonomy node tree.
 *
 * @ingroup themeable
 *
 * @param $term
 *   Current of starting term.
 *
 * @param $id
 *   Base id for list elements.
 *
 * @param $class
 *   Class for list elements.
 *
 * @param $baselink
 *   Base link for urls.
 *
 * @param $level
 *   Nesting level user for recursion.
 *
 * @return
 *   Rendered HTML tree.
 */
function theme_taxonomy_node_tree_list($term, $id, $class, $baselink = NULL, $level = NULL, $active_link = NULL) {
  if ($level == NULL) {
    $level = 0;
  }

  $level++;

  if (isset($term->tid)) {
    $output  = '<li>';

    if (isset($term->below)) {
      $output .= $term->name;
      $output .= '<ul id="' . $id .'-ul-'. $level .'" class="'. $class .'">';
      foreach ($term->below as $child) {
        $output .= theme_taxonomy_node_tree_list($child, $id, $class, $baselink, $level, $active_link);
      }
      $output .= '</ul>';
    }
    elseif (isset($term->nodes)) {
      $output .= $term->name;
      $output .= '<ul id="' . $id .'-ul-'. $level .'" class="'. $class .'">';
      foreach ($term->nodes as $node) {
        $node = node_load($node->nid);
        path_nodeapi($node, 'load', '');
        //drupal_get_path_alias(request_uri());
        $output .= '<li class="'. ($node->nid === arg(1) ? $active_link : 'item') .'">';
        $output .= ($node->path ? l($node->title,$node->path) : l($node->title, "$baselink/$node->nid"));
        //$output .= drupal_get_path_alias("node/$node->nid");
        $output .= '</li>';
      }
      $output .= '</ul>';
    }
    else {
      $output .= '<span>'. $term->name .'</span>';
    }

    $output .= '</li>';
  }

  return $output;
}

/**
 * Build an array with a node and it's parent terms.
 *
 * @param $parents
 *   Direct or all parent terms.
 *
 * @return
 *   Array with node and parent terms.
 */
function taxonomy_node_tree_trail($nid, $tid, $parents = 'direct') {
  $trail = array();

  // Add term hierarchy
  if ($parents == 'direct') {
    $function = 'taxonomy_get_parents';
  }
  else {
    $function = 'taxonomy_get_parents_all';
  }

  // Build array
  foreach (array_reverse($function($tid)) as $term) {
    $trail[] = $term->name;
  }

  // Add node title
  $node    = node_load($nid);
  $trail[] = $node->title;

  return $trail;
}