RegionController.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace Admin\Controller;
  3. /**
  4. * 地区管理
  5. * @author Devil
  6. * @blog http://gong.gg/
  7. * @version 0.0.1
  8. * @datetime 2016-12-01T21:51:08+0800
  9. */
  10. class RegionController extends CommonController
  11. {
  12. /**
  13. * [_initialize 前置操作-继承公共前置方法]
  14. * @author Devil
  15. * @blog http://gong.gg/
  16. * @version 0.0.1
  17. * @datetime 2016-12-03T12:39:08+0800
  18. */
  19. public function _initialize()
  20. {
  21. // 调用父类前置方法
  22. parent::_initialize();
  23. // 登录校验
  24. $this->Is_Login();
  25. // 权限校验
  26. $this->Is_Power();
  27. }
  28. /**
  29. * [Index 地区列表]
  30. * @author Devil
  31. * @blog http://gong.gg/
  32. * @version 0.0.1
  33. * @datetime 2016-12-06T21:31:53+0800
  34. */
  35. public function Index()
  36. {
  37. // 地区
  38. $this->assign('list', M('Region')->field(array('id', 'name'))->select());
  39. // 是否启用
  40. $this->assign('common_is_enable_list', L('common_is_enable_list'));
  41. $this->display('Index');
  42. }
  43. /**
  44. * [GetNodeSon 获取节点子列表]
  45. * @author Devil
  46. * @blog http://gong.gg/
  47. * @version 0.0.1
  48. * @datetime 2016-12-25T15:19:45+0800
  49. */
  50. public function GetNodeSon()
  51. {
  52. // 是否ajax请求
  53. if(!IS_AJAX)
  54. {
  55. $this->error(L('common_unauthorized_access'));
  56. }
  57. // 获取数据
  58. $field = array('id', 'pid', 'name', 'sort', 'is_enable');
  59. $data = M('Region')->field($field)->where(array('pid'=>intval(I('id', 0))))->select();
  60. if(!empty($data))
  61. {
  62. foreach($data as $k=>$v)
  63. {
  64. $data[$k]['is_son'] = $this->IsExistSon($v['id']);
  65. $data[$k]['ajax_url'] = U('Admin/Region/GetNodeSon', array('id'=>$v['id']));
  66. $data[$k]['delete_url'] = U('Admin/Region/Delete');
  67. $data[$k]['json'] = json_encode($v);
  68. }
  69. }
  70. $msg = empty($data) ? L('common_not_data_tips') : L('common_operation_success');
  71. $this->ajaxReturn($msg, 0, $data);
  72. }
  73. /**
  74. * [IsExistSon 节点是否存在子数据]
  75. * @author Devil
  76. * @blog http://gong.gg/
  77. * @version 0.0.1
  78. * @datetime 2016-12-25T15:22:47+0800
  79. * @param [int] $id [节点id]
  80. * @return [string] [有数据ok, 则no]
  81. */
  82. private function IsExistSon($id)
  83. {
  84. if(!empty($id))
  85. {
  86. return (M('Region')->where(array('pid'=>$id))->count() > 0) ? 'ok' : 'no';
  87. }
  88. return 'no';
  89. }
  90. /**
  91. * [Save 地区保存]
  92. * @author Devil
  93. * @blog http://gong.gg/
  94. * @version 0.0.1
  95. * @datetime 2016-12-25T22:36:12+0800
  96. */
  97. public function Save()
  98. {
  99. // 是否ajax请求
  100. if(!IS_AJAX)
  101. {
  102. $this->error(L('common_unauthorized_access'));
  103. }
  104. // id为空则表示是新增
  105. $m = D('Region');
  106. // 公共额外数据处理
  107. $m->sort = intval(I('sort'));
  108. // 添加
  109. if(empty($_POST['id']))
  110. {
  111. if($m->create($_POST, 1))
  112. {
  113. // 额外数据处理
  114. $m->add_time = time();
  115. $m->name = I('name');
  116. // 写入数据库
  117. if($m->add())
  118. {
  119. $this->ajaxReturn(L('common_operation_add_success'));
  120. } else {
  121. $this->ajaxReturn(L('common_operation_add_error'), -100);
  122. }
  123. }
  124. } else {
  125. // 编辑
  126. if($m->create($_POST, 2))
  127. {
  128. // 额外数据处理
  129. $m->name = I('name');
  130. // 移除 id
  131. unset($m->id);
  132. // 更新数据库
  133. if($m->where(array('id'=>I('id')))->save())
  134. {
  135. $this->ajaxReturn(L('common_operation_edit_success'));
  136. } else {
  137. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  138. }
  139. }
  140. }
  141. $this->ajaxReturn($m->getError(), -1);
  142. }
  143. /**
  144. * [Delete 地区删除]
  145. * @author Devil
  146. * @blog http://gong.gg/
  147. * @version 0.0.1
  148. * @datetime 2016-12-25T22:36:12+0800
  149. */
  150. public function Delete()
  151. {
  152. if(!IS_AJAX)
  153. {
  154. $this->error(L('common_unauthorized_access'));
  155. }
  156. $m = D('Region');
  157. if($m->create($_POST, 5))
  158. {
  159. if($m->delete(I('id')))
  160. {
  161. $this->ajaxReturn(L('common_operation_delete_success'));
  162. } else {
  163. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  164. }
  165. } else {
  166. $this->ajaxReturn($m->getError(), -1);
  167. }
  168. }
  169. }
  170. ?>