PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/application/modules/default/forms/ValidateUser.php

https://gitlab.com/devtoannh/cafe
PHP | 195 lines | 123 code | 29 blank | 43 comment | 33 complexity | 4e087328298bdc070d72fdcb3c0f7a4e MD5 | raw file
  1. <?php
  2. class Default_Form_ValidateUser{
  3. //Chua nhung thong bao loi cua form
  4. protected $_messagesError = null;
  5. //MANG CHUA DU LIEU SAU KHI KIEM TRA
  6. protected $_arrData;
  7. public function __construct($arrParam = array(), $options = null){
  8. //=========================================
  9. //KIEM TRA user_name
  10. //=========================================
  11. if($arrParam['action'] == 'add'){
  12. $options = array('table'=>'users','field'=>'user_name');
  13. }else if($arrParam['action'] == 'edit'){
  14. $clause = ' id !=' . $arrParam['id'];
  15. $options = array('table'=>'users','field'=>'user_name','exclude'=>$clause);
  16. }
  17. $validator = new Zend_Validate();
  18. $validator->addValidator(new Zend_Validate_NotEmpty(),true)
  19. ->addValidator(new Zend_Validate_StringLength(3,32),true)
  20. ->addValidator(new Zend_Validate_Regex('#^[a-zA-Z0-9_\.]+$#'),true)
  21. ->addValidator(new Zend_Validate_Db_NoRecordExists($options),true);
  22. if(!$validator->isValid($arrParam['user_name'])){
  23. $message = $validator->getMessages();
  24. $this->_messagesError['user_name'] = 'User name: ' . current($message);
  25. $arrParam['user_name'] = '';
  26. }
  27. //=========================================
  28. //KIEM TRA user_avatar
  29. //=========================================
  30. $upload = new Zend_File_Transfer_Adapter_Http();
  31. $fileInfo = $upload->getFileInfo('user_avatar');
  32. $fileName = $fileInfo['user_avatar']['name'];
  33. if(!empty($fileName)){
  34. //echo 'co file dc upload';
  35. $upload->addValidator('Extension',true,array('jpg','gif','png'),'user_avatar');
  36. $upload->addValidator('Size',true,array('min'=>'2KB','max'=>'1000KB'),'user_avatar');
  37. if(!$upload->isValid('user_avatar')){
  38. $message = $upload->getMessages();
  39. $this->_messagesError['user_avatar'] = 'Avatar: ' . current($message);
  40. }
  41. }else if(!empty($arrParam['current_user_avatar'])){
  42. $arrParam['user_avatar'] = $arrParam['current_user_avatar'];
  43. }
  44. //=========================================
  45. //KIEM TRA password
  46. //=========================================
  47. $flag = false;
  48. if($arrParam['action'] == 'add'){
  49. $flag = true;
  50. }else if($arrParam['action'] == 'edit'){
  51. if(!empty($arrParam['password'])){
  52. $flag = true;
  53. }
  54. }
  55. if($flag == true){
  56. $validator = new Zend_Validate();
  57. $validator->addValidator(new Zend_Validate_NotEmpty(),true)
  58. ->addValidator(new Zend_Validate_StringLength(3,32),true)
  59. ->addValidator(new Zend_Validate_Regex('#^[a-zA-Z0-9@\#\$%\^&\*\-\+]+$#'),true);
  60. if(!$validator->isValid($arrParam['password'])){
  61. $message = $validator->getMessages();
  62. $this->_messagesError['password'] = 'Password: ' . current($message);
  63. }
  64. }
  65. //=========================================
  66. //KIEM TRA email
  67. //=========================================
  68. if(!empty($arrParam['email'])) {
  69. $validator = new Zend_Validate();
  70. $validator->addValidator(new Zend_Validate_EmailAddress(),true);
  71. if(!$validator->isValid($arrParam['email'])){
  72. $message = $validator->getMessages();
  73. $this->_messagesError['email'] = 'Email: ' . current($message);
  74. }
  75. }
  76. //=========================================
  77. //KIEM TRA group_id
  78. //=========================================
  79. if($arrParam['group_id'] == 0){
  80. $this->_messagesError['group_id'] = 'Nhóm: Bạn cần chọn một nhóm cho thành viên';
  81. }
  82. //=========================================
  83. //KIEM TRA first_name
  84. //=========================================
  85. $validator = new Zend_Validate();
  86. $validator->addValidator(new Zend_Validate_NotEmpty(),true)
  87. ->addValidator(new Zend_Validate_StringLength(2),true);
  88. if(!$validator->isValid($arrParam['name'])){
  89. $message = $validator->getMessages();
  90. $this->_messagesError['first_name'] = 'Họ tên: ' . current($message);
  91. }
  92. //=========================================
  93. //KIEM TRA birthday
  94. //=========================================
  95. if(!empty($arrParam['birthday'])) {
  96. $validator = new Zend_Validate();
  97. $validator->addValidator(new Zend_Validate_Date(array('format'=>'dd/mm/YYYY')),true);
  98. if(!$validator->isValid($arrParam['birthday'])){
  99. $message = $validator->getMessages();
  100. $this->_messagesError['birthday'] = 'Ngày sinh: ' . current($message);
  101. }
  102. }
  103. //=========================================
  104. //KIEM TRA status
  105. //=========================================
  106. if(empty($arrParam['status']) || !isset($arrParam['status'])){
  107. $arrParam['status'] = 0;
  108. }
  109. //=========================================
  110. //TRUYEN CAC GIA TRI DUNG VAO MANG $_arrData
  111. //=========================================
  112. $this->_arrData = $arrParam;
  113. }
  114. //Kiem tra Error
  115. //return true neu co loi xuat hien
  116. public function isError(){
  117. if(count($this->_messagesError) > 0){
  118. return true;
  119. }else{
  120. return false;
  121. }
  122. }
  123. //Tra ve mot mang cac thong bao loi
  124. public function getMessageError(){
  125. return $this->_messagesError;
  126. }
  127. //Tra ve mot du lieu sau khi kiem tra
  128. public function getData($options = null){
  129. if($options['upload'] == true){
  130. $this->_arrData['user_avatar'] = $this->uploadFile();
  131. }
  132. return $this->_arrData;
  133. }
  134. //=========================================
  135. // 1.Upload user_avatar
  136. // 2.Resize kich thuoc (100x100 va 450x450)
  137. // 3.Tra ve ten tap tin upload
  138. //=========================================
  139. public function uploadFile(){
  140. //Duong dan den thu muc upload
  141. $upload_dir = FILE_PATH . '/users/';
  142. //=========================================
  143. //UPLOAD FILE user_avatar
  144. //=========================================
  145. $upload = new Zendvn_File_Upload();
  146. $fileInfo = $upload->getFileInfo('user_avatar');
  147. $fileName = $fileInfo['user_avatar']['name'];
  148. if(!empty($fileName)){
  149. $fileName = $upload->upload('user_avatar', $upload_dir . '/orignal',array('task'=>'rename'),'user_');
  150. $thumb = Zendvn_File_Images::create($upload_dir . '/orignal/' . $fileName);
  151. $thumb->resize(100,100)->save($upload_dir . '/img100x100/' . $fileName);
  152. $thumb = Zendvn_File_Images::create($upload_dir . '/orignal/' . $fileName);
  153. $thumb->resize(450,450)->save($upload_dir . '/img450x450/' . $fileName);
  154. if($this->_arrData['action'] == 'edit'){
  155. $upload->removeFile($upload_dir . '/orignal/' . $this->_arrData['current_user_avatar']);
  156. $upload->removeFile($upload_dir . '/img100x100/' . $this->_arrData['current_user_avatar']);
  157. $upload->removeFile($upload_dir . '/img450x450/' . $this->_arrData['current_user_avatar']);
  158. }
  159. }else{
  160. if($this->_arrData['action'] == 'edit'){
  161. $fileName = $this->_arrData['current_user_avatar'];
  162. }
  163. }
  164. return $fileName;
  165. }
  166. }