GroupController.class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace Admin\Controller;
  3. use Util\Tree;
  4. /**
  5. * 部门控制器
  6. *
  7. */
  8. class GroupController extends AdminController
  9. {
  10. /**
  11. * 部门列表
  12. *
  13. */
  14. public function index()
  15. {
  16. // 搜索
  17. $keyword = I('keyword', '', 'string');
  18. $condition = array('like', '%' . $keyword . '%');
  19. $map['id|title'] = array(
  20. $condition,
  21. $condition,
  22. '_multi' => true,
  23. );
  24. // 获取所有部门
  25. $map['status'] = array('egt', '0'); //禁用和正常状态
  26. $data_list = D('Group')
  27. ->where($map)
  28. ->order('sort asc, id asc')
  29. ->select();
  30. // 转换成树状列表
  31. $tree = new Tree();
  32. $data_list = $tree->array2tree($data_list);
  33. $right_button['no']['title'] = '超级管理员无需操作';
  34. $right_button['no']['attribute'] = 'class="label label-warning" href="#"';
  35. // 使用Builder快速建立列表页面。
  36. $builder = new \Common\Builder\ListBuilder();
  37. $builder->setMetaTitle('部门列表') // 设置页面标题
  38. ->addTopButton('addnew') // 添加新增按钮
  39. ->addTopButton('resume') // 添加启用按钮
  40. ->addTopButton('forbid') // 添加禁用按钮
  41. ->addTopButton('delete') // 添加删除按钮
  42. ->setSearch('请输入ID/部门名称', U('index'))
  43. ->addTableColumn('id', 'ID')
  44. ->addTableColumn('title_show', '标题')
  45. ->addTableColumn('icon', '图标', 'icon')
  46. ->addTableColumn('sort', '排序')
  47. ->addTableColumn('status', '状态', 'status')
  48. ->addTableColumn('right_button', '操作', 'btn')
  49. ->setTableDataList($data_list) // 数据列表
  50. ->addRightButton('edit') // 添加编辑按钮
  51. ->addRightButton('forbid') // 添加禁用/启用按钮
  52. ->addRightButton('delete') // 添加删除按钮
  53. ->alterTableData( // 修改列表数据
  54. array('key' => 'id', 'value' => '1'),
  55. array('right_button' => $right_button)
  56. )
  57. ->display();
  58. }
  59. /**
  60. * 新增部门
  61. *
  62. */
  63. public function add()
  64. {
  65. if (IS_POST) {
  66. $group_object = D('Group');
  67. $_POST['menu_auth'] = json_encode(I('post.menu_auth'));
  68. $data = $group_object->create();
  69. if ($data) {
  70. $id = $group_object->add($data);
  71. if ($id) {
  72. $this->success('新增成功', U('index'));
  73. } else {
  74. $this->error('新增失败');
  75. }
  76. } else {
  77. $this->error($group_object->getError());
  78. }
  79. } else {
  80. // 获取现有部门
  81. $map['status'] = array('egt', 0);
  82. $all_group = select_list_as_tree('Group', $map, '顶级部门');
  83. // 获取功能模块的后台菜单列表
  84. $tree = new Tree();
  85. $moule_list = D('Module')
  86. ->where(array('status' => 1))
  87. ->select(); // 获取所有安装并启用的功能模块
  88. $all_module_menu_list = array();
  89. foreach ($moule_list as $key => $val) {
  90. $temp = json_decode($val['admin_menu'], true);
  91. $menu_list_item = $tree->list2tree($temp);
  92. $all_module_menu_list[$val['name']] = $menu_list_item[0];
  93. }
  94. $this->assign('all_module_menu_list', $all_module_menu_list);
  95. $this->assign('all_group', $all_group);
  96. $this->assign('meta_title', '新增部门');
  97. $this->display('add_edit');
  98. }
  99. }
  100. /**
  101. * 编辑部门
  102. *
  103. */
  104. public function edit($id)
  105. {
  106. if (IS_POST) {
  107. $group_object = D('Group');
  108. $_POST['menu_auth'] = json_encode(I('post.menu_auth'));
  109. $data = $group_object->create();
  110. if ($data) {
  111. if ($group_object->save($data) !== false) {
  112. $this->success('更新成功', U('index'));
  113. } else {
  114. $this->error('更新失败');
  115. }
  116. } else {
  117. $this->error($group_object->getError());
  118. }
  119. } else {
  120. // 获取部门信息
  121. $info = D('Group')->find($id);
  122. $info['menu_auth'] = json_decode($info['menu_auth'], true);
  123. // 获取现有部门
  124. $map['status'] = array('egt', 0);
  125. $all_group = select_list_as_tree('Group', $map, '顶级部门');
  126. // 获取所有安装并启用的功能模块
  127. $moule_list = D('Module')
  128. ->where(array('status' => 1))
  129. ->select();
  130. // 获取功能模块的后台菜单列表
  131. $tree = new Tree();
  132. $all_module_menu_list = array();
  133. foreach ($moule_list as $key => $val) {
  134. $temp = json_decode($val['admin_menu'], true);
  135. $menu_list_item = $tree->list2tree($temp);
  136. $all_module_menu_list[$val['name']] = $menu_list_item[0];
  137. }
  138. /* echo '<pre>';
  139. print_r($all_module_menu_list);*/
  140. $this->assign('info', $info);
  141. $this->assign('all_module_menu_list', $all_module_menu_list);
  142. /*print_r($all_module_menu_list);die;*/
  143. $this->assign('all_group', $all_group);
  144. $this->assign('meta_title', '编辑部门');
  145. $this->display('add_edit');
  146. }
  147. }
  148. /**
  149. * 设置一条或者多条数据的状态
  150. *
  151. */
  152. public function setStatus($model = CONTROLLER_NAME, $script = false)
  153. {
  154. $ids = I('request.ids');
  155. if (is_array($ids)) {
  156. if (in_array('1', $ids)) {
  157. $this->error('超级管理员组不允许操作');
  158. }
  159. } else {
  160. if ($ids === '1') {
  161. $this->error('超级管理员组不允许操作');
  162. }
  163. }
  164. parent::setStatus($model);
  165. }
  166. }