Changeset 42 for trunk/lib/NodeHeirarchy.inc.php
- Timestamp:
- Dec 18, 2005 12:16:03 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/NodeHeirarchy.inc.php
r41 r42 1 1 <?php 2 2 /** 3 * NodeHeirarchy.inc.php 3 * NodeHeirarchy.inc.php 4 4 * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information 5 5 */ … … 23 23 * @version 1.0 24 24 */ 25 25 26 26 class NodeHeirarchy { 27 27 … … 50 50 51 51 /** 52 * Boolean indicating whether or not we've set 52 * Boolean indicating whether or not we've set 53 53 * the 'active' node type and id. 54 54 * @var bool $node_init 55 55 */ 56 56 var $node_init = false; 57 57 58 58 /** 59 59 * Constructor … … 66 66 $this->params = $params; 67 67 } 68 68 69 69 /** 70 70 * Defines the default child_type and child_id for this object. … … 85 85 return $this->toStringID($old_type, $old_id); 86 86 } 87 87 88 88 /** 89 89 * Takes a node type and id and returns them as a serialized identifier like … … 121 121 } 122 122 } 123 123 124 124 /** 125 125 * Takes a singlar node identifier and returns it as components of an array. … … 156 156 } 157 157 } 158 158 159 159 // Make sure this is not empty and an array, even if it has only one value. 160 160 if ('' == $parents) { … … 165 165 $parents = array($parents); 166 166 } 167 167 168 168 // Remove duplicates. 169 169 $parents = array_unique($parents); 170 170 171 171 // Test that this node does not already exist and that the new parents 172 172 // do exist before we continue. … … 182 182 App::logMsg(sprintf(_("Cannot add node <strong>%s %s</strong> to nonexistent parent <strong>%s %s</strong>."), $child_type, $child_id, $parent['node_type'], $parent['node_id']), LOG_ERR, __FILE__, __LINE__); 183 183 return false; 184 } 185 } 186 184 } 185 } 186 187 187 // Insert new nodes with the new parents. 188 188 foreach ($parents as $parent_string) { … … 190 190 DB::query(" 191 191 INSERT INTO node_tbl ( 192 parent_type, 193 parent_id, 194 child_type, 195 child_id, 192 parent_type, 193 parent_id, 194 child_type, 195 child_id, 196 196 relationship_type, 197 197 title 198 198 ) VALUES ( 199 '" . addslashes($parent['node_type']) . "', 200 '" . addslashes($parent['node_id']) . "', 201 '" . addslashes($child_type) . "', 202 '" . addslashes($child_id) . "', 199 '" . addslashes($parent['node_type']) . "', 200 '" . addslashes($parent['node_id']) . "', 201 '" . addslashes($child_type) . "', 202 '" . addslashes($child_id) . "', 203 203 " . (is_null($relationship_type) ? "NULL" : "'" . addslashes($relationship_type) . "'") . ", 204 204 '" . addslashes($title) . "' … … 267 267 * 268 268 * @return bool false on error, true otherwise. 269 */ 269 */ 270 270 function moveNode($new_parents=null, $child_type=null, $child_id=null, $relationship_type=null, $title='') 271 271 { … … 279 279 } 280 280 } 281 281 282 282 // Make sure this is not empty and an array, even if it has only one value. 283 283 if (empty($new_parents)) { … … 288 288 $new_parents = array($new_parents); 289 289 } 290 290 291 291 // Remove duplicates. 292 292 $new_parents = array_unique($new_parents); … … 299 299 App::logMsg(sprintf(_("Cannot move node <strong>%s %s</strong> to nonexistent parent <strong>%s %s</strong>."), $child_type, $child_id, $parent['node_type'], $parent['node_id']), LOG_ERR, __FILE__, __LINE__); 300 300 return false; 301 } 301 } 302 302 if ($this->isAncestor($child_type, $child_id, $parent['node_type'], $parent['node_id'])) { 303 303 App::raiseMsg(sprintf(_("Cannot move node <strong>%s %s</strong> to parent <strong>%s %s</strong> because a node cannot have itself as a parent."), $child_type, $child_id, $parent['node_type'], $parent['node_id']), MSG_ERR, __FILE__, __LINE__); 304 304 App::logMsg(sprintf(_("Cannot move node <strong>%s %s</strong> to parent <strong>%s %s</strong> because a node cannot have itself as a parent."), $child_type, $child_id, $parent['node_type'], $parent['node_id']), LOG_ERR, __FILE__, __LINE__); 305 305 return false; 306 } 307 } 308 306 } 307 } 308 309 309 if (empty($title)) { 310 310 // Select the title of the node we are moving, so we can add it again with the same info. … … 317 317 list($title) = mysql_fetch_row($qid); 318 318 } 319 319 320 320 // Delete the nodes with the old parents. 321 321 DB::query(" … … 326 326 "); 327 327 App::logMsg(sprintf('moveNode: Deleted node %s %s.', $child_type, $child_id), LOG_DEBUG, __FILE__, __LINE__); 328 328 329 329 // Insert new nodes with the new parents. 330 330 $this->insertNode($new_parents, $child_type, $child_id, $relationship_type, $title); 331 331 332 332 return true; 333 333 } … … 356 356 $qid = DB::query(" 357 357 SELECT parent_type, parent_id 358 FROM node_tbl 358 FROM node_tbl 359 359 WHERE child_type = '" . addslashes($child_type) . "' 360 AND child_id = '" . addslashes($child_id) . "' 360 AND child_id = '" . addslashes($child_id) . "' 361 361 "); 362 362 363 363 $parents = array(); 364 364 while ($row = mysql_fetch_assoc($qid)) { … … 391 391 } 392 392 } 393 393 394 394 $qid = DB::query(" 395 395 SELECT child_type, child_id, title, subnode_quantity 396 FROM node_tbl 396 FROM node_tbl 397 397 WHERE child_type = '" . addslashes($child_type) . "' 398 398 AND child_id = '" . addslashes($child_id) . "' 399 399 "); 400 400 401 401 $children = array(); 402 402 while ($row = mysql_fetch_assoc($qid)) { … … 432 432 } 433 433 } 434 434 435 435 $in_clause = ''; 436 436 if (isset($type_constraint)) { … … 440 440 $in_clause = "AND child_type IN ('" . join("','", array_map('addslashes', $type_constraint)) . "')"; 441 441 } 442 442 443 443 $qid = DB::query(" 444 444 SELECT * 445 FROM node_tbl 445 FROM node_tbl 446 446 WHERE parent_type = '" . addslashes($child_type) . "' 447 447 AND parent_id = '" . addslashes($child_id) . "' … … 449 449 " . addslashes($order) . " 450 450 "); 451 451 452 452 $children = array(); 453 453 while ($row = mysql_fetch_assoc($qid)) { … … 464 464 * Give the number of children a category has. We are talking about the 465 465 * direct children, on the next level. 466 * 466 * 467 467 * @param string optional $parent The name of the parent from where we begin. 468 468 * @param string $type_constraint An array of node types to restrict the search to. … … 481 481 } 482 482 } 483 483 484 484 $in_clause = ''; 485 485 if (isset($type_constraint)) { … … 492 492 $qid = DB::query(" 493 493 SELECT COUNT(*) 494 FROM node_tbl 494 FROM node_tbl 495 495 WHERE parent_type = '" . addslashes($child_type) . "' 496 496 AND parent_id = '" . addslashes($child_id) . "' … … 519 519 } 520 520 } 521 521 522 522 if ($this->getNumberChildren($child_type, $child_id) <= 0) { 523 523 return true; … … 544 544 $family_tree = $this->getAllAncestors($considered_parent_type, $considered_parent_id); 545 545 $family_tree = $this->toStringID($family_tree, -1); 546 546 547 547 if (in_array($this->toStringID($child_type, $child_id), $family_tree)) { 548 548 return true; … … 571 571 static $output = array(); 572 572 static $return_flag; 573 573 574 574 $qid = DB::query(" 575 575 SELECT parent_type, parent_id, child_type, child_id, title, subnode_quantity … … 583 583 continue; 584 584 } 585 585 586 586 // Build a linear path to root...no wormholes. 587 587 if ($enough_already && $go_linear) { … … 589 589 } 590 590 $enough_already = true; 591 591 592 592 // To prevent duplicates, only add the new found node 593 593 // if not already in the array of ancestors. … … 596 596 $output[] = $row; 597 597 } 598 598 599 599 $this->getAllAncestors($row['parent_type'], $row['parent_id'], $go_linear, false); 600 600 } … … 630 630 } 631 631 } 632 632 633 633 if (isset($parent_type) && isset($parent_id)) { 634 634 $qid = DB::query(" … … 665 665 * @param string $order SQL to append to the query of the getChildren 666 666 * call. Ex: 'ORDER BY child_id DESC' 667 * @return array Details of from the node table of all nodes below the 667 * @return array Details of from the node table of all nodes below the 668 668 * specified node: (type, id, title, indent level, selected status) 669 669 */ … … 672 672 static $output = array(); 673 673 static $is_a_leaf = array(); 674 674 675 675 if (!isset($child_type) || !isset($child_id)) { 676 676 if ($this->node_init) { … … 682 682 } 683 683 } 684 684 685 685 if (!is_array($preselected)) { 686 686 $preselected = array($preselected); 687 687 } 688 688 689 689 if ($_return_flag && $include_curr) { 690 690 $my_children = $this->getNode($child_type, $child_id); … … 709 709 $output[] = $row; 710 710 unset($row); 711 711 712 712 // This is so we test if each node is a string only once. We store the result in the is_a_leaf array statically. 713 713 if (!isset($is_a_leaf[$this->toStringID($my_children[$i]['child_type'], $my_children[$i]['child_id'])])) { … … 731 731 732 732 } 733 734 733 734 735 735 /** 736 736 * Counts the number of items linked to each parent node … … 744 744 // Reset all the category counters to zero. 745 745 DB::query("UPDATE node_tbl SET subnode_quantity = 0"); 746 746 747 747 // Get all the nodes. 748 748 $qid = DB::query("SELECT DISTINCT child_type, child_id FROM node_tbl"); 749 749 750 750 // For each node count the number of children... 751 751 while (list($child_type, $child_id) = mysql_fetch_row($qid)) { … … 757 757 } 758 758 } 759 759 760 760 /** 761 761 * Used internally by setSubnodeQty to add the quantity of subnodes to … … 772 772 $qid = DB::query(" 773 773 SELECT parent_type, parent_id 774 FROM node_tbl 774 FROM node_tbl 775 775 WHERE child_type = '" . addslashes($child_type) . "' 776 AND child_id = '" . addslashes($child_id) . "' 776 AND child_id = '" . addslashes($child_id) . "' 777 777 ",false); 778 778 while ((list($parent_type, $parent_id) = mysql_fetch_row($qid)) && $parent_id > 0) {
Note: See TracChangeset
for help on using the changeset viewer.