public static function GetQueryBuildArrays($arOrder, $arFilter, $arSelect) { global $DB, $USER; global $stackCacheManager; $strDefQuantityTrace = COption::GetOptionString('catalog','default_quantity_trace','N'); if ('Y' != $strDefQuantityTrace) $strDefQuantityTrace = 'N'; $strDefCanBuyZero = COption::GetOptionString('catalog','default_can_buy_zero','N'); if ('Y' != $strDefCanBuyZero) $strDefCanBuyZero = 'N'; $strDefNegAmount = COption::GetOptionString('catalog','allow_negative_amount','N'); if ('Y' != $strDefNegAmount) $strDefNegAmount = 'N'; $sResSelect = ""; $sResFrom = ""; $sResWhere = ""; $arResOrder = array(); $arJoinGroup = array(); $arSensID = array( 'PRODUCT_ID' => true, 'CATALOG_GROUP_ID' => true, 'CURRENCY' => true, 'SHOP_QUANTITY' => true, 'PRICE' => true ); $arOrderTmp = array(); foreach ($arOrder as $key => $val) { foreach ($val as $by => $order) { if ($arField = CCatalogProduct::ParseQueryBuildField($by)) { $inum = $arField["NUM"]; $by = $arField["FIELD"]; $res = ''; if (0 >= $inum && array_key_exists($by, $arSensID)) continue; if ($by == "PRICE") { $res = " ".CIBlock::_Order("CAT_P".$inum.".PRICE", $order, "asc")." "; } elseif ($by == "CURRENCY") { $res = " ".CIBlock::_Order("CAT_P".$inum.".CURRENCY", $order, "asc")." "; } elseif ($by == "QUANTITY") { $arResOrder[$key] = " ".CIBlock::_Order("CAT_PR.QUANTITY", $order, "asc", false)." "; continue; } elseif ($by == 'WEIGHT') { $arResOrder[$key] = " ".CIBlock::_Order("CAT_PR.WEIGHT", $order, "asc", false)." "; continue; } elseif ($by == 'AVAILABLE') { $arResOrder[$key] = " ".CIBlock::_Order("CATALOG_AVAILABLE", $order, "desc", false)." "; continue; } else { $res = " ".CIBlock::_Order("CAT_P".$inum.".ID", $order, "asc", false)." "; } if (!array_key_exists($inum, $arOrderTmp)) $arOrderTmp[$inum] = array(); $arOrderTmp[$inum][$key] = $res; $arJoinGroup[$inum] = true; } } } $arWhereTmp = array(); $arAddJoinOn = array(); $filter_keys = (!is_array($arFilter) ? array() : array_keys($arFilter)); for ($i=0, $cnt = count($filter_keys); $i < $cnt; $i++) { $key = strtoupper($filter_keys[$i]); $val = $arFilter[$filter_keys[$i]]; $res = CIBlock::MkOperationFilter($key); $key = $res["FIELD"]; $cOperationType = $res["OPERATION"]; if ($arField = CCatalogProduct::ParseQueryBuildField($key)) { $key = $arField["FIELD"]; $inum = $arField["NUM"]; if (0 >= $inum && array_key_exists($key, $arSensID)) continue; $res = ""; switch($key) { case "PRODUCT_ID": $res = CIBlock::FilterCreate("CAT_P".$inum.".PRODUCT_ID", $val, "number", $cOperationType); break; case "CATALOG_GROUP_ID": $res = CIBlock::FilterCreate("CAT_P".$inum.".CATALOG_GROUP_ID", $val, "number", $cOperationType); break; case "CURRENCY": $res = CIBlock::FilterCreate("CAT_P".$inum.".CURRENCY", $val, "string", $cOperationType); break; case "SHOP_QUANTITY": $res = ' 1=1 '; $arAddJoinOn[$inum] = (($cOperationType=="N") ? " NOT " : " "). " ((CAT_P".$inum.".QUANTITY_FROM <= ".intval($val)." OR CAT_P".$inum.".QUANTITY_FROM IS NULL) AND (CAT_P".$inum.".QUANTITY_TO >= ".intval($val)." OR CAT_P".$inum.".QUANTITY_TO IS NULL)) "; break; case "PRICE": $res = CIBlock::FilterCreate("CAT_P".$inum.".PRICE", $val, "number", $cOperationType); break; case "QUANTITY": $res = CIBlock::FilterCreate("CAT_PR.QUANTITY", $val, "number", $cOperationType); break; case "AVAILABLE": if ('N' !== $val) $val = 'Y'; $res = " (IF ( CAT_PR.QUANTITY > 0 OR IF (CAT_PR.QUANTITY_TRACE = 'D', '".$strDefQuantityTrace."', CAT_PR.QUANTITY_TRACE) = 'N' OR IF (CAT_PR.CAN_BUY_ZERO = 'D', '".$strDefCanBuyZero."', CAT_PR.CAN_BUY_ZERO) = 'Y', 'Y', 'N' ) ".(($cOperationType=="N") ? "<>" : "=")." '".$val."') "; break; case "WEIGHT": $res = CIBlock::FilterCreate("CAT_PR.WEIGHT", $val, "number", $cOperationType); break; } if ('' == $res) continue; if (!array_key_exists($inum, $arWhereTmp)) $arWhereTmp[$inum] = array(); $arWhereTmp[$inum][] = $res; $arJoinGroup[$inum] = true; } } $strSubWhere = ""; if (!empty($arSelect)) { foreach ($arSelect as &$strOneSelect) { $val = strtoupper($strOneSelect); if (0 != strncmp($val, 'CATALOG_GROUP_', 14)) continue; $num = intval(substr($val, 14)); if (0 < $num) $arJoinGroup[$num] = true; } if (isset($strOneSelect)) unset($strOneSelect); } if (!empty($arJoinGroup)) { $strSubWhere = implode(',', array_keys($arJoinGroup)); $strUserGroups = $USER->GetGroups(); $strCacheKey = "P_".$strUserGroups; $strCacheKey .= "_".$strSubWhere; $strCacheKey .= "_".LANGUAGE_ID; $cacheTime = CATALOG_CACHE_DEFAULT_TIME; if (defined("CATALOG_CACHE_TIME")) $cacheTime = intval(CATALOG_CACHE_TIME); $stackCacheManager->SetLength("catalog_GetQueryBuildArrays", 50); $stackCacheManager->SetTTL("catalog_GetQueryBuildArrays", $cacheTime); if ($stackCacheManager->Exist("catalog_GetQueryBuildArrays", $strCacheKey)) { $arResult = $stackCacheManager->Get("catalog_GetQueryBuildArrays", $strCacheKey); } else { $strSql = "SELECT CAT_CG.ID, CAT_CGL.NAME as CATALOG_GROUP_NAME, ". " IF(CAT_CGG.ID IS NULL, 'N', 'Y') as CATALOG_CAN_ACCESS, ". " IF(CAT_CGG1.ID IS NULL, 'N', 'Y') as CATALOG_CAN_BUY ". "FROM b_catalog_group CAT_CG ". " LEFT JOIN b_catalog_group2group CAT_CGG ON (CAT_CG.ID = CAT_CGG.CATALOG_GROUP_ID AND CAT_CGG.GROUP_ID IN (".$strUserGroups.") AND CAT_CGG.BUY <> 'Y') ". " LEFT JOIN b_catalog_group2group CAT_CGG1 ON (CAT_CG.ID = CAT_CGG1.CATALOG_GROUP_ID AND CAT_CGG1.GROUP_ID IN (".$strUserGroups.") AND CAT_CGG1.BUY = 'Y') ". " LEFT JOIN b_catalog_group_lang CAT_CGL ON (CAT_CG.ID = CAT_CGL.CATALOG_GROUP_ID AND CAT_CGL.LID = '".LANGUAGE_ID."') ". " WHERE CAT_CG.ID IN (".$strSubWhere.") ". " GROUP BY CAT_CG.ID "; $dbRes = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__); $arResult = array(); while ($arRes = $dbRes->Fetch()) $arResult[] = $arRes; $stackCacheManager->Set("catalog_GetQueryBuildArrays", $strCacheKey, $arResult); } $arCatGroups = array(); foreach ($arResult as &$row) { $i = intval($row["ID"]); if (!empty($arWhereTmp[$i]) && is_array($arWhereTmp[$i])) { $sResWhere .= ' AND '.implode(' AND ',$arWhereTmp[$i]); } if (!empty($arOrderTmp[$i]) && is_array($arOrderTmp[$i])) { foreach($arOrderTmp[$i] as $k=>$v) $arResOrder[$k] = $v; } $sResSelect .= ", CAT_P".$i.".ID as CATALOG_PRICE_ID_".$i.", ". " CAT_P".$i.".CATALOG_GROUP_ID as CATALOG_GROUP_ID_".$i.", ". " CAT_P".$i.".PRICE as CATALOG_PRICE_".$i.", ". " CAT_P".$i.".CURRENCY as CATALOG_CURRENCY_".$i.", ". " CAT_P".$i.".QUANTITY_FROM as CATALOG_QUANTITY_FROM_".$i.", ". " CAT_P".$i.".QUANTITY_TO as CATALOG_QUANTITY_TO_".$i.", ". " '".$DB->ForSql($row["CATALOG_GROUP_NAME"])."' as CATALOG_GROUP_NAME_".$i.", ". " '".$DB->ForSql($row["CATALOG_CAN_ACCESS"])."' as CATALOG_CAN_ACCESS_".$i.", ". " '".$DB->ForSql($row["CATALOG_CAN_BUY"])."' as CATALOG_CAN_BUY_".$i.", ". " CAT_P".$i.".EXTRA_ID as CATALOG_EXTRA_ID_".$i; $sResFrom .= " LEFT JOIN b_catalog_price CAT_P".$i." ON (CAT_P".$i.".PRODUCT_ID = BE.ID AND CAT_P".$i.".CATALOG_GROUP_ID = ".$row["ID"].") "; if (isset($arAddJoinOn[$i])) $sResFrom .= ' AND '.$arAddJoinOn[$i]; } if (isset($row)) unset($row); } $sResSelect .= ", CAT_PR.QUANTITY as CATALOG_QUANTITY, ". " IF (CAT_PR.QUANTITY_TRACE = 'D', '".$strDefQuantityTrace."', CAT_PR.QUANTITY_TRACE) as CATALOG_QUANTITY_TRACE, ". " CAT_PR.QUANTITY_TRACE as CATALOG_QUANTITY_TRACE_ORIG, ". " IF (CAT_PR.CAN_BUY_ZERO = 'D', '".$strDefCanBuyZero."', CAT_PR.CAN_BUY_ZERO) as CATALOG_CAN_BUY_ZERO, ". " IF (CAT_PR.NEGATIVE_AMOUNT_TRACE = 'D', '".$strDefNegAmount."', CAT_PR.NEGATIVE_AMOUNT_TRACE) as CATALOG_NEGATIVE_AMOUNT_TRACE, ". " IF ( CAT_PR.QUANTITY > 0 OR IF (CAT_PR.QUANTITY_TRACE = 'D', '".$strDefQuantityTrace."', CAT_PR.QUANTITY_TRACE) = 'N' OR IF (CAT_PR.CAN_BUY_ZERO = 'D', '".$strDefCanBuyZero."', CAT_PR.CAN_BUY_ZERO) = 'Y', 'Y', 'N' ) as CATALOG_AVAILABLE, ". " CAT_PR.WEIGHT as CATALOG_WEIGHT, ". " CAT_VAT.RATE as CATALOG_VAT, ". " CAT_PR.VAT_INCLUDED as CATALOG_VAT_INCLUDED, ". " CAT_PR.PRICE_TYPE as CATALOG_PRICE_TYPE, ". " CAT_PR.RECUR_SCHEME_TYPE as CATALOG_RECUR_SCHEME_TYPE, ". " CAT_PR.RECUR_SCHEME_LENGTH as CATALOG_RECUR_SCHEME_LENGTH, ". " CAT_PR.TRIAL_PRICE_ID as CATALOG_TRIAL_PRICE_ID, ". " CAT_PR.WITHOUT_ORDER as CATALOG_WITHOUT_ORDER, ". " CAT_PR.SELECT_BEST_PRICE as CATALOG_SELECT_BEST_PRICE, ". " CAT_PR.PURCHASING_PRICE as CATALOG_PURCHASING_PRICE, CAT_PR.PURCHASING_CURRENCY as CATALOG_PURCHASING_CURRENCY "; $sResFrom .= " LEFT JOIN b_catalog_product CAT_PR ON (CAT_PR.ID = BE.ID) "; $sResFrom .= " LEFT JOIN b_catalog_iblock CAT_IB ON ((CAT_PR.VAT_ID IS NULL OR CAT_PR.VAT_ID = 0) AND CAT_IB.IBLOCK_ID = BE.IBLOCK_ID) "; $sResFrom .= " LEFT JOIN b_catalog_vat CAT_VAT ON (CAT_VAT.ID = IF((CAT_PR.VAT_ID IS NULL OR CAT_PR.VAT_ID = 0), CAT_IB.VAT_ID, CAT_PR.VAT_ID)) "; if (!empty($arWhereTmp[0]) && is_array($arWhereTmp[0])) { $sResWhere .= ' AND '.implode(' AND ', $arWhereTmp[0]); } return array( "SELECT" => $sResSelect, "FROM" => $sResFrom, "WHERE" => $sResWhere, "ORDER" => $arResOrder ); }
public static function GetQueryBuildArrays($arOrder, $arFilter, $arSelect) { global $DB, $USER, $stackCacheManager; $strDefQuantityTrace = (string) Option::get('catalog', 'default_quantity_trace') == 'Y' ? 'Y' : 'N'; $strDefCanBuyZero = (string) Option::get('catalog', 'default_can_buy_zero') == 'Y' ? 'Y' : 'N'; $strDefNegAmount = (string) Option::get('catalog', 'allow_negative_amount') == 'Y' ? 'Y' : 'N'; $strSubscribe = (string) Option::get('catalog', 'default_subscribe') == 'N' ? 'N' : 'Y'; $sResSelect = ''; $sResFrom = ''; $sResWhere = ''; $arResOrder = array(); $arJoinGroup = array(); $arStoreWhere = array(); $arStore = array(); $arStoreOrder = array(); $arSensID = array('PRODUCT_ID' => true, 'CATALOG_GROUP_ID' => true, 'CURRENCY' => true, 'SHOP_QUANTITY' => true, 'PRICE' => true, 'STORE_AMOUNT' => true); $arOrderTmp = array(); foreach ($arOrder as $key => $val) { foreach ($val as $by => $order) { if ($arField = CCatalogProduct::ParseQueryBuildField($by)) { $res = ''; $join = true; $inum = (int) $arField["NUM"]; $by = (string) $arField["FIELD"]; if ($by == '' || $inum <= 0 && isset($arSensID[$by])) { continue; } switch ($by) { case 'PRICE': $res = " " . CIBlock::_Order("CAT_P" . $inum . ".PRICE", $order, "asc") . " "; break; case 'CURRENCY': $res = " " . CIBlock::_Order("CAT_P" . $inum . ".CURRENCY", $order, "asc") . " "; break; case 'QUANTITY': $arResOrder[$key] = " " . CIBlock::_Order("CAT_PR.QUANTITY", $order, "asc", false) . " "; $join = false; break; case 'WEIGHT': $arResOrder[$key] = " " . CIBlock::_Order("CAT_PR.WEIGHT", $order, "asc", false) . " "; $join = false; break; case 'AVAILABLE': $arResOrder[$key] = " " . CIBlock::_Order("CATALOG_AVAILABLE", $order, "desc", false) . " "; $join = false; break; case 'TYPE': $arResOrder[$key] = " " . CIBlock::_Order("CAT_PR.TYPE", $order, "asc", false) . " "; $join = false; break; case 'PURCHASING_PRICE': $arResOrder[$key] = " " . CIBlock::_Order("CAT_PR.PURCHASING_PRICE", $order, "asc") . " "; $join = false; break; case 'PURCHASING_CURRENCY': $arResOrder[$key] = " " . CIBlock::_Order("CAT_PR.PURCHASING_CURRENCY", $order, "asc") . " "; $join = false; break; case 'STORE_AMOUNT': $arStore[$inum] = true; if (!isset($arStoreOrder[$inum])) { $arStoreOrder[$inum] = array(); } $arStoreOrder[$inum][$key] = " " . CIBlock::_Order("CAT_SP" . $inum . ".AMOUNT", $order, "asc") . " "; $join = false; break; default: $res = " " . CIBlock::_Order("CAT_P" . $inum . ".ID", $order, "asc", false) . " "; break; } if ($join) { if (!isset($arOrderTmp[$inum])) { $arOrderTmp[$inum] = array(); } $arOrderTmp[$inum][$key] = $res; $arJoinGroup[$inum] = true; } } } } $productWhere = array(); $arWhereTmp = array(); $arAddJoinOn = array(); $filter_keys = !is_array($arFilter) ? array() : array_keys($arFilter); for ($i = 0, $cnt = count($filter_keys); $i < $cnt; $i++) { $key = strtoupper($filter_keys[$i]); $val = $arFilter[$filter_keys[$i]]; $res = CIBlock::MkOperationFilter($key); $key = $res["FIELD"]; $cOperationType = $res["OPERATION"]; if ($arField = CCatalogProduct::ParseQueryBuildField($key)) { $res = ''; $join = true; $key = (string) $arField["FIELD"]; $inum = (int) $arField["NUM"]; if ($key == '' || $inum <= 0 && isset($arSensID[$key])) { continue; } switch ($key) { case "PRODUCT_ID": $res = CIBlock::FilterCreate("CAT_P" . $inum . ".PRODUCT_ID", $val, "number", $cOperationType); break; case "CATALOG_GROUP_ID": $res = CIBlock::FilterCreate("CAT_P" . $inum . ".CATALOG_GROUP_ID", $val, "number", $cOperationType); break; case "CURRENCY": $res = CIBlock::FilterCreate("CAT_P" . $inum . ".CURRENCY", $val, "string", $cOperationType); break; case "SHOP_QUANTITY": $val = (int) $val; $res = ' 1=1 '; $arAddJoinOn[$inum] = ($cOperationType == "N" ? " NOT " : " ") . " ((CAT_P" . $inum . ".QUANTITY_FROM <= " . $val . " OR CAT_P" . $inum . ".QUANTITY_FROM IS NULL) AND (CAT_P" . $inum . ".QUANTITY_TO >= " . $val . " OR CAT_P" . $inum . ".QUANTITY_TO IS NULL)) "; break; case "PRICE": $res = CIBlock::FilterCreate("CAT_P" . $inum . ".PRICE", $val, "number", $cOperationType); break; case "QUANTITY": $res = CIBlock::FilterCreate("CAT_PR.QUANTITY", $val, "number", $cOperationType); $join = false; break; case "AVAILABLE": if ('N' !== $val) { $val = 'Y'; } $res = " (IF (\n\t\t\t\t\tCAT_PR.QUANTITY > 0 OR\n\t\t\t\t\tIF (CAT_PR.QUANTITY_TRACE = 'D', '" . $strDefQuantityTrace . "', CAT_PR.QUANTITY_TRACE) = 'N' OR\n\t\t\t\t\tIF (CAT_PR.CAN_BUY_ZERO = 'D', '" . $strDefCanBuyZero . "', CAT_PR.CAN_BUY_ZERO) = 'Y',\n\t\t\t\t\t'Y', 'N'\n\t\t\t\t\t) " . ($cOperationType == "N" ? "<>" : "=") . " '" . $val . "') "; $join = false; break; case "WEIGHT": $res = CIBlock::FilterCreate("CAT_PR.WEIGHT", $val, "number", $cOperationType); $join = false; break; case 'TYPE': $res = CIBlock::FilterCreate("CAT_PR.TYPE", $val, "number", $cOperationType); $join = false; break; case 'PURCHASING_PRICE': $res = CIBlock::FilterCreate("CAT_PR.PURCHASING_PRICE", $val, "number", $cOperationType); $join = false; break; case 'PURCHASING_CURRENCY': $res = CIBlock::FilterCreate("CAT_PR.PURCHASING_PRICE", $val, "string", $cOperationType); $join = false; break; case 'STORE_AMOUNT': $arStore[$inum] = true; if (!isset($arStoreWhere[$inum])) { $arStoreWhere[$inum] = array(); } $arStoreWhere[$inum][] = CIBlock::FilterCreate("CAT_SP" . $inum . ".AMOUNT", $val, "number", $cOperationType); $join = false; break; } if ($res == '') { continue; } if ($join) { if (!isset($arWhereTmp[$inum])) { $arWhereTmp[$inum] = array(); } $arWhereTmp[$inum][] = $res; $arJoinGroup[$inum] = true; } else { $productWhere[] = $res; } } } if (!empty($arSelect)) { foreach ($arSelect as &$strOneSelect) { $val = strtoupper($strOneSelect); if (strncmp($val, 'CATALOG_GROUP_', 14) == 0) { $num = (int) substr($val, 14); if ($num > 0) { $arJoinGroup[$num] = true; } } elseif (strncmp($val, 'CATALOG_STORE_AMOUNT_', 21) == 0) { $num = (int) substr($val, 21); if ($num > 0) { $arStore[$num] = true; } } } unset($strOneSelect); } if (!empty($arJoinGroup)) { $strSubWhere = implode(',', array_keys($arJoinGroup)); $strUserGroups = CCatalog::IsUserExists() ? $USER->GetGroups() : '2'; $strCacheKey = "P_" . $strUserGroups; $strCacheKey .= "_" . $strSubWhere; $strCacheKey .= "_" . LANGUAGE_ID; $cacheTime = CATALOG_CACHE_DEFAULT_TIME; if (defined("CATALOG_CACHE_TIME")) { $cacheTime = intval(CATALOG_CACHE_TIME); } $stackCacheManager->SetLength("catalog_GetQueryBuildArrays", 50); $stackCacheManager->SetTTL("catalog_GetQueryBuildArrays", $cacheTime); if ($stackCacheManager->Exist("catalog_GetQueryBuildArrays", $strCacheKey)) { $arResult = $stackCacheManager->Get("catalog_GetQueryBuildArrays", $strCacheKey); } else { $strSql = "SELECT CAT_CG.ID, CAT_CGL.NAME as CATALOG_GROUP_NAME, " . "\tIF(CAT_CGG.ID IS NULL, 'N', 'Y') as CATALOG_CAN_ACCESS, " . "\tIF(CAT_CGG1.ID IS NULL, 'N', 'Y') as CATALOG_CAN_BUY " . "FROM b_catalog_group CAT_CG " . "\tLEFT JOIN b_catalog_group2group CAT_CGG ON (CAT_CG.ID = CAT_CGG.CATALOG_GROUP_ID AND CAT_CGG.GROUP_ID IN (" . $strUserGroups . ") AND CAT_CGG.BUY <> 'Y') " . "\tLEFT JOIN b_catalog_group2group CAT_CGG1 ON (CAT_CG.ID = CAT_CGG1.CATALOG_GROUP_ID AND CAT_CGG1.GROUP_ID IN (" . $strUserGroups . ") AND CAT_CGG1.BUY = 'Y') " . "\tLEFT JOIN b_catalog_group_lang CAT_CGL ON (CAT_CG.ID = CAT_CGL.CATALOG_GROUP_ID AND CAT_CGL.LANG = '" . LANGUAGE_ID . "') " . " WHERE CAT_CG.ID IN (" . $strSubWhere . ") " . " GROUP BY CAT_CG.ID "; $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); $arResult = array(); while ($arRes = $dbRes->Fetch()) { $arResult[] = $arRes; } $stackCacheManager->Set("catalog_GetQueryBuildArrays", $strCacheKey, $arResult); } foreach ($arResult as &$row) { $i = (int) $row["ID"]; if (!empty($arWhereTmp[$i]) && is_array($arWhereTmp[$i])) { $sResWhere .= ' AND ' . implode(' AND ', $arWhereTmp[$i]); } if (!empty($arOrderTmp[$i]) && is_array($arOrderTmp[$i])) { foreach ($arOrderTmp[$i] as $k => $v) { $arResOrder[$k] = $v; } } $sResSelect .= ", CAT_P" . $i . ".ID as CATALOG_PRICE_ID_" . $i . ", " . " CAT_P" . $i . ".CATALOG_GROUP_ID as CATALOG_GROUP_ID_" . $i . ", " . " CAT_P" . $i . ".PRICE as CATALOG_PRICE_" . $i . ", " . " CAT_P" . $i . ".CURRENCY as CATALOG_CURRENCY_" . $i . ", " . " CAT_P" . $i . ".QUANTITY_FROM as CATALOG_QUANTITY_FROM_" . $i . ", " . " CAT_P" . $i . ".QUANTITY_TO as CATALOG_QUANTITY_TO_" . $i . ", " . " '" . $DB->ForSql($row["CATALOG_GROUP_NAME"]) . "' as CATALOG_GROUP_NAME_" . $i . ", " . " '" . $DB->ForSql($row["CATALOG_CAN_ACCESS"]) . "' as CATALOG_CAN_ACCESS_" . $i . ", " . " '" . $DB->ForSql($row["CATALOG_CAN_BUY"]) . "' as CATALOG_CAN_BUY_" . $i . ", " . " CAT_P" . $i . ".EXTRA_ID as CATALOG_EXTRA_ID_" . $i; $sResFrom .= ' left join b_catalog_price CAT_P' . $i . ' on (CAT_P' . $i . '.PRODUCT_ID = BE.ID AND CAT_P' . $i . '.CATALOG_GROUP_ID = ' . $row['ID'] . ') '; if (isset($arAddJoinOn[$i])) { $sResFrom .= ' and ' . $arAddJoinOn[$i]; } } if (isset($row)) { unset($row); } } $sResSelect .= ", CAT_PR.QUANTITY as CATALOG_QUANTITY, CAT_PR.QUANTITY_RESERVED as CATALOG_QUANTITY_RESERVED, " . " IF (CAT_PR.QUANTITY_TRACE = 'D', '" . $strDefQuantityTrace . "', CAT_PR.QUANTITY_TRACE) as CATALOG_QUANTITY_TRACE, " . " CAT_PR.QUANTITY_TRACE as CATALOG_QUANTITY_TRACE_ORIG, " . " IF (CAT_PR.CAN_BUY_ZERO = 'D', '" . $strDefCanBuyZero . "', CAT_PR.CAN_BUY_ZERO) as CATALOG_CAN_BUY_ZERO, " . " CAT_PR.CAN_BUY_ZERO as CATALOG_CAN_BUY_ZERO_ORIG, " . " IF (CAT_PR.NEGATIVE_AMOUNT_TRACE = 'D', '" . $strDefNegAmount . "', CAT_PR.NEGATIVE_AMOUNT_TRACE) as CATALOG_NEGATIVE_AMOUNT_TRACE, " . " CAT_PR.NEGATIVE_AMOUNT_TRACE as CATALOG_NEGATIVE_AMOUNT_ORIG, " . " IF (CAT_PR.SUBSCRIBE = 'D', '" . $strSubscribe . "', CAT_PR.SUBSCRIBE) as CATALOG_SUBSCRIBE, " . " CAT_PR.SUBSCRIBE as CATALOG_SUBSCRIBE_ORIG, " . " IF (\n\t\t\t\tCAT_PR.QUANTITY > 0 OR\n\t\t\t\tIF (CAT_PR.QUANTITY_TRACE = 'D', '" . $strDefQuantityTrace . "', CAT_PR.QUANTITY_TRACE) = 'N' OR\n\t\t\t\tIF (CAT_PR.CAN_BUY_ZERO = 'D', '" . $strDefCanBuyZero . "', CAT_PR.CAN_BUY_ZERO) = 'Y',\n\t\t\t\t'Y', 'N'\n\t\t\t) as CATALOG_AVAILABLE, " . " CAT_PR.WEIGHT as CATALOG_WEIGHT, CAT_PR.WIDTH as CATALOG_WIDTH, CAT_PR.LENGTH as CATALOG_LENGTH, CAT_PR.HEIGHT as CATALOG_HEIGHT, " . " CAT_PR.MEASURE as CATALOG_MEASURE, " . " CAT_VAT.RATE as CATALOG_VAT, CAT_PR.VAT_INCLUDED as CATALOG_VAT_INCLUDED, " . " CAT_PR.PRICE_TYPE as CATALOG_PRICE_TYPE, CAT_PR.RECUR_SCHEME_TYPE as CATALOG_RECUR_SCHEME_TYPE, " . " CAT_PR.RECUR_SCHEME_LENGTH as CATALOG_RECUR_SCHEME_LENGTH, CAT_PR.TRIAL_PRICE_ID as CATALOG_TRIAL_PRICE_ID, " . " CAT_PR.WITHOUT_ORDER as CATALOG_WITHOUT_ORDER, CAT_PR.SELECT_BEST_PRICE as CATALOG_SELECT_BEST_PRICE, " . " CAT_PR.PURCHASING_PRICE as CATALOG_PURCHASING_PRICE, CAT_PR.PURCHASING_CURRENCY as CATALOG_PURCHASING_CURRENCY, CAT_PR.TYPE as CATALOG_TYPE "; $sResFrom .= " left join b_catalog_product CAT_PR on (CAT_PR.ID = BE.ID) "; $sResFrom .= " left join b_catalog_iblock CAT_IB on ((CAT_PR.VAT_ID IS NULL OR CAT_PR.VAT_ID = 0) AND CAT_IB.IBLOCK_ID = BE.IBLOCK_ID) "; $sResFrom .= " left join b_catalog_vat CAT_VAT on (CAT_VAT.ID = IF((CAT_PR.VAT_ID IS NULL OR CAT_PR.VAT_ID = 0), CAT_IB.VAT_ID, CAT_PR.VAT_ID)) "; if (!empty($productWhere)) { $sResWhere .= ' and ' . implode(' and ', $productWhere); } unset($productWhere); if (!empty($arStore)) { foreach (array_keys($arStore) as $inum) { $sResFrom .= " left join b_catalog_store_product CAT_SP" . $inum . " on (CAT_SP" . $inum . ".PRODUCT_ID = BE.ID and CAT_SP" . $inum . ".STORE_ID = " . $inum . ") "; $sResSelect .= ", CAT_SP" . $inum . ".AMOUNT as CATALOG_STORE_AMOUNT_" . $inum . " "; } if (!empty($arStoreOrder)) { foreach ($arStoreOrder as $oneStoreOrder) { if (!empty($oneStoreOrder) && is_array($oneStoreOrder)) { foreach ($oneStoreOrder as $k => $v) { $arResOrder[$k] = $v; } unset($k, $v); } } unset($oneStoreOrder); } if (!empty($arStoreWhere)) { foreach ($arStoreWhere as $where) { $sResWhere .= ' and ' . implode(' and ', $where); } } } return array('SELECT' => $sResSelect, 'FROM' => $sResFrom, 'WHERE' => $sResWhere, 'ORDER' => $arResOrder); }