3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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-2014
80 class CRM_Utils_Tree
{
83 * Store the tree information as a string or array
89 * Constructor for the tree.
91 * @param string $nodeName
93 * @internal param string $rootNode
95 * @return CRM_Utils_Tree
97 public function __construct($nodeName) {
98 // create the root node
99 $rootNode = &$this->createNode($nodeName);
101 // add the root node to the tree
102 $this->tree
['rootNode'] = &$rootNode;
106 * Find a node that matches the given string
108 * @param string $name
109 * 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
115 //public function &findNode(&$parentNode, $name)
116 public function &findNode($name, &$parentNode) {
117 // if no parent node specified, please start from root node
119 $parentNode = &$this->tree
['rootNode'];
122 // first check the nodename of subtree itself
123 if ($parentNode['name'] == $name) {
128 // no children ? return false
129 if ($this->isLeafNode($node)) {
133 // search children of the subtree
134 foreach ($parentNode['children'] as $key => $childNode) {
135 $cNode = &$parentNode['children'][$key];
136 if ($node = &$this->findNode($name, $cNode)) {
141 // name does not match subtree or any of the children, negative result
146 * Check if node is a leaf node.
147 * Currently leaf nodes are strings and non-leaf nodes are arrays
150 ref) $node node which needs to checked
154 public function isLeafNode(&$node) {
155 return (count($node['children']) ?
TRUE : FALSE);
161 * @param string $name
166 public function &createNode($name) {
167 $node['name'] = $name;
168 $node['children'] = array();
169 $node['data'] = array();
177 * @param string $parentName
178 * Name of the parent ?.
179 * @param array (ref) - node to be added
183 public function addNode($parentName, &$node) {
185 $parentNode = &$this->findNode($parentName, $temp);
187 $parentNode['children'][] = &$node;
193 * @param string $parentName
194 * Name of the parent ?.
195 * @param mixed - data to be added
196 * @param string - key to be used (optional)
200 public function addData($parentName, $childName, $data) {
202 if ($parentNode = &$this->findNode($parentName, $temp)) {
203 foreach ($parentNode['children'] as $key => $childNode) {
204 $cNode = &$parentNode['children'][$key];
205 if ($cNode = &$this->findNode($childName, $parentNode)) {
206 $cNode['data']['fKey'] = &$data;
219 public function getTree() {
230 public function display() {
231 print_r($this->tree
);