[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/vendor/propel-generator/templates/om/php4/ -> Node.tpl (source)

   1  <?php
   2  
   3  // Template for creating base node class on tree table.
   4  //
   5  // $Id: Node.tpl,v 1.8 2005/04/04 11:02:40 dlawson_mi Exp $
   6  
   7  
   8  require_once 'propel/engine/builder/om/ClassTools.php';
   9  
  10  $db = $table->getDatabase();
  11  if($table->getPackage()) {
  12      $package = $table->getPackage();
  13  } else {
  14      $package = $targetPackage;
  15  }
  16  
  17  $CLASS = $table->getPhpName() . 'NodePeer';
  18  echo '<' . '?' . 'php';
  19  
  20  ?>
  21  
  22  
  23  require_once '<?php echo ClassTools::getFilePath($package, $table->getPhpName() . 'NodePeer') ?>';
  24  
  25  /**
  26   * Base tree node class for manipulating a tree of <?php echo $table->getPhpName() ?> objects.
  27   * This class will wrap these objects within a "node" interface. It provides a
  28   * method overload mechanism which allows you to use a <?php echo $table->getPhpName() ?>Node
  29   * object just like a <?php $table->getPhpName() ?> object.
  30   * 
  31   * To avoid tree corruption, you should always use this class to make changes to
  32   * the tree and objects within it rather than using the <?php echo $table->getPhpName() ?> 
  33   * class directly.
  34   *
  35  <?php if ($addTimeStamp) { ?>
  36   * This class was autogenerated by Propel on:
  37   *
  38   * [<?php echo $now ?>]
  39   *
  40  <?php } ?>
  41   * @package <?php echo $package ?> 
  42   *
  43   */
  44  class <?php echo $basePrefix . $table->getPhpName() ?>Node /*implements IteratorAggregate*/
  45  {
  46      /**
  47       * @var <?php echo $table->getPhpName() ?> Object wrapped by this node.
  48       */
  49      var $obj = null;
  50      
  51      /**
  52       * The parent node for this node.
  53       * @var <?php echo $table->getPhpName() ?>Node
  54       */
  55      var $parentNode = null;
  56  
  57      /**
  58       * Array of child nodes for this node. Nodes indexes are one-based.
  59       * @var array
  60       */
  61      var $childNodes = array();
  62  
  63      /**
  64       * Constructor.
  65       *
  66       * @param <?php echo $table->getPhpName() ?> Object wrapped by this node.
  67       */
  68      function <?php echo $table->getPhpName() ?>Node($obj = null)
  69      {
  70          Propel::assertParam($obj, '<?php echo $CLASS; ?>', '<?php echo $table->getPhpName() ?>Node', 1);
  71          $obj =& Param::get($obj);
  72          
  73          if ($obj !== null)
  74          {
  75              $this->obj =& $obj;
  76          }
  77          else
  78          {
  79              $setNodePath = "set" . <?php echo $table->getPhpName() ?>NodePeer::NPATH_PHPNAME();
  80              $this->obj =& new <?php echo $table->getPhpName() ?>();
  81              $this->obj->$setNodePath('0');
  82          }
  83      }
  84  
  85      /**
  86       * Convenience overload for wrapped object methods.
  87       *
  88       * @param string Method name to call on wrapped object.
  89       * @param mixed Parameter accepted by wrapped object set method.
  90       * @return mixed Return value of wrapped object method.
  91       * @throws PropelException Fails if method is not defined for wrapped object.
  92       */
  93      function & callObjMethod($name, $parms = null)
  94      {
  95          $params =& Param::get($parms);
  96          if (method_exists($this->obj, $name)) {
  97              if (is_array($params)) {
  98                  $implode = array();
  99                  $keys = array_keys($params);
 100                  foreach($keys as $key)
 101                      $implode [] = "\$params['{$key}']";
 102                  eval('$result =& $this->obj->$name(' . implode(',', $implode) . ');');
 103              }
 104              else {
 105                  $result =& $this->obj->$name($params);
 106              }
 107              return $result;
 108          }
 109          else
 110              return new PropelException(PROPEL_ERROR, "get method not defined: $name");
 111      }
 112  
 113      /**
 114       * Sets the default options for iterators created from this object.
 115       * The options are specified in map format. The following options 
 116       * are supported by all iterators. Some iterators may support other
 117       * options:
 118       *
 119       *   "querydb" - True if nodes should be retrieved from database.
 120       *   "con" - Connection to use if retrieving from database.
 121       *
 122       * @param string Type of iterator to use ("pre", "post", "level").
 123       * @param array Map of option name => value.
 124       * @return void
 125       * @todo Implement other iterator types (i.e. post-order, level, etc.)
 126       */ 
 127      function setIteratorOptions($type, $opts)
 128      {
 129          $this->itType = $type;
 130          $this->itOpts = $opts;
 131      }
 132      
 133      /**
 134       * Returns a pre-order iterator for this node and its children.
 135       *
 136       * @param string Type of iterator to use ("pre", "post", "level")
 137       * @param array Map of option name => value.
 138       * @return NodeIterator
 139       */
 140      function & getIterator($type = null, $opts = null)
 141      {
 142          if ($type === null)
 143              $type = (isset($this->itType) ? $this->itType : 'Pre');
 144  
 145          if ($opts === null)
 146              $opts = (isset($this->itOpts) ? $this->itOpts : array());
 147              
 148          $itclass = ucfirst(strtolower($type)) . 'OrderNodeIterator';
 149          
 150          require_once('propel/om/' . $itclass . '.php');
 151          return new $itclass($this, $opts);
 152      }
 153          
 154      /**
 155       * Returns the object wrapped by this class.
 156       * @return <?php echo $table->getPhpName() . "\n" ?>
 157       */
 158      function & getNodeObj()
 159      {
 160          return $this->obj;
 161      }
 162          
 163      /**
 164       * Convenience method for retrieving nodepath.
 165       * @return string
 166       */
 167      function getNodePath()
 168      {
 169          $getNodePath = 'get' . <?php echo $table->getPhpName() ?>NodePeer::NPATH_PHPNAME();
 170          return $this->obj->$getNodePath();
 171      }
 172  
 173      /**
 174       * Returns one-based node index among siblings.
 175       * @return int
 176       */
 177      function getNodeIndex()
 178      {
 179          $npath = $this->getNodePath();
 180          //strrpos fix
 181          $sep = 0;
 182          while(false !== ($last = strpos($npath, <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP(), $sep))) {
 183              $sep = $last + strlen(<?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP());
 184          }
 185          return (int) ($sep != 0 ? substr($npath, $sep) : $npath);
 186      }
 187      
 188      /**
 189       * Returns one-based node level within tree (root node is level 1).
 190       * @return int
 191       */
 192      function getNodeLevel()
 193      {
 194          return (substr_count($this->getNodePath(), <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP()) + 1);
 195      }
 196      
 197      /** 
 198       * Returns true if specified node is a child of this node. If recurse is
 199       * true, checks if specified node is a descendant of this node.
 200       *
 201       * @param <?php echo $table->getPhpName() ?>Node Node to look for.
 202       * @param boolean True if strict comparison should be used.
 203       * @param boolean True if all descendants should be checked.
 204       * @return boolean
 205       */
 206      function hasChildNode(&$node, $strict = false, $recurse = false)
 207      {
 208          foreach ($this->childNodes as $key => $childNode)
 209          {
 210              if ($this->childNodes[$key]->equals($node, $strict))
 211                  return true;
 212                  
 213              if ($recurse && $this->childNodes[$key]->hasChildNode($node, $recurse))
 214                  return true;
 215          }
 216          
 217          return false;
 218      }
 219  
 220      /**
 221       * Returns child node at one-based index. Retrieves from database if not 
 222       * loaded yet.
 223       *
 224       * @param int One-based child node index.
 225       * @param boolean True if child should be retrieved from database.
 226       * @param Connection Connection to use if retrieving from database.
 227       * @return <?php echo $table->getPhpName() ?>Node
 228       */
 229      function & getChildNodeAt($i, $querydb = false, $con = null)
 230      {
 231          Propel::assertParam($con, '<?php echo $CLASS; ?>', 'getChildNodeAt', 3);
 232          $con =& Param::get($con);
 233          
 234          if ($querydb && 
 235              !$this->obj->isNew() && 
 236              !$this->obj->isDeleted() && 
 237              !isset($this->childNodes[$i]))
 238          {
 239              $criteria =& new Criteria(<?php echo $table->getPhpName() ?>Peer::DATABASE_NAME());
 240              $criteria->add(<?php echo $table->getPhpName() ?>NodePeer::NPATH_COLNAME(),
 241                  $this->getNodePath() . <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP() . $i,
 242                  Criteria::EQUAL());
 243  
 244              if ($childObj =& <?php echo $table->getPhpName() ?>Peer::doSelectOne($criteria, Param::set($con)))
 245                  $this->attachChildNode(new <?php echo $table->getPhpName() ?>Node(Param::set($childObj)));
 246          }
 247  
 248          return (isset($this->childNodes[$i]) ? $this->childNodes[$i] : null);
 249      }
 250  
 251      /**
 252       * Returns first child node (if any). Retrieves from database if not loaded yet.
 253       *
 254       * @param boolean True if child should be retrieved from database.
 255       * @param Connection Connection to use if retrieving from database.
 256       * @return <?php echo $table->getPhpName() ?>Node
 257       */
 258      function & getFirstChildNode($querydb = false, $con = null)
 259      {
 260          Propel::assertParam($con, '<?php echo $CLASS; ?>', 'getFirstChildNode', 2);
 261          $con =& Param::get($con);
 262          return $this->getChildNodeAt(1, $querydb, Param::set($con));
 263      }
 264          
 265      /**
 266       * Returns last child node (if any). 
 267       *
 268       * @param boolean True if child should be retrieved from database.
 269       * @param Connection Connection to use if retrieving from database.
 270       */
 271      function & getLastChildNode($querydb = false, $con = null)
 272      {
 273          Propel::assertParam($con, '<?php echo $CLASS; ?>', 'getLastChildNode', 2);
 274          $con =& Param::get($con);
 275          $lastNode = null;
 276  
 277          if ($this->obj->isNew() || $this->obj->isDeleted())
 278          {
 279              if (count($this->childNodes)) {
 280                  end($this->childNodes);
 281                  $lastNode =& $this->childNodes[key($this->childNodes)];
 282              }
 283          }
 284          else if ($querydb)
 285          {
 286              $db =& Propel::getDb(<?php echo $table->getPhpName() ?>Peer::DATABASE_NAME());
 287              $criteria =& new Criteria(<?php echo $table->getPhpName() ?>Peer::DATABASE_NAME());
 288              $criteria->add(<?php echo $table->getPhpName() ?>NodePeer::NPATH_COLNAME(),
 289                  $this->getNodePath() . <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP() . '%',
 290                  Criteria::LIKE());
 291              $criteria->addAnd(<?php echo $table->getPhpName() ?>NodePeer::NPATH_COLNAME(),
 292                  $this->getNodePath() . <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP() . '%' . <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP() . '%',
 293                  Criteria::NOT_LIKE());
 294              $criteria->addAsColumn('npathlen', $db->strLength(<?php echo $table->getPhpName() ?>NodePeer::NPATH_COLNAME()));
 295              $criteria->addDescendingOrderByColumn('npathlen');
 296              $criteria->addDescendingOrderByColumn(<?php echo $table->getPhpName() ?>NodePeer::NPATH_COLNAME());
 297  
 298              $lastObj =& <?php echo $table->getPhpName() ?>Peer::doSelectOne($criteria, Param::set($con));
 299  
 300              if ($lastObj !== null)
 301              {
 302                  $lastNode =& new <?php echo $table->getPhpName() ?>Node(Param::set($lastObj));
 303                  $endNode = null;
 304  
 305                  if (count($this->childNodes)) {
 306                      end($this->childNodes);
 307                      $endNode =& $this->childNodes[key($this->childNodes)];
 308                  }
 309                  
 310                  if ($endNode)
 311                  {
 312                      if ($endNode->getNodePath() > $lastNode->getNodePath())
 313                          return new PropelException(PROPEL_ERROR, 'Cached child node inconsistent with database.');
 314                      else if ($endNode->getNodePath() == $lastNode->getNodePath())
 315                          $lastNode =& $endNode;
 316                      else
 317                          $this->attachChildNode($lastNode);
 318                  }
 319                  else
 320                  {
 321                      $this->attachChildNode($lastNode);
 322                  }
 323              }
 324          }
 325  
 326          return $lastNode;
 327      }
 328  
 329      /**
 330       * Returns next (or previous) sibling node or null. Retrieves from database if 
 331       * not loaded yet.
 332       *
 333       * @param boolean True if previous sibling should be returned.
 334       * @param boolean True if sibling should be retrieved from database.
 335       * @param Connection Connection to use if retrieving from database.
 336       * @return <?php echo $table->getPhpName() ?>Node
 337       */
 338      function & getSiblingNode($prev = false, $querydb = false, $con = null)
 339      {
 340          Propel::assertParam($con, '<?php echo $CLASS; ?>', 'getSiblingNode', 3);
 341          $con =& Param::get($con);
 342          $nidx = $this->getNodeIndex();
 343          
 344          if ($this->isRootNode())
 345          {
 346              return null;
 347          }
 348          else if ($prev)
 349          {
 350              if ($nidx > 1 && ($parentNode =& $this->getParentNode($querydb, Param::set($con))))
 351                  return $parentNode->getChildNodeAt($nidx-1, $querydb, Param::set($con));
 352              else
 353                  return null;
 354          }
 355          else
 356          {
 357              if ($parentNode =& $this->getParentNode($querydb, Param::set($con)))
 358                  return $parentNode->getChildNodeAt($nidx+1, $querydb, Param::set($con));
 359              else
 360                  return null;
 361          }
 362      }
 363  
 364      /**
 365       * Returns parent node. Loads from database if not cached yet.
 366       *
 367       * @param boolean True if parent should be retrieved from database.
 368       * @param Connection Connection to use if retrieving from database.
 369       * @return <?php echo $table->getPhpName() ?>Node
 370       */
 371      function & getParentNode($querydb = true, $con = null)
 372      {
 373          Propel::assertParam($con, '<?php echo $CLASS; ?>', 'getParentNode', 2);
 374          $con =& Param::get($con);
 375          
 376          if ($querydb &&
 377              $this->parentNode === null && 
 378              !$this->isRootNode() &&
 379              !$this->obj->isNew() && 
 380              !$this->obj->isDeleted())
 381          {
 382              $npath =& $this->getNodePath();
 383              //strrpos fix
 384              $sep = 0;
 385              while(false !== ($last = strpos($npath, <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP(), $sep))) {
 386                  $sep = $last + strlen(<?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP());
 387              }
 388              $ppath = substr($npath, 0, $sep - strlen(<?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP()));
 389  
 390              $criteria =& new Criteria(<?php echo $table->getPhpName() ?>Peer::DATABASE_NAME());
 391              $criteria->add(<?php echo $table->getPhpName() ?>NodePeer::NPATH_COLNAME(), $ppath, Criteria::EQUAL());
 392  
 393              if ($parentObj =& <?php echo $table->getPhpName() ?>Peer::doSelectOne($criteria, Param::set($con)))
 394              {
 395                  $parentNode =& new <?php echo $table->getPhpName() ?>Node(Param::set($parentObj));
 396                  $parentNode->attachChildNode($this);
 397              }
 398          }
 399          
 400          return $this->parentNode;
 401      }
 402  
 403      /** 
 404       * Returns an array of all ancestor nodes, starting with the root node 
 405       * first.
 406       *
 407       * @param boolean True if ancestors should be retrieved from database.
 408       * @param Connection Connection to use if retrieving from database.
 409       * @return array
 410       */
 411      function & getAncestors($querydb = false, $con = null)
 412      {
 413          Propel::assertParam($con, '<?php echo $CLASS; ?>', 'getAncestors', 2);
 414          $con =& Param::get($con);
 415          
 416          $ancestors = array();
 417          $parentNode = $this;
 418          
 419          while ($parentNode =& $parentNode->getParentNode($querydb, Param::set($con)))
 420              array_unshift($ancestors, $parentNode);
 421          
 422          return $ancestors;
 423      }
 424      
 425      /**
 426       * Returns true if node is the root node of the tree.
 427       * @return boolean
 428       */
 429      function isRootNode()
 430      {
 431          return ($this->getNodePath() === '1');
 432      }
 433  
 434      /**
 435       * Changes the state of the object and its descendants to 'new'.
 436       * Also changes the node path to '0' to indicate that it is not a 
 437       * stored node.
 438       *
 439       * @param boolean
 440       * @return void
 441       */
 442      function setNew($b)
 443      {
 444          $this->adjustStatus('new', $b);
 445          $this->adjustNodePath($this->getNodePath(), '0');
 446      }
 447      
 448      /**
 449       * Changes the state of the object and its descendants to 'deleted'.
 450       *
 451       * @param boolean
 452       * @return void
 453       */
 454      function setDeleted($b)
 455      {
 456          $this->adjustStatus('deleted', $b);
 457      }
 458       
 459      /**
 460       * Adds the specified node (and its children) as a child to this node. If a
 461       * valid $beforeNode is specified, the node will be inserted in front of 
 462       * $beforeNode. If $beforeNode is not specified the node will be appended to
 463       * the end of the child nodes.
 464       *
 465       * @param <?php echo $table->getPhpName() ?>Node Node to add.
 466       * @param <?php echo $table->getPhpName() ?>Node Node to insert before.
 467       * @param Connection Connection to use.
 468       */
 469      function addChildNode(&$node, $beforeNode = null, $con = null)
 470      {
 471          Propel::assertParam($beforeNode, '<?php echo $CLASS; ?>', 'addChildNode', 2);
 472          Propel::assertParam($con, '<?php echo $CLASS; ?>', 'addChildNode', 3);
 473          $beforeNode =& Param::get($beforeNode);
 474          $con =& Param::get($con);
 475  
 476          if ($this->obj->isNew() && !$node->obj->isNew())
 477              return new PropelException(PROPEL_ERROR, 'Cannot add stored nodes to a new node.');
 478              
 479          if ($this->obj->isDeleted() || $node->obj->isDeleted())
 480              return new PropelException(PROPEL_ERROR, 'Cannot add children in a deleted state.');
 481          
 482          if ($this->hasChildNode($node))
 483              return new PropelException(PROPEL_ERROR, 'Node is already a child of this node.');
 484  
 485          if ($beforeNode && !$this->hasChildNode($beforeNode))
 486              return new PropelException(PROPEL_ERROR, 'Invalid beforeNode.');
 487              
 488          if ($con === null)
 489              $con =& Propel::getConnection(<?php echo $table->getPhpName() ?>Peer::DATABASE_NAME());
 490              
 491          if (Propel::isError($con))
 492              return $con;
 493  
 494          if (!$this->obj->isNew()) {
 495            $e = $con->begin();
 496            if (Creole::isError($e)) { $con->rollback(); return new PropelException(PROPEL_ERROR_DB, $e); }
 497          }
 498  
 499          if ($beforeNode)
 500          {
 501              // Inserting before a node.
 502              $childIdx = $beforeNode->getNodeIndex();
 503              $e = $this->shiftChildNodes(1, $beforeNode->getNodeIndex(), $con);
 504              if (Propel::isError($e)) { $con->rollback(); return $e; }
 505          }
 506          else
 507          {
 508              // Appending child node.
 509              if ($lastNode =& $this->getLastChildNode(true, Param::set($con)))
 510                  $childIdx = $lastNode->getNodeIndex()+1;
 511              else
 512                  $childIdx = 1;
 513          }
 514  
 515          // Add the child (and its children) at the specified index.
 516  
 517          if (!$this->obj->isNew() && $node->obj->isNew())
 518          {
 519              $e = $this->insertNewChildNode($node, $childIdx, $con);
 520              if (Propel::isError($e)) { $con->rollback(); return $e; }
 521          }
 522          else
 523          {
 524              // $this->isNew() && $node->isNew() ||
 525              // !$this->isNew() && !node->isNew()
 526                  
 527              $srcPath = $node->getNodePath();
 528              $dstPath = $this->getNodePath() . <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP() . $childIdx;
 529                  
 530              if (!$node->obj->isNew())
 531              {
 532                  $e = <?php echo $table->getPhpName() ?>NodePeer::moveNodeSubTree($srcPath, $dstPath, Param::set($con));
 533                  if (Propel::isError($e)) { $con->rollback(); return $e; }
 534  
 535                  $parentNode =& $node->getParentNode(true, Param::set($con));
 536              }
 537              else
 538              {
 539                  $parentNode =& $node->getParentNode();
 540              }
 541  
 542              if ($parentNode)
 543              {
 544                  $parentNode->detachChildNode($node);
 545                  $e = $parentNode->shiftChildNodes(-1, $node->getNodeIndex()+1, $con);
 546                  if (Propel::isError($e)) { $con->rollback(); return $e; }
 547              }
 548              
 549              $node->adjustNodePath($srcPath, $dstPath);
 550          }
 551  
 552          if (!$this->obj->isNew()) {
 553              $e = $con->commit();
 554              if (Creole::isError($e)) { return new PropelException(PROPEL_ERROR_DB, $e); }
 555          }
 556  
 557          $this->attachChildNode($node);
 558      }
 559  
 560      /**
 561       * Moves the specified child node in the specified direction.
 562       *
 563       * @param <?php $table->getPhpName() ?>Node Node to move.
 564       * @param int Number of spaces to move among siblings (may be negative).
 565       * @param Connection Connection to use.
 566       * @throws PropelException
 567       */
 568      function moveChildNode(&$node, $direction, $con = null)
 569      {
 570          Propel::assertParam($con, '<?php echo $CLASS; ?>', 'moveChildNode', 3);
 571          $con =& Param::get($con);
 572          return new PropelException(PROPEL_ERROR, 'moveChildNode() not implemented yet.');
 573      }
 574      
 575      /**
 576       * Saves modified object data to the datastore.
 577       *
 578       * @param boolean If true, descendants will be saved as well.
 579       * @param Connection Connection to use.
 580       */
 581      function save($recurse = false, $con = null)
 582      {
 583          Propel::assertParam($con, '<?php echo $CLASS; ?>', 'save', 2);
 584          $con =& Param::get($con);
 585  
 586          if ($this->obj->isDeleted())
 587              return new PropelException(PROPEL_ERROR, 'Cannot save deleted node.');
 588              
 589          if (substr($this->getNodePath(), 0, 1) == '0')
 590              return new PropelException(PROPEL_ERROR, 'Cannot save unattached node.');
 591  
 592          if ($this->obj->isColumnModified(<?php echo $table->getPhpName() ?>NodePeer::NPATH_COLNAME()))
 593              return new PropelException(PROPEL_ERROR, 'Cannot save manually modified node path.');
 594          
 595          $this->obj->save(Param::set($con));
 596          
 597          if ($recurse)
 598          {
 599              foreach ($this->childNodes as $key => $childNode)
 600                  $this->childNodes[$key]->save($recurse, Param::set($con));
 601          }
 602      }
 603      
 604      /**
 605       * Removes this object and all descendants from datastore.
 606       *
 607       * @param Connection Connection to use.
 608       * @return void
 609       * @throws PropelException
 610       */
 611      function delete($con = null)
 612      {
 613          Propel::assertParam($con, '<?php echo $CLASS; ?>', 'delete', 1);
 614          $con =& Param::get($con);
 615  
 616          if ($this->obj->isDeleted())
 617              return new PropelException(PROPEL_ERROR, 'This node has already been deleted.');
 618  
 619          if (!$this->obj->isNew())
 620          {
 621              <?php echo $table->getPhpName() ?>NodePeer::deleteNodeSubTree($this->getNodePath(), Param::set($con));
 622          }
 623          
 624          if ($parentNode =& $this->getParentNode(true, Param::set($con)))
 625          {
 626              $parentNode->detachChildNode($this);
 627              $parentNode->shiftChildNodes(-1, $this->getNodeIndex()+1, $con);
 628          }
 629          
 630          $this->setDeleted(true);
 631      }
 632  
 633      /**
 634       * Compares the object wrapped by this node with that of another node. Use 
 635       * this instead of equality operators to prevent recursive dependency 
 636       * errors.
 637       *
 638       * @param <?php echo $table->getPhpName() ?>Node Node to compare.
 639       * @param boolean True if strict comparison should be used.
 640       * @return boolean
 641       */
 642      function equals(&$node, $strict = false)
 643      {
 644          if ($strict)
 645              return ($this->obj === $node->obj);
 646          else
 647              return ($this->obj == $node->obj);
 648      }
 649  
 650      /**
 651       * This method is used internally when constructing the tree structure 
 652       * from the database. To set the parent of a node, you should call 
 653       * addChildNode() on the parent.
 654       * @param <?php echo $table->getPhpName() ?>Node Parent node to attach.
 655       * @return void
 656       * @throws PropelException
 657       */
 658      function attachParentNode(&$node)
 659      {
 660          if (!$node->hasChildNode($this, true))
 661              return new PropelException(PROPEL_ERROR, 'Failed to attach parent node for non-child.');
 662  
 663          $this->parentNode =& $node;
 664      }
 665  
 666      /**
 667       * This method is used internally when constructing the tree structure 
 668       * from the database. To add a child to a node you should call the 
 669       * addChildNode() method instead. 
 670       *
 671       * @param <?php echo $table->getPhpName() ?>Node Child node to attach.
 672       * @return void
 673       * @throws PropelException
 674       */
 675      function attachChildNode(&$node)
 676      {
 677          if ($this->hasChildNode($node))
 678              return new PropelException(PROPEL_ERROR, 'Failed to attach child node. Node already exists.');
 679          
 680          if ($this->obj->isDeleted() || $node->obj->isDeleted())
 681              return new PropelException(PROPEL_ERROR, 'Failed to attach node in deleted state.');
 682              
 683          if ($this->obj->isNew() && !$node->obj->isNew())
 684              return new PropelException(PROPEL_ERROR, 'Failed to attach non-new child to new node.');
 685  
 686          if (!$this->obj->isNew() && $node->obj->isNew())
 687              return new PropelException(PROPEL_ERROR, 'Failed to attach new child to non-new node.');
 688  
 689          if ($this->getNodePath() . <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP() . $node->getNodeIndex() != $node->getNodePath())
 690              return new PropelException(PROPEL_ERROR, 'Failed to attach child node. Node path mismatch.');
 691          
 692          $this->childNodes[$node->getNodeIndex()] =& $node;
 693          ksort($this->childNodes);
 694          
 695          $node->attachParentNode($this);
 696      }
 697  
 698      /**
 699       * This method is used internally when deleting nodes. It is used to break
 700       * the link to this node's parent.
 701       * @param <?php echo $table->getPhpName() ?>Node Parent node to detach from.
 702       * @return void
 703       * @throws PropelException
 704       */
 705      function detachParentNode(&$node)
 706      {
 707          if (!$node->hasChildNode($this, true))
 708              return new PropelException(PROPEL_ERROR, 'Failed to detach parent node from non-child.');
 709  
 710          unset($node->childNodes[$this->getNodeIndex()]);
 711          $this->parentNode = null;
 712      }
 713      
 714      /**
 715       * This method is used internally when deleting nodes. It is used to break
 716       * the link to this between this node and the specified child.
 717       * @param <?php echo $table->getPhpName() ?>Node Child node to detach.
 718       * @return void
 719       * @throws PropelException
 720       */
 721      function detachChildNode(&$node)
 722      {
 723          if (!$this->hasChildNode($node, true))
 724              return new PropelException(PROPEL_ERROR, 'Failed to detach non-existent child node.');
 725  
 726          $this->childNodes[$node->getNodeIndex()] = & Propel::null();
 727          unset($this->childNodes[$node->getNodeIndex()]);
 728          $node->parentNode =& Propel::null();
 729      }
 730      
 731      /**
 732       * Shifts child nodes in the specified direction and offset index. This 
 733       * method assumes that there is already space available in the 
 734       * direction/offset indicated. 
 735       *
 736       * @param int Direction/# spaces to shift. 1=leftshift, 1=rightshift
 737       * @param int Node index to start shift at.
 738       * @param Connection The connection to be used.
 739       * @return void
 740       * @throws PropelException
 741       */
 742      function shiftChildNodes($direction, $offsetIdx, &$con)
 743      {
 744          if ($this->obj->isDeleted())
 745              return new PropelException(PROPEL_ERROR, 'Cannot shift nodes for deleted object');
 746  
 747          $lastNode =& $this->getLastChildNode(true, Param::set($con));
 748          $lastIdx = ($lastNode !== null ? $lastNode->getNodeIndex() : 0);
 749  
 750          if ($lastNode === null || $offsetIdx > $lastIdx)
 751              return;
 752  
 753          if ($con === null)
 754              $con =& Propel::getConnection(<?php echo $table->getPhpName() ?>Peer::DATABASE_NAME());
 755  
 756          if (Propel::isError($con)) {
 757                  return $con;
 758          }
 759  
 760          if (!$this->obj->isNew())
 761          {
 762              // Shift nodes in database.
 763  
 764              $e = $con->begin();
 765              if (Creole::isError($e)) { $con->rollback(); return new PropelException(PROPEL_ERROR_DB, $e); }
 766  
 767              $n = $lastIdx - $offsetIdx + 1;
 768              $i = $direction < 1 ? $offsetIdx : $lastIdx;
 769  
 770              while ($n--)
 771              {
 772                  $srcPath = $this->getNodePath() . <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP() . $i;              // 1.2.2
 773                  $dstPath = $this->getNodePath() . <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP() . ($i+$direction); // 1.2.3
 774  
 775                  $e = <?php echo $table->getPhpName() ?>NodePeer::moveNodeSubTree($srcPath, $dstPath, Param::set($con));
 776                  if (Propel::isError($e)) { $con->rollback(); return $e; }
 777  
 778                  $i -= $direction;
 779              }
 780  
 781              $e = $con->commit();
 782              if (Creole::isError($e)) { $con->rollback(); return new PropelException(PROPEL_ERROR_DB, $e); }
 783          }
 784          
 785          // Shift the in-memory objects.
 786          
 787          $n = $lastIdx - $offsetIdx + 1;
 788          $i = $direction < 1 ? $offsetIdx : $lastIdx;
 789  
 790          while ($n--)
 791          {
 792              if (isset($this->childNodes[$i]))
 793              {
 794                  $srcPath = $this->getNodePath() . <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP() . $i;              // 1.2.2
 795                  $dstPath = $this->getNodePath() . <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP() . ($i+$direction); // 1.2.3
 796                  
 797                  $this->childNodes[$i+$direction] =& $this->childNodes[$i];
 798                  $this->childNodes[$i+$direction]->adjustNodePath($srcPath, $dstPath);
 799  
 800                  unset($this->childNodes[$i]);
 801              }
 802              
 803              $i -= $direction;
 804          }
 805          
 806          ksort($this->childNodes);
 807      }
 808      
 809      /**
 810       * Inserts the node and its children at the specified childIdx.
 811       *
 812       * @param <?php echo $table->getPhpName() ?>Node Node to insert.
 813       * @param int One-based child index to insert at.
 814       * @param Connection Connection to use.
 815       * @param void
 816       */
 817      function insertNewChildNode(&$node, $childIdx, &$con)
 818      {
 819          if (!$node->obj->isNew())
 820              return new PropelException(PROPEL_ERROR, 'Failed to insert non-new node.');
 821  
 822          $setNodePath = "set" . <?php echo $table->getPhpName() ?>NodePeer::NPATH_PHPNAME();
 823  
 824          $node->obj->$setNodePath($this->getNodePath() . <?php echo $table->getPhpName() ?>NodePeer::NPATH_SEP() . $childIdx);
 825          $node->obj->save(Param::set($con));
 826          
 827          $i = 1;
 828          foreach($node->childNodes as $key => $childNode)
 829              $node->insertNewChildNode($node->childNodes[$key], $i++, $con);
 830      }
 831  
 832      /**
 833       * Adjust new/deleted status of node and all children. 
 834       *
 835       * @param string Status to change ('New' or 'Deleted')
 836       * @param boolean Value for status.
 837       * @return void
 838       */
 839      function adjustStatus($status, $b)
 840      {
 841          $setStatus = 'set' . $status;
 842          
 843          $this->obj->$setStatus($b);
 844          
 845          foreach ($this->childNodes as $key => $childNode)
 846              $this->childNodes[$key]->obj->$setStatus($b);
 847      }
 848      
 849      /**
 850       * Adjust path of node and all children. This is used internally when 
 851       * inserting/moving nodes.
 852       *
 853       * @param string Section of old path to change.
 854       * @param string New section to replace old path with.
 855       * @return void
 856       */
 857      function adjustNodePath($oldBasePath, $newBasePath)
 858      {
 859          $setNodePath = "set" . <?php echo $table->getPhpName() ?>NodePeer::NPATH_PHPNAME();
 860          
 861          $this->obj->$setNodePath($newBasePath . 
 862                                   substr($this->getNodePath(), strlen($oldBasePath)));
 863          $this->obj->resetModified(<?php echo $table->getPhpName() ?>NodePeer::NPATH_COLNAME());
 864  
 865          foreach ($this->childNodes as $key => $childNode)
 866              $this->childNodes[$key]->adjustNodePath($oldBasePath, $newBasePath);
 867      }
 868  
 869  }
 870  
 871  ?>


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7