Example #1
0
    protected function displaySortCriteriaPanel($objCrit = false)
    {
        if (Tools::getValue('pm_load_function') != 'displaySortCriteriaPanel') {
            $this->_html .= '<div id="sortCriteriaPanel">';
        } else {
            $objCrit = new AdvancedSearchCriterionGroupClass(Tools::getValue('id_criterion_group'), Tools::getValue('id_search'));
            if (Tools::getValue('sort_way')) {
                $objCrit->sort_by = Tools::getValue('sort_by');
                $objCrit->sort_way = Tools::getValue('sort_way');
                $objCrit->save();
                $msgConfirm = $this->l('Specific sort apply');
                if ($objCrit->sort_by == 'position') {
                    $msgConfirm .= '<br />' . $this->l('Now, you can sort criteria by drag n drop');
                }
                $this->_html .= '<script type="text/javascript">show_info("' . addcslashes($msgConfirm, '"') . '");</script>';
            }
        }
        $criterions = AdvancedSearchClass::getCriterionsFromCriterionGroup($objCrit->criterion_group_type, $objCrit->id_criterion_group_linked, $objCrit->id_search, $objCrit->sort_by, $objCrit->sort_way, $this->_cookie->id_lang);
        $this->_html .= '
		<div class="clear"></div>
		<div class="criterionGroupActions">
			<ul>
				<li><a href="' . $this->_base_config_url . '&pm_load_function=processEnableAllCriterions&id_criterion_group=' . $objCrit->id . '&id_search=' . $objCrit->id_search . '" class="ajax_script_load activeAllCriterions" title="' . $this->l('Activate all criterions') . '">' . $this->l('Activate all criterions') . '</a></li>
				<li><a href="' . $this->_base_config_url . '&pm_load_function=processDisableAllCriterions&id_criterion_group=' . $objCrit->id . '&id_search=' . $objCrit->id_search . '" class="ajax_script_load disableAllCriterions" title="' . $this->l('Disable all criterions') . '">' . $this->l('Disable all criterions') . '</a></li>
			</ul>
		</div>
		';
        $this->_html .= '<div class="clear"></div><ul class="sortableCriterion" id="sortableCriterion_' . (int) $objCrit->id_criterion_group_linked . '">';
        foreach ($criterions as $row) {
            $objCritClass = new AdvancedSearchCriterionClass($row['id_criterion'], $objCrit->id_search);
            $this->_html .= '<li class="ui-state-highlight' . (isset($row['is_custom']) && $row['is_custom'] ? ' customCriterionLi' : '') . '" id="criterion_' . $row['id_criterion'] . '" style="height:30px;">';
            $this->_html .= '<span class="ui-icon ui-icon-arrow-4-diag dragIcon dragIconCriterion" style="float:left;margin:0!important;' . ($objCrit->sort_by == 'position' ? '' : ' visibility:hidden') . '">
							</span>';
            $this->_html .= '<span class="critName">' . $row['value'] . '</span>';
            if ($objCrit->display_type == 2) {
                $this->_html .= '<div class="criterionImageContainer">';
                $this->_html .= '<form class="criterionForm" action="' . $this->_base_config_url . '" method="post" enctype="multipart/form-data" target="dialogIframePostForm">';
                $this->displayInlineUploadFile($objCritClass, 'icon' . $row['id_criterion'], 'icon', $this->l('Image'), '/search_files/criterions/');
                $this->_html .= '<input name="id_search" value="' . (int) $objCrit->id_search . '" type="hidden" />';
                $this->_html .= '<input name="id_criterion" value="' . (int) $row['id_criterion'] . '" type="hidden" />';
                $this->_html .= '<input name="key_criterions_group" value="' . $objCrit->criterion_group_type . '-' . (int) $objCrit->id_criterion_group_linked . '-' . (int) $objCrit->id_search . '" type="hidden" />';
                $this->_html .= '<a href="' . $this->_base_config_url . '&pm_load_function=processActiveCriterion&id_criterion=' . $row['id_criterion'] . '&id_search=' . $objCrit->id_search . '" class="ajax_script_load activeCriterion">
									<img src="../img/admin/' . ($row['visible'] ? 'enabled' : 'disabled') . '.gif" id="imgActiveCriterion' . $row['id_criterion'] . '" class="imgActiveCrit" />
								</a>';
                $this->_html .= '</form>';
                $this->_html .= '</div>';
            } else {
                if ($objCrit->criterion_group_type == 'attribute' && AdvancedSearchClass::isColorAttributesGroup($objCrit->id_criterion_group_linked) && isset($row['is_custom']) && $row['is_custom']) {
                    $this->_html .= '<div class="criterionCustomLiveEditContainer" data-id-criterion="' . (int) $row['id_criterion'] . '" data-id-search="' . (int) $objCrit->id_search . '">';
                    $this->_html .= '<div class="criterionCustomLiveField">';
                    $this->_displayInputTextLang(array('obj' => $objCritClass, 'key' => 'value', 'label' => $this->l('Criterion label:')));
                    $this->_html .= '</div>';
                    $this->_html .= '<div class="criterionCustomLiveField">';
                    $this->_displayInputColor(array('obj' => $objCritClass, 'key' => 'color', 'label' => $this->l('Color:')));
                    $this->_html .= '</div>';
                    $this->_html .= '<div style="float: right;">';
                    $this->_displaySubmit($this->l('Save'), 'submitCustomCriterionForm');
                    $this->_html .= '</div>';
                    $this->_html .= '</div>';
                    $this->_html .= '<a href="' . $this->_base_config_url . '&pm_load_function=processActiveCriterion&id_criterion=' . $row['id_criterion'] . '&id_search=' . $objCrit->id_search . '" class="ajax_script_load activeCriterion"><img src="../img/admin/' . ($row['visible'] ? 'enabled' : 'disabled') . '.gif" id="imgActiveCriterion' . $row['id_criterion'] . '" /></a>';
                    $this->_html .= '<a href="javascript:void(0)" class="editCustomCriterion"><img src="../img/admin/edit.gif" id="imgEditCriterion' . $row['id_criterion'] . '" class="imgEditCriterion" /></a>';
                    $this->_html .= '<a href="' . $this->_base_config_url . '&pm_load_function=processDeleteCustomCriterion&id_criterion=' . $row['id_criterion'] . '&id_search=' . $objCrit->id_search . '" class="ajax_script_load deleteCustomCriterion"><img src="../img/admin/delete.gif" id="imgDeleteCriterion' . $row['id_criterion'] . '" class="imgDeleteCriterion" /></a>';
                } else {
                    if ($objCrit->criterion_group_type == 'attribute' && AdvancedSearchClass::isColorAttributesGroup($objCrit->id_criterion_group_linked) && (!isset($row['is_custom']) || isset($row['is_custom']) && !$row['is_custom'])) {
                        $this->_html .= '<a href="' . $this->_base_config_url . '&pm_load_function=processActiveCriterion&id_criterion=' . $row['id_criterion'] . '&id_search=' . $objCrit->id_search . '" class="ajax_script_load activeCriterion"><img src="../img/admin/' . ($row['visible'] ? 'enabled' : 'disabled') . '.gif" id="imgActiveCriterion' . $row['id_criterion'] . '" /></a>';
                        $customCriterionsList = AdvancedSearchCriterionClass::getCustomCriterions($objCrit->id_search, $objCrit->id, $this->_cookie->id_lang);
                        $customCriterionsIdsList = AdvancedSearchCriterionClass::getCustomMasterIdCriterion((int) $objCrit->id_search, $row['id_criterion']);
                        if (is_array($customCriterionsList) && sizeof($customCriterionsList)) {
                            $customCriterionsList = array(0 => $this->l('None')) + $customCriterionsList;
                            $this->_html .= '<div class="addCriterionToCustomGroupContainer">';
                            $this->_displaySelect(array('obj' => (object) array('custom_group_link_id_' . (int) $row['id_criterion'] => AdvancedSearchCriterionClass::getCustomMasterIdCriterion((int) $objCrit->id_search, $row['id_criterion'])), 'label' => $this->l('Add to a group of custom criterion:'), 'key' => 'custom_group_link_id_' . (int) $row['id_criterion'], 'options' => $customCriterionsList, 'defaultvalue' => false, 'onchange' => 'processAddCustomCriterionToGroup($jqPm(this), ' . (int) $objCrit->id_search . ', ' . (int) Tools::getValue('id_criterion_group') . ')'));
                            $this->_html .= '</div>';
                        }
                    } else {
                        $this->_html .= '<a href="' . $this->_base_config_url . '&pm_load_function=processActiveCriterion&id_criterion=' . $row['id_criterion'] . '&id_search=' . $objCrit->id_search . '" class="ajax_script_load activeCriterion"><img src="../img/admin/' . ($row['visible'] ? 'enabled' : 'disabled') . '.gif" id="imgActiveCriterion' . $row['id_criterion'] . '" /></a>';
                    }
                }
            }
            $this->_html .= '</li>';
        }
        $this->_html .= '</ul>';
        if ($objCrit->sort_by == 'position') {
            $this->_html .= '<script type="text/javascript">$jqPm("#sortableCriterion_' . (int) $objCrit->id_criterion_group_linked . '").sortable({
					        handle : ".dragIconCriterion",
					                update: function(event, ui) {
					           var order = $jqPm(this).sortable("toArray");
					                saveOrder(order.join(","),"orderCriterion",' . (int) $objCrit->id_search . ');
					              }
					      });</script>';
        }
        if (Tools::getValue('pm_load_function') != 'displaySortCriteriaPanel') {
            $this->_html .= '</div>';
        }
    }
Example #2
0
    public static function makeLeftJoinWhereCriterion($fromMethod, $search, $id_lang, $selected_criterion, $selected_criteria_groups_type = array(), $current_id_criterion_group = false, $is_attribute_group = false, $id_currency = false, $id_country = false, $id_group = false, $include_price_table = false, $include_product_table = false, $group_type = false, $criterion_groups = array())
    {
        if (version_compare(_PS_VERSION_, '1.5.0.0', '>=')) {
            $context = Context::getContext();
            if (!$id_currency) {
                $id_currency = $context->currency->id;
            }
        } else {
            if (!$id_currency) {
                $id_currency = Currency::getCurrent()->id;
            }
        }
        $join_criterion_tables = array();
        $join_criterion = array();
        $count_criterion = array();
        $where_criterion = array();
        $where_qty = array();
        $field_select = array();
        $attribute_selected = false;
        $lastAttributeCombinationTableId = false;
        $stock_management = (int) Configuration::get('PS_STOCK_MANAGEMENT') ? true : false;
        if (!$stock_management) {
            $search['search_on_stock'] = false;
        }
        if ($group_type == 'stock' && $stock_management) {
            $strict_stock = true;
        } else {
            $strict_stock = false;
        }
        if ($stock_management && AdvancedSearchCoreClass::_isFilledArray($selected_criterion) && AdvancedSearchCoreClass::_isFilledArray($criterion_groups)) {
            foreach ($selected_criterion as $id_criterion_group_tmp => $id_criterion_tmp) {
                foreach ($criterion_groups as $criterion_group) {
                    if ($criterion_group['id_criterion_group'] == $id_criterion_group_tmp && $criterion_group['criterion_group_type'] == 'stock') {
                        $search['search_on_stock'] = true;
                        $strict_stock = true;
                        break;
                    }
                }
            }
        }
        $having = array();
        $where_price_range = array();
        if (version_compare(_PS_VERSION_, '1.5.0.0', '>=')) {
            $table_stock_index = 0;
        }
        $idSelectedCriteria = implode('-', self::array_values_recursive($selected_criterion));
        $cacheKey = sha1($fromMethod . $search['id_search'] . $idSelectedCriteria . '-' . implode('-', array_keys($selected_criterion)) . '-' . (int) $current_id_criterion_group . (int) $include_price_table . (int) $include_product_table . (int) $id_lang . (int) $is_attribute_group . (int) $group_type . (int) $strict_stock);
        if (isset(self::$_cacheLeftJoinWhereCriterion[$cacheKey])) {
            return self::$_cacheLeftJoinWhereCriterion[$cacheKey];
        }
        if ($group_type && !$include_product_table && $search['display_empty_criteria']) {
            $make_union = true;
        } else {
            $make_union = false;
        }
        $price_is_included = false;
        if (version_compare(_PS_VERSION_, '1.5.0.0', '>=')) {
            $join_criterion[] = 'JOIN `' . _DB_PREFIX_ . 'product_shop` ps ON (' . (AdvancedSearchCoreClass::_isFilledArray(PM_AdvancedSearch4::$productFilterList) ? ' ps.`id_product` IN (' . implode(',', PM_AdvancedSearch4::$productFilterList) . ') AND ' : '') . 'ps.id_shop IN (' . implode(', ', Shop::getContextListShopID()) . ') AND ps.`id_product` = acp.`id_product`)';
            $join_criterion_tables[] = 'ps';
        }
        if (AdvancedSearchCoreClass::_isFilledArray($selected_criterion)) {
            $price_is_included = false;
            $attribute_qty_compare_on_join = array();
            $now = date('Y-m-d H:i:s');
            foreach ($selected_criterion as $id_criterion_group => $id_criterion) {
                if ($selected_criteria_groups_type[$id_criterion_group]['criterion_group_type'] == 'stock') {
                    $strict_stock = true;
                    continue;
                }
                $join_criterion_table = false;
                $join_price_table = false;
                $where_join = array();
                $where_single_value_range = array();
                $where_translatable_value_range = array();
                if (isset($selected_criteria_groups_type[$id_criterion_group]) && ($selected_criteria_groups_type[$id_criterion_group]['display_type'] == 5 || $selected_criteria_groups_type[$id_criterion_group]['range'])) {
                    $id_currency_default = Configuration::get('PS_CURRENCY_DEFAULT');
                    if ($id_currency != $id_currency_default) {
                        $currency = new Currency($id_currency);
                        $conversion_rate = $currency->conversion_rate;
                    } else {
                        $conversion_rate = 0;
                    }
                    $where_price_criterion = array();
                    foreach ($id_criterion as $range) {
                        $range = explode('-', $range);
                        $original_range = $range;
                        if ($conversion_rate > 0) {
                            $range[0] = $range[0] / $conversion_rate;
                            if (isset($range[1])) {
                                $range[1] = $range[1] / $conversion_rate;
                            }
                        }
                        if (in_array($selected_criteria_groups_type[$id_criterion_group]['criterion_group_type'], array('weight', 'width', 'height', 'depth'))) {
                            $where_single_value_range[] = 'ROUND(ac' . (int) $id_criterion_group . '.`single_value`,5) >= ROUND("' . $range[0] . '",5)' . (isset($range[1]) && $range[1] ? ' AND ROUND(ac' . (int) $id_criterion_group . '.`single_value`,5) <= ROUND("' . $range[1] . '",5)' : '');
                        } elseif ($selected_criteria_groups_type[$id_criterion_group]['criterion_group_type'] == 'price') {
                            $price_is_included = true;
                            list($taxConversion, $taxConversionForReduction, $specificPriceCondition, $specificPriceGroupCondition) = self::getPriceRangeConditions($id_group);
                            $specificPriceCondition .= $specificPriceGroupCondition;
                            $priceMinCondition = '
                            IF(app.`is_specific` = 1 AND app.`id_currency` IN (0, ' . $id_currency . '),
                                ' . sprintf($specificPriceCondition . ' >= TRUNCATE(%f' . $taxConversion . ', 2)', (double) $original_range[0]) . ',
                                ' . sprintf($specificPriceCondition . ' >= TRUNCATE(%f' . $taxConversion . ', 2)', (double) $range[0]) . '
                            )';
                            $priceMaxCondition = '';
                            if (isset($range[1]) && $range[1]) {
                                $priceMaxCondition = '
                                AND 
                                IF(app.`is_specific` = 1 AND app.`id_currency` IN (0, ' . $id_currency . '),
                                    ' . sprintf($specificPriceCondition . ' <= ROUND(%f' . $taxConversion . ', 2)', (double) $original_range[1]) . ',
                                    ' . sprintf($specificPriceCondition . ' <= ROUND(%f' . $taxConversion . ', 2)', (double) $range[1]) . '
                                )';
                            }
                            $where_price_criterion[] = ' (
                                /*AS4-PR-Start*/
                                ' . $priceMinCondition . $priceMaxCondition . '
                                /*AS4-PR-End*/
                                AND app.`id_country` IN (0, ' . (int) $id_country . ') 
                                AND app.`id_group` IN (0, ' . (int) $id_group . ')
                                AND ((app.`from` = \'0000-00-00 00:00:00\' OR \'' . $now . '\' >= app.`from`) AND (app.`to` = \'0000-00-00 00:00:00\' OR \'' . $now . '\' <= app.`to`))' . (version_compare(_PS_VERSION_, '1.5.0.0', '>=') ? ' AND app.`id_shop` IN (0, ' . implode(', ', Shop::getContextListShopID()) . ') ' : '') . ') ';
                        } else {
                            $where_translatable_value_range[] = 'ROUND(CAST(REPLACE(acl' . (int) $id_criterion_group . '.`value`, ",", ".") AS DECIMAL(10,2)), 5) >= ROUND("' . $range[0] . '",5)' . (isset($range[1]) && $range[1] ? ' AND ROUND(CAST(REPLACE(acl' . (int) $id_criterion_group . '.`value`, ",", ".") AS DECIMAL(10,2)), 5) <= ROUND("' . $range[1] . '",5)' : '');
                        }
                    }
                    if (isset($where_price_criterion) && AdvancedSearchCoreClass::_isFilledArray($where_price_criterion)) {
                        $where_criterion[] = '( ' . implode(' OR ', $where_price_criterion) . ' )';
                    }
                    $subQueryForRange = '';
                    if (AdvancedSearchCoreClass::_isFilledArray($where_single_value_range) && !AdvancedSearchCoreClass::_isFilledArray($where_translatable_value_range)) {
                        $subQueryForRange = '
						AND acpc' . (int) $id_criterion_group . '.`id_criterion` IN (
							SELECT ac' . (int) $id_criterion_group . '.`id_criterion`
							FROM `' . _DB_PREFIX_ . 'pm_advancedsearch_criterion_' . (int) $search['id_search'] . '` ac' . (int) $id_criterion_group . '
							JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_criterion_' . (int) $search['id_search'] . '_link` aclink' . (int) $id_criterion_group . ' ON (ac' . (int) $id_criterion_group . '.`id_criterion` = aclink' . (int) $id_criterion_group . '.`id_criterion`)
							WHERE ac' . (int) $id_criterion_group . '.`id_criterion_group` = ' . (int) $id_criterion_group . '
							' . (AdvancedSearchCoreClass::_isFilledArray($where_single_value_range) ? 'AND (' . implode(' OR ', $where_single_value_range) . ')' : '') . '
						)
						';
                    }
                    if (AdvancedSearchCoreClass::_isFilledArray($where_translatable_value_range)) {
                        $subQueryForRange = '
						AND acpc' . (int) $id_criterion_group . '.`id_criterion` IN (
							SELECT ac' . (int) $id_criterion_group . '.`id_criterion`
							FROM `' . _DB_PREFIX_ . 'pm_advancedsearch_criterion_' . (int) $search['id_search'] . '` ac' . (int) $id_criterion_group . '
							JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_criterion_' . (int) $search['id_search'] . '_link` aclink' . (int) $id_criterion_group . ' ON (ac' . (int) $id_criterion_group . '.`id_criterion` = aclink' . (int) $id_criterion_group . '.`id_criterion`)
							JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_criterion_' . (int) $search['id_search'] . '_lang` acl' . (int) $id_criterion_group . ' ON (ac' . (int) $id_criterion_group . '.`id_criterion` = acl' . (int) $id_criterion_group . '.`id_criterion` AND acl' . (int) $id_criterion_group . '.`id_lang` = ' . (int) $id_lang . ' AND (' . implode(' OR ', $where_translatable_value_range) . '))
							WHERE ac' . (int) $id_criterion_group . '.`id_criterion_group` = ' . (int) $id_criterion_group . '
							' . (AdvancedSearchCoreClass::_isFilledArray($where_single_value_range) ? 'AND (' . implode(' OR ', $where_single_value_range) . ')' : '') . '
						)
						';
                    }
                    if ($selected_criteria_groups_type[$id_criterion_group]['criterion_group_type'] != 'price') {
                        if (!in_array('acpc' . (int) $id_criterion_group, $join_criterion_tables)) {
                            $join_criterion[] = 'JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_cache_product_criterion_' . (int) $search['id_search'] . '` acpc' . (int) $id_criterion_group . ' ON ( acp.`id_cache_product` = acpc' . (int) $id_criterion_group . '.`id_cache_product`' . $subQueryForRange . ')';
                            $join_criterion_tables[] = 'acpc' . (int) $id_criterion_group;
                        }
                    }
                } else {
                    if (is_array($id_criterion) && sizeof($id_criterion)) {
                        $customCriterions = AdvancedSearchCriterionClass::getCustomCriterionsLinkIds($search['id_search'], $id_criterion);
                        foreach ($id_criterion as $idCriterionKey => $idCriterion) {
                            if (isset($customCriterions[$idCriterion]) && is_array($customCriterions[$idCriterion]) && sizeof($customCriterions[$idCriterion])) {
                                unset($id_criterion[$idCriterionKey]);
                                $id_criterion = array_unique(array_merge($id_criterion, $customCriterions[$idCriterion]));
                            }
                        }
                    }
                    $current_where = '`id_criterion` IN (' . implode(', ', $id_criterion) . ')';
                    if ($selected_criteria_groups_type[$id_criterion_group]['criterion_group_type'] == 'attribute') {
                        $prev_where_criterion = $current_where;
                    }
                    $where_join[] = 'acpc' . (int) $id_criterion_group . '.' . $current_where;
                    $join_criterion[] = 'JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_cache_product_criterion_' . (int) $search['id_search'] . '` acpc' . (int) $id_criterion_group . ' ON ( acp.`id_cache_product` = acpc' . (int) $id_criterion_group . '.`id_cache_product`' . (AdvancedSearchCoreClass::_isFilledArray($where_join) ? ' AND ' . implode(' OR ', $where_join) : '') . ')';
                    $join_criterion_tables[] = 'acpc' . (int) $id_criterion_group;
                }
                if ($selected_criteria_groups_type[$id_criterion_group]['criterion_group_type'] != 'price') {
                    $count_criterion[$id_criterion_group] = 'acpc' . (int) $id_criterion_group . '.`id_cache_product`';
                } else {
                    $count_criterion[$id_criterion_group] = 'app.`id_cache_product`';
                }
                if (isset($selected_criteria_groups_type[$id_criterion_group]) && $selected_criteria_groups_type[$id_criterion_group]['criterion_group_type'] == 'attribute') {
                    $attribute_selected = true;
                    $join_criterion['criterion_' . (int) $search['id_search'] . '_' . (int) $id_criterion_group] = 'JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_criterion_' . (int) $search['id_search'] . '` ac' . (int) $id_criterion_group . ' ON (acpc' . (int) $id_criterion_group . '.`id_criterion` = ac' . (int) $id_criterion_group . '.`id_criterion`)';
                    $join_criterion['criterion_link_' . (int) $search['id_search'] . '_' . (int) $id_criterion_group] = 'JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_criterion_' . (int) $search['id_search'] . '_link` aclink' . (int) $id_criterion_group . ' ON (ac' . (int) $id_criterion_group . '.`id_criterion` = aclink' . (int) $id_criterion_group . '.`id_criterion`)';
                    if (!isset($previousIdCriterionGroupSelected)) {
                        $previousIdCriterionGroupSelected = null;
                    }
                    $join_criterion['pa' . (int) $id_criterion_group] = 'JOIN `' . _DB_PREFIX_ . 'product_attribute` pa' . (int) $id_criterion_group . ' ON (pa' . (int) $id_criterion_group . '.`id_product` = acp.`id_product`)';
                    $join_criterion[] = 'JOIN `' . _DB_PREFIX_ . 'product_attribute_combination` pac' . (int) $id_criterion_group . ' ON (pa' . (int) $id_criterion_group . '.`id_product_attribute` = pac' . (int) $id_criterion_group . '.`id_product_attribute` AND pac' . (int) $id_criterion_group . '.`id_attribute` = aclink' . (int) $id_criterion_group . '.`id_criterion_linked`' . ($previousIdCriterionGroupSelected != null ? ' AND pa' . (int) $previousIdCriterionGroupSelected . '.`id_product_attribute` = pa' . (int) $id_criterion_group . '.`id_product_attribute` ' : '') . ')';
                    $join_criterion_tables[] = 'ac' . (int) $id_criterion_group;
                    $join_criterion_tables[] = 'pa' . (int) $id_criterion_group;
                    $join_criterion_tables[] = 'pac' . (int) $id_criterion_group;
                    $lastAttributeCombinationTableId = 'pac' . (int) $id_criterion_group;
                    $previousIdCriterionGroupSelected = (int) $id_criterion_group;
                    if (!isset($attribute_check_table) && (!$include_product_table && !$search['display_empty_criteria'])) {
                        $attribute_check_table = $id_criterion_group;
                    } elseif (!isset($attribute_check_table)) {
                        $attribute_check_table = $id_criterion_group;
                        if ($search['priority_on_combination_image'] && $include_product_table) {
                            $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute_image` pai ON (pac' . (int) $attribute_check_table . '.`id_product_attribute` = pai.`id_product_attribute`)';
                            $join_criterion_tables[] = 'pai';
                            $field_select[] = 'pai.id_image as attribute_image';
                        }
                    }
                    $attribute_qty_compare_on_join[] = 'pa' . (int) $attribute_check_table . '.`id_product_attribute` = pac' . (int) $id_criterion_group . '.`id_product_attribute`';
                }
            }
            if ($strict_stock || $search['search_on_stock'] || $include_product_table || $price_is_included) {
                if ($strict_stock) {
                    $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'product` p ON (' . (AdvancedSearchCoreClass::_isFilledArray(PM_AdvancedSearch4::$productFilterList) ? ' p.`id_product` IN (' . implode(',', PM_AdvancedSearch4::$productFilterList) . ') AND ' : '') . ' p.`id_product` = acp.`id_product`)';
                    $join_criterion_tables[] = 'p';
                    if (version_compare(_PS_VERSION_, '1.5.0.0', '<')) {
                        $where_qty[] = 'p.`quantity` > 0';
                    }
                } else {
                    $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'product` p ON (' . (AdvancedSearchCoreClass::_isFilledArray(PM_AdvancedSearch4::$productFilterList) ? ' p.`id_product` IN (' . implode(',', PM_AdvancedSearch4::$productFilterList) . ') AND ' : '') . ' p.`id_product` = acp.`id_product`)';
                    $join_criterion_tables[] = 'p';
                    if ($search['search_on_stock'] && version_compare(_PS_VERSION_, '1.5.0.0', '<')) {
                        $where_qty[] = 'IF(p.`quantity` > 0, 1, IF(p.`out_of_stock` = 2, ' . Configuration::get('PS_ORDER_OUT_OF_STOCK') . ' = 1, p.`out_of_stock` = 1))';
                    }
                }
            }
            if ($price_is_included || $include_product_table) {
                $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'tax_rule` tr ON (' . (version_compare(_PS_VERSION_, '1.5.0.0', '>=') ? 'ps' : 'p') . '.`id_tax_rules_group` = tr.`id_tax_rules_group`
				AND tr.`id_country` = ' . (int) (version_compare(_PS_VERSION_, '1.5.0.0', '>=') ? Context::getContext()->country->id : Country::getDefaultCountryId()) . '
				AND tr.`id_state` = 0)';
                $join_criterion[] = ' LEFT JOIN `' . _DB_PREFIX_ . 'tax` t ON (t.`id_tax` = tr.`id_tax`)';
                $join_criterion_tables[] = 'tr';
                $join_criterion_tables[] = 't';
                $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'group_reduction` grc ON (grc.`id_group`=' . (int) $id_group . ' AND ' . (version_compare(_PS_VERSION_, '1.5.0.0', '>=') ? 'ps' : 'p') . '.`id_category_default` = grc.`id_category`)';
                $join_criterion_tables[] = 'grc';
            }
            if ($price_is_included) {
                $field_select[] = self::_getScoreQuery(version_compare(_PS_VERSION_, '1.5.0.0', '>=') ? Context::getContext()->shop->id : 0, $id_currency, $id_country, $id_group);
                $join_criterion[] = 'JOIN `' . _DB_PREFIX_ . 'pm_advancedsearch_product_price_' . (int) $search['id_search'] . '` app ON ( acp.`id_cache_product` = app.`id_cache_product` AND ((app.`valid_id_specific_price`=1 AND app.`is_specific`=1) OR app.`has_no_specific`=1) ' . (version_compare(_PS_VERSION_, '1.5.0.0', '>=') ? ' AND app.`id_shop` IN (0, ' . implode(', ', Shop::getContextListShopID()) . ') ' : '') . ')';
                $join_price_table = true;
            }
            if ($search['search_on_stock'] || $strict_stock && !$group_type && !isset($attribute_check_table)) {
                if (version_compare(_PS_VERSION_, '1.5.0.0', '>=')) {
                    $table_stock_index++;
                    if ($strict_stock) {
                        $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'stock_available` sa' . $table_stock_index . ' ON ( sa' . $table_stock_index . '.`id_product` = acp.`id_product` AND sa' . $table_stock_index . '.`id_product_attribute`=0 ' . self::_addSqlShopRestrictionStockAvailable('sa' . $table_stock_index) . ')';
                        $join_criterion_tables[] = 'sa' . $table_stock_index;
                        $where_qty[] = 'IF (sa' . $table_stock_index . '.`quantity` > 0, 1, IF (sa' . $table_stock_index . '.`out_of_stock` = 2, ' . Configuration::get('PS_ORDER_OUT_OF_STOCK') . ' = 1, sa' . $table_stock_index . '.`out_of_stock` = 1))';
                    } else {
                        if (!(($group_type || $include_product_table) && isset($attribute_check_table) && sizeof($attribute_qty_compare_on_join))) {
                            $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'stock_available` sa' . $table_stock_index . ' ON ( sa' . $table_stock_index . '.`id_product` = acp.`id_product` AND sa' . $table_stock_index . '.`id_product_attribute`=0 ' . self::_addSqlShopRestrictionStockAvailable('sa' . $table_stock_index) . ')';
                            $join_criterion_tables[] = 'sa' . $table_stock_index;
                            $where_qty[] = 'IF (sa' . $table_stock_index . '.`quantity` > 0, 1, IF (sa' . $table_stock_index . '.`out_of_stock` = 2, ' . Configuration::get('PS_ORDER_OUT_OF_STOCK') . ' = 1, sa' . $table_stock_index . '.`out_of_stock` = 1))';
                        }
                    }
                }
            }
            if ($current_id_criterion_group && isset($attribute_check_table) && $group_type) {
                if ($is_attribute_group) {
                    if (!isset($previousIdCriterionGroupSelected)) {
                        $previousIdCriterionGroupSelected = null;
                    }
                    if (!in_array('pa' . (int) $current_id_criterion_group, $join_criterion_tables)) {
                        $join_criterion[] = 'JOIN `' . _DB_PREFIX_ . 'product_attribute` pa' . (int) $current_id_criterion_group . ' ON (pa' . (int) $current_id_criterion_group . '.`id_product` = acp.`id_product`)';
                        $join_criterion_tables[] = 'pa' . (int) $current_id_criterion_group;
                    }
                    $join_criterion[] = 'JOIN `' . _DB_PREFIX_ . 'product_attribute_combination` pac' . (int) $current_id_criterion_group . ' ON (pa' . (int) $current_id_criterion_group . '.`id_product_attribute` = pac' . (int) $current_id_criterion_group . '.`id_product_attribute` AND pac' . (int) $current_id_criterion_group . '.`id_attribute` = aclink.`id_criterion_linked`' . ($previousIdCriterionGroupSelected != null ? ' AND pa' . (int) $previousIdCriterionGroupSelected . '.`id_product_attribute` = pa' . (int) $current_id_criterion_group . '.`id_product_attribute` ' : '') . ')';
                    $join_criterion_tables[] = 'pac' . (int) $current_id_criterion_group;
                    $lastAttributeCombinationTableId = 'pac' . (int) $current_id_criterion_group;
                    $attribute_qty_compare_on_join[] = 'pa' . (int) $attribute_check_table . '.`id_product_attribute` = pac' . (int) $current_id_criterion_group . '.`id_product_attribute`';
                    $previousIdCriterionGroupSelected = (int) $current_id_criterion_group;
                }
            } else {
                if (($search['search_on_stock'] || $strict_stock) && $group_type && !isset($attribute_check_table)) {
                    if ($strict_stock && version_compare(_PS_VERSION_, '1.5.0.0', '<')) {
                        $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute` pa' . (int) $current_id_criterion_group . ' ON (pa' . (int) $current_id_criterion_group . '.`id_product` = acp.`id_product` ' . ($fromMethod != 'getCriterionsForSearchBloc' ? ' AND pa' . (int) $current_id_criterion_group . '.id_product_attribute = aclink.id_criterion_linked ' : '') . ')';
                        $join_criterion_tables[] = 'pa' . (int) $current_id_criterion_group;
                        $where_qty[] = 'IF (pa' . (int) $current_id_criterion_group . '.`quantity` = NULL, IF (p.`quantity` > 0, 1, IF (p.`out_of_stock` = 2, ' . Configuration::get('PS_ORDER_OUT_OF_STOCK') . ' = 1, p.`out_of_stock` = 1)), pa' . (int) $current_id_criterion_group . '.`quantity` > 0)';
                    } else {
                        $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute` pa' . (int) $current_id_criterion_group . ' ON (pa' . (int) $current_id_criterion_group . '.`id_product` = acp.`id_product` ' . ($fromMethod != 'getCriterionsForSearchBloc' ? ' AND pa' . (int) $current_id_criterion_group . '.id_product_attribute = aclink.id_criterion_linked ' : '') . ')';
                        $join_criterion_tables[] = 'pa' . (int) $current_id_criterion_group;
                        if ($search['search_on_stock'] && version_compare(_PS_VERSION_, '1.5.0.0', '<')) {
                            $where_qty[] = 'IF (pa' . (int) $current_id_criterion_group . '.`quantity` = NULL, IF (p.`quantity` > 0, 1, IF (p.`out_of_stock` = 2, ' . Configuration::get('PS_ORDER_OUT_OF_STOCK') . ' = 1, p.`out_of_stock` = 1)), IF (pa' . (int) $current_id_criterion_group . '.`quantity` > 0, 1, IF (p.`out_of_stock` = 2, ' . Configuration::get('PS_ORDER_OUT_OF_STOCK') . ' = 1, p.`out_of_stock` = 1)))';
                        }
                    }
                    if (version_compare(_PS_VERSION_, '1.5.0.0', '>=') && $search['search_on_stock']) {
                        $table_stock_index++;
                        if ($strict_stock) {
                            $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'stock_available` sa' . $table_stock_index . ' ON ( sa' . $table_stock_index . '.`id_product` = acp.`id_product` AND sa' . $table_stock_index . '.`id_product_attribute` = pa' . (int) $current_id_criterion_group . '.`id_product_attribute` ' . self::_addSqlShopRestrictionStockAvailable('sa' . $table_stock_index) . ')';
                            $join_criterion_tables[] = 'sa' . $table_stock_index;
                            $where_qty[] = 'IF (sa' . $table_stock_index . '.`quantity` > 0,1, IF (sa' . $table_stock_index . '.`out_of_stock` = 2, ' . Configuration::get('PS_ORDER_OUT_OF_STOCK') . ' = 1, sa' . $table_stock_index . '.`out_of_stock` = 1))';
                        } else {
                            $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'stock_available` sa' . $table_stock_index . ' ON ( sa' . $table_stock_index . '.`id_product` = acp.`id_product` AND sa' . $table_stock_index . '.`id_product_attribute` = pa' . (int) $current_id_criterion_group . '.`id_product_attribute` ' . self::_addSqlShopRestrictionStockAvailable('sa' . $table_stock_index) . ')';
                            $join_criterion_tables[] = 'sa' . $table_stock_index;
                            $where_qty[] = 'IF (sa' . $table_stock_index . '.`quantity` > 0,1, IF (sa' . $table_stock_index . '.`out_of_stock` = 2, ' . Configuration::get('PS_ORDER_OUT_OF_STOCK') . ' = 1, sa' . $table_stock_index . '.`out_of_stock` = 1))';
                        }
                    }
                    if ($is_attribute_group) {
                        if (!in_array('pa' . (int) $current_id_criterion_group, $join_criterion_tables)) {
                            $join_criterion['pa' . (int) $current_id_criterion_group] = 'JOIN `' . _DB_PREFIX_ . 'product_attribute` pa' . (int) $current_id_criterion_group . ' ON (pa' . (int) $current_id_criterion_group . '.`id_product` = acp.`id_product`)';
                            $join_criterion_tables[] = 'pa' . (int) $current_id_criterion_group;
                        }
                        $join_criterion[] = 'JOIN `' . _DB_PREFIX_ . 'product_attribute_combination` pac' . (int) $current_id_criterion_group . ' ON (' . ($is_attribute_group ? 'pa' . (int) $current_id_criterion_group . '.`id_product_attribute` = pac' . (int) $current_id_criterion_group . '.`id_product_attribute` AND ' : '') . 'pac' . (int) $current_id_criterion_group . '.`id_attribute` = aclink.`id_criterion_linked`)';
                        $join_criterion_tables[] = 'pac' . (int) $current_id_criterion_group;
                        $lastAttributeCombinationTableId = 'pac' . (int) $current_id_criterion_group;
                    }
                }
            }
            if (($group_type || $include_product_table) && isset($attribute_check_table) && sizeof($attribute_qty_compare_on_join)) {
                if ($strict_stock && version_compare(_PS_VERSION_, '1.5.0.0', '<')) {
                    if (!in_array('pa' . (int) $attribute_check_table, $join_criterion_tables)) {
                        $join_criterion['pa' . (int) $attribute_check_table] = 'JOIN `' . _DB_PREFIX_ . 'product_attribute` pa' . (int) $attribute_check_table . ' ON (' . implode(' AND ', $attribute_qty_compare_on_join) . ' AND pa' . (int) $attribute_check_table . '.`id_product` = acp.`id_product`)';
                        $join_criterion_tables[] = 'pa' . (int) $attribute_check_table;
                    }
                    $where_qty[] = 'pa' . (int) $attribute_check_table . '.`quantity` > 0';
                } else {
                    if (!in_array('pa' . (int) $attribute_check_table, $join_criterion_tables)) {
                        $join_criterion['pa' . (int) $attribute_check_table] = 'JOIN `' . _DB_PREFIX_ . 'product_attribute` pa' . (int) $attribute_check_table . ' ON (' . implode(' AND ', $attribute_qty_compare_on_join) . ' AND pa' . (int) $attribute_check_table . '.`id_product` = acp.`id_product`)';
                        $join_criterion_tables[] = 'pa' . (int) $attribute_check_table;
                    }
                    if ($search['search_on_stock'] && version_compare(_PS_VERSION_, '1.5.0.0', '<')) {
                        $where_qty[] = 'IF (pa' . (int) $attribute_check_table . '.`quantity` > 0, 1, IF(p.`out_of_stock` = 2, ' . Configuration::get('PS_ORDER_OUT_OF_STOCK') . ' = 1, p.`out_of_stock` = 1))';
                    } elseif (version_compare(_PS_VERSION_, '1.5.0.0', '<')) {
                        $where_qty[] = 'pa' . (int) $attribute_check_table . '.`quantity` IS NOT NULL';
                    }
                }
                if (version_compare(_PS_VERSION_, '1.5.0.0', '>=') && ($search['search_on_stock'] || $strict_stock)) {
                    $table_stock_index++;
                    if ($strict_stock) {
                        $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'stock_available` sa' . $table_stock_index . ' ON ( sa' . $table_stock_index . '.`id_product` = acp.`id_product` AND sa' . $table_stock_index . '.`id_product_attribute` = pa' . (int) $attribute_check_table . '.`id_product_attribute` ' . self::_addSqlShopRestrictionStockAvailable('sa' . $table_stock_index) . ')';
                        $join_criterion_tables[] = 'sa' . $table_stock_index;
                        $where_qty[] = 'IF (sa' . $table_stock_index . '.`quantity` > 0, 1, IF(sa' . $table_stock_index . '.`out_of_stock` = 2, ' . Configuration::get('PS_ORDER_OUT_OF_STOCK') . ' = 1, sa' . $table_stock_index . '.`out_of_stock` = 1))';
                    } else {
                        $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'stock_available` sa' . $table_stock_index . ' ON ( sa' . $table_stock_index . '.`id_product` = acp.`id_product` AND sa' . $table_stock_index . '.`id_product_attribute` = pa' . (int) $attribute_check_table . '.`id_product_attribute` ' . self::_addSqlShopRestrictionStockAvailable('sa' . $table_stock_index) . ')';
                        $join_criterion_tables[] = 'sa' . $table_stock_index;
                        $where_qty[] = 'IF (sa' . $table_stock_index . '.`quantity` > 0, 1, IF(sa' . $table_stock_index . '.`out_of_stock` = 2, ' . Configuration::get('PS_ORDER_OUT_OF_STOCK') . ' = 1, sa' . $table_stock_index . '.`out_of_stock` = 1))';
                    }
                }
            }
        } else {
            if (($search['search_on_stock'] || $strict_stock) && version_compare(_PS_VERSION_, '1.5.0.0', '<') || $include_product_table && $include_price_table) {
                if ($strict_stock) {
                    $join_criterion[] = 'JOIN `' . _DB_PREFIX_ . 'product` p ON ( ' . (AdvancedSearchCoreClass::_isFilledArray(PM_AdvancedSearch4::$productFilterList) ? ' p.`id_product` IN (' . implode(',', PM_AdvancedSearch4::$productFilterList) . ') AND ' : '') . ' p.`id_product` = acp.`id_product`)';
                    $join_criterion_tables[] = 'p';
                    if (version_compare(_PS_VERSION_, '1.5.0.0', '<')) {
                        $where_qty[] = 'p.`quantity` > 0';
                    }
                } else {
                    $join_criterion[] = 'JOIN `' . _DB_PREFIX_ . 'product` p ON ( ' . (AdvancedSearchCoreClass::_isFilledArray(PM_AdvancedSearch4::$productFilterList) ? ' p.`id_product` IN (' . implode(',', PM_AdvancedSearch4::$productFilterList) . ') AND ' : '') . ' p.`id_product` = acp.`id_product`)';
                    $join_criterion_tables[] = 'p';
                    if ($search['search_on_stock'] && version_compare(_PS_VERSION_, '1.5.0.0', '<')) {
                        $where_qty[] = 'IF (p.`quantity` > 0, 1, IF (p.`out_of_stock` = 2, ' . Configuration::get('PS_ORDER_OUT_OF_STOCK') . ' = 1, p.`out_of_stock` = 1))';
                    }
                }
            }
            if ($include_product_table && $include_price_table) {
                $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'tax_rule` tr ON (' . (version_compare(_PS_VERSION_, '1.5.0.0', '>=') ? 'ps' : 'p') . '.`id_tax_rules_group` = tr.`id_tax_rules_group`
				AND tr.`id_country` = ' . (int) (version_compare(_PS_VERSION_, '1.5.0.0', '>=') ? Context::getContext()->country->id : Country::getDefaultCountryId()) . '
				AND tr.`id_state` = 0)';
                $join_criterion[] = ' LEFT JOIN `' . _DB_PREFIX_ . 'tax` t ON (t.`id_tax` = tr.`id_tax`)';
                $join_criterion_tables[] = 'tr';
                $join_criterion_tables[] = 't';
                $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'group_reduction` grc ON (grc.`id_group`=' . (int) $id_group . ' AND ' . (version_compare(_PS_VERSION_, '1.5.0.0', '>=') ? 'ps' : 'p') . '.`id_category_default` = grc.`id_category`)';
                $join_criterion_tables[] = 'grc';
            }
            if ($is_attribute_group && ($search['search_on_stock'] || $strict_stock)) {
                if ($strict_stock && version_compare(_PS_VERSION_, '1.5.0.0', '<')) {
                    if (!in_array('pa' . (int) $current_id_criterion_group, $join_criterion_tables)) {
                        $join_criterion['pa' . (int) $current_id_criterion_group] = 'JOIN `' . _DB_PREFIX_ . 'product_attribute` pa' . (int) $current_id_criterion_group . ' ON (pa' . (int) $current_id_criterion_group . '.`id_product` = acp.`id_product`)';
                        $join_criterion_tables[] = 'pa' . (int) $current_id_criterion_group;
                    }
                    $where_qty[] = 'pa' . (int) $current_id_criterion_group . '.`quantity` > 0';
                } else {
                    if (!in_array('pa' . (int) $current_id_criterion_group, $join_criterion_tables)) {
                        $join_criterion['pa' . (int) $current_id_criterion_group] = 'JOIN `' . _DB_PREFIX_ . 'product_attribute` pa' . (int) $current_id_criterion_group . ' ON (pa' . (int) $current_id_criterion_group . '.`id_product` = acp.`id_product`)';
                        $join_criterion_tables[] = 'pa' . (int) $current_id_criterion_group;
                    }
                    if ($search['search_on_stock'] && version_compare(_PS_VERSION_, '1.5.0.0', '<')) {
                        $where_qty[] = 'IF (pa' . (int) $current_id_criterion_group . '.`quantity` > 0, 1, IF(p.`out_of_stock` = 2, ' . Configuration::get('PS_ORDER_OUT_OF_STOCK') . ' = 1, p.`out_of_stock` = 1))';
                    }
                }
                if (version_compare(_PS_VERSION_, '1.5.0.0', '>=')) {
                    $table_stock_index++;
                    if ($strict_stock) {
                        $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'stock_available` sa' . $table_stock_index . ' ON ( sa' . $table_stock_index . '.`id_product` = acp.`id_product` AND sa' . $table_stock_index . '.`id_product_attribute` = pa' . (int) $current_id_criterion_group . '.`id_product_attribute` ' . self::_addSqlShopRestrictionStockAvailable('sa' . $table_stock_index) . ')';
                        $join_criterion_tables[] = 'sa' . $table_stock_index;
                        $where_qty[] = 'IF (sa' . $table_stock_index . '.`quantity` > 0, 1, IF (sa' . $table_stock_index . '.`out_of_stock` = 2, ' . Configuration::get('PS_ORDER_OUT_OF_STOCK') . ' = 1, sa' . $table_stock_index . '.`out_of_stock` = 1))';
                    } else {
                        $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'stock_available` sa' . $table_stock_index . ' ON ( sa' . $table_stock_index . '.`id_product` = acp.`id_product` AND sa' . $table_stock_index . '.`id_product_attribute` = pa' . (int) $current_id_criterion_group . '.`id_product_attribute` ' . self::_addSqlShopRestrictionStockAvailable('sa' . $table_stock_index) . ')';
                        $join_criterion_tables[] = 'sa' . $table_stock_index;
                        $where_qty[] = 'IF (sa' . $table_stock_index . '.`quantity` > 0, 1, IF (sa' . $table_stock_index . '.`out_of_stock` = 2, ' . Configuration::get('PS_ORDER_OUT_OF_STOCK') . ' = 1, sa' . $table_stock_index . '.`out_of_stock` = 1))';
                    }
                }
                if ($is_attribute_group) {
                    $join_criterion[] = 'JOIN `' . _DB_PREFIX_ . 'product_attribute_combination` pac' . (int) $current_id_criterion_group . ' ON (' . ($is_attribute_group ? 'pa' . (int) $current_id_criterion_group . '.`id_product_attribute` = pac' . (int) $current_id_criterion_group . '.`id_product_attribute` AND ' : '') . 'pac' . (int) $current_id_criterion_group . '.`id_attribute` = aclink.`id_criterion_linked`)';
                    $join_criterion_tables[] = 'pac' . (int) $current_id_criterion_group;
                    $lastAttributeCombinationTableId = 'pac' . (int) $current_id_criterion_group;
                }
            } elseif ($search['search_on_stock'] || $strict_stock) {
                if (version_compare(_PS_VERSION_, '1.5.0.0', '>=')) {
                    $table_stock_index++;
                    if ($strict_stock) {
                        $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'stock_available` sa' . $table_stock_index . ' ON ( sa' . $table_stock_index . '.`id_product` = acp.`id_product` AND sa' . $table_stock_index . '.`id_product_attribute`=0 ' . self::_addSqlShopRestrictionStockAvailable('sa' . $table_stock_index) . ')';
                        $join_criterion_tables[] = 'sa' . $table_stock_index;
                        $where_qty[] = 'sa' . $table_stock_index . '.`quantity`';
                    } else {
                        $join_criterion[] = 'LEFT JOIN `' . _DB_PREFIX_ . 'stock_available` sa' . $table_stock_index . ' ON ( sa' . $table_stock_index . '.`id_product` = acp.`id_product` AND sa' . $table_stock_index . '.`id_product_attribute`=0 ' . self::_addSqlShopRestrictionStockAvailable('sa' . $table_stock_index) . ')';
                        $join_criterion_tables[] = 'sa' . $table_stock_index;
                        $where_qty[] = 'IF(sa' . $table_stock_index . '.`quantity` > 0,1,IF(sa' . $table_stock_index . '.`out_of_stock` = 2, ' . Configuration::get('PS_ORDER_OUT_OF_STOCK') . ' = 1, sa' . $table_stock_index . '.`out_of_stock` = 1))';
                    }
                }
            }
        }
        if ($include_product_table || $fromMethod == 'getCriterionsForSearchBloc') {
            if (version_compare(_PS_VERSION_, '1.5.0.0', '>=')) {
                $where_criterion[] = 'ps.`active` = 1';
                $where_criterion[] = 'ps.`visibility` IN ("both", "search")';
            } else {
                if (AdvancedSearchCoreClass::_isFilledArray(PM_AdvancedSearch4::$productFilterList)) {
                    $where_criterion[] = ' p.`id_product` IN (' . implode(',', PM_AdvancedSearch4::$productFilterList) . ') ';
                }
                $where_criterion[] = 'p.`active` = 1';
            }
        }
        if (!$include_product_table && AdvancedSearchCoreClass::_isFilledArray(PM_AdvancedSearch4::$productFilterList)) {
            $where_criterion[] = ' acp.`id_product` IN (' . implode(',', PM_AdvancedSearch4::$productFilterList) . ') ';
        }
        if (AdvancedSearchCoreClass::_isFilledArray($where_qty)) {
            if ($is_attribute_group || $attribute_selected) {
                $where_criterion[] = '(' . implode(' AND ', $where_qty) . ')';
            } else {
                $where_criterion[] = '(' . implode(' OR ', $where_qty) . ')';
            }
        }
        if ($price_is_included && ($fromMethod == 'getCriterionsForSearchBloc' || $fromMethod == 'getQueryCountResults' || $fromMethod == 'getProductsSearched' || $fromMethod == 'getPriceRangeForSearchBloc')) {
            $idCacheProductMaxScoreQuery = 'SELECT DISTINCT app.id_cache_product FROM `' . _DB_PREFIX_ . 'pm_advancedsearch_cache_product_' . (int) $search['id_search'] . '` acp ';
            if ($join_criterion && AdvancedSearchCoreClass::_isFilledArray($join_criterion)) {
                foreach ($join_criterion as $tmp_join_criterion) {
                    if (version_compare(_PS_VERSION_, '1.5.0.0', '>=') && preg_match('#stock_available#', $tmp_join_criterion)) {
                        continue;
                    }
                    if (preg_match('#product_attribute_combination|product_attribute_image#', $tmp_join_criterion)) {
                        continue;
                    }
                    $idCacheProductMaxScoreQuery .= ' ' . $tmp_join_criterion;
                }
            }
            if (AdvancedSearchCoreClass::_isFilledArray($where_qty)) {
                $where_criterion_price_included = array_slice($where_criterion, 0, sizeof($where_criterion) - 1);
            } else {
                $where_criterion_price_included = $where_criterion;
            }
            if ($where_criterion_price_included && AdvancedSearchCoreClass::_isFilledArray($where_criterion_price_included)) {
                $add_where = false;
                foreach ($where_criterion_price_included as $where) {
                    if (preg_match('#' . preg_quote('/*AS4-PR-Start*/') . '#', $where) && preg_match('#' . preg_quote('/*AS4-PR-End*/') . '#', $where)) {
                        while (strpos($where, '/*AS4-PR-Start*/') !== false) {
                            $where_tmp = substr($where, 0, strpos($where, '/*AS4-PR-Start*/'));
                            $where_tmp .= 'app.`id_currency` IN (0, ' . $id_currency . ')';
                            $where_tmp .= substr($where, strpos($where, '/*AS4-PR-End*/') + strlen('/*AS4-PR-End*/'), strlen($where));
                            $where = $where_tmp;
                        }
                    }
                    if (!$add_where) {
                        $idCacheProductMaxScoreQuery .= ' WHERE ' . $where;
                        $add_where = true;
                    } else {
                        $idCacheProductMaxScoreQuery .= ' AND ' . $where;
                    }
                }
            }
            $idCacheProductMaxScoreSQLResult = Db::getInstance()->ExecuteS($idCacheProductMaxScoreQuery);
            if (AdvancedSearchCoreClass::_isFilledArray($idCacheProductMaxScoreSQLResult)) {
                $idCacheProductMaxScoreResult = array();
                foreach ($idCacheProductMaxScoreSQLResult as $idCacheProductMaxScore) {
                    $idCacheProductMaxScoreResult[] = (int) $idCacheProductMaxScore['id_cache_product'];
                }
                $where_criterion[] = 'app.id_cache_product IN (' . implode(',', $idCacheProductMaxScoreResult) . ')';
            }
        }
        $return = array('count' => $count_criterion, 'join' => $join_criterion, 'where' => $where_criterion, 'select' => $field_select, 'make_union' => $make_union, 'whereUnion' => array(), 'joinUnion' => array(), 'nbSelectedCriterions' => sizeof($selected_criterion), 'priceIncluded' => $price_is_included, 'productTableIncluded' => $include_product_table, 'lastAttributeCombinationTableId' => $lastAttributeCombinationTableId);
        self::$_cacheLeftJoinWhereCriterion[$cacheKey] = $return;
        return $return;
    }