Changeset 3520
- Timestamp:
- 02/19/07 18:14:05 (2 years ago)
- Files:
-
- plugins/sfPropelActAsNestedSetBehaviorPlugin/README (modified) (4 diffs)
- plugins/sfPropelActAsNestedSetBehaviorPlugin/config/config.php (modified) (3 diffs)
- plugins/sfPropelActAsNestedSetBehaviorPlugin/lib/sfPropelActAsNestedSetBehavior.class.php (modified) (8 diffs)
- plugins/sfPropelActAsNestedSetBehaviorPlugin/package.xml (modified) (2 diffs)
- plugins/sfPropelActAsNestedSetBehaviorPlugin/test/unit/PropelActAsNestedSetBehaviorTest.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelActAsNestedSetBehaviorPlugin/README
r3519 r3520 168 168 * `void insertAsNextSiblingOf(BaseObject $dest_node)` : Inserts node as next sibling of given node. 169 169 * `void insertAsPrevSiblingOf(BaseObject $dest_node)` : Inserts node as previous sibling of given node. 170 * `void insertAsParentOf(BaseObject $dest_node)` : '''NOT IMPLEMENTED YET'''170 * `void insertAsParentOf(BaseObject $dest_node)` : Inserts node as parent of given node 171 171 172 172 === Informational methods === … … 178 178 * `bool hasPrevSibling()` : Returns true if given node has a previous sibling. 179 179 * `bool isLeaf()` : Returns true if given node does not have children. 180 * `bool isChildOf()` : '''NOT IMPLEMENTED YET''' 181 * `bool isChildOfOrSiblingTo()` : '''NOT IMPLEMENTED YET''' 180 * `bool isChildOf()` : Returns true if given node is parent of node. 182 181 * `integer getNumberOfChildren()` : Returns given node number of direct children. 183 182 * `integer getNumberOfDescendants()` : Returns given node number of descendants (n level). … … 193 192 * `BaseObject retrieveLastChild()` : Returns given node last child. 194 193 * `BaseObject retrieveParent()` : Returns given node parent. 195 * `array retrieveSiblings()` : '''NOT IMPLEMENTED YET'''194 * `array retrieveSiblings()` : Returns node siblings. 196 195 * `array getPath()` : Returns path to a specific node as an array, useful to create breadcrumbs. 197 196 … … 209 208 * `void makeRoot()` : Sets node properties to make it a root node. 210 209 * `BaseObject reload()` : Returns an up to date version of node 210 * `bool isEqualTo(BaseObject $node)` : Returns true if given node is equivalent to node. 211 211 212 212 == Changelog == 213 214 === 2007-02-19 | 0.8.0 === 215 216 Implemented more methods (+ unit tests) : 217 218 * `insertAsParentOf` 219 * `retrieveSiblings` 220 * `isEqualTo` 221 * `isChildOf` 213 222 214 223 === 2007-02-19 | 0.7.0 === plugins/sfPropelActAsNestedSetBehaviorPlugin/config/config.php
r3519 r3520 69 69 array ( 70 70 'sfPropelActAsNestedSetBehavior', 71 'insertAsParentOf' 72 ), 73 array ( 74 'sfPropelActAsNestedSetBehavior', 71 75 'hasChildren' 72 76 ), … … 106 110 'sfPropelActAsNestedSetBehavior', 107 111 'isLeaf' 112 ), 113 array ( 114 'sfPropelActAsNestedSetBehavior', 115 'isEqualTo' 116 ), 117 array ( 118 'sfPropelActAsNestedSetBehavior', 119 'isChildOf' 108 120 ), 109 121 array ( … … 153 165 array ( 154 166 'sfPropelActAsNestedSetBehavior', 167 'retrieveSiblings' 168 ), 169 array ( 170 'sfPropelActAsNestedSetBehavior', 155 171 'getLevel' 156 172 ), plugins/sfPropelActAsNestedSetBehaviorPlugin/lib/sfPropelActAsNestedSetBehavior.class.php
r3519 r3520 332 332 } 333 333 334 /** 335 * Inserts node as parent of given node. 336 * 337 * @param BaseObject $node 338 * @param BaseObject $dest_node 339 * @throws Exception When trying to insert node as parent of a root node 340 */ 341 public function insertAsParentOf(BaseObject $node, BaseObject $dest_node) 342 { 343 if ($dest_node->isRoot()) 344 { 345 $msg = 'Impossible to insert a node as parent of a root node'; 346 throw new Exception($msg); 347 } 348 349 $peer_name = get_class($node->getPeer()); 350 351 $this->addPreSaveStackEntries(self::shiftRLValues($peer_name, $dest_node->getLeftValue(), 1, $dest_node->getScopeIdValue())); 352 $this->addPreSaveStackEntries(self::shiftRLValues($peer_name, $dest_node->getRightValue() + 2, -1, $dest_node->getScopeIdValue())); 353 354 $node->setLeftValue($dest_node->getLeftValue()); 355 $node->setRightValue($dest_node->getRightValue() + 2); 356 357 $previous_parent = $dest_node->getParentIdValue(); 358 $node->setParentIdValue($previous_parent); 359 $dest_node->setParentIdValue($node->getPrimaryKey()); 360 361 $this->addPreSaveStackEntries(array($dest_node)); 362 363 } 364 334 365 # ---- INFORMATIONAL METHODS 335 366 … … 398 429 { 399 430 return $node->getRightValue() - $node->getLeftValue() == 1; 431 } 432 433 /** 434 * Returns true if given node is identical to node. 435 * 436 * @param BaseObject $node 437 * @param BaseObject $compared_node 438 * @return bool 439 */ 440 public function isEqualTo(BaseObject $node, BaseObject $compared_node) 441 { 442 return ($node->getLeftValue() === $compared_node->getLeftValue() 443 && $node->getRightValue() === $compared_node->getRightValue() 444 && $node->getScopeIdValue() === $compared_node->getScopeIdValue() 445 && $node->getParentIdValue() === $compared_node->getParentIdValue()); 446 } 447 448 /** 449 * Returns true if given node is parent of node. 450 * 451 * @param BaseObject $node 452 * @param BaseObject $parent_node 453 */ 454 public function isChildOf(BaseObject $node, BaseObject $parent_node) 455 { 456 return ($node->getParentIdValue() === $parent_node->getPrimaryKey() 457 && $node->getScopeIdValue() === $parent_node->getScopeIdValue()); 400 458 } 401 459 … … 598 656 } 599 657 658 /** 659 * Returns node siblings. 660 * 661 * @param BaseObject $node 662 * @param string $peer_method (optional) defaults to "doSelect" 663 * @return array 664 */ 665 public function retrieveSiblings(BaseObject $node, $peer_method = 'doSelect') 666 { 667 $c = new Criteria(); 668 $c->add(self::getColumnConstant(get_class($node), 'parent'), $node->getParentIdValue()); 669 $c->add(self::getColumnConstant(get_class($node), 'scope'), $node->getScopeIdValue()); 670 671 $results = call_user_func(array($node->getPeer(), $peer_method), $c); 672 $final_results = array(); 673 if (is_array($results)) 674 { 675 foreach ($results as $r) 676 { 677 if ($node->isEqualTo($r)) 678 { 679 continue; 680 } 681 $final_results[] = $r; 682 } 683 } 684 685 return $final_results; 686 } 600 687 601 688 /** … … 631 718 $node->setParentIdValue($dest_node->getPrimaryKey()); 632 719 $node->setScopeIdValue($dest_node->getScopeIdValue()); 633 $this->addPreSaveStackEntries( $this->getUpdateTreeQueries($node, $dest_node->getLeftValue() + 1));720 $this->addPreSaveStackEntries(self::getUpdateTreeQueries($node, $dest_node->getLeftValue() + 1)); 634 721 } 635 722 … … 644 731 $node->setParentIdValue($dest_node->getPrimaryKey()); 645 732 $node->setScopeIdValue($dest_node->getScopeIdValue()); 646 $this->addPreSaveStackEntries( $this->getUpdateTreeQueries($node, $dest_node->getRightValue()));733 $this->addPreSaveStackEntries(self::getUpdateTreeQueries($node, $dest_node->getRightValue())); 647 734 } 648 735 … … 657 744 $node->setParentIdValue($dest_node->getParentIdValue()); 658 745 $node->setScopeIdValue($dest_node->getScopeIdValue()); 659 $this->addPreSaveStackEntries( $this->getUpdateTreeQueries($node, $dest_node->getRightValue() + 1));746 $this->addPreSaveStackEntries(self::getUpdateTreeQueries($node, $dest_node->getRightValue() + 1)); 660 747 } 661 748 … … 670 757 $node->setParentIdValue($dest_node->getParentIdValue()); 671 758 $node->setScopeIdValue($dest_node->getScopeIdValue()); 672 $this->addPreSaveStackEntries( $this->getUpdateTreeQueries($node, $dest_node->getLeftValue()));759 $this->addPreSaveStackEntries(self::getUpdateTreeQueries($node, $dest_node->getLeftValue())); 673 760 } 674 761 … … 914 1001 * @return array 915 1002 */ 916 private function getUpdateTreeQueries(BaseObject $node, $dest_left)1003 private static function getUpdateTreeQueries(BaseObject $node, $dest_left) 917 1004 { 918 1005 $statements = array(); plugins/sfPropelActAsNestedSetBehaviorPlugin/package.xml
r3519 r3520 13 13 <date>2007-02-19</date> 14 14 <version> 15 <release>0. 7.0</release>16 <api>0. 7.0</api>15 <release>0.8.0</release> 16 <api>0.8.0</api> 17 17 </version> 18 18 <stability> … … 53 53 54 54 <changelog> 55 56 <release> 57 <version> 58 <release>0.8.0</release> 59 <api>0.8.0</api> 60 </version> 61 <stability> 62 <release>beta</release> 63 <api>beta</api> 64 </stability> 65 <date>2007-02-19</date> 66 <license uri="http://www.symfony-project.com/license">MIT license</license> 67 <notes> 68 Implemented more methods (+ unit tests) : 69 70 * `insertAsParentOf` 71 * `retrieveSiblings` 72 * `isEqualTo` 73 * `isChildOf` 74 </notes> 75 </release> 55 76 56 77 <release> plugins/sfPropelActAsNestedSetBehaviorPlugin/test/unit/PropelActAsNestedSetBehaviorTest.php
r3519 r3520 82 82 83 83 // insertAsFirstChildOf 84 $t = new lime_test( 24, new lime_output_color());84 $t = new lime_test(6, new lime_output_color()); 85 85 $t->diag('insertAsFirstChildOf'); 86 86 $t->diag('insert N1 as first child of ROOT'); … … 98 98 $t->is($n1->getParentIdValue(), $root->getId(), 'N1 parent id equals ROOT id'); 99 99 $t->is($n1->getScopeIdValue(), $root->getScopeIdValue(), 'N1 scope id equals ROOT id'); 100 101 // isEqualTo 102 $t = new lime_test(2, new lime_output_color()); 103 $t->is($n1->isEqualTo($n1), true, 'isEqualTo() returns true when nodes are equivalent'); 104 $t->is($root->isEqualTo($n1), false, 'isEqualTo() returns false when nodes are different'); 100 105 101 106 $t->diag('insert N2 as first child of ROOT'); … … 611 616 $t->is($n6->getParentIdValue(), $n8->getPrimaryKey(), 'N6 parent id equals N8 id'); 612 617 $t->is($n6->getScopeIdValue(), $n8->getScopeIdValue(), 'N6 scope id equals N8 scope id'); 618 619 // retrieveSiblings 620 $t = new lime_test(15, new lime_output_color()); 621 $t->diag('retrieveSiblings'); 622 $t->diag('Retrieve siblings of N7'); 623 $expected = array($n3->getId(), $n4->getId()); 624 $got = array(); 625 foreach ($n7->retrieveSiblings() as $node) 626 { 627 $got[] = $node->getId(); 628 } 629 630 $t->is($got, $expected, 'retrieveSiblings() returns the right siblings'); 613 631 614 632 // delete … … 837 855 838 856 // deleteDescendants 839 $t = new lime_test( 22, new lime_output_color());857 $t = new lime_test(14, new lime_output_color()); 840 858 $t->diag('deleteDescendants'); 841 859 $t->diag('Delete N8 descendants'); … … 879 897 $t->is($n8->getScopeIdValue(), $root->getScopeIdValue(), 'N8 scope id equals ROOT scope id'); 880 898 899 // insertAsParentOf 900 $t = new lime_test(15, new lime_output_color()); 901 $t->diag('insertAsParentOf'); 902 $t->diag('Insert N8 as parent of N4'); 903 $t->diag('root'); 904 $t->diag(' |-N8'); 905 $t->diag(' |-N4'); 906 $t->diag(' |-N7'); 907 908 $n8->insertAsParentOf($n4); 909 $n8->save(); 910 911 // -- reload nodes 912 $n4 = $n4->reload(); 913 $n7 = $n7->reload(); 914 $n8 = $n8->reload(); 915 $root = $root->reload(); 916 917 $t->is($root->getLeftValue(), 1, 'root "left" is "1"'); 918 $t->is($root->getRightValue(), 8, 'root "right" is "8"'); 919 $t->is($n4->getLeftValue(), 3, 'N4 "left" is "3"'); 920 $t->is($n4->getRightValue(), 4, 'N4 "right" is "4"'); 921 $t->is($n4->getParentIdValue(), $n8->getPrimaryKey(), 'N4 parent is N8'); 922 $t->is($n4->getScopeIdValue(), $n8->getScopeIdValue(), 'N4 scope id equals N8 scope id'); 923 $t->is($n7->getLeftValue(), 6, 'N7 "left" is "6"'); 924 $t->is($n7->getRightValue(), 7, 'N7 "right" is "7"'); 925 $t->is($n7->getParentIdValue(), $root->getPrimaryKey(), 'N7 parent is ROOT'); 926 $t->is($n7->getScopeIdValue(), $root->getScopeIdValue(), 'N7 scope id equals ROOT scope id'); 927 $t->is($n8->getLeftValue(), 2, 'N8 "left" is "2"'); 928 $t->is($n8->getRightValue(), 5, 'N8 "right" is "5"'); 929 $t->is($n8->getParentIdValue(), $root->getPrimaryKey(), 'N8 parent is ROOT'); 930 $t->is($n8->getScopeIdValue(), $root->getScopeIdValue(), 'N8 scope id equals ROOT scope id'); 931 932 try 933 { 934 $n8->insertAsParentOf($root); 935 $t->fail('insertAsParentOf() throws an exception when asked to insert a node as parent of a root node'); 936 } 937 catch (Exception $e) 938 { 939 $t->pass('insertAsParentOf() throws an exception when asked to insert a node as parent of a root node'); 940 } 941 942 // isChildOf 943 $t = new lime_test(2, new lime_output_color()); 944 $t->diag('isChildOf'); 945 $t->is($n4->isChildOf($n8), true, 'isChildOf() returns true when given node is node parent'); 946 $t->is($n4->isChildOf($root), false, 'isChildOf() returns false when given node is not node parent'); 947 881 948 // Helper functions 882 949