Department.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\admin\controller\department;
  3. use app\common\controller\Backend;
  4. use ba\Tree;
  5. /**
  6. * 部门
  7. *
  8. */
  9. class Department extends Backend
  10. {
  11. /**
  12. * Department模型对象
  13. * @var \app\admin\model\department\Department
  14. */
  15. protected $model = null;
  16. protected $tree = null;
  17. protected $keyword = false;
  18. protected $quickSearchField = 'name';
  19. protected $defaultSortField = 'weigh,desc';
  20. protected $preExcludeFields = ['createtime', 'updatetime'];
  21. public function initialize()
  22. {
  23. parent::initialize();
  24. $this->model = new \app\admin\model\department\Department;
  25. $this->tree = Tree::instance();
  26. $this->keyword = $this->request->request("quick_search");
  27. }
  28. public function index()
  29. {
  30. if ($this->request->param('select')) {
  31. $this->select();
  32. }
  33. $this->success('', [
  34. 'list' => $this->getDepartment(),
  35. 'remark' => get_route_remark(),
  36. ]);
  37. }
  38. public function edit($id = null)
  39. {
  40. $row = $this->model->find($id);
  41. if (!$row) {
  42. $this->error(__('Record not found'));
  43. }
  44. if ($this->request->isPost()) {
  45. parent::edit($id);
  46. }
  47. $this->success('', [
  48. 'row' => $row
  49. ]);
  50. }
  51. public function select()
  52. {
  53. $isTree = $this->request->param('isTree');
  54. $data = $this->getDepartment([['status', '=', '1']]);
  55. if ($isTree && !$this->keyword) {
  56. $data = $this->tree->assembleTree($this->tree->getTreeArray($data, 'name'));
  57. }
  58. $this->success('', [
  59. 'options' => $data
  60. ]);
  61. }
  62. protected function getDepartment($where = [])
  63. {
  64. if ($this->keyword) {
  65. $keyword = explode(' ', $this->keyword);
  66. foreach ($keyword as $item) {
  67. $where[] = [$this->quickSearchField, 'like', '%' . $item . '%'];
  68. }
  69. }
  70. $data = $this->model->where($where)->order('weigh desc,id asc')->select()->toArray();
  71. return $this->tree->assembleChild($data);
  72. }
  73. }