AdminController.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace Admin\Controller;
  3. use Common\Controller\ControllerController;
  4. /**
  5. * 后台公共控制器
  6. * 为什么要继承AdminController?
  7. * 因为AdminController的初始化函数中读取了顶部导航栏和左侧的菜单,
  8. * 如果不继承的话,只能复制AdminController中的代码来读取导航栏和左侧的菜单。
  9. * 这样做会导致一个问题就是当AdminController被官方修改后不会同步更新,从而导致错误。
  10. * 所以综合考虑还是继承比较好。
  11. *
  12. */
  13. class AdminController extends ControllerController
  14. {
  15. /**
  16. * 初始化方法
  17. *
  18. */
  19. protected function _initialize()
  20. {
  21. // 登录检测
  22. if (!is_login()) {
  23. //还没登录跳转到登录页面
  24. $this->redirect('Admin/Public/login');
  25. }
  26. C('PARSE_VAR', true);
  27. // 权限检测
  28. $current_url = MODULE_NAME . '/' . CONTROLLER_NAME . '/' . ACTION_NAME;
  29. if ('Admin/Index/index' !== $current_url) {
  30. if (!D('Admin/Group')->checkMenuAuth()) {
  31. $this->error('权限不足!',$_SERVER['HTTP_REFERER']);
  32. }
  33. $this->assign('_admin_tabs', C('ADMIN_TABS'));
  34. }
  35. // 获取所有导航
  36. $module_object = D('Admin/Module');
  37. $menu_list = $module_object->getAllMenu();
  38. if(session('user_auth.uid')==1 && C('ADMIN_TABS')){
  39. array_splice($menu_list[MODULE_NAME]['_child'][3]['_child'],0,2);//TODO 暂时没找到更好的办法,临时隐藏 xtj
  40. }
  41. $this->assign('_menu_list', $menu_list); // 后台主菜单
  42. // 获取左侧导航
  43. if (!C('ADMIN_TABS')) {
  44. $parent_menu_list = $module_object->getParentMenu();
  45. if (isset($parent_menu_list[0]['top'])) {
  46. $current_menu_list = $menu_list[$parent_menu_list[0]['top']];
  47. } else {
  48. if(session('user_auth.uid')==1){
  49. array_splice($menu_list[MODULE_NAME]['_child'][3]['_child'],0,2);//TODO 暂时没找到更好的办法,临时隐藏 xtj
  50. }
  51. $current_menu_list = $menu_list[MODULE_NAME];
  52. }
  53. $this->assign('_current_menu_list', $current_menu_list); // 后台左侧菜单
  54. $this->assign('_parent_menu_list', $parent_menu_list); // 后台父级菜单
  55. }
  56. }
  57. /**
  58. * 设置一条或者多条数据的状态
  59. * @param $script 严格模式要求处理的纪录的uid等于当前登陆用户UID
  60. *
  61. */
  62. public function setStatus($model = CONTROLLER_NAME, $script = false)
  63. {
  64. $ids = I('request.ids');
  65. $status = I('request.status');
  66. if (empty($ids)) {
  67. $this->error('请选择要操作的数据');
  68. }
  69. $model_primary_key = D($model)->getPk();
  70. $map[$model_primary_key] = array('in', $ids);
  71. if ($script) {
  72. $map['uid'] = array('eq', is_login());
  73. }
  74. switch ($status) {
  75. case 'forbid': // 禁用条目
  76. $data = array('status' => 0);
  77. $this->editRow(
  78. $model,
  79. $data,
  80. $map,
  81. array('success' => '禁用成功', 'error' => '禁用失败')
  82. );
  83. break;
  84. case 'resume': // 启用条目
  85. $data = array('status' => 1);
  86. $map = array_merge(array('status' => 0), $map);
  87. $this->editRow(
  88. $model,
  89. $data,
  90. $map,
  91. array('success' => '启用成功', 'error' => '启用失败')
  92. );
  93. break;
  94. case 'recycle': // 移动至回收站
  95. $data['status'] = -1;
  96. $this->editRow(
  97. $model,
  98. $data,
  99. $map,
  100. array('success' => '成功移至回收站', 'error' => '删除失败')
  101. );
  102. break;
  103. case 'restore': // 从回收站还原
  104. $data = array('status' => 1);
  105. $map = array_merge(array('status' => -1), $map);
  106. $this->editRow(
  107. $model,
  108. $data,
  109. $map,
  110. array('success' => '恢复成功', 'error' => '恢复失败')
  111. );
  112. break;
  113. case 'delete': // 删除条目
  114. $result = D($model)->where($map)->delete();
  115. if ($result) {
  116. $this->success('删除成功,不可恢复!');
  117. } else {
  118. $this->error('删除失败');
  119. }
  120. break;
  121. default:
  122. $this->error('参数错误');
  123. break;
  124. }
  125. }
  126. /**
  127. * 对数据表中的单行或多行记录执行修改 GET参数id为数字或逗号分隔的数字
  128. * @param string $model 模型名称,供M函数使用的参数
  129. * @param array $data 修改的数据
  130. * @param array $map 查询时的where()方法的参数
  131. * @param array $msg 执行正确和错误的消息
  132. * array(
  133. * 'success' => '',
  134. * 'error' => '',
  135. * 'url' => '', // url为跳转页面
  136. * 'ajax' => false //是否ajax(数字则为倒数计时)
  137. * )
  138. *
  139. */
  140. final protected function editRow($model, $data, $map, $msg)
  141. {
  142. $id = array_unique((array) I('id', 0));
  143. $id = is_array($id) ? implode(',', $id) : $id;
  144. //如存在id字段,则加入该条件
  145. $fields = D($model)->getDbFields();
  146. if (in_array('id', $fields) && !empty($id)) {
  147. $where = array_merge(
  148. array('id' => array('in', $id)),
  149. (array) $where
  150. );
  151. }
  152. $msg = array_merge(
  153. array(
  154. 'success' => '操作成功!',
  155. 'error' => '操作失败!',
  156. 'url' => ' ',
  157. 'ajax' => IS_AJAX,
  158. ),
  159. (array) $msg
  160. );
  161. $result = D($model)->where($map)->save($data);
  162. if ($result != false) {
  163. $this->success($msg['success'], $msg['url'], $msg['ajax']);
  164. } else {
  165. $this->error($msg['error'], $msg['url'], $msg['ajax']);
  166. }
  167. }
  168. /**
  169. * 模块配置方法
  170. *
  171. */
  172. public function module_config()
  173. {
  174. if (IS_POST) {
  175. $id = (int) I('id');
  176. $config = I('config');
  177. $flag = D('Admin/Module')
  178. ->where("id={$id}")
  179. ->setField('config', json_encode($config));
  180. if ($flag !== false) {
  181. $this->success('保存成功');
  182. } else {
  183. $this->error('保存失败');
  184. }
  185. } else {
  186. $name = MODULE_NAME;
  187. $config_file = realpath(APP_PATH . $name) . '/' . D('Admin/Module')->install_file();
  188. if (!$config_file) {
  189. $this->error('配置文件不存在');
  190. }
  191. $module_config = include $config_file;
  192. $module_info = D('Admin/Module')->where(array('name' => $name))->find($id);
  193. $db_config = $module_info['config'];
  194. // 构造配置
  195. if ($db_config) {
  196. $db_config = json_decode($db_config, true);
  197. foreach ($module_config['config'] as $key => $value) {
  198. if ($value['type'] != 'group') {
  199. $module_config['config'][$key]['value'] = $db_config[$key];
  200. } else {
  201. foreach ($value['options'] as $gourp => $options) {
  202. foreach ($options['options'] as $gkey => $value) {
  203. $module_config['config'][$key]['options'][$gourp]['options'][$gkey]['value'] = $db_config[$gkey];
  204. }
  205. }
  206. }
  207. }
  208. }
  209. // 构造表单名
  210. foreach ($module_config['config'] as $key => $val) {
  211. if ($val['type'] == 'group') {
  212. foreach ($val['options'] as $key2 => $val2) {
  213. foreach ($val2['options'] as $key3 => $val3) {
  214. $module_config['config'][$key]['options'][$key2]['options'][$key3]['name'] = 'config[' . $key3 . ']';
  215. }
  216. }
  217. } else {
  218. $module_config['config'][$key]['name'] = 'config[' . $key . ']';
  219. }
  220. }
  221. //使用FormBuilder快速建立表单页面。
  222. $builder = new \Common\Builder\FormBuilder();
  223. $builder->setMetaTitle('设置') //设置页面标题
  224. ->setPostUrl(U('')) //设置表单提交地址
  225. ->addFormItem('id', 'hidden', 'ID', 'ID')
  226. ->setExtraItems($module_config['config']) //直接设置表单数据
  227. ->setFormData($module_info)
  228. ->display();
  229. }
  230. }
  231. /**
  232. * 扩展日期搜索map
  233. * @param $map array 引用型
  234. * @param string $field 搜索的时间范围字段
  235. * @param string $type datetime 类型 或 timestamp 时间戳
  236. * @param boolean $not_empty 是否允许空值搜索到
  237. */
  238. public function extendDates(&$map, $field = 'update_time', $type = 'datetime', $not_empty = false)
  239. {
  240. $dates = I('dates', '', 'trim');
  241. if ($dates) {
  242. $start_date = substr($dates, 0, 10);
  243. $end_date = substr($dates, 11, 10);
  244. if ($type == 'datetime') {
  245. $map[$field] = [
  246. ['egt', $start_date . ' 00:00:00'],
  247. ['lt', $end_date . ' 23:59:59'],
  248. ];
  249. if ($not_empty) {
  250. $map[$field][] = ['exp', 'IS NOT NUll'];
  251. }
  252. } else {
  253. $map[$field] = [
  254. ['egt', strtotime($start_date . ' 00:00:00')],
  255. ['lt', strtotime($end_date . ' 23:59:59')],
  256. ];
  257. }
  258. }
  259. // else {
  260. // $start_date = datetime("-365 days", 'Y-m-d');
  261. // $end_date = datetime('now', 'Y-m-d');
  262. // }
  263. }
  264. }