mysql 8 fixes (#1639)
[openemr.git] / library / classes / Tree.class.php
blob1d306c8b53bb9dd14785a31cbc39b926e89e9599
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 = add_escape_custom($root);
44 $this->_root_type = add_escape_custom($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='".$root."'";
57 if ($this->root_type == ROOT_TYPE_NAME) {
58 $sql = "SELECT * FROM " . $this->_table . " WHERE name='".$root."'";
61 $result = $this->_db->Execute($sql) or die("Error: " . $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 " . $row['lft'] . " AND " . $row['rght'] . " ORDER BY parent,name ASC;";
75 $result = $this->_db->Execute($sql);
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='" . $parent . "';";
156 $result = $this->_db->Execute($sql) or die("Error: " . $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='" . $parent . "' ORDER BY id;";
169 $result = $this->_db->Execute($sql) or die("Error: " . $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=".$left.", rght=".$right." WHERE id='".$parent."';";
187 //echo $sql . "<br>";
188 $this->_db->Execute($sql) or die("Error: $sql" . $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 = '" . $parent_id . "' and name='" . $name . "'";
207 $result = $this->_db->Execute($sql) or die("Error: " . $this->_db->ErrorMsg());
209 if ($result && !$result->EOF) {
210 die("You cannot add a node with the name '" . $name ."' because one already exists under parent " . $parent_id . "<br>");
213 $sql = "SELECT * from " . $this->_table . " where id = '" . $parent_id . "'";
214 $result = $this->_db->Execute($sql) or die("Error: " . $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>=" . $next_right;
223 $this->_db->Execute($sql) or die("Error: " . $this->_db->ErrorMsg());
224 $sql = "UPDATE " . $this->_table . " SET lft=lft+2 WHERE lft>=" . $next_right;
225 $this->_db->Execute($sql) or die("Error: " . $this->_db->ErrorMsg());
227 $id = $this->_db->GenID($this->_table . "_seq");
228 $sql = "INSERT INTO " . $this->_table . " SET name='" . add_escape_custom($name) .
229 "', value='" . add_escape_custom($value) . "', aco_spec='" . add_escape_custom($aco_spec) .
230 "', lft='" . $next_right . "', rght='" . ($next_right + 1) .
231 "', parent='" . $parent_id . "', id='" . $id . "'";
232 $this->_db->Execute($sql) or die("Error: $sql :: " . $this->_db->ErrorMsg());
233 //$this->rebuild_tree(1,1);
234 $this->load_tree();
235 return $id;
239 * Call this to modify a node's attributes.
240 * @param int $id id of the node to change
241 * @param string $name the new name of the new node
242 * @param string $value optional value this node is to contain
243 * @param string $aco_spec optional ACO value in section|value format
244 * @return int same as input id
246 function edit_node($id, $name, $value = "", $aco_spec = "patients|docs")
248 $sql = "SELECT c2.id FROM " . $this->_table . " AS c1, " . $this->_table . " AS c2 WHERE " .
249 "c1.id = $id AND c2.id != c1.id AND c2.parent = c1.parent AND c2.name = '" .
250 add_escape_custom($name) . "'";
251 $result = $this->_db->Execute($sql) or die(xlt('Error') . ": " . $this->_db->ErrorMsg());
252 if ($result && !$result->EOF) {
253 die(xlt('This name already exists under this parent.') . "<br>");
256 $sql = "UPDATE " . $this->_table . " SET name = '" . add_escape_custom($name) .
257 "', value = '" . add_escape_custom($value) .
258 "', aco_spec = '" . add_escape_custom($aco_spec) . "' WHERE id = $id";
259 $this->_db->Execute($sql) or die(xlt('Error') . ": " . $this->_db->ErrorMsg());
260 $this->load_tree();
261 return $id;
265 * Call this to delete a node from the tree, the nodes children (and their children, etc) will become children
266 * of the deleted nodes parent
267 * @param int $id id of the node you want to delete
269 function delete_node($id)
272 $sql = "SELECT * from " . $this->_table . " where id = '" . $id . "'";
273 //echo $sql . "<br>";
274 $result = $this->_db->Execute($sql) or die("Error: " . $this->_db->ErrorMsg());
276 $left = 0;
277 $right = 1;
278 $new_parent = 0;
280 if ($result && !$result->EOF) {
281 $left = $result->fields['lft'];
282 $right = $result->fields['rght'];
283 $new_parent = $result->fields['parent'];
286 $sql = "UPDATE " . $this->_table . " SET rght=rght-2 WHERE rght>" . $right;
287 //echo $sql . "<br>";
288 $this->_db->Execute($sql) or die("Error: " . $this->_db->ErrorMsg());
290 $sql = "UPDATE " . $this->_table . " SET lft=lft-2 WHERE lft>" . $right;
291 //echo $sql . "<br>";
292 $this->_db->Execute($sql) or die("Error: " . $this->_db->ErrorMsg());
294 $sql = "UPDATE " . $this->_table . " SET lft=lft-1, rght=rght-1 WHERE lft>" . $left . " and rght < " . $right;
295 //echo $sql . "<br>";
296 $this->_db->Execute($sql) or die("Error: " . $this->_db->ErrorMsg());
298 //only update the childrens parent setting if the node has children
299 if ($right > ($left +1)) {
300 $sql = "UPDATE " . $this->_table . " SET parent='" . $new_parent . "' WHERE parent='" . $id . "'";
301 //echo $sql . "<br>";
302 $this->_db->Execute($sql) or die("Error: " . $this->_db->ErrorMsg());
305 $sql = "DELETE FROM " . $this->_table . " where id='" . $id . "'";
306 //echo $sql . "<br>";
307 $this->_db->Execute($sql) or die("Error: " . $this->_db->ErrorMsg());
308 $this->load_tree();
310 return true;
313 function get_node_info($id)
315 if (!empty($this->_id_name[$id])) {
316 return $this->_id_name[$id];
317 } else {
318 return array();
322 function get_node_name($id)
324 if (!empty($this->_id_name[$id])) {
325 return $this->_id_name[$id]['name'];
326 } else {
327 return false;
332 function array_merge_2(&$array, &$array_i)
334 // For each element of the array (key => value):
335 foreach ($array_i as $k => $v) {
336 // If the value itself is an array, the process repeats recursively:
337 if (is_array($v)) {
338 if (!isset($array[$k])) {
339 $array[$k] = array();
342 array_merge_2($array[$k], $v);
344 // Else, the value is assigned to the current element of the resulting array:
345 } else {
346 if (isset($array[$k]) && is_array($array[$k])) {
347 $array[$k][0] = $v;
348 } else {
349 if (isset($array) && !is_array($array)) {
350 $temp = $array;
351 $array = array();
352 $array[0] = $temp;
355 $array[$k] = $v;
362 function array_merge_n()
364 // Initialization of the resulting array:
365 $array = array();
367 // Arrays to be merged (function's arguments):
368 $arrays = func_get_args();
370 // Merging of each array with the resulting one:
371 foreach ($arrays as $array_i) {
372 if (is_array($array_i)) {
373 array_merge_2($array, $array_i);
377 return $array;
382 $db = $GLOBALS['adodb']['db'];
383 $sql = "USE document;";
384 $db->Execute($sql);
385 $t = new Tree(1);
386 echo "<pre>";
387 print_r($t->tree);
388 //$nid = $t->add_node(0,"test node2","test value");
389 //$t->add_node($nid,"test child","another value");
390 //$t->add_node($nid,"test child");
391 print_r($t->tree);
392 echo "</pre>";