Usergangwei.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. /**
  6. * 用户岗位
  7. */
  8. class Usergangwei extends Backend
  9. {
  10. protected $model = null;
  11. protected $categorylist = [];
  12. protected $noNeedRight = ['selectpage'];
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. $this->model = new \app\common\model\Usergangwei;
  17. $tree = Tree::instance();
  18. $tree->init(collection($this->model->order('weigh desc,id desc')->select())->toArray(), 'pid');
  19. $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  20. $categorydata = [0 => ['name' => __('None')]];
  21. foreach ($this->categorylist as $k => $v) {
  22. $categorydata[$v['id']] = $v;
  23. }
  24. $this->view->assign("parentList", $categorydata);
  25. }
  26. /**
  27. * 查看
  28. */
  29. public function index()
  30. {
  31. //设置过滤方法
  32. $this->request->filter(['strip_tags']);
  33. if ($this->request->isAjax()) {
  34. $list = $this->categorylist;
  35. $total = count($list);
  36. $result = array("total" => $total, "rows" => $list);
  37. return json($result);
  38. }
  39. return $this->view->fetch();
  40. }
  41. /**
  42. * 编辑
  43. */
  44. public function edit($ids = null)
  45. {
  46. $row = $this->model->get($ids);
  47. if (!$row) {
  48. $this->error(__('No Results were found'));
  49. }
  50. $adminIds = $this->getDataLimitAdminIds();
  51. if (is_array($adminIds)) {
  52. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  53. $this->error(__('You have no permission'));
  54. }
  55. }
  56. if ($this->request->isPost()) {
  57. $this->token();
  58. $params = $this->request->post("row/a");
  59. if ($params) {
  60. $params = $this->preExcludeFields($params);
  61. if ($params['pid'] != $row['pid']) {
  62. $childrenIds = Tree::instance()->init(collection(\app\common\model\Usergangwei::select())->toArray())->getChildrenIds($row['id'], true);
  63. if (in_array($params['pid'], $childrenIds)) {
  64. $this->error(__('Can not change the parent to child or itself'));
  65. }
  66. }
  67. try {
  68. //是否采用模型验证
  69. if ($this->modelValidate) {
  70. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  71. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  72. $row->validate($validate);
  73. }
  74. $result = $row->allowField(true)->save($params);
  75. if ($result !== false) {
  76. $this->success();
  77. } else {
  78. $this->error($row->getError());
  79. }
  80. } catch (\think\exception\PDOException $e) {
  81. $this->error($e->getMessage());
  82. } catch (\think\Exception $e) {
  83. $this->error($e->getMessage());
  84. }
  85. }
  86. $this->error(__('Parameter %s can not be empty', ''));
  87. }
  88. $this->view->assign("row", $row);
  89. return $this->view->fetch();
  90. }
  91. /**
  92. * Selectpage搜索
  93. *
  94. * @internal
  95. */
  96. public function selectpage()
  97. {
  98. return parent::selectpage();
  99. }
  100. }