--- nfo/php/libs/net.php.pear/Tree/Common.php 2003/02/27 16:49:56 1.1 +++ nfo/php/libs/net.php.pear/Tree/Common.php 2004/07/07 02:49:19 1.2 @@ -16,8 +16,7 @@ // | Authors: Wolfram Kriesing | // +----------------------------------------------------------------------+ // -// Id: Common.php,v 1.17 2003/01/28 19:18:37 cain Exp -// $Id: Common.php,v 1.1 2003/02/27 16:49:56 joko Exp $ +// $Id: Common.php,v 1.2 2004/07/07 02:49:19 joko Exp $ require_once('Tree/OptionsDB.php'); @@ -68,18 +67,20 @@ * @access public * @author Wolfram Kriesing * @param integer $id ID of the element that the children shall be retreived for + * @param integer how many levels deep into the tree * @return mixed an array of all the ids of the children of the element with id=$id, * or false if there are no children */ - function getChildrenIds( $id ) + function getChildrenIds($id,$levels=1) { - if( !($children = $this->getChildren( $id )) ) // returns false if no children exist + if (!($children = $this->getChildren($id,$levels))) { // returns false if no children exist return array(); // return an empty array, if you want to know if there are children, use hasChildren + } - if( $children && sizeof($children) ) - { - foreach( $children as $aChild ) + if ($children && sizeof($children)) { + foreach ($children as $aChild) { $childrenIds[] = $aChild['id']; + } } return $childrenIds; @@ -95,11 +96,11 @@ * @return mixed an array of all the children of the element with id=$id, * or false if there are no children */ - function getAllChildren( $id ) +// FIXXXME remove this method and replace it by getChildren($id,0) + function getAllChildren($id) { $retChildren = false; - if( $children = $this->hasChildren( $id ) ) - { + if ($children = $this->hasChildren($id)) { $retChildren = $this->_getAllChildren( $id ); } return $retChildren; @@ -116,13 +117,11 @@ * @return mixed an array of all the ids of the children of the element with id=$id, * or false if there are no children */ - function &_getAllChildren( $id ) + function &_getAllChildren($id) { $retChildren = array(); - if( $children = $this->getChildren( $id ) ) - { - foreach( $children as $key=>$aChild ) - { + if ($children = $this->getChildren($id)) { + foreach ($children as $key=>$aChild) { $retChildren[] = &$children[$key]; $retChildren = array_merge($retChildren,$this->_getAllChildren( $aChild['id'] )); } @@ -291,14 +290,32 @@ * @access public * @author Wolfram Kriesing * @param mixed $id the id of the node to get the path for + * @param integer If offset is positive, the sequence will + * start at that offset in the array . If + * offset is negative, the sequence will start that far from the end of the array . + * @param integer If length is given and is positive, then + * the sequence will have that many elements in it. If + * length is given and is negative then the + * sequence will stop that many elements from the end of the + * array. If it is omitted, then the sequence will have everything + * from offset up until the end of the array. * @return array this array contains all elements from the root to the element given by the id * */ - function getPathAsString( $id , $seperator='/' ) + function getPathAsString( $id , $seperator='/' , $offset=0 , $length=0 ) { $path = $this->getPath($id); - foreach( $path as $aNode ) + foreach ($path as $aNode) { $pathArray[] = $aNode['name']; + } + + if ($offset) { + if ($length) { + $pathArray = array_slice($pathArray,$offset,$length); + } else { + $pathArray = array_slice($pathArray,$offset); + } + } $pathString = ''; if( sizeof($pathArray) ) @@ -364,6 +381,18 @@ return $this->_throwError( 'not implemented, at least not overwritten the abstract declaration' , __LINE__ ); } // end of function + /** + * return the maximum depth of the tree + * + * @version 2003/02/25 + * @access public + * @author Wolfram Kriesing + * @return int the depth of the tree + */ + function getDepth() + { + return $this->_treeDepth; + } // end of function