123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace app\admin\controller\department;
- use app\common\controller\Backend;
- use ba\Tree;
- /**
- * 部门
- *
- */
- class Department extends Backend
- {
- /**
- * Department模型对象
- * @var \app\admin\model\department\Department
- */
- protected $model = null;
- protected $tree = null;
- protected $keyword = false;
- protected $quickSearchField = 'name';
- protected $defaultSortField = 'weigh,desc';
- protected $preExcludeFields = ['createtime', 'updatetime'];
- public function initialize()
- {
- parent::initialize();
- $this->model = new \app\admin\model\department\Department;
- $this->tree = Tree::instance();
- $this->keyword = $this->request->request("quick_search");
- }
- public function index()
- {
- if ($this->request->param('select')) {
- $this->select();
- }
- $this->success('', [
- 'list' => $this->getDepartment(),
- 'remark' => get_route_remark(),
- ]);
- }
- public function edit($id = null)
- {
- $row = $this->model->find($id);
- if (!$row) {
- $this->error(__('Record not found'));
- }
- if ($this->request->isPost()) {
- parent::edit($id);
- }
- $this->success('', [
- 'row' => $row
- ]);
- }
- public function select()
- {
- $isTree = $this->request->param('isTree');
- $data = $this->getDepartment([['status', '=', '1']]);
- if ($isTree && !$this->keyword) {
- $data = $this->tree->assembleTree($this->tree->getTreeArray($data, 'name'));
- }
- $this->success('', [
- 'options' => $data
- ]);
- }
- protected function getDepartment($where = [])
- {
- if ($this->keyword) {
- $keyword = explode(' ', $this->keyword);
- foreach ($keyword as $item) {
- $where[] = [$this->quickSearchField, 'like', '%' . $item . '%'];
- }
- }
- $data = $this->model->where($where)->order('weigh desc,id asc')->select()->toArray();
- return $this->tree->assembleChild($data);
- }
- }
|