Exemple #1
0
    public static function find($id_lang, $expr, $page_number = 1, $page_size = 1, $order_by = 'position', $order_way = 'desc', $ajax = false, $use_cookie = true, Context $context = null, $facets = null)
    {
        $convermax = new ConvermaxAPI();
        if ($ajax) {
            return $convermax->autocomplete($expr);
        }
        unset($use_cookie);
        if (!$context) {
            $context = Context::getContext();
        }
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        if (!Validate::isOrderBy($order_by) || !Validate::isOrderWay($order_way)) {
            return false;
        }
        if ($order_way == 'desc') {
            $order_desc = true;
        } else {
            $order_desc = false;
        }
        $search_results = $convermax->search($expr, $page_number - 1, $page_size, $facets, $order_by, $order_desc);
        if (!$search_results) {
            return false;
        }
        $product_pool = '';
        $items = 'Items';
        foreach ($search_results->{$items} as $item) {
            $product_pool .= (int) $item->id_product . ',';
        }
        $product_order_by = rtrim($product_pool, ',');
        if (empty($product_pool)) {
            return array('total' => 0, 'result' => array(), 'cm_result' => $search_results);
        }
        $product_pool = strpos($product_pool, ',') === false ? ' = ' . (int) $product_pool . ' ' : ' IN (' . rtrim($product_pool, ',') . ') ';
        //sort by convermax result
        $order = 'FIELD(p.`id_product`, ' . $product_order_by . ')';
        $sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity,
				pl.`description_short`, pl.`available_now`, pl.`available_later`, pl.`link_rewrite`, pl.`name`,
			 MAX(image_shop.`id_image`) id_image, il.`legend`, m.`name` manufacturer_name ,
			 MAX(product_attribute_shop.`id_product_attribute`) id_product_attribute,
				DATEDIFF(
					p.`date_add`,
					DATE_SUB(
						NOW(),
						INTERVAL ' . (Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20) . ' DAY
					)
				) > 0 new
				FROM ' . _DB_PREFIX_ . 'product p
				' . Shop::addSqlAssociation('product', 'p') . '
				INNER JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON (
					p.`id_product` = pl.`id_product`
					AND pl.`id_lang` = ' . (int) $id_lang . Shop::addSqlRestrictionOnLang('pl') . '
				)
				LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute` pa	ON (p.`id_product` = pa.`id_product`)
				' . Shop::addSqlAssociation('product_attribute', 'pa', false, 'product_attribute_shop.`default_on` = 1') . '
				' . Product::sqlStock('p', 'product_attribute_shop', false, $context->shop) . '
				LEFT JOIN `' . _DB_PREFIX_ . 'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer`
				LEFT JOIN `' . _DB_PREFIX_ . 'image` i ON (i.`id_product` = p.`id_product`)' . Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1') . '
				LEFT JOIN `' . _DB_PREFIX_ . 'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = ' . (int) $id_lang . ')
				WHERE p.`id_product` ' . $product_pool . '
				GROUP BY product_shop.id_product
				' . ($order ? 'ORDER BY  ' . $order : '');
        $result = $db->executeS($sql);
        $total_hits = 'TotalHits';
        $total = $search_results->{$total_hits};
        if (!$result) {
            $result_properties = false;
        } else {
            $result_properties = Product::getProductsProperties((int) $id_lang, $result);
        }
        $count = count($search_results->Facets);
        for ($i = 0; $i < $count; $i++) {
            if ($search_results->Facets[$i]->FieldName == 'a_Color') {
                $c = count($search_results->Facets[$i]->Values);
                for ($j = 0; $j < $c; $j++) {
                    $colors = Cmsearch::getColor($search_results->Facets[$i]->Values[$j]->Term);
                    $search_results->Facets[$i]->Values[$j]->Value = $colors['name'];
                    $search_results->Facets[$i]->Values[$j]->ColorCode = empty($colors['color']) ? '#FFFFFF' : $colors['color'];
                }
            }
        }
        return array('total' => $total, 'result' => $result_properties, 'cm_result' => $search_results);
    }