<?php
/**
* 2007-2020 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@buy-addons.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author    Buy-Addons <contact@buy-addons.com>
*  @copyright 2007-2020 PrestaShop SA
*  @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
* @since 1.6
*/

class AskforaquotemodulcustomersendmessModuleFrontController extends ModuleFrontController
{

    public function run()
    {
        $quote_module_token = Tools::getValue('quote_token');
        if ($quote_module_token != md5(sha1(_COOKIE_KEY_ . 'askforaquotemodul'))) {
            die('error!');
        } else {
            require_once(_PS_MODULE_DIR_.'askforaquotemodul/libs/phpmailer/vendor/autoload.php');
            $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
            if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) {
                    $this->_cipherTool = new PhpEncryption(_NEW_COOKIE_KEY_);
            } else {
                $this->_cipherTool = new Blowfish(_COOKIE_KEY_, _COOKIE_IV_);
            }
            $opt_email_noticequote_cus = Configuration::get('Option_email_noticequote_cus');
            $opt_email_noticequote_adm = Configuration::get('Option_email_noticequote_adm');
            $content_mess = Tools::getValue('content_message_cus');
            $id_quote_dbboo = Tools::getValue('id_quote_cus');
            $date_now = date('Y-m-d H:i:s');
            $arr_language = Language::getLanguages(false);
            $language_site = $this->context->cookie->id_lang;

            $dp_info_cus = 'Select * from ' . _DB_PREFIX_ . 'product_quote_dbboo where id_quote like "';
            $dp_info_cus .= (int) $id_quote_dbboo . '"';
            $info_customer = $db->ExecuteS($dp_info_cus, true, false);
            $ins_mes = 'Insert into ' . _DB_PREFIX_ . 'message_dbboo(id_quote,email,content_mes,date_send,type,status)';
            $ins_mes .= 'values("' . (int) $id_quote_dbboo . '","' . pSQL($info_customer[0]['email']) . '",';
            $ins_mes .= '"' . pSQL($content_mess) . '","' . pSQL($date_now) . '","0","0")';
            $db->query($ins_mes);
            $id_quote_dbboo_view = "id_quote=" . $id_quote_dbboo;
            $encode = $this->_cipherTool->encrypt($id_quote_dbboo_view);
            $link = new Link();
            if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) {
                $link_cus_rep_seo = $link->getModuleLink('askforaquotemodul', 'formrepcustomquote', array(
                'data' => $encode,
                ));
            } else {
                $link_cus_rep_seo = $link->getModuleLink('askforaquotemodul', 'formrepcustom', array(
                'data' => $encode,
                ));
            }
            $this->context->smarty->assign("link_cus_rep", $link_cus_rep_seo);
            $tpl_module = _PS_MODULE_DIR_ . '/askforaquotemodul/views/templates/front/linkmessquotefront.tpl';
            $link_cus_rep = $this->context->smarty->fetch($tpl_module);
            $email_admin_setting = Configuration::get('email_adm_setting');
            $email_from = Configuration::get('email_from_setting');
            $name_from = Configuration::get('name_from_setting');
            $company_name = $info_customer[0]['company_name'];
            $company_address = $info_customer[0]['company_address'];
            $phone = $info_customer[0]['phone'];
            $email = $info_customer[0]['email'];
            $quote_name = $info_customer[0]['quote_name'];
            $shipping_cost = $info_customer[0]['shipping_cost'];
            $tax_cost = $info_customer[0]['tax_cost'];
            $full_name = trim($info_customer[0]['first_name'].' '.$info_customer[0]['last_name']);
            $id_format = $this->module->getIDQuoteformatted($id_quote_dbboo);
            foreach ($arr_language as $key => $value_lang) {
                $value_lang;
                $array_id_language = $arr_language[$key]['id_lang'];
                if ($array_id_language == $language_site) {
                    $array_language_iso_code = $arr_language[$key]['iso_code'];
                    $content_mess_noti_lang = Configuration::get('label_notice_' . $array_language_iso_code);
                    $noti_mess_lang = $this->_cipherTool->decrypt($content_mess_noti_lang);
                    $noti_mess_lang = str_replace("[last_name]", $info_customer[0]['last_name'], $noti_mess_lang);
                    $noti_mess_lang = str_replace("[first_name]", $info_customer[0]['first_name'], $noti_mess_lang);
                    $noti_mess_lang = str_replace("[company_name]", $company_name, $noti_mess_lang);
                    $noti_mess_lang = str_replace("[company_address]", $company_address, $noti_mess_lang);
                    $noti_mess_lang = str_replace("[shipping_cost]", $shipping_cost, $noti_mess_lang);
                    $noti_mess_lang = str_replace("[phone]", $phone, $noti_mess_lang);
                    $noti_mess_lang = str_replace("[quote_name]", $quote_name, $noti_mess_lang);
                    $noti_mess_lang = str_replace("[tax_cost]", $tax_cost, $noti_mess_lang);
                    $noti_mess_lang = str_replace("[email]", $email, $noti_mess_lang);
                    $noti_mess_lang = str_replace("[name]", $full_name, $noti_mess_lang);
                    $noti_mess_lang = str_replace("[quote_id]", $id_format, $noti_mess_lang);
                    $noti_mess_lang = str_replace("[quote_ID]", $id_format, $noti_mess_lang);
                    $noti_mess_lang = str_replace("[message]", nl2br($content_mess), $noti_mess_lang);
                    $noti_mess_lang = str_replace("[messages]", nl2br($content_mess), $noti_mess_lang);
                    $noti_mess_lang = str_replace("[quote_link]", $link_cus_rep, $noti_mess_lang);
                    $noti_mess_lang = str_replace("[quote_url]", $link_cus_rep_seo, $noti_mess_lang);

                    $mail_cus = new PHPMailer\PHPMailer\PHPMailer;
                    $option_button_smtp = Configuration::get('Option_button_smtp');
                    if ($option_button_smtp == '1') {
                        $mail_cus->isSMTP();
                        $mail_cus->Host = Configuration::get('sever_smpt');
                        $mail_cus->SMTPAuth = true;
                        $mail_cus->SMTPAutoTLS = false;
                        $mail_cus->Username = Configuration::get('user_smpt');
                        $mail_cus->Password = Configuration::get('pass_smtp');
                        $mail_cus->SMTPSecure = Configuration::get('secure_smtp');
                        $mail_cus->Port = Configuration::get('port_smtp');
                    }
                    $mail_cus->CharSet = 'UTF-8';
                    $mail_cus->Encoding = "base64";
                    $mail_cus->SetFrom($email_from, $name_from);
                    if ($opt_email_noticequote_cus == 1) {
                        $mail_cus->AddAddress($email);
                        $mail_cus->Subject = $info_customer[0]['quote_name'];
                        $mail_cus->MsgHTML($noti_mess_lang);
                        $mail_cus->Send();
                    }
                    $mail_adm = new PHPMailer\PHPMailer\PHPMailer;
                    $mail_adm->CharSet = 'UTF-8';
                    $mail_adm->Encoding = "base64";
                    $mail_adm->SetFrom($email_from, $name_from);
                    if ($option_button_smtp == '1') {
                        $mail_adm->isSMTP();
                        $mail_adm->Host = Configuration::get('sever_smpt');
                        $mail_adm->SMTPAuth = true;
                        $mail_adm->SMTPAutoTLS = false;
                        $mail_adm->Username = Configuration::get('user_smpt');
                        $mail_adm->Password = Configuration::get('pass_smtp');
                        $mail_adm->SMTPSecure = Configuration::get('secure_smtp');
                        $mail_adm->Port = Configuration::get('port_smtp');
                    }
                    if ($opt_email_noticequote_adm == 1) {
                        if ($email_admin_setting != '') {
                            $mail_adm->AddAddress($email_admin_setting);
                            $mail_adm->Subject = $info_customer[0]['quote_name'];
                            $mail_adm->MsgHTML($noti_mess_lang);
                            $mail_adm->Send();
                        }
                    }
                }
            }
            $sl_mess = 'Select * from ' . _DB_PREFIX_ . 'message_dbboo where id_quote like "';
            $sl_mess .= (int) $id_quote_dbboo . '" and status like "0" and type like "0"';
            $count_mess = $db->ExecuteS($sl_mess, true, false);
            $number_unread = Count($count_mess);
            $ud_pro_mess = 'UPDATE ' . _DB_PREFIX_ . 'product_quote_dbboo SET nb_mess_ud = "';
            $ud_pro_mess .= (int) $number_unread . '" where id_quote like "' . (int) $id_quote_dbboo . '"';
            $db->query($ud_pro_mess);
            $success = 'Success';
            echo Tools::jsonEncode($success);
        }
    }
}
