THINKPHP3.2.3含有树形结构的无限分类的实现
程序员文章站
2023-12-26 20:44:51
...
THINKPHP3.2.3含有树形结构的无限分类的实现
<?php namespace CommonHelper; class Category { //一维数组 static public function toLevel($cate, $delimiter = '———', $parent_id = 0, $level = 0) { $arr = array(); foreach ($cate as $v) { if ($v['parent_id'] == $parent_id) { $v['level'] = $level + 1; $v['delimiter'] = str_repeat($delimiter, $level); $arr[] = $v; $arr = array_merge($arr, self::toLevel($cate, $delimiter, $v['id'], $v['level'])); } } return $arr; } //组成多维数组 static public function toLayer($cate, $name = 'child', $pid = 0){ $arr = array(); foreach ($cate as $v) { if ($v['pid'] == $pid) { $v[$name] = self::toLayer($cate, $name, $v['id']); $arr[] = $v; } } return $arr; } //一维数组(同模型)(model = tablename相同),删除其他模型的分类 static public function getLevelOfModel($cate, $tablename = 'article') { $arr = array(); foreach ($cate as $v) { if ($v['tablename'] == $tablename) { $arr[] = $v; } } return $arr; } //一维数组(同模型)(modelid),删除其他模型的分类 static public function getLevelOfModelId($cate, $modelid = 0) { $arr = array(); foreach ($cate as $v) { if ($v['modelid'] == $modelid) { $arr[] = $v; } } return $arr; } //传递一个子分类ID返回他的所有父级分类 static public function getParents($cate, $id) { $arr = array(); foreach ($cate as $v) { if ($v['id'] == $id) { $arr[] = $v; $arr = array_merge(self::getParents($cate, $v['pid']), $arr); } } return $arr; } //传递一个子分类ID返回他的同级分类 static public function getSameCate($cate, $id) { $arr = array(); $self = self::getSelf($cate, $id); if (empty($self)) { return $arr; } foreach ($cate as $v) { if ($v['id'] == $self['pid']) { $arr[] = $v; } } return $arr; } //判断分类是否有子分类,返回false,true static public function hasChild($cate, $id) { $arr = false; foreach ($cate as $v) { if ($v['pid'] == $id) { $arr = true; return $arr; } } return $arr; } //传递一个父级分类ID返回所有子分类ID /** *@param $cate 全部分类数组 *@param $pid 父级ID *@param $flag 是否包括父级自己的ID,默认不包括 **/ static public function getChildsId($cate, $pid, $flag = 0) { $arr = array(); if ($flag) { $arr[] = $pid; } foreach ($cate as $v) { if ($v['pid'] == $pid) { $arr[] = $v['id']; $arr = array_merge($arr , self::getChildsId($cate, $v['id'])); } } return $arr; } //传递一个父级分类ID返回所有子级分类 static public function getChilds($cate, $pid) { $arr = array(); foreach ($cate as $v) { if ($v['pid'] == $pid) { $arr[] = $v; $arr = array_merge($arr, self::getChilds($cate, $v['id'])); } } return $arr; } //传递一个分类ID返回该分类相当信息 static public function getSelf($cate, $id) { $arr = array(); foreach ($cate as $v) { if ($v['id'] == $id) { $arr = $v; return $arr; } } return $arr; } //传递一个分类ID返回该分类相当信息 static public function getSelfByEName($cate, $ename) { $arr = array(); foreach ($cate as $v) { if ($v['ename'] == $ename) { $arr = $v; return $arr; } } return $arr; } } ?> <?php namespace AdminController; use ThinkController; use CommonHelperCategory; class CategoryController extends AuthController { //栏目列表页 public function index(){ $category_list = M('category')->order('sort desc')->select(); $category_list = Category::toLevel($category_list, ' ',0); $this->assign('category_list', $category_list); $this->display('category_list'); } //增加 栏目页面 public function add(){ $parent_id = I('parent_id', 0); $parent_category_info = M('Category')->where(array('id' => $parent_id))->find(); if($parent_category_info){ $this->assign('parent_id', $parent_id); $this->assign('parent_category_info', $parent_category_info); } $model_list = M('Model')->where(array('status' => 1))->order('sort asc')->select(); $category_list = M('category')->order('sort desc')->select(); $category_list = Category::toLevel($category_list, '---',0); $this->assign('category_list', $category_list); $this->assign('action', 'add'); $this->assign('model_list', $model_list); $this->display('category_add'); } //修改栏目 public function edit(){ $category_id = I('id'); $category_info = M('Category')->find($category_id); $model_list = M('Model')->order('sort desc')->select(); $category_list = M('category')->order('sort desc')->select(); $category_list = Category::toLevel($category_list, '---',0); $this->assign('category_list', $category_list); $this->assign('action', 'edit'); $this->assign('category_id', $category_id); $this->assign('category_info', $category_info); $this->assign('model_list', $model_list); $this->display('category_add'); } //保存栏目 public function save(){ $Category = D("Category"); if (!$Category->create()){ //验证没有通过 输出错误提示信息 $errormsg = $Category->getError(); $this->error($errormsg); }else{ // 验证通过进行其他数据操作 $id = I('id', 0); $data['title'] = I('title', ''); $data['flag'] = I('flag', ''); $data['parent_id'] = I('parent_id', 0); $data['content'] = I('content', ''); $data['sort'] = I('sort', 0); $data['status'] = I('status', 1); $data['is_show'] = I('is_show', 1); $data['has_cover'] = I('has_cover', 1); $data['cover'] = I('cover', ''); $category_info = $Category->find($id); if($category_info){ //更新操作 $data['update_time'] = time(); $result = $Category->where(array('id' => $id))->save($data); if(false !== $result || 0 !== $result){ $this->success('栏目更新成功', U('Admin/Category/index')); }else{ $this->error('栏目更新失败'); } }else{ $data['model_id'] = I('model_id', 0); $data['model_flag'] = M('Model')->where(array('id' => I('model_id')))->getField('flag'); $data['add_time'] = time(); //print_r($data);exit; $result = $Category->add($data); if($result){ $this->success('栏目添加成功', U('Admin/Category/index')); }else{ $this->error('栏目添加失败'); } } } } //更新状态 public function change_status() { $id = I('id'); $Category = M('Category'); $category_info = $Category->find($id); if(!$category_info){ $this->ajaxReturn(array('info' => '该栏目不存在')); }else{ if($category_info['status']){ $status = 0; }else{ $status = 1; } $result = $Category->where(array('id' => $id))->setField('status',$status); if(false != $result){ $this->ajaxReturn(array('status' => 'ok', 'info' => '栏目状态更新成功')); }else{ $this->ajaxReturn(array('info' => '栏目状态更新失败')); } } } //保存排序 public function save_sort() { $id = I('id'); $sort = I('sort'); $Category = M('Category'); $result = $Category->where(array('id' => $id))->setField('sort',$sort); if(false !== $result || 0 !== $result){ $this->ajaxReturn(array('status' => 'ok', 'info' => '排序更新成功')); }else{ $this->ajaxReturn(array('info' => '排序更新失败')); } } //删除栏目 public function delete() { $id = I('id'); $Category = M('Category'); $category_info = $Category->find($id); if(!$category_info){ return array('info' => '该模型不存在'); }else if($category_info['is_system']){ return array('info' => '系统模型不允许删除'); }else{ $result = $Category->delete($id); if($result){ $this->ajaxReturn(array('status' => 'ok', 'info' => '栏目删除成功')); }else{ $this->ajaxReturn(array('info' => '栏目删除失败')); } } } }