Category.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /**
  15. * @var \app\common\model\Category
  16. */
  17. protected $model = null;
  18. protected $categorylist = [];
  19. protected $noNeedRight = ['selectpage'];
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->request->filter(['strip_tags']);
  24. $this->model = model('app\common\model\Category');
  25. $tree = Tree::instance();
  26. $tree->init(collection($this->model->order('weigh desc,id desc')->select())->toArray(), 'pid');
  27. $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  28. $categorydata = [0 => ['type' => 'all', 'name' => __('None')]];
  29. foreach ($this->categorylist as $k => $v)
  30. {
  31. $categorydata[$v['id']] = $v;
  32. }
  33. $this->view->assign("flagList", $this->model->getFlagList());
  34. $this->view->assign("typeList", CategoryModel::getTypeList());
  35. $this->view->assign("parentList", $categorydata);
  36. }
  37. /**
  38. * 查看
  39. */
  40. public function index()
  41. {
  42. if ($this->request->isAjax())
  43. {
  44. $search = $this->request->request("search");
  45. $type = $this->request->request("type");
  46. //构造父类select列表选项数据
  47. $list = [];
  48. foreach ($this->categorylist as $k => $v)
  49. {
  50. if ($search) {
  51. if ($v['type'] == $type && stripos($v['name'], $search) !== false || stripos($v['nickname'], $search) !== false)
  52. {
  53. if($type == "all" || $type == null) {
  54. $list = $this->categorylist;
  55. } else {
  56. $list[] = $v;
  57. }
  58. }
  59. } else {
  60. if($type == "all" || $type == null) {
  61. $list = $this->categorylist;
  62. } else if ($v['type'] == $type){
  63. $list[] = $v;
  64. }
  65. }
  66. }
  67. $total = count($list);
  68. $result = array("total" => $total, "rows" => $list);
  69. return json($result);
  70. }
  71. return $this->view->fetch();
  72. }
  73. /**
  74. * Selectpage搜索
  75. *
  76. * @internal
  77. */
  78. public function selectpage()
  79. {
  80. return parent::selectpage();
  81. }
  82. }