[geeklog-cvs] Geeklog-2/system/Propel/Geeklog-2 Gl2Comment.php,1.1.1.1,1.2

vinny at iowaoutdoors.org vinny at iowaoutdoors.org
Mon Jan 17 10:41:49 EST 2005


Update of /var/cvs/Geeklog-2/system/Propel/Geeklog-2
In directory www:/tmp/cvs-serv4645/d/d

Modified Files:
	Gl2Comment.php 
Log Message:
First draft of Comment class, missing functionality (marked with FIXME)
requires changes in DAO to support UPDATEs.


Index: Gl2Comment.php
===================================================================
RCS file: /var/cvs/Geeklog-2/system/Propel/Geeklog-2/Gl2Comment.php,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -C2 -d -r1.1.1.1 -r1.2
*** Gl2Comment.php	17 Dec 2004 23:35:47 -0000	1.1.1.1
--- Gl2Comment.php	17 Jan 2005 15:41:47 -0000	1.2
***************
*** 15,17 ****
--- 15,225 ----
  class Gl2Comment extends BaseGl2Comment {
  
+     /**
+      * comment parent id.
+      * @var int
+      */
+     protected $pid;
+ 
+     /**
+      * Get the parent id of $this
+      * 
+      * @copyright 2005 Vincent Furia
+      * @author Vincent Furia <vfuria at gmail.com>
+      * @return int parent id
+      */
+     public function getPid() {
+         if ( empty($this->pid) ) {
+             require_once 'DataAccess/DAO.php';
+             $dao = &Geeklog_DAO::singleton();
+             $comments = $dao->find( 'GetCommentPid', 
+                     array($this->getItemId(), $this->getLeftIndex(), $this->getRightIndex()) );
+ 
+             // Check that a usable result was found, otherwise assume that the $this is a
+             // top level comment (pid = 0)
+             if ( ( is_array($comments) ) && ( $comments[0] instanceof Gl2Comment ) ) {
+                 $this->pid = $comments[0]->getCommentId();
+             } else {
+                 $this->pid = 0;
+             }
+         }
+ 
+         return $this->pid;
+     }
+ 
+     /**
+      * Set the parent id of $this
+      *
+      * This method essentially moves a comment around in the hierarchial order.  It shouldn't
+      * be used often (if at all) when $this->isNew !== true
+      *
+      * @copyright 2005 Vincent Furia
+      * @author Vincent Furia <vfuria at gmail.com>
+      * @param int $pid parent id 
+      */
+     public function setPid($pid) {
+         if ( $this->isNew() ) {
+             $this->pid = $pid;
+         } else if ( $pid !== $this->pid ) {
+             $this->pid = $pid;
+ 
+             // Update the hierarchial tree to reflect the new pid
+             $this->treeDelete();
+             $this->treeAdd( $this->pid );
+         }
+     }
+ 
+     /**
+      * Returns an object containing the parent comment of $this
+      *
+      * @copyright 2005 Vincent Furia
+      * @author Vincent Furia <vfuria at gmail.com>
+      * @return Gl2Comment parent comment object
+      */
+     public function getParent() {
+         require_once 'Geeklog-2/Gl2CommentPeer';
+ 
+         $pid = $this->getPid();
+         if ( $pid > 0 ) {
+             return Gl2CommentPeer::retrieveByPK($pid);
+         }
+ 
+         return 0;
+     }
+ 
+     /**
+      * Sets the parent of $this from a comment object
+      *
+      * @copyright 2005 Vincent Furia
+      * @author Vincent Furia <vfuria at gmail.com>
+      * @param Gl2Comment $parent parent comment object
+      */
+     public function setParent($parent) {
+         if ( $parent instanceof Gl2Comment && !$parent->isNew() ) {
+             $this->setPid($parent->getCommentId());
+         } else {
+             throw new PropelException('supplied object to setParent not of type Gl2Comment '
+                                     . 'or isNew');
+         }
+     }
+ 
+     /**
+      * Overriden to protect leftIndex
+      *
+      * @copyright 2005 Vincent Furia
+      * @author Vincent Furia <vfuria at gmail.com>
+      * @param int $v value to set
+      */
+     protected function setLeftIndex($v) {
+         parent::setLeftIndex($v);
+     }
+ 
+     /**
+      * Overriden to protect rightIndex
+      *
+      * @copyright 2005 Vincent Furia
+      * @author Vincent Furia <vfuria at gmail.com>
+      * @param int $v value to set
+      */
+     protected function setRightIndex($v) {
+         parent::setRightIndex($v);
+     }
+ 
+     /**
+      * Insert $this into the hierarchial tree represented by the leftIndex and rightIndex
+      * columns
+      *
+      * @copyright 2005 Vincent Furia
+      * @author Vincent Furia <vfuria at gmail.com>
+      * @param int pid comment to insert $this under
+      */
+     protected function treeInsert($pid = null) {
+ 
+         if ( empty($pid) ) {
+             $this->pid = $pid; 
+         }
+ 
+         // Default to a top level comment
+         if ( empty($this->pid) ) {
+             $this->pid = 0;
+         }
+ 
+         require_once 'DataAccess/DAO.php';
+         $dao = &Geeklog_DAO::singleton();
+         $comments = $dao->find( 'GetCommentPid', 
+                 array($this->getItemId(), $this->getLeftIndex(), $this->getRightIndex()) );
+ 
+         //FIXME: Complete the necessary steps to insert $this in the tree
+         if ( ( is_array($comments) ) && ( $comments[0] instanceof Gl2Comment ) && 
+              ( $comments[0]->getRightIndex() > 0 ) ) {
+             // For comments with pid != 0
+             // 1. Get the parent comment's rightIndex (pid.right)
+             // 2. Add 2 to every rightIndex and leftIndex >= pid.right for comments associated with
+             //    the current ItemId
+             // 3. Set $this->leftIndex = pid.right and $this->rightIndex = pid.right + 1
+         } else {
+             // For commentw with pid == 0
+             // 1. Get the max(rightIndex) for the comments associated with the current ItemId
+             //    Use "0" if there are no other comments associated with the current ItemId
+             // 2. Set $this->leftIndex = max(rightIndex) + 1 and 
+             //        $this->rightIndex = max(rightIndex) + 2
+         }
+     }
+ 
+     /**
+      * Removes $this from the hierarchial tree represented by the leftIndex and rightIndex
+      * columns
+      *
+      * @copyright 2005 Vincent Furia
+      * @author Vincent Furia <vfuria at gmail.com>
+      */
+     protected function treeDelete() {
+         //FIXME: Complete the necessary steps to remove $this from the tree
+         // 1. Get the rightIndex for the $this
+         // 2. Subtract 2 from every rightIndex and leftIndex >= this.rightIndex for comments 
+         //    associated with the current ItemId
+         // 3. Set $this->leftIndex = $this->rightIndex = 0
+     }
+ 
+     /**
+      * Save the comment, overrideen to maintain comment ordering
+      * 
+      * @copyright 2005 Vincent Furia
+      * @author Vincent Furia <vfuria at gmail.com>
+      * @param Connection $con Creole DB Connection
+      */
+     public function save($pid = null, $con = null) {
+ 
+         if ( empty($pid) ) {
+             $this->pid = $pid; 
+         }
+ 
+         // Default to a top level comment
+         if ( empty($this->pid) ) {
+             $this->pid = 0;
+         }
+         
+         // For newly inserted objects, insert into the hierarchial tree
+         if ( $this->isNew() ) {
+             $this->treeInsert($this->pid);
+         }
+ 
+         parent::save($con);
+     }
+ 
+     /**
+      * Save the comment, overrideen to maintain comment ordering
+      * 
+      * @copyright 2005 Vincent Furia
+      * @author Vincent Furia <vfuria at gmail.com>
+      * @param Connection $con Creole DB Connection
+      */
+     public function delete($con = null) {
+ 
+         // Removed this comment from the hierarchial tree, required to
+         // maintain tree integrity before the comment is deleted
+         $this->treeDelete();        
+ 
+         parent::delete($con);
+     }
+ 
  }




More information about the geeklog-cvs mailing list