>php教程 >php手册 >thinkphp实现左右值增、改、删操作类

thinkphp实现左右值增、改、删操作类

WBOY
WBOY원래의
2016-06-07 11:43:331277검색

新手,写了基本功能,未写排序等相关操作;求高手改进,
数据库:
thinkphp实现左右值增、改、删操作类
类库文件:
Category.class.php<?php <br /> /**<br> 应用基于thinkphp的左右值无限分类<br> **/<br> class Category<br> {<br>     //传入实化的对象【M('表名')】<br>     private $objCategory;<br>     //基础节点ID号<br>     public $intCurrentId;<br>     //设置制表符样式<br>     private $arrTabsStyle = array(<br>                             'indent' => '   ',<br>                             'process' => '├ ',<br>                             'end'=>'└ '<br>                         );<br>     //构造函数初始化<br>     public function __construct($objCategory)<br>     {<br>         $this->objCategory = $objCategory;<br>     }<br>     //验证传入ID【大于0的数字】<br>     private function checkFun($intId)<br>     {<br>         //$intId优先验证<br>         if(isset($intId))<br>         {<br>             $this->intCurrentId = $intId;<br>             return true;<br>         }<br>         //如果$this->intCurrentId 已设置,验证<br>         else<br>         {<br>             if(isset($this->intCurrentId))<br>             {<br>                 return true;<br>             }<br>             else<br>             {<br>                 return false;<br>             }<br>         }<br>     }<br>     //根据ID号获取当前节点左右值<br>     private function setCurrentData($intId)<br>     {<br>         if(false == $this->checkFun($intId))<br>         {<br>             return false;<br>         }<br>         $map['id'] = $this->intCurrentId;<br>         return $this->objCategory->field(array('lft','rgt'))->where($map)->find();<br>     }<br>     /*<br>     *作用:<br>         设置输出列表数据的制表符样式<br>     *参数:<br>         $key:arrTabsStyle的KEY<br>         $value:arrTabsStyle的值<br>     */<br>     public function setTabStyle($key, $value = '')<br>     {<br>         if(isset($this->arrTabsStyle[$key]))<br>         {<br>             $this->arrTabsStyle[$key] = $value;<br>         }<br>     }<br>     /*<br>     *作用:<br>         根据ID号获取当前节点数据<br>     *参数:<br>         $intId:可设置的,需读取节点ID号<br>     */<br>     public function getCurrentData($intId)<br>     {<br>         if(false == $this->checkFun($intId))<br>         {<br>             return false;<br>         }<br>         $map['id'] = $this->intCurrentId;<br>         return $this->objCategory->field(array('id','title'))->where($map)->find();<br>     }<br>     /*<br>     *作用:<br>         获取当前节点的父节点数据<br>     *参数:<br>         $intId:需要读取节点的ID<br>     */<br>     public function getParentCategoryData($intId)<br>     {<br>         $arrRoot = $this->setCurrentData($intId);<br>         if($arrRoot)<br>         {<br>             $map['lft'] = array('LT', $arrRoot['lft']);<br>             $map['rgt'] = array('GT', $arrRoot['rgt']);<br>             return $this->objCategory->where($map)->find();<br>         }<br>         else<br>         {<br>             return false;<br>         }<br>     }<br>     /*<br>     *作用:<br>         获取ID下节点列表<br>     *参数:<br>         $intId:需要读取节点的父ID<br>         $intLevel:目录等级默认到100级<br>     */<br>     public function getCategoryList($intId = 1, $intLevel = 100)<br>     {<br>         //获取选定节点左右值,得出取值区间<br>         $arrRoot = $this->setCurrentData($intId);<br>         if($arrRoot)<br>         {<br>             //读取数据库符合条件的数据<br>             $map['lft'] = array('BETWEEN', array($arrRoot['lft'], $arrRoot['rgt']));<br>             $arrChildList = $this->objCategory->where($map)->order('lft')->select();<br>             //return $arrChildList;<br>             //对取出数据进行格式化<br>             $arrRight = array();<br>             foreach($arrChildList as $v)<br>             {<br>                 <br>                 if(count($arrRight))<br>                 {<br>                     while ($arrRight[count($arrRight) - 1]                      {<br>                         array_pop($arrRight);<br>                     }<br>                 }<br>                 //设置读取目录等级<br>                 if($intLevel > count($arrRight))<br>                 {<br>                     $title = $v['title'];<br>                     //设置输出时的样式<br>                     if(count($arrRight))<br>                     {<br>                         $title = $this->arrTabsStyle['process'].$title;<br>                     }<br>                     $title = str_repeat($this->arrTabsStyle['indent'], count($arrRight)).$title;<br>                     $returnCategoryList[] = array('id'=>$v['id'],'title'=>$title,'lft'=>$v['lft'],'rgt'=>$v['rgt']);<br>                     $arrRight[]  = $v['rgt'];<br>                 }<br>             }<br>             return $returnCategoryList;<br>         }<br>         return false;<br>     }<br>     /*<br>     *作用:<br>         获取节点的子节点数<br>     *参数:<br>         $intId:需要读取节点的父ID<br>     */<br>     public function getCategoryCount($intId)<br>     {<br>         $arrRoot = $this->setCurrentData($intId);<br>         return ($arrRoot['rgt'] - $arrRoot['lft'] - 1) / 2;<br>     }<br>     /*<br>     *作用:<br>         添加节点<br>     *参数:<br>         $bolType:true添加到节点前面,false添加到节点尾部<br>         $intId:添加到的父节点<br>     */<br>     public function insertCategory($bolType = false, $intPid)<br>     {<br>         $data = I('param.');<br>         if(!isset($intPid))<br>         {<br>             $intPid = $data['pid'];<br>         }<br>         $arrRoot = $this->setCurrentData($intPid);<br>         if($arrRoot)<br>         {<br>             if($bolType)<br>             //true添加到节点前面<br>             {<br>                 $this->objCategory->where('rgt>'.$arrRoot['lft'])->setInc('rgt',2);<br>                 $this->objCategory->where('lft>'.$arrRoot['lft'])->setInc('lft',2);<br>                 //设置当前节点的左右值<br>                 $data['lft'] = $arrRoot['lft'] + 1;<br>                 $data['rgt'] = $arrRoot['lft'] + 2;<br>             }<br>             else<br>             //false添加到节点尾部<br>             {<br>                 $this->objCategory->where('rgt>='.$arrRoot['rgt'])->setInc('rgt',2);<br>                 $this->objCategory->where('lft>'.$arrRoot['rgt'])->setInc('lft',2);<br>                 $data['lft'] = $arrRoot['rgt'];<br>                 $data['rgt'] = $arrRoot['rgt'] + 1;<br>             }<br>             return $this->objCategory->add($data);<br>         }<br>         else<br>         {<br>             return false;<br>         }<br>     }<br>     /*<br>     *作用:<br>         删除节点<br>     *参数:<br>         $intId:被删除的节点ID<br>     */<br>     public function deleteCategory($intId)<br>     {<br>         $arrRoot = $this->setCurrentData($intId);<br>         if($arrRoot)<br>         {<br>             $ints = $arrRoot['rgt'] - $arrRoot['lft'] + 1;<br>             $map['lft'] = array('BETWEEN', array($arrRoot['lft'], $arrRoot['rgt']));<br>             $this->objCategory->where($map)->delete();<br>             $this->objCategory->where('lft>'.$arrRoot['rgt'])->setDec('lft',$ints);<br>             $this->objCategory->where('rgt>'.$arrRoot['rgt'])->setDec('rgt',$ints);<br>             return true;<br>         }<br>         else<br>         {<br>             return false;<br>         }<br>     }<br>     /*<br>     *作用:<br>         更新节点<br>     *参数:<br>         $intId:被删除的节点ID<br>     */<br>     public function updateCategory()<br>     {<br>         //读取POST数据存入数组<br>         $data = I('param.');<br>         //父ID等于子ID,直接跳出<br>         if($data['pid'] == $data['id']){return false;}<br>         //post.pid和当前父post.old相等说明未改变目录,不更新左右值<br>         if($data['pid'] !== $data['oldpid'])<br>         {<br> /**********************************【读取所需的相关值】********************************/<br>             //获取新的父节点的数据<br>             $arrParent = $this->setCurrentData($data['pid']);<br>             //取当前节点的数据<br>             $arrCurrent = $this->setCurrentData($data['id']);<br> /* 任务:删除节点 */<br> /**********************************【A-1:隔离数据】************************************/<br>             //将需要调整位置的左右值+100000<br>             $map['lft'] = array(<br>                         array('EGT', $arrCurrent['lft']),<br>                         array('ELT', $arrCurrent['rgt'])<br>                     );<br>             $this->objCategory->where($map)->setInc('lft',100000);<br>             //因为左值已更新,所以条件变化+100000<br>             $map['lft'] = array(<br>                         array('EGT', $arrCurrent['lft'] + 100000),<br>                         array('ELT', $arrCurrent['rgt'] + 100000)<br>                     );<br>             $this->objCategory->where($map)->setInc('rgt',100000);<br>             unset($map);<br> /**********************************【A-2:更新正常节点值】******************************/<br>             //获取隔离节点后续更新的步长值<br>             $intStep = $arrCurrent['rgt'] - $arrCurrent['lft'] + 1;<br>             //更新节点左右值<br>             $map['lft'] = array(<br>                         array('GT', $arrCurrent['rgt']),<br>                         array('LT', 100000)<br>                     );<br>             $this->objCategory->where($map)->setDec('lft',$intStep);<br>             unset($map);<br>             $map['rgt'] = array(<br>                         array('GT', $arrCurrent['rgt']),<br>                         array('LT', 100000)<br>                     );<br>             $this->objCategory->where($map)->setDec('rgt',$intStep);<br>             unset($map);<br> /* 完成:删除节点 */<br> /* 任务:更新节点 */<br> /**********************************【B-1:新父节目点提供下级节点的空间】****************/<br>             $map['lft'] = array(<br>                         array('GT', $arrParent['lft']),<br>                         array('LT', 100000)<br>                     );<br>             $this->objCategory->where($map)->setInc('lft',$intStep);<br>             unset($map);<br>             $map['rgt'] = array(<br>                         array('GT', $arrParent['lft']),<br>                         array('LT', 100000)<br>                     );<br>             $this->objCategory->where($map)->setInc('rgt',$intStep);<br>             unset($map);<br> /**********************************【B-2:将节点放入指定下级的空间】********************/<br>             //获取隔离节点后续更新的步长值<br>             $intStep = 100000 + ($arrCurrent['lft'] - ($arrParent['lft'] + 1));<br>             //更新左右值大于父节点左值的节点的左右值<br>             $map['lft'] = array(<br>                         array('EGT', $arrCurrent['lft'] + 100000),<br>                         array('ELT', $arrCurrent['rgt'] + 100000)<br>                     );<br>             $this->objCategory->where($map)->setDec('lft',$intStep);<br>             unset($map);<br>             $map['rgt'] = array(<br>                         array('EGT', $arrCurrent['lft'] + 100000),<br>                         array('ELT', $arrCurrent['rgt'] + 100000)<br>                     );<br>             $this->objCategory->where($map)->setDec('rgt',$intStep);<br>         }<br>         return $this->objCategory->where('id='.$data['id'])->setField('title', $data['title']);<br>     }<br> }<br> ?>使用:<?php <br /> class CategoryAction extends CommonAction {<br>     private $objCG;<br>     public function _initialize(){<br>         //导入分类库<br>         import('@.ORG.Util.Category');<br>         $this->objCG = new Category(M('CommonCategory'));<br>     }<br>     //目录列表<br>     public function index($id = 1){<br>         $this->cateorylist = $this->objCG->getCategoryList($id);<br>         $this->display();<br>     }<br>     //添加目录<br>     public function add($id=1){<br>         $this->cateorylist = $this->objCG->getCategoryList($id);<br>         $this->display();<br>     }<br>     //编辑目录<br>     public function edit($id){<br>         if (!empty($id)){<br>             //输出所有的节点<br>             $this->cateorylist = $this->objCG->getCategoryList(1);<br>             //读取当前节点数据<br>             $vo = $this->objCG->getCurrentData($id);<br>             if ($vo){<br>                 //读取当前节点的父节点数据<br>                 $arrParent = $this->objCG->getParentCategoryData($id);<br>                 $vo['pid'] = $arrParent['id'];<br>                 $this->assign('vo',$vo);<br>                 $this->display();<br>             }else{<br>                 $this->error('数据不存在!');<br>             }<br>         }else{<br>             $this->error('数据不存在!');<br>         }<br>     }<br>     //添加目录:操作<br>     public function insert(){<br>         $list = $this->objCG->insertCategory();<br>         if ($list !== false)<br>         {<br>             $this->success('数据保存成功!');<br>         }<br>         else<br>         {<br>             $this->error('数据写入错误!');<br>         }<br>     }<br>     public function delete($id){<br>         if (!empty($id))<br>         {<br>             $result = $this->objCG->deleteCategory($id);<br>             if ($result)<br>             {<br>                 $this->success('删除成功!');<br>             }<br>             else<br>             {<br>                 $this->error('删除出错!');<br>             }<br>         }<br>         else<br>         {<br>             $this->error('ID错误!');<br>         }<br>     }<br>     public function update(){<br>         $list = $this->objCG->updateCategory();<br>         if ($list !== false)<br>         {<br>             $this->success('更新成功!');<br>         }<br>         else<br>         {<br>             $this->error("操作失败!");<br>         }<br>     }<br> }

AD:真正免费,域名+虚机+企业邮箱=0元

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.