StudentImportModel.class.php 5.5 KB

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