<?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 AskforaquotemoduladdnewproductModuleFrontController extends ModuleFrontController
{
    public function run()
    {
        $quote_module_token = Tools::getValue('quote_token');
        if ($quote_module_token != md5(sha1(_COOKIE_KEY_.'askforaquotemodul'))) {
            die;
        } else {
            $id_quote = (int) Tools::getValue('td_id_quote');
            $this->module->setCurrencyQuote($id_quote);
            $this->module->findQuoteCurrencyById($id_quote);
            ////////////
            $response = array();
            $id_lang = (int) $this->context->language->id;
            $id_shop = (int) Tools::getValue('jsshopid');
            $key_search = Tools::getValue('search_product');
            
            $db = Db::getInstance(_PS_USE_SQL_SLAVE_);

            $sql_search = 'SELECT DISTINCT pl.name, pl.id_product FROM '. _DB_PREFIX_ .'product_lang as pl INNER JOIN ';
            $sql_search .= _DB_PREFIX_ . 'product as p ON pl.id_product = p.id_product';
            $sql_search .= ' INNER JOIN '._DB_PREFIX_ . 'product_shop as pss ON pl.id_product = pss.id_product';
            $sql_search .= ' WHERE (';
            $sql_search .= ' pl.name LIKE "%'.pSQL($key_search).'%" ';
            $sql_search .= ' OR p.reference LIKE "%'.pSQL($key_search).'%" ';
            $sql_search .= ' OR p.upc LIKE "%'.pSQL($key_search).'%" ';
            $sql_search .= ' OR p.ean13 LIKE "%'.pSQL($key_search).'%" ';
            $sql_search .= ' ) ';
            $sql_search .= ' AND pl.id_lang='.(int)$id_lang.' AND pss.id_shop='.(int)$id_shop;
            $products = $db->executeS($sql_search, true, false);
            if (!empty($products)) {
                foreach ($products as &$product) {
                    $name_formatted = $product['name'].$this->module->l(' (id: ');
                    $name_formatted .= $product['id_product'].$this->module->l(')');
                    $product['name_formatted'] = $name_formatted;
                }
            }
            $response['number_pr'] = count($products);
            $response['products'] = $products;
            echo Tools::jsonEncode($response);
            die;
        }
    }
}
