3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * Manage simple Tree data structure
42 * --------------------------------------------------------------
46 * ------------- --------- / -------- ------------------------
48 * 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o'
50 * ----------------------
56 * From the above diagram we have
60 * 'j' - node with multiple parents 'c' and 'd'
63 * All nodes of the tree (including root and leaf node) contain the following properties
64 * Name - what is the node name ?
65 * Children - who are it's children
66 * Data - any other auxillary data
69 * Internally all nodes are an array with the following keys
76 * @copyright CiviCRM LLC (c) 2004-2013
80 class CRM_Utils_Tree
{
83 * Store the tree information as a string or array
89 * Constructor for the tree.
93 * @return CRM_Utils_Tree
98 public function __construct($nodeName) {
99 // create the root node
100 $rootNode = &$this->createNode($nodeName);
102 // add the root node to the tree
103 $this->tree
['rootNode'] = &$rootNode;
107 * Find a node that matches the given string
109 * @param string $name name of the node we are searching for.
110 * @param array (ref) $parentNode which parent node should we search in ?
113 ref) | false node if found else false
117 //public function &findNode(&$parentNode, $name)
118 public function &findNode($name, &$parentNode) {
119 // if no parent node specified, please start from root node
121 $parentNode = &$this->tree
['rootNode'];
124 // first check the nodename of subtree itself
125 if ($parentNode['name'] == $name) {
130 // no children ? return false
131 if ($this->isLeafNode($node)) {
135 // search children of the subtree
136 foreach ($parentNode['children'] as $key => $childNode) {
137 $cNode = &$parentNode['children'][$key];
138 if ($node = &$this->findNode($name, $cNode)) {
143 // name does not match subtree or any of the children, negative result
148 * Function to check if node is a leaf node.
149 * Currently leaf nodes are strings and non-leaf nodes are arrays
152 ref) $node node which needs to checked
158 public function isLeafNode(&$node) {
159 return (count($node['children']) ?
TRUE : FALSE);
165 * @param string $name
167 * @return array (ref)
171 public function &createNode($name) {
172 $node['name'] = $name;
173 $node['children'] = array();
174 $node['data'] = array();
182 * @param string $parentName - name of the parent ?
183 * @param array (ref) - node to be added
189 public function addNode($parentName, &$node) {
191 $parentNode = &$this->findNode($parentName, $temp);
193 $parentNode['children'][] = &$node;
199 * @param string $parentName - name of the parent ?
200 * @param mixed - data to be added
201 * @param string - key to be used (optional)
207 public function addData($parentName, $childName, $data) {
209 if ($parentNode = &$this->findNode($parentName, $temp)) {
210 foreach ($parentNode['children'] as $key => $childNode) {
211 $cNode = &$parentNode['children'][$key];
212 if ($cNode = &$this->findNode($childName, $parentNode)) {
213 $cNode['data']['fKey'] = &$data;
228 public function getTree() {
241 public function display() {
242 print_r($this->tree
);