treenode.inc 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * @file
  4. * Utility classes for catalog module.
  5. */
  6. /**
  7. * Data structure to mimic Drupal's menu system.
  8. */
  9. class UcTreeNode {
  10. public $tid = 0;
  11. public $name = 'Catalog';
  12. public $children = array();
  13. public $depth = -1;
  14. public $sequence = 0;
  15. /**
  16. * Constructor.
  17. */
  18. function __construct($term = NULL) {
  19. if ($term) {
  20. $this->tid = $term->tid;
  21. $this->name = $term->name;
  22. $this->depth = $term->depth;
  23. $this->sequence = $term->sequence;
  24. }
  25. }
  26. /**
  27. * Determines if new child is an immediate descendant or not.
  28. *
  29. * This function is completely dependent on the structure of the array
  30. * returned by taxonomy_get_tree(). Each element in the array knows its
  31. * depth in the tree and the array is a preorder iteration of the logical
  32. * tree structure. Therefore, if the parameter is more than one level
  33. * deeper than $this, it should be passed to the last child of $this.
  34. */
  35. function add_child(&$child) {
  36. if ($child->depth - $this->depth == 1) {
  37. $this->children[] = $child;
  38. }
  39. else {
  40. $last_child =&$this->children[count($this->children)-1];
  41. $last_child->add_child($child);
  42. }
  43. }
  44. }