Category.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace app\admin\controller\blog;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. use think\Controller;
  6. use think\Request;
  7. /**
  8. * 博客分类管理
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Category extends Backend
  13. {
  14. protected $noNeedRight = ['selectpage', 'check_element_available'];
  15. /**
  16. * BlogCategory模型对象
  17. */
  18. protected $model = null;
  19. protected $categorylist = [];
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = model('BlogCategory');
  24. $this->view->assign("flagList", $this->model->getFlagList());
  25. $this->view->assign("statusList", $this->model->getStatusList());
  26. $tree = Tree::instance();
  27. $tree->init(collection($this->model->order('weigh desc,id desc')->select())->toArray(), 'pid');
  28. $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  29. $categorydata = [0 => ['name' => __('None')]];
  30. foreach ($this->categorylist as $k => $v) {
  31. $categorydata[$v['id']] = $v;
  32. }
  33. $this->view->assign("parentList", $categorydata);
  34. }
  35. public function selectpage()
  36. {
  37. return parent::selectpage();
  38. }
  39. /**
  40. * 检测元素是否可用
  41. * @internal
  42. */
  43. public function check_element_available()
  44. {
  45. $id = $this->request->request('id');
  46. $name = $this->request->request('name');
  47. $value = $this->request->request('value');
  48. $name = substr($name, 4, -1);
  49. if (!$name) {
  50. $this->error(__('Parameter %s can not be empty', 'name'));
  51. }
  52. if ($id) {
  53. $this->model->where('id', '<>', $id);
  54. }
  55. $exist = $this->model->where($name, $value)->find();
  56. if ($exist) {
  57. $this->error(__('The data already exist'));
  58. } else {
  59. $this->success();
  60. }
  61. }
  62. }