StudentModel.class.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace Admin\Model;
  3. use Think\Model;
  4. /**
  5. * 学生模型
  6. * @author Devil
  7. * @blog http://gong.gg/
  8. * @version 0.0.1
  9. * @datetime 2016-12-01T21:51:08+0800
  10. */
  11. class StudentModel extends CommonModel
  12. {
  13. // 数据自动校验
  14. protected $_validate = array(
  15. // 添加,编辑
  16. array('username', 'CheckUserName', '{%student_username_format}', 1, 'callback', 3),
  17. array('id_card', 'CheckIdCard', '{%common_view_id_card_format}', 1, 'callback', 3),
  18. array('gender', array(0,1,2), '{%common_gender_tips}', 1, 'in', 3),
  19. array('birthday', 'CheckBirthday', '{%student_birthday_format}', 1, 'callback', 3),
  20. array('class_id', 'IsExistClass', '{%student_class_tips}', 1, 'callback', 3),
  21. array('region_id', 'IsExistRegion', '{%student_region_tips}', 1, 'callback', 3),
  22. array('state', array(0,1,2,3,4), '{%common_student_state_tips}', 1, 'in', 3),
  23. array('tel', 'CheckTel', '{%common_view_tel_error}', 2, 'callback', 3),
  24. array('my_mobile', 'CheckMyMobile', '{%student_my_mobile_error}', 2, 'callback', 3),
  25. array('parent_mobile', 'CheckParentMobile', '{%common_view_parent_mobile_error}', 1, 'callback', 3),
  26. array('email', 'CheckEmail', '{%common_email_format_error}', 2, 'callback', 3),
  27. array('tuition_state', array(0,1), '{%common_tuition_state_tips}', 1, 'in', 3),
  28. // 添加
  29. array('id_card', 'UniqueIdCard', '{%common_student_exist_error}', 1, 'callback', 1),
  30. // 编辑
  31. array('id_card', 'NoExistIdCard', '{%common_no_exist_id_card_tips}', 1, 'callback', 2),
  32. );
  33. /**
  34. * [UniqueIdCard 身份证和学期号必须唯一]
  35. * @author Devil
  36. * @blog http://gong.gg/
  37. * @version 0.0.1
  38. * @datetime 2016-12-29T17:12:27+0800
  39. * @return [boolean] [存在false, 不存在true]
  40. */
  41. public function UniqueIdCard()
  42. {
  43. // 读取学期配置信息
  44. $semester_id = MyC('admin_semester_id');
  45. if(empty($semester_id) || empty($_POST['id_card']))
  46. {
  47. return false;
  48. }
  49. // 校验是否唯一
  50. $id = $this->db(0)->where(array('id_card'=>I('id_card'), 'semester_id'=>$semester_id))->getField('id');
  51. return empty($id);
  52. }
  53. /**
  54. * [CheckUserName 姓名校验]
  55. * @author Devil
  56. * @blog http://gong.gg/
  57. * @version 0.0.1
  58. * @datetime 2016-12-13T19:29:30+0800
  59. */
  60. public function CheckUserName()
  61. {
  62. $len = Utf8Strlen(I('username'));
  63. return ($len >= 2 && $len <= 16);
  64. }
  65. /**
  66. * [CheckIdCard 身份证号码校验]
  67. * @author Devil
  68. * @blog http://gong.gg/
  69. * @version 0.0.1
  70. * @datetime 2016-12-13T15:12:32+0800
  71. */
  72. public function CheckIdCard()
  73. {
  74. return (preg_match('/'.L('common_regex_id_card').'/', I('id_card')) == 1) ? true : false;
  75. }
  76. /**
  77. * [CheckBirthday 生日校验]
  78. * @author Devil
  79. * @blog http://gong.gg/
  80. * @version 0.0.1
  81. * @datetime 2016-12-13T15:12:32+0800
  82. */
  83. public function CheckBirthday()
  84. {
  85. return (preg_match('/'.L('common_regex_birthday').'/', I('birthday')) == 1) ? true : false;
  86. }
  87. /**
  88. * [IsExistClass 班级id是否存在]
  89. * @author Devil
  90. * @blog http://gong.gg/
  91. * @version 0.0.1
  92. * @datetime 2016-12-21T22:13:52+0800
  93. * @return [boolean] [存在true, 不存在false]
  94. */
  95. public function IsExistClass()
  96. {
  97. // 当用户操作自身的情况下不需要校验
  98. $class = $this->db(0)->table('__CLASS__')->field(array('id', 'pid'))->find(I('class_id'));
  99. if(empty($class))
  100. {
  101. return false;
  102. }
  103. if($class['pid'] == 0)
  104. {
  105. // 是否存在子级
  106. $count = $this->db(0)->table('__CLASS__')->where(array('pid'=>$class['id']))->count();
  107. return ($count == 0);
  108. } else {
  109. // 父级是否存在
  110. $count = $this->db(0)->table('__CLASS__')->where(array('id'=>$class['pid']))->count();
  111. return ($count > 0);
  112. }
  113. }
  114. /**
  115. * [IsExistRegion 地区是否存在]
  116. * @author Devil
  117. * @blog http://gong.gg/
  118. * @version 0.0.1
  119. * @datetime 2016-12-10T14:09:40+0800
  120. * @return [boolean] [存在true, 不存在false]
  121. */
  122. public function IsExistRegion()
  123. {
  124. $id = $this->db(0)->table('__REGION__')->where(array('id'=>I('region_id')))->getField('id');
  125. return !empty($id);
  126. }
  127. /**
  128. * [CheckTel 座机号码校验]
  129. * @author Devil
  130. * @blog http://gong.gg/
  131. * @version 0.0.1
  132. * @datetime 2016-12-13T15:12:32+0800
  133. */
  134. public function CheckTel()
  135. {
  136. return (preg_match('/'.L('common_regex_tel').'/', I('tel')) == 1) ? true : false;
  137. }
  138. /**
  139. * [CheckMyMobile 学生手机号码校验]
  140. * @author Devil
  141. * @blog http://gong.gg/
  142. * @version 0.0.1
  143. * @datetime 2016-12-13T15:12:32+0800
  144. */
  145. public function CheckMyMobile()
  146. {
  147. return (preg_match('/'.L('common_regex_mobile').'/', I('my_mobile')) == 1) ? true : false;
  148. }
  149. /**
  150. * [CheckParentMobile 家长手机号码校验]
  151. * @author Devil
  152. * @blog http://gong.gg/
  153. * @version 0.0.1
  154. * @datetime 2016-12-13T15:12:32+0800
  155. */
  156. public function CheckParentMobile()
  157. {
  158. return (preg_match('/'.L('common_regex_mobile').'/', I('parent_mobile')) == 1) ? true : false;
  159. }
  160. /**
  161. * [CheckEmail 电子邮箱校验]
  162. * @author Devil
  163. * @blog http://gong.gg/
  164. * @version 0.0.1
  165. * @datetime 2016-12-13T15:12:32+0800
  166. */
  167. public function CheckEmail()
  168. {
  169. return (preg_match('/'.L('common_regex_email').'/', I('email')) == 1) ? true : false;
  170. }
  171. /**
  172. * [NoExistIdCard 身份证号码是否存在]
  173. * @author Devil
  174. * @blog http://gong.gg/
  175. * @version 0.0.1
  176. * @datetime 2016-12-10T14:09:40+0800
  177. * @return [boolean] [存在true, 不存在false]
  178. */
  179. public function NoExistIdCard()
  180. {
  181. $id = $this->db(0)->where(array('id_card'=>I('id_card')))->getField('id');
  182. return !empty($id);
  183. }
  184. }
  185. ?>