3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
35 * Manage simple Tree data structure.
41 * --------------------------------------------------------------
45 * ------------- --------- / -------- ------------------------
47 * 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o'
49 * ----------------------
55 * From the above diagram we have
59 * 'j' - node with multiple parents 'c' and 'd'
62 * All nodes of the tree (including root and leaf node) contain the following properties
63 * Name - what is the node name ?
64 * Children - who are it's children
65 * Data - any other auxiliary data
68 * Internally all nodes are an array with the following keys
73 class CRM_Utils_Tree
{
76 * Store the tree information as a string or array.
82 * Constructor for the tree.
84 * @param string $nodeName
86 * @internal param string $rootNode
88 * @return CRM_Utils_Tree
90 public function __construct($nodeName) {
91 // create the root node
92 $rootNode = &$this->createNode($nodeName);
94 // add the root node to the tree
95 $this->tree
['rootNode'] = &$rootNode;
99 * Find a node that matches the given string.
101 * @param string $name
102 * Name of the node we are searching for.
103 * @param array (ref) $parentNode which parent node should we search in ?
105 * @return array(ref) | false node if found else false
107 public function &findNode($name, &$parentNode) {
108 // if no parent node specified, please start from root node
110 $parentNode = &$this->tree
['rootNode'];
113 // first check the nodename of subtree itself
114 if ($parentNode['name'] == $name) {
119 // no children ? return false
120 if ($this->isLeafNode($node)) {
124 // search children of the subtree
125 foreach ($parentNode['children'] as $key => $childNode) {
126 $cNode = &$parentNode['children'][$key];
127 if ($node = &$this->findNode($name, $cNode)) {
132 // name does not match subtree or any of the children, negative result
137 * Check if node is a leaf node.
139 * Currently leaf nodes are strings and non-leaf nodes are arrays
141 * @param array $node node which needs to checked
145 public function isLeafNode(&$node) {
146 return (count($node['children']) ?
TRUE : FALSE);
152 * @param string $name
157 public function &createNode($name) {
158 $node['name'] = $name;
159 $node['children'] = array();
160 $node['data'] = array();
168 * @param string $parentName
169 * Name of the parent ?.
170 * @param array (ref) $node - node to be added
172 public function addNode($parentName, &$node) {
174 $parentNode = &$this->findNode($parentName, $temp);
176 $parentNode['children'][] = &$node;
182 * @param string $parentName Name of the parent ?.
183 * @param string $childName - key to be used (optional)
184 * @param mixed $data - data to be added
186 public function addData($parentName, $childName, $data) {
188 if ($parentNode = &$this->findNode($parentName, $temp)) {
189 foreach ($parentNode['children'] as $key => $childNode) {
190 $cNode = &$parentNode['children'][$key];
191 if ($cNode = &$this->findNode($childName, $parentNode)) {
192 $cNode['data']['fKey'] = &$data;
203 public function getTree() {
210 public function display() {
211 print_r($this->tree
);