ClassController.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 ClassController 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. $this->assign('list', M('Class')->field(array('id', 'name'))->where(array('pid'=>0))->select());
  38. $this->assign('common_is_enable_list', L('common_is_enable_list'));
  39. $this->display('Index');
  40. }
  41. /**
  42. * [GetNodeSon 获取节点子列表]
  43. * @author Devil
  44. * @blog http://gong.gg/
  45. * @version 0.0.1
  46. * @datetime 2016-12-25T15:19:45+0800
  47. */
  48. public function GetNodeSon()
  49. {
  50. // 是否ajax请求
  51. if(!IS_AJAX)
  52. {
  53. $this->error(L('common_unauthorized_access'));
  54. }
  55. // 获取数据
  56. $field = array('id', 'pid', 'name', 'sort', 'is_enable');
  57. $data = M('Class')->field($field)->where(array('pid'=>intval(I('id', 0))))->select();
  58. if(!empty($data))
  59. {
  60. foreach($data as $k=>$v)
  61. {
  62. $data[$k]['is_son'] = $this->IsExistSon($v['id']);
  63. $data[$k]['ajax_url'] = U('Admin/Class/GetNodeSon', array('id'=>$v['id']));
  64. $data[$k]['delete_url'] = U('Admin/Class/Delete');
  65. $data[$k]['json'] = json_encode($v);
  66. }
  67. }
  68. $msg = empty($data) ? L('common_not_data_tips') : L('common_operation_success');
  69. $this->ajaxReturn($msg, 0, $data);
  70. }
  71. /**
  72. * [IsExistSon 节点是否存在子数据]
  73. * @author Devil
  74. * @blog http://gong.gg/
  75. * @version 0.0.1
  76. * @datetime 2016-12-25T15:22:47+0800
  77. * @param [int] $id [节点id]
  78. * @return [string] [有数据ok, 则no]
  79. */
  80. private function IsExistSon($id)
  81. {
  82. if(!empty($id))
  83. {
  84. return (M('Class')->where(array('pid'=>$id))->count() > 0) ? 'ok' : 'no';
  85. }
  86. return 'no';
  87. }
  88. /**
  89. * [Save 班级保存]
  90. * @author Devil
  91. * @blog http://gong.gg/
  92. * @version 0.0.1
  93. * @datetime 2016-12-25T22:36:12+0800
  94. */
  95. public function Save()
  96. {
  97. // 是否ajax请求
  98. if(!IS_AJAX)
  99. {
  100. $this->error(L('common_unauthorized_access'));
  101. }
  102. // id为空则表示是新增
  103. $m = D('Class');
  104. // 公共额外数据处理
  105. $m->sort = intval(I('sort'));
  106. // 添加
  107. if(empty($_POST['id']))
  108. {
  109. if($m->create($_POST, 1))
  110. {
  111. // 额外数据处理
  112. $m->add_time = time();
  113. $m->name = I('name');
  114. // 写入数据库
  115. if($m->add())
  116. {
  117. $this->ajaxReturn(L('common_operation_add_success'));
  118. } else {
  119. $this->ajaxReturn(L('common_operation_add_error'), -100);
  120. }
  121. }
  122. } else {
  123. // 编辑
  124. if($m->create($_POST, 2))
  125. {
  126. // 额外数据处理
  127. $m->name = I('name');
  128. // 移除 id
  129. unset($m->id);
  130. // 更新数据库
  131. if($m->where(array('id'=>I('id')))->save())
  132. {
  133. $this->ajaxReturn(L('common_operation_edit_success'));
  134. } else {
  135. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  136. }
  137. }
  138. }
  139. $this->ajaxReturn($m->getError(), -1);
  140. }
  141. /**
  142. * [Delete 班级删除]
  143. * @author Devil
  144. * @blog http://gong.gg/
  145. * @version 0.0.1
  146. * @datetime 2016-12-25T22:36:12+0800
  147. */
  148. public function Delete()
  149. {
  150. if(!IS_AJAX)
  151. {
  152. $this->error(L('common_unauthorized_access'));
  153. }
  154. $m = D('Class');
  155. if($m->create($_POST, 5))
  156. {
  157. if($m->delete(I('id')))
  158. {
  159. $this->ajaxReturn(L('common_operation_delete_success'));
  160. } else {
  161. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  162. }
  163. } else {
  164. $this->ajaxReturn($m->getError(), -1);
  165. }
  166. }
  167. }
  168. ?>