Category.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\model\Category as CategoryModel;
  5. use fast\Tree;
  6. /**
  7. * 分类管理
  8. *
  9. * @icon fa fa-list
  10. * @remark 用于统一管理网站的所有分类,分类可进行无限级分类
  11. */
  12. class Category extends Backend
  13. {
  14. protected $model = null;
  15. protected $categorylist = [];
  16. protected $noNeedRight = ['selectpage'];
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->request->filter(['strip_tags']);
  21. $this->model = model('app\common\model\Category');
  22. $tree = Tree::instance();
  23. $tree->init(collection($this->model->order('weigh desc,id desc')->select())->toArray(), 'pid');
  24. $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  25. $categorydata = [0 => ['type' => 'all', 'name' => __('None')]];
  26. foreach ($this->categorylist as $k => $v)
  27. {
  28. $categorydata[$v['id']] = $v;
  29. }
  30. $this->view->assign("flagList", $this->model->getFlagList());
  31. $this->view->assign("typeList", CategoryModel::getTypeList());
  32. $this->view->assign("parentList", $categorydata);
  33. }
  34. /**
  35. * 查看
  36. */
  37. public function index()
  38. {
  39. if ($this->request->isAjax())
  40. {
  41. $search = $this->request->request("search");
  42. $type = $this->request->request("type");
  43. //构造父类select列表选项数据
  44. $list = [];
  45. foreach ($this->categorylist as $k => $v)
  46. {
  47. if ($search) {
  48. if ($v['type'] == $type && stripos($v['name'], $search) !== false || stripos($v['nickname'], $search) !== false)
  49. {
  50. if($type == "all" || $type == null) {
  51. $list = $this->categorylist;
  52. } else {
  53. $list[] = $v;
  54. }
  55. }
  56. } else {
  57. if($type == "all" || $type == null) {
  58. $list = $this->categorylist;
  59. } else if ($v['type'] == $type){
  60. $list[] = $v;
  61. }
  62. }
  63. }
  64. $total = count($list);
  65. $result = array("total" => $total, "rows" => $list);
  66. return json($result);
  67. }
  68. return $this->view->fetch();
  69. }
  70. /**
  71. * Selectpage搜索
  72. *
  73. * @internal
  74. */
  75. public function selectpage()
  76. {
  77. return parent::selectpage();
  78. }
  79. }