解析thinkphp的左右值无限分类(2)


      * @access public
      * @param $currentId int
      * @param $nodeName string 新节点名称     
      * @param $targetId int 追加到当前节点下子节点的指定节点后
      * @return bool
      +----------------------------------------------------------
     */   
    public  function addNode($nodeId,$newData,$type=0,$targetId=0)
    {
        if(empty($newData))
        {
            throw_exception('新分类不能为空');
        }
        $currentNode = $this->getNodeById($nodeId);
        switch ($type) {
            case 0:
                $leftNode  = $currentNode[$this->_right]; //新节点的左值为父节点的右值
                $rightNode = $leftNode+1;
                break;
            case 1:
                $leftNode = $currentNode[$this->_left]+1; //新节点的左值为父节点的左值+1
                $rightNode = $leftNode+1;
                break;
            case 2:
                $otherNode = $this->getNodeById($targetId);
                $leftNode = $otherNode[$this->_right]+1;
                $rightNode = $leftNode+1;
            default:
                break;
        }
//         $sql = "UPDATE ".TABLE_NAME." SET ".$this->_right."=".$this->_right."+2 WHERE ".$this->_right." >= ".$leftNode;
//        $sql2 = "UPDATE ".TABLE_NAME." SET ".$this->_left."=".$this->_left."+2 WHERE ".$this->_left.">".$leftNode;
        $this->setInc($this->_right,$this->_right.">=".$leftNode,2); //把所有右值大于新节点左值的节点的右值+2,注意效率
        $this->setInc($this->_left,$this->_left.">".$leftNode,2);   //把所有大于新节点的左值+2
        $newData[$this->_left] = (int)$leftNode;
        $newData[$this->_right] =(int) $rightNode;
        return $this->add($newData);
    }
    /**
         +----------------------------------------------------------
         * 删除节点
         * @access public
         * @param type 操作类型,默认为0删除当前节点下的所有子节点,1为删除包括自身的节点
         * @param $nodeId int 要删除的$this->_id
         * @return bool
         +----------------------------------------------------------
        */    
    public  function rmNode($nodeId,$type =1)
    {
        $currentNode = $this->getNodeById($nodeId);
        if($type == 1) //删除包含自身的节点
        {
            $sql = "DELETE FROM __TABLE__ WHERE ".$this->_left.">= {$currentNode[$this->_left]} AND ".$this->_right."<= {$currentNode[$this->_right]}";
            $childCount = ($this->getChildCount($nodeId)+1)*2; //要更新的值
            $sql2 = "UPDATE  __TABLE__  SET ".$this->_right."=".$this->_right."-".$childCount." WHERE ".$this->_right.">".$currentNode[$this->_right];
            $sql3 = "UPDATE  __TABLE__  SET ".$this->_left."=".$this->_left."-".$childCount." WHERE ".$this->_left.">".$currentNode[$this->_left];
        }
        else //删除当前节点下的所有节点
        {
            $sql ="DELETE FROM __TABLE__ WHERE ".$this->_left."> {$currentNode[$this->_left]} AND ".$this->_right."< {$currentNode[$this->_right]}";
            $childCount = $this->getChildCount($nodeId)*2; //要更新的值
            $sql2 = "UPDATE __TABLE__ SET ".$this->_right."=".$this->_right ."-".$childCount." WHERE ".$this->_right.">=".$currentNode[$this->_right];
            $sql3 = "UPDATE __TABLE__ SET ".$this->_left."=".$this->_left."-".$childCount." WHERE ".$this->_left.">".$currentNode[$this->_left];
        }
         $this->execute($sql); 
         $this->execute($sql2); 
         $this->execute($sql3); 
        return true;
    }
     /**
      +----------------------------------------------------------
      * 修改节点,名称等
      * @access public
      * @param $newData array()必须含有 要修改的$this->_id,k-v必须对齐,如arr['node_name'] = '商品'
      * @return bool
      +----------------------------------------------------------
     */    
    public  function modiNode($newData)
       {
            if(!empty($newData))
            {
                $id = $newData[$this->_id];               
                unset($newData[$this->_id]);
                return $this->save($newData,$this->_id.'='.$id);                              
          }
       }
}
?>


您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/ef4f788dc60a5649f2ba3e3ead984fe0.html