dbnested.php

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 969 行 · 第 1/3 页

PHP
969
字号
        //$this->dbh->commit();        return true;    } // end of function    /**    *   move an entry under a given parent or behind a given entry.    *   If a newPrevId is given the newParentId is dismissed!    *   call it either like this:    *       $tree->move( x , y )    *       to move the element (or entire tree) with the id x    *       under the element with the id y    *   or    *       $tree->move( x , 0 , y );   // ommit the second parameter by setting it to 0    *       to move the element (or entire tree) with the id x    *       behind the element with the id y    *   or    *       $tree->move( array(x1,x2,x3) , ...    *       the first parameter can also be an array of elements that shall be moved    *       the second and third para can be as described above    *   If you are using the Memory_DBnested then this method would be invain,    *   since Memory.php already does the looping through multiple elements, but if    *   Dynamic_DBnested is used we need to do the looping here    *    *   @version    2002/06/08    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      integer     the id(s) of the element(s) that shall be moved    *   @param      integer     the id of the element which will be the new parent    *   @param      integer     if prevId is given the element with the id idToMove    *                           shall be moved _behind_ the element with id=prevId    *                           if it is 0 it will be put at the beginning    *   @return     mixed       true for success, Tree_Error on failure    */    function move( $idsToMove , $newParentId , $newPrevId=0 )    {        settype($idsToMove,'array');        $errors = array();        foreach( $idsToMove as $idToMove )        {            $ret = $this->_move( $idToMove , $newParentId , $newPrevId );            if( PEAR::isError($ret) )                $errors[] = $ret;        }// FIXXME the error in a nicer way, or even better let the throwError method do it!!!        if( sizeof($errors) )        {            return $this->_throwError(serialize($errors),__LINE__);        }        return true;    }    /**    *   this method moves one tree element    *    *   @see        move()    *   @version    2002/04/29    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      integer     the id of the element that shall be moved    *   @param      integer     the id of the element which will be the new parent    *   @param      integer     if prevId is given the element with the id idToMove    *                           shall be moved _behind_ the element with id=prevId    *                           if it is 0 it will be put at the beginning    *   @return     mixed       true for success, Tree_Error on failure    */    function _move( $idToMove , $newParentId , $newPrevId=0 )    {        // do some integrity checks first        if ($newPrevId) {            if ($newPrevId==$idToMove) {            // dont let people move an element behind itself, tell it succeeded, since it already is there :-)                return true;            }            if (PEAR::isError($newPrevious=$this->getElement($newPrevId))) {                return $newPrevious;            }            $newParentId = $newPrevious['parentId'];        } else {            if ($newParentId==0) {                return $this->_throwError( 'no parent id given' , __LINE__ );            }            if ($this->isChildOf($idToMove,$newParentId)) { // if the element shall be moved under one of its children, return false                return $this->_throwError( 'can not move an element under one of its children' , __LINE__ );            }            if ($newParentId==$idToMove) {          // dont do anything to let an element be moved under itself, which is bullshit                return true;            }            if (PEAR::isError($newParent=$this->getElement($newParentId))) { // try to retreive the data of the parent element                return $newParent;            }        }        if (PEAR::isError($element=$this->getElement($idToMove))) { // get the data of the element itself            return $element;        }        $numberOfElements = ($element['right'] - $element['left']+1)/2;        $prevVisited = $newPrevId ? $newPrevious['right'] : $newParent['left'];// FIXXME start transaction        // add the left/right values in the new parent, to have the space to move the new values in        if (PEAR::isError($err=$this->_add( $prevVisited , $numberOfElements ))) {// FIXXME rollback            //$this->dbh->rollback();            return $err;        }        // update the parentId of the element with $idToMove        if (PEAR::isError($err=$this->update($idToMove,array('parentId'=>$newParentId)))) {// FIXXME rollback            //$this->dbh->rollback();            return $err;        }        // update the lefts and rights of those elements that shall be moved        // first get the offset we need to add to the left/right values        // if $newPrevId is given we need to get the right value, otherwise the left        // since the left/right has changed, because we already updated it up there we need to        // get them again, we have to do that anyway, to have the proper new left/right values        if ($newPrevId) {            if (PEAR::isError($temp = $this->getElement( $newPrevId ))) {// FIXXME rollback                //$this->dbh->rollback();                return $temp;            }            $calcWith = $temp['right'];        } else {            if (PEAR::isError($temp=$this->getElement($newParentId))) {// FIXXME rollback                //$this->dbh->rollback();                return $temp;            }            $calcWith = $temp['left'];        }        // get the element that shall be moved again, since the left and right might have changed by the add-call        if (PEAR::isError($element=$this->getElement($idToMove))) {            return $element;        }        $offset = $calcWith - $element['left'];     // calc the offset that the element to move has to the spot where it should go        $offset++;                                  // correct the offset by one, since it needs to go inbetween!        $lName = $this->_getColName('left');        $rName = $this->_getColName('right');        $query = sprintf(   "UPDATE %s SET %s=%s+$offset,%s=%s+$offset WHERE%s %s>%s AND %s<%s",                            $this->table,                            $rName,$rName,                            $lName,$lName,                            $this->_getWhereAddOn(),                            $lName,$element['left']-1,                            $rName,$element['right']+1 );        if (DB::isError($res=$this->dbh->query($query))) {// FIXXME rollback            //$this->dbh->rollback();            return $this->_throwError( $res->getMessage() , __LINE__ );        }        // remove the part of the tree where the element(s) was/were before        if (PEAR::isError($err=$this->_remove($element))) {// FIXXME rollback            //$this->dbh->rollback();            return $err;        }// FIXXME commit all changes        //$this->dbh->commit();        return true;    } // end of function    /**    *   update the tree element given by $id with the values in $newValues    *    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      int     the id of the element to update    *   @param      array   the new values, the index is the col name    *   @return     mixed   either true or an Tree_Error    */    function update($id,$newValues)    {        // jsut to be sure nothing gets screwed up :-)        unset($newValues[$this->_getColName('left')]);        unset($newValues[$this->_getColName('right')]);        unset($newValues[$this->_getColName('parentId')]);        // updating _one_ element in the tree        $values = array();        foreach ($newValues as $key=>$value) {            $values[] = $this->_getColName($key).'='.$this->dbh->quote($value);        }        $query = sprintf(   'UPDATE %s SET %s WHERE%s %s=%s',                            $this->table,                            implode(',',$values),                            $this->_getWhereAddOn(),                            $this->_getColName('id'),                            $id);        if (DB::isError( $res=$this->dbh->query($query))) {// FIXXME raise PEAR error            return $this->_throwError( $res->getMessage() , __LINE__ );        }        return true;    } // end of function    /**    *   copy a subtree/node/... under a new parent or/and behind a given element    *    *    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param    *   @return    */    function copy( $id , $parentId=0 , $prevId=0 )    {        return $this->_throwError( 'copy-method is not implemented yet!' , __LINE__ );        // get element tree        // $this->addTree    } // end of function    /**    *   get the root    *    *   @access     public    *   @version    2002/03/02    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param    *   @return     mixed   either the data of the root element or an Tree_Error    */    function getRoot()    {        $query = sprintf(   'SELECT * FROM %s WHERE%s %s=1',                            $this->table,                            $this->_getWhereAddOn(),                            $this->_getColName('left'));        if( DB::isError( $res = $this->dbh->getRow($query) ) )        {            return $this->_throwError( $res->getMessage() , __LINE__ );        }        return $this->_prepareResult( $res );    } // end of function    /**    *    *    *   @access     public    *   @version    2002/03/02    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param    *   @return     mixed   either the data of the requested element or an Tree_Error    */    function getElement( $id )    {        $query = sprintf(   'SELECT * FROM %s WHERE%s %s=%s',                            $this->table,                            $this->_getWhereAddOn(),                            $this->_getColName('id'),                            $id);        if( DB::isError( $res = $this->dbh->getRow($query) ) )        {            return $this->_throwError( $res->getMessage() , __LINE__ );        }        if( !$res )            return $this->_throwError( "Element with id $id does not exist!" , __LINE__ );        return $this->_prepareResult( $res );    } // end of function    /**    *    *    *   @access     public    *   @version    2002/03/02    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param    *   @return     mixed   either the data of the requested element or an Tree_Error    */    function getChild($id)    {        // subqueries would be cool :-)        $curElement = $this->getElement( $id );        if (PEAR::isError($curElement)) {            return $curElement;        }        $query = sprintf(   'SELECT * FROM %s WHERE%s %s=%s',                            $this->table,                            $this->_getWhereAddOn(),                            $this->_getColName('left'),                            $curElement['left']+1 );        if (DB::isError( $res = $this->dbh->getRow($query))) {            return $this->_throwError( $res->getMessage() , __LINE__ );        }        return $this->_prepareResult( $res );    }    /**    *   gets the path from the element with the given id down    *   to the root    *   the returned array is sorted to start at root    *   for simply walking through and retreiving the path    *    *   @access     public    *   @version    2002/03/02    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param    *   @return     mixed   either the data of the requested elements or an Tree_Error    */    function getPath( $id )    {        // subqueries would be cool :-)        $curElement = $this->getElement( $id );        $query = sprintf(   'SELECT * FROM %s WHERE%s %s<=%s AND %s>=%s ORDER BY %s',                            $this->table,                            $this->_getWhereAddOn(),                            $this->_getColName('left'),                            $curElement['left'],                            $this->_getColName('right'),                            $curElement['right'],

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?