Beispiel #1
0
 /**
  * Process posted data and send email
  * @param type $module
  * @param type $params Back-end params
  * @param type $fields All fields of contact form
  */
 public function doPost($module, $params, $fields)
 {
     //xu ly page break và post
     if (JRequest::get('post') && JRequest::getVar('btqc' . $module->id)) {
         $data = JRequest::getVar('btqc' . $module->id);
         //get submitted data
         //validate data by PHP
         foreach ($fields as $field) {
             if ($field->type == 'pagebreak' || $field->type == 'separator') {
                 continue;
             }
             //nếu là checkbox và radio được required mà không được submit hoặc không có giá trị nào
             if ($field->type == 'checkbox' && $field->type == 'radio' && $field->required) {
                 if ($data[$field->alias] && !count($data[$field->alias])) {
                     $this->_result = false;
                     $this->_errorMessages[] = JText::_('ERROR_REQUIRED');
                 }
                 continue;
             } else {
                 if ($field->type == 'file') {
                     //nếu file required thì bắt buộc file check
                     if ($field->required) {
                         if (empty($_FILES) || !$_FILES['btqc' . $module->id]['tmp_name']['btqc_f_' . $field->alias]) {
                             $this->_result = false;
                             $this->_errorMessages[] = JText::_('ERROR_FILE_REQUIRED');
                             continue;
                         }
                     }
                     if (!empty($_FILES)) {
                         if (!key_exists('btqc_f_' . $field->alias, $_FILES['btqc' . $module->id]['tmp_name'])) {
                             continue;
                         }
                         $file = $_FILES['btqc' . $module->id]['tmp_name']['btqc_f_' . $field->alias];
                         if ($file) {
                             $fileExt = explode('.', $_FILES['btqc' . $module->id]['name']['btqc_f_' . $field->alias]);
                             $fileExt = strtolower($fileExt[1]);
                             //check ext
                             if ($field->ext && strpos($field->ext, $fileExt) === false) {
                                 $this->_result = false;
                                 $this->_errorMessages[] = sprintf(JText::_('ERROR_EXT'), str_replace('|', ',', $field->ext));
                             }
                             //check size
                             if (filesize($file) > $field->maxSize * 1024 * 1024) {
                                 $this->_result = false;
                                 $this->_errorMessages[] = sprintf(JText::_('ERROR_MAXSIZE'), $field->maxSize . 'MB');
                             }
                             //neu file khong được sumbit mà được required
                         }
                     }
                     continue;
                 } else {
                     if ($field->required && (!key_exists('btqc_f_' . $field->alias, $data) || !$data['btqc_f_' . $field->alias])) {
                         $this->_result = false;
                         $this->_errorMessages[] = JText::_('ERROR_REQUIRED');
                         continue;
                     }
                     if (!$field->required && (!key_exists('btqc_f_' . $field->alias, $data) || $data['btqc_f_' . $field->alias] == '')) {
                         continue;
                     }
                     //kiểu số
                     if ($field->type == 'number' && $data['btqc_f_' . $field->alias] && !is_numeric($data['btqc_f_' . $field->alias])) {
                         $this->_result = false;
                         $this->_errorMessages[] = JText::_('ERROR_NUMBER');
                     }
                     //kiểu email
                     if ($field->type == 'email' && $data['btqc_f_' . $field->alias]) {
                         $preg = "/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\\._-] +)+\$/";
                         if (preg_match($preg, $data['btqc_f_' . $field->alias])) {
                             $this->_result = false;
                             $this->_errorMessages[] = JText::_('ERROR_EMAIL');
                         }
                     }
                     //kiểu ngày
                     if ($field->type == 'date' && $data['btqc_f_' . $field->alias] && !strtotime($data['btqc_f_' . $field->alias])) {
                         $this->_result = false;
                         $this->_errorMessages[] = JText::_('ERROR_DATE');
                     }
                     continue;
                 }
             }
         }
         //nếu không có lỗi post và có sài captcha thì kiểm tra captcha
         if ($this->_result) {
             if ($params->get('captcha') != '0') {
                 $plugin = BTQuickContactHelper::getCaptchaPlugin($params);
                 if ($plugin) {
                     $captcha = JCaptcha::getInstance($plugin);
                     if (!$captcha->checkAnswer('')) {
                         $this->_result = false;
                         $this->_errorMessages[] = JText::_('ERROR_CAPTCHA');
                         $this->_jsonResponse['captchaError'] = true;
                     }
                 }
             }
         }
         if ($this->_result) {
             //send mail
             if (self::sendMail($data, $fields, $params, $module)) {
                 //show thank you msg
                 $this->_thanksMessage = $params->get('thank_message');
                 //redirect
                 if ($params->get('redirect_url')) {
                     $this->_jsonResponse['redirectUrl'] = $params->get('redirect_url');
                     $this->_jsonResponse['timeOut'] = 3000;
                 }
             } else {
                 $this->_result = false;
                 $this->_errorMessages[] = jText::_('ERROR_SEND_EMAIL');
             }
         }
         if ($this->_result) {
             $this->_jsonResponse['success'] = true;
             $this->_jsonResponse['messages'] = json_encode(array($this->_thanksMessage));
         } else {
             $this->_jsonResponse['success'] = false;
             $this->_jsonResponse['messages'] = json_encode($this->_errorMessages);
         }
         $level = ob_get_level();
         while ($level > 0) {
             ob_end_clean();
             $level--;
         }
         echo json_encode($this->_jsonResponse);
         exit;
     }
 }