Exemplo n.º 1
0
 private function _getCriteriaFromEmplacement($id_search, $id_category_root = false)
 {
     $cacheKey = sha1(serialize(func_get_args()));
     if (isset(self::$_getCriteriaFromEmplacementCache[$cacheKey])) {
         return self::$_getCriteriaFromEmplacementCache[$cacheKey];
     }
     if ($id_category_root !== false && is_numeric($id_category_root) && (int) $id_category_root > 0) {
         $criterion = AdvancedSearchClass::getCriterionsWithIdGroupFromIdLinked('category', (int) $id_category_root, (int) $id_search);
     } else {
         if ($id_category = Tools::getIsset('id_category') ? (int) Tools::getValue('id_category') : (int) Tools::getValue('id_category_search')) {
             $criterion = AdvancedSearchClass::getCriterionsWithIdGroupFromIdLinked('category', $id_category, (int) $id_search);
         } elseif ($id_manufacturer = Tools::getIsset('id_manufacturer') ? (int) Tools::getValue('id_manufacturer') : (int) Tools::getValue('id_manufacturer_search')) {
             $criterion = AdvancedSearchClass::getCriterionsWithIdGroupFromIdLinked('manufacturer', $id_manufacturer, (int) $id_search);
         } elseif ($id_supplier = Tools::getIsset('id_supplier') ? (int) Tools::getValue('id_supplier') : (int) Tools::getValue('id_supplier_search')) {
             $criterion = AdvancedSearchClass::getCriterionsWithIdGroupFromIdLinked('supplier', $id_supplier, (int) $id_search);
         }
     }
     self::$_getCriteriaFromEmplacementCache[$cacheKey] = isset($criterion) && $criterion ? $criterion : array();
     return self::$_getCriteriaFromEmplacementCache[$cacheKey];
 }
Exemplo n.º 2
0
    public static function getCriterionsForSearchBloc($search, $id_criterion_group, $id_lang, $count_product = false, $selected_criterion = array(), $selected_criteria_groups_type = array(), $id_currency = false, $id_country = false, $id_group = false, $visible = false, $groupInfos = false, $base_selection = false, $criterion_groups)
    {
        if ($groupInfos['sort_by'] == '' || $groupInfos['sort_by'] == 'position' || !$id_lang) {
            $field_order_by = '`position`';
        } elseif ($groupInfos['criterion_group_type'] == 'weight' || $groupInfos['criterion_group_type'] == 'width' || $groupInfos['criterion_group_type'] == 'height' || $groupInfos['criterion_group_type'] == 'depth') {
            $field_order_by = '`value`';
        } elseif ($groupInfos['sort_by'] == 'numeric') {
            $field_order_by = 'CAST(REPLACE(IF(ac.`single_value` != "",ac.`single_value`,acl.`value`), ",", ".") AS DECIMAL(10,2))';
        } elseif ($groupInfos['sort_by'] == 'nb_product') {
            $field_order_by = 'nb_product';
        } else {
            $field_order_by = '`value`';
        }
        $selected_criterion = PM_AdvancedSearch4::array_map_recursive('intval', $selected_criterion);
        $selected_criterion_copy = $selected_criterion;
        if (isset($selected_criterion[$id_criterion_group])) {
            unset($selected_criterion[$id_criterion_group]);
        }
        if (!$id_country) {
            $id_country = (int) (version_compare(_PS_VERSION_, '1.5.0.0', '>=') ? Context::getContext()->country->id : Country::getDefaultCountryId());
        }
        $now = date('Y-m-d H:i:s');
        $cacheKey = sha1(serialize(func_get_args()) . $id_country);
        if (isset(self::$_getCriterionsForSearchBlocCache[$cacheKey])) {
            return self::$_getCriterionsForSearchBlocCache[$cacheKey];
        }
        $leftJoinWhereCriterion = self::makeLeftJoinWhereCriterion('getCriterionsForSearchBloc', $search, $id_lang, $selected_criterion, $selected_criteria_groups_type, $id_criterion_group, $groupInfos['criterion_group_type'] == 'attribute', $id_currency, $id_country, $id_group, false, false, $groupInfos['criterion_group_type'], $criterion_groups);
        if ($groupInfos['criterion_group_type'] == 'category' && !$groupInfos['id_criterion_group_linked'] && !$groupInfos['show_all_depth']) {
            if (isset($selected_criterion_copy[$id_criterion_group]) && sizeof($selected_criterion_copy[$id_criterion_group])) {
                $current_level_depth = self::getAsCriterionCategoryHigherLevelDepth($search['id_search'], $selected_criterion_copy[$id_criterion_group]);
                $current_linked_categories = self::getIdLinkedFromCriterion($selected_criterion_copy[$id_criterion_group], $search['id_search'], $current_level_depth);
                $leftJoinWhereCriterion['where'][] = $leftJoinWhereCriterion['whereUnion'][] = '(ac.`id_criterion` IN (' . implode(',', $selected_criterion_copy[$id_criterion_group]) . ') OR (ac.`level_depth` = ' . ((int) $current_level_depth + 1) . ' AND ac.`id_parent` IN (' . implode(',', $current_linked_categories) . ')))';
            } else {
                $leftJoinWhereCriterion['where'][] = $leftJoinWhereCriterion['whereUnion'][] = 'ac.`level_depth` = 1';
            }
        }
        if ($search['filter_by_emplacement'] && Tools::getValue('id_seo') == false && (Tools::getValue('id_manufacturer', Tools::getValue('id_manufacturer_search')) || Tools::getValue('id_supplier', Tools::getValue('id_supplier')))) {
            $id_manufacturer = Tools::getIsset('id_manufacturer') ? (int) Tools::getValue('id_manufacturer') : (int) Tools::getValue('id_manufacturer_search');
            $id_supplier = Tools::getIsset('id_supplier') ? (int) Tools::getValue('id_supplier') : (int) Tools::getValue('id_supplier_search');
            $preSelectedCriterion = array();
            if ($id_manufacturer) {
                $preSelectedCriterion = AdvancedSearchClass::getCriterionsWithIdGroupFromIdLinked('manufacturer', $id_manufacturer, (int) $search['id_search']);
            } else {
                if ($id_supplier) {
                    $preSelectedCriterion = AdvancedSearchClass::getCriterionsWithIdGroupFromIdLinked('supplier', $id_supplier, (int) $search['id_search']);
                }
            }
            if (is_array($preSelectedCriterion) && isset($preSelectedCriterion[$id_criterion_group]) && AdvancedSearchCoreClass::_isFilledArray($preSelectedCriterion[$id_criterion_group])) {
                $leftJoinWhereCriterion['where'][] = $leftJoinWhereCriterion['whereUnion'][] = '(ac.`id_criterion` IN (' . implode(',', $preSelectedCriterion[$id_criterion_group]) . '))';
            }
        }
        if ($search['filter_by_emplacement'] && Tools::getValue('id_seo') !== false && is_numeric(Tools::getValue('id_seo')) && Tools::getValue('id_seo') > 0) {
            $search['selected_criteres_seo'] = array();
            $seo_search = new AdvancedSearchSeoClass((int) Tools::getValue('id_seo'));
            if (Validate::isLoadedObject($seo_search) && isset($seo_search->criteria) && !empty($seo_search->criteria)) {
                $criteres_seo = @unserialize($seo_search->criteria);
                if (AdvancedSearchCoreClass::_isFilledArray($criteres_seo)) {
                    foreach ($criteres_seo as $critere_seo) {
                        $critere_seo = explode('_', $critere_seo);
                        $id_criterion_group_seo = (int) $critere_seo[0];
                        $id_criterion_value = (int) $critere_seo[1];
                        if (isset($selected_criterion[$id_criterion_group_seo])) {
                            if (!in_array($id_criterion_value, $selected_criterion[$id_criterion_group_seo])) {
                                $search['selected_criteres_seo'][$id_criterion_group_seo][] = $id_criterion_value;
                            }
                        } else {
                            $search['selected_criteres_seo'][$id_criterion_group_seo][] = $id_criterion_value;
                        }
                    }
                }
            }
        }
        if (isset($search['selected_criteres_seo']) && AdvancedSearchCoreClass::_isFilledArray($search['selected_criteres_seo']) && $search['filter_by_emplacement'] && Tools::getValue('id_seo') !== false && is_numeric(Tools::getValue('id_seo')) && Tools::getValue('id_seo') > 0) {
            $leftJoinWhereCriterion['where'][] = $leftJoinWhereCriterion['whereUnion'][] = '(ac.`id_criterion` IN (' . implode(',', $search['selected_criteres_seo'][$id_criterion_group]) . '))';
        }
        if (self::isCategoryGroup($search['id_search'], $id_criterion_group, true)) {
            $current_category_depth = $groupInfos['id_criterion_group_linked'];
            if (is_array($selected_criterion_copy) && sizeof($selected_criterion_copy)) {
                foreach ($selected_criterion_copy as $id_criterion_group2 => $criteria2) {
                    if ($selected_criteria_groups_type[$id_criterion_group2]['criterion_group_type'] == 'category' && $id_criterion_group2 != $id_criterion_group && $selected_criteria_groups_type[$id_criterion_group2]['id_criterion_group_linked'] < $current_category_depth) {
                        if (!isset($prev_category_depth) || $prev_category_depth < $selected_criteria_groups_type[$id_criterion_group2]['id_criterion_group_linked']) {
                            $prev_category_depth = $selected_criteria_groups_type[$id_criterion_group2]['id_criterion_group_linked'];
                            $criteria_category_parent = $criteria2;
                        }
                    }
                }
                if (isset($criteria_category_parent)) {
                    $childsCategoriesId = self::getChildsCategoriesId(self::getIdLinkedFromCriterion($criteria_category_parent, $search['id_search']));
                    $leftJoinWhereCriterion['where'][] = '(aclink.`id_criterion_linked` IN(' . implode(', ', $childsCategoriesId) . '))';
                }
            }
        }
        if ($groupInfos['criterion_group_type'] == 'category') {
            $groups = FrontController::getCurrentCustomerGroups();
            $leftJoinWhereCriterion['join'][] = $leftJoinWhereCriterion['joinUnion'][] = 'LEFT JOIN `' . _DB_PREFIX_ . 'category_group` cg ON ( cg.`id_category` = aclink.`id_criterion_linked`)';
            $leftJoinWhereCriterion['where'][] = $leftJoinWhereCriterion['whereUnion'][] = 'cg.`id_group` ' . (count($groups) ? 'IN (' . implode(',', $groups) . ')' : '= 1');
        }
        $query = '';
        if ($leftJoinWhereCriterion['make_union']) {
            if ($groupInfos['sort_by'] == 'numeric') {
                $field_order_by = 'CAST(REPLACE(IF(`single_value` != "",`single_value`,`value`), ",", ".") AS DECIMAL(10,2))';
            }
            $query .= 'SELECT * FROM (';
        }
        $query .= 'SELECT ac.*, aclink.`id_criterion_linked` ' . ((int) $id_lang ? ', acl.id_lang, acl.icon' : '') . ', IF(ac.`single_value` != "",ac.`single_value`,acl.`value`) AS `value`, (' . (AdvancedSearchCoreClass::_isFilledArray($leftJoinWhereCriterion['count']) ? 'COUNT(DISTINCT ' . implode(' + ', $leftJoinWhereCriterion['count']) . ')' : 'COUNT(DISTINCT acpc.`id_cache_product`)') . ') AS nb_product' . '
		FROM `' . _DB_PREFIX_ . 'pm_advancedsearch_criterion_' . (int) $search['id_search'] . '` ac
		JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_criterion_' . (int) $search['id_search'] . '_link` aclink ON (ac.`id_criterion` = aclink.`id_criterion`)
		JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_criterion_' . (int) $search['id_search'] . '_list` aclist ON (ac.`id_criterion` = aclist.`id_criterion_parent`)
		' . ($id_lang ? 'LEFT JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_criterion_' . (int) $search['id_search'] . '_lang` acl ON (ac.`id_criterion` = acl.`id_criterion` AND acl.`id_lang` = ' . (int) $id_lang . ')' : '') . ($search['display_empty_criteria'] ? 'LEFT ' : '') . 'JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_cache_product_criterion_' . (int) $search['id_search'] . '` acpc ON (aclist.`id_criterion` = acpc.`id_criterion`)
		LEFT JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_cache_product_' . (int) $search['id_search'] . '` acp ON ( ' . (AdvancedSearchCoreClass::_isFilledArray(PM_AdvancedSearch4::$productFilterList) ? ' acp.`id_product` IN (' . implode(',', PM_AdvancedSearch4::$productFilterList) . ') AND ' : '') . ' acp.`id_cache_product` = acpc.`id_cache_product`)
		' . (AdvancedSearchCoreClass::_isFilledArray($leftJoinWhereCriterion['join']) ? implode(' ', $leftJoinWhereCriterion['join']) : '') . 'WHERE ' . (AdvancedSearchCoreClass::_isFilledArray(PM_AdvancedSearch4::$productFilterList) ? ' acp.`id_product` IN (' . implode(',', PM_AdvancedSearch4::$productFilterList) . ') AND ' : '') . '
		ac.`id_criterion_group` = ' . (int) $id_criterion_group . ($visible ? ' AND ac.`visible` = 1' : '') . ' AND ' . (AdvancedSearchCoreClass::_isFilledArray($leftJoinWhereCriterion['where']) ? implode(' AND ', $leftJoinWhereCriterion['where']) : '1') . ' GROUP BY ac.`id_criterion`' . ($leftJoinWhereCriterion['make_union'] ? '' : '
		ORDER BY ' . $field_order_by . ' ' . $groupInfos['sort_way']);
        if ($leftJoinWhereCriterion['make_union']) {
            $query .= ' UNION
			 SELECT ac.*, aclink.`id_criterion_linked`, acl.id_lang, acl.icon, IF(ac.`single_value` != "",ac.`single_value`,acl.`value`) AS `value`,
			 0 AS nb_product
			 FROM `' . _DB_PREFIX_ . 'pm_advancedsearch_criterion_' . (int) $search['id_search'] . '` ac
			 JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_criterion_' . (int) $search['id_search'] . '_link` aclink ON (ac.`id_criterion` = aclink.`id_criterion`)
			 JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_criterion_' . (int) $search['id_search'] . '_list` aclist ON (ac.`id_criterion` = aclist.`id_criterion`)
			 LEFT JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_criterion_' . (int) $search['id_search'] . '_lang` acl ON (ac.`id_criterion` = acl.`id_criterion` AND acl.`id_lang` = ' . (int) $id_lang . ')
			 ' . (AdvancedSearchCoreClass::_isFilledArray($leftJoinWhereCriterion['joinUnion']) ? implode(' ', $leftJoinWhereCriterion['joinUnion']) : '') . ($base_selection ? '
			JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_cache_product_criterion_' . (int) $search['id_search'] . '` acpc ON (aclist.`id_criterion` = acpc.`id_criterion`)
			JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_cache_product_' . (int) $search['id_search'] . '` acp ON ( ' . (AdvancedSearchCoreClass::_isFilledArray(PM_AdvancedSearch4::$productFilterList) ? ' acp.`id_product` IN (' . implode(',', PM_AdvancedSearch4::$productFilterList) . ') AND ' : '') . ' acp.`id_cache_product` = acpc.`id_cache_product`)
			JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_cache_product_criterion_' . (int) $search['id_search'] . '` acpc2 ON ( acp.`id_cache_product` = acpc2.`id_cache_product` AND acpc2.`id_criterion` IN(' . implode(', ', $base_selection) . '))
			' : '') . '
			WHERE ' . ($base_selection && AdvancedSearchCoreClass::_isFilledArray(PM_AdvancedSearch4::$productFilterList) ? ' acp.`id_product` IN (' . implode(',', PM_AdvancedSearch4::$productFilterList) . ') AND ' : '') . '
			ac.`id_criterion_group` = ' . (int) $id_criterion_group . ($visible ? ' AND ac.`visible` = 1' : '') . ' AND ' . (AdvancedSearchCoreClass::_isFilledArray($leftJoinWhereCriterion['whereUnion']) ? implode(' AND ', $leftJoinWhereCriterion['whereUnion']) : '1') . '
			GROUP BY ac.`id_criterion`
			 ) as tmp GROUP BY `id_criterion`
			ORDER BY ' . $field_order_by . ' ' . $groupInfos['sort_way'];
        }
        self::$_getCriterionsForSearchBlocCache[$cacheKey] = Db::getInstance()->ExecuteS($query);
        return self::$_getCriterionsForSearchBlocCache[$cacheKey];
    }