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
<?php

/**
 * Holds node data so it can be passed around to various functions. Supplying
 * a numeric node id and data ($bits) will store it. Calling the function with
 * the node id alone will retrieve everything for the node. Calling it with a
 * key will return all associated information across all node ids. And calling
 * it with no parameters will return everything stored.
 *
 * @param $key
 *  (optional) String or integer, used to store and retrieve.
 * @param $bits
 *  (optional) Mainly used to store when $key is a node id.
 */
function dojo_node_bits($key = NULL, $bits = array()) {
  static $node_bits = array();
  $output = array();
  
  if (is_numeric($key)) {
    if (!empty($bits)) {
      $node_bits[$key] = isset($node_bits[$key]) ? array_merge($node_bits[$key], $bits) : $bits;
    }
    else {
      $output = isset($node_bits[$key]) ? $node_bits[$key] : FALSE;
    }
  }
  else {
    foreach ($node_bits as $nid => $bits) {
      if (isset($bits[$key])) {
        if (is_array($bits[$key])) {
          $output = array_merge($output, $bits[$key]);
        }
        else {
          $output[$nid] = $bits[$key];
        }
      }
    }
    if (empty($output) && $key == NULL) {
      $output = $node_bits;
    }
  }
  
  return $output;
}