Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //==============================================================================
- // submit()
- //==============================================================================
- public function submit() {
- $this->load->model((version_compare(VERSION, '3.0', '<') ? 'extension' : 'setting') . '/module');
- $form = $this->{'model_' . (version_compare(VERSION, '3.0', '<') ? 'extension' : 'setting') . '_module'}->getModule($this->request->get['module_id']);
- // AOE Summit Code
- $customerType = '';
- $refererType = '';
- $deliveryMethod = '';
- $message = '0';
- $modelCode='0';
- $gtmData = '';
- $str = '';
- $jsonResponse = array();
- $formResponse = array();
- $form_response_id = 0;
- if(isset($_REQUEST['model'])) {
- $modelCode = $_REQUEST['model'];
- }
- // AOE Summit Code End
- if (!$form['status']) return;
- $language = (isset($this->session->data['language'])) ? $this->session->data['language'] : $this->config->get('config_language');
- $store_name = $this->config->get('config_name');
- if (is_array($store_name)) $store_name = array_shift($store_name);
- // Build jsonRespone for opportunity
- $jsonResponse['form_id'] = $form['module_id'];
- $jsonResponse['form_name'] = $form['name'];
- $jsonResponse['form_title'] = $form['heading_'.$language];
- $jsonResponse['store'] = $store_name;
- $jsonResponse['form_customer_email'] = (isset($form['customer_email']) ? $form['customer_email']:'');
- $jsonResponse['created_at'] = date("Y-m-d H:i:s");
- $jsonResponse['customer_id'] = $this->customer->getId();
- // Check captcha
- foreach ($form['fields'] as $field) {
- if(!isset($field['type'])) continue;
- // AOE Summit Code
- if($field['type'] == 'submit' ) {
- $message = $form['success_message_'.$language];
- }
- if($field['type'] == 'modelselector') {
- $modelCode = $_REQUEST[$field['key']];
- }
- // AOE Summit Code End
- if ($field['type'] == 'captcha') {
- if (!empty($form['recaptcha_secret_key'])) {
- $secret_key = $form['recaptcha_secret_key'];
- } elseif (version_compare(VERSION, '2.1', '<')) {
- $secret_key = $this->config->get('config_google_captcha_secret');
- } elseif (version_compare(VERSION, '3.0', '<')) {
- $secret_key = $this->config->get('google_captcha_secret');
- } else {
- $secret_key = $this->config->get('captcha_google_secret');
- }
- $recaptcha = json_decode(file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secret_key) . '&response=' . $this->request->get['captcha'] . '&remoteip=' . $this->request->server['REMOTE_ADDR']), true);
- if (!$recaptcha['success']) {
- echo $form['error_captcha_' . $language];
- return;
- }
- }
- }
- // Set up e-mail
- if (version_compare(VERSION, '2.0.2', '<')) {
- $mail = new Mail($this->config->get('config_mail'));
- } else {
- if (version_compare(VERSION, '3.0', '<')) {
- $mail = new Mail();
- $mail->protocol = $this->config->get('config_mail_protocol');
- $protocol_engine = $this->config->get('config_mail_protocol');
- } else {
- $mail = new Mail($this->config->get('config_mail_engine'));
- $protocol_engine = $this->config->get('config_mail_engine');
- }
- $mail->parameter = $this->config->get('config_mail_parameter');
- $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
- $mail->smtp_username = $this->config->get('config_mail_smtp_username');
- $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
- $mail->smtp_port = $this->config->get('config_mail_smtp_port');
- $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
- }
- // Format responses
- $responses = array();
- $customer_emails = array();
- $files = array();
- $admin_response_list = '';
- $customer_response_list = '';
- /* sort fields by Key
- $sort_field = array();
- foreach ($form['fields'] as $key => $value) $sort_field[$key] = $value['key'];
- array_multisort($sort_field, SORT_ASC, $form['fields']);
- */
- $replace = array();
- $with = array();
- foreach ($form['fields'] as $field) {
- if(!isset($field['type'])) continue;
- if (in_array($field['type'], array('captcha', 'html', 'submit'))) continue;
- $response = (isset($_REQUEST[$field['key']])) ? $_REQUEST[$field['key']] : '';
- $responses[$field['key']] = ($field['type'] == 'file') ? array() : $response;
- if ($field['type'] == 'email' && !empty($response)) {
- $customer_emails[] = trim($response);
- $jsonResponse["form_customer_email"] = trim($response);
- // MailChimp Integration connection
- $prefix = (version_compare(VERSION, '3.0', '<')) ? '' : 'module_';
- if ($field['key'] == 'mailchimp' && $this->config->get($prefix . 'mailchimp_integration_status')) {
- if (version_compare(VERSION, '2.1', '<')) $this->load->library('mailchimp_integration');
- $mailchimp_integration = new MailChimp_Integration($this->config, $this->db, $this->session, $this->url);
- $mailchimp_integration->send(array('email' => $response, 'newsletter' => 1));
- }
- } elseif ($field['type'] == 'file' && !empty($response)) {
- $filename_array = array();
- foreach ($response as $encrypted_file) {
- if (version_compare(VERSION, '2.1', '<')) $this->load->library('encryption');
- $encryption = new Encryption($this->config->get('config_encryption'));
- $decrypted_file = $encryption->decrypt($encrypted_file);
- $filename = str_replace(strrchr(basename($decrypted_file), '.'), '', basename($decrypted_file));
- $i = 1;
- while (file_exists(DIR_CACHE . $filename)) {
- $filename = $i . '-' . $filename;
- $i++;
- }
- $filename_array[] = $filename;
- $responses[$field['key']][] = $decrypted_file;
- if (file_exists(DIR_DOWNLOAD . $decrypted_file)) {
- copy(DIR_DOWNLOAD . $decrypted_file, DIR_CACHE . $filename);
- if (!$form['record_responses']) {
- unlink(DIR_DOWNLOAD . $decrypted_file);
- }
- $mail->addAttachment(DIR_CACHE . $filename);
- $files[] = DIR_CACHE . $filename;
- }
- }
- $response = $filename_array;
- } else if ($field['type'] == 'modelselector') {
- $modelCode = $response;
- if($modelCode == '-1') {
- $response = 'None Selected';
- }
- }
- $response_string = (is_array($response)) ? nl2br(implode(', ', $response)) : nl2br($response);
- $formResponse[$field['key']] = $response_string;
- $replace[] = '[' . $field['key'] . ']';
- $with[] = $response_string;
- $field_title = strip_tags(html_entity_decode($field['title_' . $language], ENT_QUOTES, 'UTF-8'));
- $response_list_line = '<tr><td style="white-space: nowrap"><strong>' . $field_title . (strpos($field_title, ':') === false ? ':' : '') . '</strong></td> <td>' . $response_string . '</td></tr>' . "\n";
- $admin_response_list .= $response_list_line;
- if ($field['type'] != 'hidden' || !empty($field['email'])) {
- $customer_response_list .= $response_list_line;
- }
- }
- // Put together cart contents
- $products = $this->cart->getProducts();
- if (strpos($form['admin_message_' . $language], '[cart_contents]') && !empty($products)) {
- $cart_contents = '<table><tr><td style="white-space: nowrap"><strong>Cart Contents:</strong></td> <td>';
- $responses['CartContents'] = '';
- foreach ($products as $product) {
- $options_text = '';
- if (!empty($product['option'])) {
- $options = array();
- foreach ($product['option'] as $option) {
- $options[] = $option['name'] . ': ' . $option['value'];
- }
- $options_text = '(' . implode(', ', $options) . ')';
- }
- $product_text = '- ' . $product['name'] . $options_text . ' x ' . $product['quantity'] . ': ' . $this->currency->format($product['total'], $this->session->data['currency']) . '<br />' . "\n";
- $cart_contents .= $product_text;
- $responses['CartContents'] .= $product_text;
- }
- if ($this->cart->countProducts() > 1) {
- $cart_contents .= '<b>Total: ' . $this->currency->format($this->cart->getSubTotal(), $this->session->data['currency']) . '</b>';
- }
- $cart_contents .= '</td></tr></table>' . "\n";
- $replace[] = '[cart_contents]';
- $with[] = $cart_contents;
- }
- foreach($_REQUEST as $name => $value) {
- $splitString = explode("_", $name);
- if(count($splitString) > 1) {
- $dateField = $splitString[0]."_".$splitString[1];
- if ($dateField == "form".$form['module_id']."_d") {
- $responses[$splitString[2]] = $_REQUEST[$name];
- $formResponse[$splitString[2]] = $_REQUEST[$name];
- }
- if ($dateField == "form".$form['module_id']."_t") {
- $responses[$splitString[2].'_slot'] = $_REQUEST[$name];
- $formResponse[$splitString[2].'_slot'] = $_REQUEST[$name];
- }
- }
- }
- // Record response into database
- if ($form['record_responses']) {
- $this->db->query("
- INSERT INTO " . DB_PREFIX . "form_builder_response SET
- module_id = " . (int)$this->request->get['module_id'] . ",
- customer_id = " . (int)$this->customer->getId() . ",
- date_added = NOW(),
- ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "',
- response = '" . $this->db->escape(serialize($responses)) . "',
- readable_response = '" . $this->db->escape(strip_tags($admin_response_list)) . "'
- ");
- // Call Opportunity API to submit for response
- $form_response_id = $this->db->getLastId();
- $jsonResponse['form_builder_response_id'] = $form_response_id;
- if (isset($form['form_type'])) {
- $jsonResponse["form_type"] = $form['form_type'];
- }
- if(isset($_SERVER["HTTP_REFERER"])) {
- if(strpos($_SERVER["HTTP_REFERER"],'dealer') !== false) {
- $refererType = 'dealer';
- } else {
- $refererType = 'national';
- }
- }
- if(isset($_REQUEST['customerType'])) {
- $customerType = $_REQUEST['customerType'];
- }
- if(isset($_REQUEST['deliverymethod'])) {
- $deliveryMethod = $_REQUEST['deliverymethod'];
- }
- if(isset($_COOKIE['_ga'])) {
- $jsonResponse['ga_cookie'] = $_COOKIE['_ga'];
- }
- $jsonResponse["campaign_code"] = $this->getCampaignCode($refererType,$customerType,$deliveryMethod,$form['module_id']);
- $jsonResponse['response'] = $formResponse;
- $record_id = $this->sendOpportunity($form_response_id,json_encode($jsonResponse));
- if($record_id){
- $this->db->query("
- UPDATE " . DB_PREFIX . "form_builder_response SET
- exported = NOW(),
- opportunity_reference = '" . $this->db->escape($record_id) . "'
- WHERE form_builder_response_id =".$form_response_id);
- }
- }
- // Send out e-mails
- // only send emails to admins if its enabled
- if(isset($form['enable_admin_emails']) && $form['enable_admin_emails']) {
- $admin_emails = array_map('trim', explode(',', $form['admin_email']));
- $first_admin = '';
- $html = html_entity_decode($form['admin_message_' . $language], ENT_QUOTES, 'UTF-8');
- $html = str_replace($replace, $with, $html);
- $html = str_replace('[form_responses]', '<table>' . $admin_response_list . '</table>', $html);
- $html = $this->replaceShortcodesByModel($html, $modelCode);
- $html = $this->replaceShortcodes($html, $form);
- $mail->setSender(!empty($customer_emails) ? $customer_emails[0] : str_replace(array(',', '&'), array('', 'and'), html_entity_decode($store_name, ENT_QUOTES, 'UTF-8')));
- $mail->setSubject($this->replaceShortcodes(str_replace($replace, $with, $form['admin_subject_' . $language]), $form));
- $mail->setHtml($html);
- $mail->setText(strip_tags($html));
- foreach ($admin_emails as $email) {
- if (strpos($email, ':')) {
- $if_then = array_map('trim', explode('=', $email));
- $key_value = array_map('trim', explode(':', $if_then[0]));
- if (empty($responses[$key_value[0]]) || $responses[$key_value[0]] != $key_value[1]) {
- continue;
- }
- $email = $if_then[1];
- }
- if ($protocol_engine == 'smtp') {
- $mail->setFrom($this->config->get('config_email'));
- } else {
- $mail->setFrom($email);
- }
- $mail->setReplyTo(!empty($customer_emails) ? $customer_emails[0] : $email);
- $mail->setTo($email);
- $mail->send();
- if (empty($first_admin)) {
- $first_admin = $email;
- }
- }
- }
- if (!empty($customer_emails) && $form['customer_email']) {
- $html = html_entity_decode($form['customer_message_' . $language], ENT_QUOTES, 'UTF-8');
- // Code for IF condition
- $blocks = $this->mappingConditions($html);
- foreach($blocks as $block){
- if(isset($_REQUEST[$block['fieldKey']])) {
- if($_REQUEST[$block['fieldKey']] == $block['fieldVal']) {
- $html = str_replace($block['mainBlock'],$block['showBlock'],$html);
- } else if (is_array($_REQUEST[$block['fieldKey']])) {
- foreach($_REQUEST[$block['fieldKey']] as $fieldKey) {
- if($fieldKey == $block['fieldVal']) {
- $html = str_replace($block['mainBlock'],$block['showBlock'],$html);
- }
- }
- } else {
- $html = str_replace($block['mainBlock'],'',$html);
- }
- }
- else
- {
- $html = str_replace($block['mainBlock'],'',$html);
- }
- }
- // End of code for IF condition
- $html = str_replace($replace, $with, $html);
- $html = str_replace('[form_responses]', '<table>' . $customer_response_list . '</table>', $html);
- $html = $this->replaceShortcodesByModel($html, $modelCode);
- $html = $this->replaceShortcodes($html, $form);
- if ($protocol_engine == 'smtp') {
- $mail->setFrom($form['admin_from']);
- } else {
- $mail->setFrom($form['admin_from']);
- }
- $mail->setSender($form['admin_sender']);
- $mail->setSubject($this->replaceShortcodes(str_replace($replace, $with, $form['customer_subject_' . $language]), $form));
- $mail->setHtml($html);
- $mail->setText(strip_tags($html));
- foreach ($customer_emails as $email) {
- $mail->setTo($email);
- $mail->send();
- }
- }
- // AOE Code for GTM Data Layer Object
- $modelData = array();
- $pageNamePrefix = "-1";
- $virtualPageSuccess = "-1";
- if($modelCode != '-1') {
- $this->load->model('extension/summit/vehicle_model_selector');
- $modelData = $this->model_extension_summit_vehicle_model_selector->getVehicleByModel($modelCode);
- }
- $str = "";
- if(!empty($form['datalayer_success_page_'.$language]) )
- {
- $originalStr = $form['datalayer_success_page_'.$language];
- $arrStr = explode("\n", $originalStr);
- $countVal = 0;
- foreach($arrStr as $val)
- {
- $valArr = explode(":", $val);
- if($countVal > 0)
- {
- $str .= ",";
- }
- $str .= '"'.$valArr[0].'":"'.$valArr[1].'"';
- $countVal = 1;
- }
- if($form['virtual_page'] == "0" || $form['virtual_page'] == "2" )
- {
- $virtualPageSuccess = $form["virtual_page_success"];
- }
- if(isset($form['page_name']) && ($form['page_name'] == "0" || $form['page_name'] == "2"))
- {
- $pageNamePrefix = isset($form['page_name_prefix']) ? $form['page_name_prefix'] : '';
- $pageNamePrefix .= isset($form['success_page_name_prefix']) ? $form['success_page_name_prefix'] : '';
- }
- if($form['vehicle_model_style'] == "0" || $form['vehicle_model_style'] == "2" )
- {
- if(isset($modelData['model_lcdv']))
- {
- $str .= ',"vehicleModelBodystyle":"'.$modelData['model_lcdv'].'"';
- }
- }
- if($form['forms_lead_type'] == "0" || $form['forms_lead_type'] == "2" )
- {
- $str .= ',"formleadType":"'.$form['forms_lead_type_success'].'"';
- }
- if($form['page_category'] == "0" || $form['page_category'] == "2")
- {
- $str .= ',"pageCategory":"'.$form['page_category_success'].'"';
- }
- if($form['vehicle_model_style_label'] == "0" || $form['vehicle_model_style_label'] == "2")
- {
- if($modelCode != '-1' && $modelCode != '0')
- {
- $str .= ',"vehicleModelBodystyleLabel":"'.$modelCode.'"';
- }
- }
- $gtmData = $str;
- }
- // End Code for GTM Data Layer
- // AOE IF Condition Mapping
- $blocks = $this->mappingConditions($message);
- foreach($blocks as $block){
- if(isset($_REQUEST[$block['fieldKey']])) {
- if($_REQUEST[$block['fieldKey']] == $block['fieldVal']) {
- $message = str_replace($block['mainBlock'],$block['showBlock'],$message);
- } else if (is_array($_REQUEST[$block['fieldKey']])) {
- foreach($_REQUEST[$block['fieldKey']] as $fieldKey) {
- if($fieldKey == $block['fieldVal']) {
- $message = str_replace($block['mainBlock'],$block['showBlock'],$message);
- }
- }
- } else {
- $message = str_replace($block['mainBlock'],'',$message);
- }
- }
- else
- {
- $message = str_replace($block['mainBlock'],'',$message);
- }
- }
- // End If Condition Mapping
- // Destroy files
- foreach ($files as $file) {
- if (file_exists($file)) unlink($file);
- }
- $message = $this->replaceShortcodesByModel($message,$modelCode);
- $message = $this->replaceShortcodes($message, $form);
- $message = $message;
- $json = array();
- $json['success'] = 'success';
- $json['pageNamePrefix'] = $pageNamePrefix;
- $json['virtualPageSuccess'] = $virtualPageSuccess;
- $json['message'] = html_entity_decode(trim($message), ENT_QUOTES, 'UTF-8');
- $json['gtmData'] ='{'.$gtmData.'}';
- $json['form_response_id'] = $form_response_id;
- $json['form_id'] =$form['module_id'];
- $json['formresponse'] = json_encode($formResponse);
- $callback = $this->validateRequest($this->request->get['callback']);
- if($callback) {
- $this->response->setOutput($callback."(".json_encode($json).")");
- } else {
- $this->response->setOutput('Invalid request!');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement