some portal work
[openemr.git] / library / classes / Tree.class.php
blob61703396c490eedcd78b9211d804fc70ae5e94b2
1 <?php
3 define("ROOT_TYPE_ID", 1);
4 define("ROOT_TYPE_NAME", 2);
6 /**
7 * class Tree
8 * This class is a clean implementation of a modified preorder tree traversal hierachy to relational model
9 * Don't use this class directly as it won't work, extend it and set the $this->_table variable, currently
10 * this class needs its own sequence per table. MPTT uses a lot of self referential parent child relationships
11 * and having ids that are more or less sequential makes human reading, fixing and reconstruction much easier.
14 class Tree
18 * This is the name of the table this tree is stored in
19 * @var string
21 var $_table;
24 * This is a lookup table so that you can get a node name or parent id from its id
25 * @var array
27 var $_id_name;
30 * This is a db abstraction object compatible with ADODB
31 * @var object the constructor expects it to be available as $GLOBALS['adodb']['db']
33 var $_db;
36 * The constructor takes a value and a flag determining if the value is the id of a the desired root node or the name
37 * @param mixed $root name or id of desired root node
38 * @param int $root_type optional flag indicating if $root is a name or id, defaults to id
40 function __construct($root, $root_type = ROOT_TYPE_ID)
42 $this->_db = $GLOBALS['adodb']['db'];
43 $this->_root = $root;
44 $this->_root_type = $root_type;
45 $this->load_tree();
48 function load_tree()
50 $root = $this->_root;
51 $tree = array();
52 $tree_tmp = array();
54 //get the left and right value of the root node
55 $sql = "SELECT * FROM " . $this->_table . " WHERE id=?";
57 if ($this->root_type == ROOT_TYPE_NAME) {
58 $sql = "SELECT * FROM " . $this->_table . " WHERE name=?";
61 $result = $this->_db->Execute($sql, [$root]) or die("Error: " . text($this->_db->ErrorMsg()));
62 $row = array();
64 if ($result && !$result->EOF) {
65 $row = $result->fields;
66 } else {
67 $this->tree = array();
70 // start with an empty right stack
71 $right = array();
73 // now, retrieve all descendants of the root node
74 $sql = "SELECT * FROM " . $this->_table . " WHERE lft BETWEEN ? AND ? ORDER BY parent,name ASC;";
75 $result = $this->_db->Execute($sql, [$row['lft'], $row['rght']]);
76 $this->_id_name = array();
79 while ($result && !$result->EOF) {
80 $ar = array();
81 $row = $result->fields;
83 //create a lookup table of id to name for every node that will end up in this tree, this is used
84 //by the array building code below to find the chain of parents for each node
86 // ADDED below by BM on 06-2009 to translate categories, if applicable
87 if ($this->_table == "categories") {
88 $this->_id_name[$row['id']] = array("id" => $row['id'], "name" => xl_document_category($row['name']),
89 "parent" => $row['parent'], "value" => $row['value'], "aco_spec" => $row['aco_spec']);
90 } else {
91 $this->_id_name[$row['id']] = array("id" => $row['id'], "name" => $row['name'], "parent" => $row['parent']);
94 // only check stack if there is one
95 if (count($right)>0) {
96 // check if we should remove a node from the stack
97 while ($right[count($right)-1]<$row['rght']) {
98 array_pop($right);
102 //set up necessary variables to then determine the chain of parents for each node
103 $parent = $row['parent'];
104 $loop = 0;
106 //this is a string that gets evaled below to create the array representing the tree
107 $ar_string = "[\"".($row['id']) ."\"] = \$row[\"value\"]";
109 //if parent is 0 then the node has no parents, the number of nodes in the id_name lookup always includes any nodes
110 //that could be the parent of any future node in the record set, the order is deterministic because of the algorithm
111 while ($parent != 0 && $loop < count($this->_id_name)) {
112 $ar_string = "[\"" . ($this->_id_name[$parent]['id']) . "\"]" . $ar_string;
113 $loop++;
114 $parent = $this->_id_name[$parent]['parent'];
117 $ar_string = '$ar' . $ar_string . ";";
118 //echo $ar_string;
120 //now eval the string to create the tree array
121 //there must be a more efficient way to do this than eval?
122 eval($ar_string);
124 //merge the evaled array with all of the already exsiting tree elements,
125 //merge recursive is used so that no keys are replaced in other words a key
126 //with a specific value will not be replace but instead that value will be turned into an array
127 //consisting of the previous value and the new value
128 $tree = array_merge_n($tree, $ar);
130 // add this node to the stack
131 $right[] = $row['rght'];
132 $result->MoveNext();
135 $this->tree = $tree;
139 * This function completely rebuilds a tree starting from parent to ensure that all of its preorder values
140 * are integrous.
141 * Upside is that it fixes any kind of goofiness, downside is that it is recursive and consequently
142 * exponentially expensive with the size of the tree.
143 * On adds and deletes the tree does dynamic updates as appropriate to maintain integrity of the algorithm,
144 * however you can still force it to do goofy things and afterwards you will need this function to fix it.
145 * If you need to do a huge number of adds or deletes it will be much faster to act directly on the db and then
146 * call this to fix the mess than to use the add and delete functions.
147 * @param int $parent id of the node you would like to rebuild all nodes below
148 * @param int $left optional proper left value of the node you are rebuilding below, then used recursively
150 function rebuild_tree($parent, $left = null)
153 //if no left is supplied assume the existing left is proper
154 if ($left == null) {
155 $sql = "SELECT lft FROM " . $this->_table . " WHERE id=?;";
156 $result = $this->_db->Execute($sql, [$parent]) or die("Error: " . text($this->_db->ErrorMsg()));
158 if ($result && !$result->EOF) {
159 $left = $result->fields['lft'];
160 } else {
161 //the node you are rebuilding below if goofed up and you didn't supply a proper value
162 //nothing we can do so error
163 die("Error: The node you are rebuilding from could not be found, please supply an existing node id.");
167 // get all children of this node
168 $sql = "SELECT id FROM " . $this->_table . " WHERE parent=? ORDER BY id;";
169 $result = $this->_db->Execute($sql, [$parent]) or die("Error: " . text($this->_db->ErrorMsg()));
171 // the right value of this node is the left value + 1
172 $right = $left+1;
174 while ($result && !$result->EOF) {
175 $row = $result->fields;
176 // recursive execution of this function for each
177 // child of this node
178 // $right is the current right value, which is
179 // incremented by the rebuild_tree function
180 $right = $this->rebuild_tree($row['id'], $right);
181 $result->MoveNext();
184 // we've got the left value, and now that we've processed
185 // the children of this node we also know the right value
186 $sql = "UPDATE " . $this->_table . " SET lft=?, rght=? WHERE id=?;";
187 //echo $sql . "<br>";
188 $this->_db->Execute($sql, [$left, $right, $parent]) or die("Error: " . text($sql) . " " . text($this->_db->ErrorMsg()));
190 // return the right value of this node + 1
191 return $right+1;
196 * Call this to add a new node to the tree
197 * @param int $parent id of the node you would like the new node to have as its parent
198 * @param string $name the name of the new node, it will be used to reference its value in the tree array
199 * @param string $value optional value this node is to contain
200 * @param string $aco_spec optional ACO value in section|value format
201 * @return int id of newly added node
203 function add_node($parent_id, $name, $value = "", $aco_spec = "patients|docs")
206 $sql = "SELECT * from " . $this->_table . " where parent = ? and name=?";
207 $result = $this->_db->Execute($sql, [$parent_id, $name]) or die("Error: " . text($this->_db->ErrorMsg()));
209 if ($result && !$result->EOF) {
210 die("You cannot add a node with the name '" . text($name) ."' because one already exists under parent " . text($parent_id) . "<br>");
213 $sql = "SELECT * from " . $this->_table . " where id = ?";
214 $result = $this->_db->Execute($sql, [$parent_id]) or die("Error: " . text($this->_db->ErrorMsg()));
216 $next_right = 0;
218 if ($result && !$result->EOF) {
219 $next_right = $result->fields['rght'];
222 $sql = "UPDATE " . $this->_table . " SET rght=rght+2 WHERE rght>=?";
223 $this->_db->Execute($sql, [$next_right]) or die("Error: " . text($this->_db->ErrorMsg()));
224 $sql = "UPDATE " . $this->_table . " SET lft=lft+2 WHERE lft>=?";
225 $this->_db->Execute($sql, [$next_right]) or die("Error: " . text($this->_db->ErrorMsg()));
227 $id = $this->_db->GenID($this->_table . "_seq");
228 $sql = "INSERT INTO " . $this->_table . " SET name=?, value=?, aco_spec=?, lft=?, rght=?, parent=?, id=?";
229 $this->_db->Execute($sql, [$name, $value, $aco_spec, $next_right, ($next_right + 1), $parent_id, $id]) or die("Error: " . text($sql) . " :: " . text($this->_db->ErrorMsg()));
230 //$this->rebuild_tree(1,1);
231 $this->load_tree();
232 return $id;
236 * Call this to modify a node's attributes.
237 * @param int $id id of the node to change
238 * @param string $name the new name of the new node
239 * @param string $value optional value this node is to contain
240 * @param string $aco_spec optional ACO value in section|value format
241 * @return int same as input id
243 function edit_node($id, $name, $value = "", $aco_spec = "patients|docs")
245 $sql = "SELECT c2.id FROM " . $this->_table . " AS c1, " . $this->_table . " AS c2 WHERE " .
246 "c1.id = ? AND c2.id != c1.id AND c2.parent = c1.parent AND c2.name = ?";
247 $result = $this->_db->Execute($sql, [$id, $name]) or die(xlt('Error') . ": " . text($this->_db->ErrorMsg()));
248 if ($result && !$result->EOF) {
249 die(xlt('This name already exists under this parent.') . "<br>");
252 $sql = "UPDATE " . $this->_table . " SET name = ?, value = ?, aco_spec = ? WHERE id = ?";
253 $this->_db->Execute($sql, [$name, $value, $aco_spec, $id]) or die(xlt('Error') . ": " . text($this->_db->ErrorMsg()));
254 $this->load_tree();
255 return $id;
259 * Call this to delete a node from the tree, the nodes children (and their children, etc) will become children
260 * of the deleted nodes parent
261 * @param int $id id of the node you want to delete
263 function delete_node($id)
266 $sql = "SELECT * from " . $this->_table . " where id = ?";
267 //echo $sql . "<br>";
268 $result = $this->_db->Execute($sql, [$id]) or die("Error: " . text($this->_db->ErrorMsg()));
270 $left = 0;
271 $right = 1;
272 $new_parent = 0;
274 if ($result && !$result->EOF) {
275 $left = $result->fields['lft'];
276 $right = $result->fields['rght'];
277 $new_parent = $result->fields['parent'];
280 $sql = "UPDATE " . $this->_table . " SET rght=rght-2 WHERE rght>?";
281 //echo $sql . "<br>";
282 $this->_db->Execute($sql, [$right]) or die("Error: " . text($this->_db->ErrorMsg()));
284 $sql = "UPDATE " . $this->_table . " SET lft=lft-2 WHERE lft>?";
285 //echo $sql . "<br>";
286 $this->_db->Execute($sql, [$right]) or die("Error: " . text($this->_db->ErrorMsg()));
288 $sql = "UPDATE " . $this->_table . " SET lft=lft-1, rght=rght-1 WHERE lft>? and rght < ?";
289 //echo $sql . "<br>";
290 $this->_db->Execute($sql, [$left, $right]) or die("Error: " . text($this->_db->ErrorMsg()));
292 //only update the childrens parent setting if the node has children
293 if ($right > ($left +1)) {
294 $sql = "UPDATE " . $this->_table . " SET parent=? WHERE parent=?";
295 //echo $sql . "<br>";
296 $this->_db->Execute($sql, [$new_parent, $id]) or die("Error: " . text($this->_db->ErrorMsg()));
299 $sql = "DELETE FROM " . $this->_table . " where id=?";
300 //echo $sql . "<br>";
301 $this->_db->Execute($sql, [$id]) or die("Error: " . text($this->_db->ErrorMsg()));
302 $this->load_tree();
304 return true;
307 function get_node_info($id)
309 if (!empty($this->_id_name[$id])) {
310 return $this->_id_name[$id];
311 } else {
312 return array();
316 function get_node_name($id)
318 if (!empty($this->_id_name[$id])) {
319 return $this->_id_name[$id]['name'];
320 } else {
321 return false;
326 function array_merge_2(&$array, &$array_i)
328 // For each element of the array (key => value):
329 foreach ($array_i as $k => $v) {
330 // If the value itself is an array, the process repeats recursively:
331 if (is_array($v)) {
332 if (!isset($array[$k])) {
333 $array[$k] = array();
336 array_merge_2($array[$k], $v);
338 // Else, the value is assigned to the current element of the resulting array:
339 } else {
340 if (isset($array[$k]) && is_array($array[$k])) {
341 $array[$k][0] = $v;
342 } else {
343 if (isset($array) && !is_array($array)) {
344 $temp = $array;
345 $array = array();
346 $array[0] = $temp;
349 $array[$k] = $v;
356 function array_merge_n()
358 // Initialization of the resulting array:
359 $array = array();
361 // Arrays to be merged (function's arguments):
362 $arrays = func_get_args();
364 // Merging of each array with the resulting one:
365 foreach ($arrays as $array_i) {
366 if (is_array($array_i)) {
367 array_merge_2($array, $array_i);
371 return $array;