TeacherController.class.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 TeacherController 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. $param = array_merge($_POST, $_GET);
  39. // 模型对象
  40. $m = M('Teacher');
  41. // 条件
  42. $where = $this->GetIndexWhere();
  43. // 分页
  44. $number = MyC('admin_page_number');
  45. $page_param = array(
  46. 'number' => $number,
  47. 'total' => $m->where($where)->count(),
  48. 'where' => $param,
  49. 'url' => U('Admin/Teacher/Index'),
  50. );
  51. $page = new \My\Page($page_param);
  52. // 获取列表
  53. $list = $this->SetDataHandle($m->where($where)->limit($page->GetPageStarNumber(), $number)->select());
  54. // 性别
  55. $this->assign('common_gender_list', L('common_gender_list'));
  56. // 教师状态
  57. $this->assign('common_teacher_state_list', L('common_teacher_state_list'));
  58. // 参数
  59. $this->assign('param', $param);
  60. // 分页
  61. $this->assign('page_html', $page->GetPageHtml());
  62. // 数据列表
  63. $this->assign('list', $list);
  64. // Excel地址
  65. $this->assign('excel_url', U('Admin/Teacher/ExcelExport', $param));
  66. $this->display('Index');
  67. }
  68. /**
  69. * [ExcelExport excel文件导出]
  70. * @author Devil
  71. * @blog http://gong.gg/
  72. * @version 0.0.1
  73. * @datetime 2017-01-10T15:46:00+0800
  74. */
  75. public function ExcelExport()
  76. {
  77. // 条件
  78. $where = $this->GetIndexWhere();
  79. // 读取数据
  80. $data = $this->SetDataHandle(M('Teacher')->where($where)->select());
  81. // Excel驱动导出数据
  82. $excel = new \My\Excel(array('filename'=>'teacher', 'title'=>L('excel_teacher_title_list'), 'data'=>$data, 'msg'=>L('common_not_data_tips')));
  83. $excel->Export();
  84. }
  85. /**
  86. * [SetDataHandle 数据处理]
  87. * @author Devil
  88. * @blog http://gong.gg/
  89. * @version 0.0.1
  90. * @datetime 2016-12-29T21:27:15+0800
  91. * @param [array] $data [教师数据]
  92. * @return [array] [处理好的数据]
  93. */
  94. private function SetDataHandle($data)
  95. {
  96. if(!empty($data))
  97. {
  98. $ts = M('TeacherSubject');
  99. foreach($data as $k=>$v)
  100. {
  101. // 出生
  102. $data[$k]['birthday'] = date('Y-m-d', $v['birthday']);
  103. // 创建时间
  104. $data[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
  105. // 更新时间
  106. $data[$k]['upd_time'] = date('Y-m-d H:i:s', $v['upd_time']);
  107. // 性别
  108. $data[$k]['gender'] = L('common_gender_list')[$v['gender']]['name'];
  109. // 状态
  110. $data[$k]['state'] = L('common_teacher_state_list')[$v['state']]['name'];
  111. // 贯通科目
  112. $temp = $ts->alias('AS ts')->join('__SUBJECT__ AS s ON ts.subject_id = s.id')->where(array('ts.teacher_id'=>$v['id']))->field(array('s.name AS name'))->getField('name', true);
  113. $data[$k]['subject_list'] = empty($temp) ? '' : implode(', ', $temp);
  114. }
  115. }
  116. return $data;
  117. }
  118. /**
  119. * [GetIndexWhere 教师列表条件]
  120. * @author Devil
  121. * @blog http://gong.gg/
  122. * @version 0.0.1
  123. * @datetime 2016-12-10T22:16:29+0800
  124. */
  125. private function GetIndexWhere()
  126. {
  127. $where = array();
  128. // 模糊
  129. if(!empty($_REQUEST['keyword']))
  130. {
  131. $like_keyword = array('like', '%'.I('keyword').'%');
  132. $where[] = array(
  133. 'username' => $like_keyword,
  134. 'id_card' => $like_keyword,
  135. 'tel' => $like_keyword,
  136. 'mobile' => $like_keyword,
  137. 'address' => $like_keyword,
  138. 'email' => $like_keyword,
  139. '_logic' => 'or',
  140. );
  141. }
  142. // 是否更多条件
  143. if(I('is_more', 0) == 1)
  144. {
  145. // 等值
  146. if(I('gender', -1) > -1)
  147. {
  148. $where['gender'] = intval(I('gender', 0));
  149. }
  150. if(I('state', -1) > -1)
  151. {
  152. $where['state'] = intval(I('state', 0));
  153. }
  154. // 表达式
  155. if(!empty($_REQUEST['time_start']))
  156. {
  157. $where['birthday'][] = array('gt', strtotime(I('time_start')));
  158. }
  159. if(!empty($_REQUEST['time_end']))
  160. {
  161. $where['birthday'][] = array('lt', strtotime(I('time_end')));
  162. }
  163. }
  164. return $where;
  165. }
  166. /**
  167. * [SaveInfo 教师添加/编辑页面]
  168. * @author Devil
  169. * @blog http://gong.gg/
  170. * @version 0.0.1
  171. * @datetime 2016-12-14T21:37:02+0800
  172. */
  173. public function SaveInfo()
  174. {
  175. // 教师信息
  176. $data = empty($_REQUEST['id']) ? array() : M('Teacher')->find(I('id'));
  177. if(!empty($data['birthday']))
  178. {
  179. $data['birthday'] = date('Y-m-d', $data['birthday']);
  180. // 科目关联数据
  181. $data['subject_id'] = M('TeacherSubject')->where(array('teacher_id'=>$data['id']))->getField('subject_id', true);
  182. }
  183. $this->assign('data', $data);
  184. // 性别
  185. $this->assign('common_gender_list', L('common_gender_list'));
  186. // 教师状态
  187. $this->assign('common_teacher_state_list', L('common_teacher_state_list'));
  188. // 科目列表
  189. $this->assign('subject_list', M('Subject')->field(array('id', 'name'))->where(array('is_enable'=>1))->select());
  190. $this->display('SaveInfo');
  191. }
  192. /**
  193. * [Save 教师添加/编辑]
  194. * @author Devil
  195. * @blog http://gong.gg/
  196. * @version 0.0.1
  197. * @datetime 2016-12-14T21:37:02+0800
  198. */
  199. public function Save()
  200. {
  201. // 是否ajax请求
  202. if(!IS_AJAX)
  203. {
  204. $this->error(L('common_unauthorized_access'));
  205. }
  206. // 贯通科目参数处理
  207. if(!empty($_POST['subject_id']))
  208. {
  209. $_POST['subject_id'] = explode(',', $_POST['subject_id']);
  210. } else {
  211. $this->ajaxReturn(L('teacher_subject_format'), -2);
  212. }
  213. // 添加
  214. if(empty($_POST['id']))
  215. {
  216. $this->Add();
  217. // 编辑
  218. } else {
  219. $this->Edit();
  220. }
  221. }
  222. /**
  223. * [Add 教师添加]
  224. * @author Devil
  225. * @blog http://gong.gg/
  226. * @version 0.0.1
  227. * @datetime 2016-12-18T16:20:59+0800
  228. */
  229. private function Add()
  230. {
  231. // 教师对象
  232. $m = D('Teacher');
  233. // 数据自动校验
  234. if($m->create($_POST, 1))
  235. {
  236. // 额外数据处理
  237. $m->add_time = time();
  238. $m->upd_time = time();
  239. $m->birthday = strtotime($m->birthday);
  240. $m->username = I('username');
  241. $m->address = I('address');
  242. // 开启事务
  243. $m->startTrans();
  244. // 教师写入数据库
  245. $teacher_id = $m->add();
  246. // 添加教师科目关联数据
  247. $ts_state = true;
  248. $ts = M('TeacherSubject');
  249. foreach($_POST['subject_id'] as $subject_id)
  250. {
  251. if(!empty($subject_id))
  252. {
  253. $temp_data = array(
  254. 'teacher_id' => $teacher_id,
  255. 'subject_id' => $subject_id,
  256. 'add_time' => time(),
  257. );
  258. if(!$ts->add($temp_data))
  259. {
  260. $ts_state = false;
  261. break;
  262. }
  263. }
  264. }
  265. if($teacher_id && $ts_state)
  266. {
  267. // 提交事务
  268. $m->commit();
  269. $this->ajaxReturn(L('common_operation_add_success'));
  270. } else {
  271. // 回滚事务
  272. $m->rollback();
  273. $this->ajaxReturn(L('common_operation_add_error'), -100);
  274. }
  275. } else {
  276. $this->ajaxReturn($m->getError(), -1);
  277. }
  278. }
  279. /**
  280. * [Edit 教师编辑]
  281. * @author Devil
  282. * @blog http://gong.gg/
  283. * @version 0.0.1
  284. * @datetime 2016-12-17T22:13:40+0800
  285. */
  286. private function Edit()
  287. {
  288. // 教师对象
  289. $m = D('Teacher');
  290. // 数据自动校验
  291. if($m->create($_POST, 2))
  292. {
  293. // 额外数据处理
  294. if(!empty($m->birthday))
  295. {
  296. $m->birthday = strtotime($m->birthday);
  297. }
  298. $m->username = I('username');
  299. $m->address = I('address');
  300. $m->upd_time = time();
  301. // 移除不能更新的数据
  302. unset($m->id_card);
  303. // 开启事务
  304. $m->startTrans();
  305. // 教师id
  306. $teacher_id = I('id');
  307. // 更新教师
  308. $t_state = $m->where(array('id'=>$teacher_id, 'id_card'=>I('id_card')))->save();
  309. // 删除教师科目关联数据
  310. $ts = M('TeacherSubject');
  311. $ts_state_del = $ts->where(array('teacher_id'=>$teacher_id))->delete();
  312. // 添加教师科目关联数据
  313. $ts_state = true;
  314. foreach($_POST['subject_id'] as $subject_id)
  315. {
  316. if(!empty($subject_id))
  317. {
  318. $temp_data = array(
  319. 'teacher_id' => $teacher_id,
  320. 'subject_id' => $subject_id,
  321. 'add_time' => time(),
  322. );
  323. if(!$ts->add($temp_data))
  324. {
  325. $ts_state = false;
  326. break;
  327. }
  328. }
  329. }
  330. if($t_state !== false && $ts_state_del !== false && $ts_state !== false)
  331. {
  332. // 提交事务
  333. $m->commit();
  334. $this->ajaxReturn(L('common_operation_edit_success'));
  335. } else {
  336. // 回滚事务
  337. $m->rollback();
  338. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  339. }
  340. } else {
  341. $this->ajaxReturn($m->getError(), -1);
  342. }
  343. }
  344. /**
  345. * [Delete 教师删除]
  346. * @author Devil
  347. * @blog http://gong.gg/
  348. * @version 0.0.1
  349. * @datetime 2016-12-15T11:03:30+0800
  350. */
  351. public function Delete()
  352. {
  353. // 是否ajax请求
  354. if(!IS_AJAX)
  355. {
  356. $this->error(L('common_unauthorized_access'));
  357. }
  358. // 参数处理
  359. list($id, $id_card) = (stripos(I('id'), '-') === false) ? array() : explode('-', I('id'));
  360. // 删除数据
  361. if($id != null && $id_card != null)
  362. {
  363. // 教师模型
  364. $s = M('Teacher');
  365. // 教师是否存在
  366. $teacher = $s->where(array('id'=>$id, 'id_card'=>$id_card))->getField('id');
  367. if(empty($teacher))
  368. {
  369. $this->ajaxReturn(L('teacher_no_exist_error'), -2);
  370. }
  371. // 开启事务
  372. $s->startTrans();
  373. // 删除教师
  374. $t_state = $s->where(array('id'=>$id, 'id_card'=>$id_card))->delete();
  375. // 删除课程
  376. $c_state = M('Course')->where(array('teacher_id'=>$id))->delete();
  377. if($t_state !== false && $c_state !== false)
  378. {
  379. // 提交事务
  380. $s->commit();
  381. $this->ajaxReturn(L('common_operation_delete_success'));
  382. } else {
  383. // 回滚事务
  384. $s->rollback();
  385. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  386. }
  387. } else {
  388. $this->ajaxReturn(L('common_param_error'), -1);
  389. }
  390. }
  391. }
  392. ?>