function PrepareGetList(&$arIblockElementFields, &$arJoinProps, &$bOnlyCount, &$bDistinct, &$arSelectFields, &$sSelect, &$arAddSelectFields, &$arFilter, &$sWhere, &$sSectionWhere, &$arAddWhereFields, &$arGroupBy, &$sGroupBy, &$arOrder, &$arSqlOrder, &$arAddOrderByFields, &$arIBlockFilter, &$arIBlockMultProps, &$arIBlockConvProps, &$arIBlockAllProps, &$arIBlockNumProps, &$arIBlockLongProps) { if (is_array($arSelectFields) && (in_array("DETAIL_PAGE_URL", $arSelectFields) || in_array("CANONICAL_PAGE_URL", $arSelectFields)) && !in_array("LANG_DIR", $arSelectFields)) { $arSelectFields[] = "LANG_DIR"; } global $DB, $USER; if (!is_array($arSelectFields) && $arSelectFields == "" || count($arSelectFields) <= 0 || $arSelectFields === false) { $arSelectFields = array("*"); } if (is_bool($arGroupBy) && $arGroupBy !== false) { $arGroupBy = array(); } if (is_array($arGroupBy) && count($arGroupBy) == 0) { $bOnlyCount = true; } $iPropCnt = 0; $arJoinProps = array("FP" => array(), "FPV" => array(), "FPS" => array(), "FPEN" => array(), "BE" => array(), "BE_FP" => array(), "BE_FPV" => array(), "BE_FPS" => array(), "BE_FPEN" => array(), "BES" => "", "RV" => false, "RVU" => false, "RVV" => false, "FC" => ""); $arIBlockMultProps = array(); $arIBlockAllProps = array(); $arIBlockNumProps = array(); $bWasGroup = false; //********************************ORDER BY PART*********************************************** $arSqlOrder = array(); $arAddOrderByFields = array(); $iOrdNum = -1; if (!is_array($arOrder)) { $arOrder = array(); } foreach ($arOrder as $by => $order) { $by_orig = $by; $by = strtoupper($by); //Remove aliases if ($by == "EXTERNAL_ID") { $by = "XML_ID"; } elseif ($by == "DATE_ACTIVE_FROM") { $by = "ACTIVE_FROM"; } elseif ($by == "DATE_ACTIVE_TO") { $by = "ACTIVE_TO"; } if (array_key_exists($by, $arSqlOrder)) { continue; } if (substr($by, 0, 8) == "CATALOG_") { $iOrdNum++; $arAddOrderByFields[$iOrdNum] = array($by => $order); //Reserve for future fill $arSqlOrder[$iOrdNum] = false; } else { if ($by == "ID") { $arSqlOrder[$by] = CIBlock::_Order("BE.ID", $order, "desc", false); } elseif ($by == "NAME") { $arSqlOrder[$by] = CIBlock::_Order("BE.NAME", $order, "desc", false); } elseif ($by == "STATUS") { $arSqlOrder[$by] = CIBlock::_Order("BE.WF_STATUS_ID", $order, "desc"); } elseif ($by == "XML_ID") { $arSqlOrder[$by] = CIBlock::_Order("BE.XML_ID", $order, "desc"); } elseif ($by == "CODE") { $arSqlOrder[$by] = CIBlock::_Order("BE.CODE", $order, "desc"); } elseif ($by == "TAGS") { $arSqlOrder[$by] = CIBlock::_Order("BE.TAGS", $order, "desc"); } elseif ($by == "TIMESTAMP_X") { $arSqlOrder[$by] = CIBlock::_Order("BE.TIMESTAMP_X", $order, "desc"); } elseif ($by == "CREATED") { $arSqlOrder[$by] = CIBlock::_Order("BE.DATE_CREATE", $order, "desc"); } elseif ($by == "CREATED_DATE") { $arSqlOrder[$by] = CIBlock::_Order($DB->DateFormatToDB("YYYY.MM.DD", "BE.DATE_CREATE"), $order, "desc"); } elseif ($by == "IBLOCK_ID") { $arSqlOrder[$by] = CIBlock::_Order("BE.IBLOCK_ID", $order, "desc"); } elseif ($by == "MODIFIED_BY") { $arSqlOrder[$by] = CIBlock::_Order("BE.MODIFIED_BY", $order, "desc"); } elseif ($by == "CREATED_BY") { $arSqlOrder[$by] = CIBlock::_Order("BE.CREATED_BY", $order, "desc"); } elseif ($by == "ACTIVE") { $arSqlOrder[$by] = CIBlock::_Order("BE.ACTIVE", $order, "desc"); } elseif ($by == "ACTIVE_FROM") { $arSqlOrder[$by] = CIBlock::_Order("BE.ACTIVE_FROM", $order, "desc"); } elseif ($by == "ACTIVE_TO") { $arSqlOrder[$by] = CIBlock::_Order("BE.ACTIVE_TO", $order, "desc"); } elseif ($by == "SORT") { $arSqlOrder[$by] = CIBlock::_Order("BE.SORT", $order, "desc"); } elseif ($by == "IBLOCK_SECTION_ID") { $arSqlOrder[$by] = CIBlock::_Order("BE.IBLOCK_SECTION_ID", $order, "desc"); } elseif ($by == "SHOW_COUNTER") { $arSqlOrder[$by] = CIBlock::_Order("BE.SHOW_COUNTER", $order, "desc"); } elseif ($by == "SHOW_COUNTER_START") { $arSqlOrder[$by] = CIBlock::_Order("BE.SHOW_COUNTER_START", $order, "desc"); } elseif ($by == "RAND") { $arSqlOrder[$by] = CIBlockElement::GetRandFunction(true); } elseif ($by == "SHOWS") { $arSqlOrder[$by] = CIBlock::_Order(CIBlockElement::GetShowedFunction(), $order, "desc", false); } elseif ($by == "HAS_PREVIEW_PICTURE") { $arSqlOrder[$by] = CIBlock::_Order(CIBlock::_NotEmpty("BE.PREVIEW_PICTURE"), $order, "desc", false); } elseif ($by == "HAS_DETAIL_PICTURE") { $arSqlOrder[$by] = CIBlock::_Order(CIBlock::_NotEmpty("BE.DETAIL_PICTURE"), $order, "desc", false); } elseif ($by == "RATING_TOTAL_VALUE") { $arSqlOrder[$by] = CIBlock::_Order("RV.TOTAL_VALUE", $order, "desc"); $arJoinProps["RV"] = true; } elseif ($by == "CNT") { if (is_array($arGroupBy) && count($arGroupBy) > 0) { $arSqlOrder[$by] = " CNT " . $order . " "; } } elseif (substr($by, 0, 9) == "PROPERTY_") { $propID = strtoupper(substr($by_orig, 9)); if (preg_match("/^([^.]+)\\.([^.]+)\$/", $propID, $arMatch)) { $db_prop = CIBlockProperty::GetPropertyArray($arMatch[1], CIBlock::_MergeIBArrays($arFilter["IBLOCK_ID"], $arFilter["IBLOCK_CODE"])); if (is_array($db_prop) && $db_prop["PROPERTY_TYPE"] == "E") { CIBlockElement::MkPropertyOrder($arMatch, $order, false, $db_prop, $arJoinProps, $arSqlOrder); } } else { if ($db_prop = CIBlockProperty::GetPropertyArray($propID, CIBlock::_MergeIBArrays($arFilter["IBLOCK_ID"], $arFilter["IBLOCK_CODE"]))) { CIBlockElement::MkPropertyOrder($by, $order, false, $db_prop, $arJoinProps, $arSqlOrder); } } } elseif (substr($by, 0, 13) == "PROPERTYSORT_") { $propID = strtoupper(substr($by_orig, 13)); if (preg_match("/^([^.]+)\\.([^.]+)\$/", $propID, $arMatch)) { $db_prop = CIBlockProperty::GetPropertyArray($arMatch[1], CIBlock::_MergeIBArrays($arFilter["IBLOCK_ID"], $arFilter["IBLOCK_CODE"])); if (is_array($db_prop) && $db_prop["PROPERTY_TYPE"] == "E") { CIBlockElement::MkPropertyOrder($arMatch, $order, true, $db_prop, $arJoinProps, $arSqlOrder); } } else { if ($db_prop = CIBlockProperty::GetPropertyArray($propID, CIBlock::_MergeIBArrays($arFilter["IBLOCK_ID"], $arFilter["IBLOCK_CODE"]))) { CIBlockElement::MkPropertyOrder($by, $order, true, $db_prop, $arJoinProps, $arSqlOrder); } } } else { $by = "ID"; if (!array_key_exists($by, $arSqlOrder)) { $arSqlOrder[$by] = CIBLock::_Order("BE.ID", $order, "desc"); } } //Check if have to add select field in order to correctly sort if (is_array($arSqlOrder[$by])) { if (is_array($arGroupBy) && count($arGroupBy) > 0) { $arGroupBy[] = $arSqlOrder[$by][1]; } else { $arSelectFields[] = $arSqlOrder[$by][1]; } // COLUMN ALIAS COLUMN EXPRESSION $arIblockElementFields[$arSqlOrder[$by][1]] = $arSqlOrder[$by][0]; // ORDER EXPRESSION $arSqlOrder[$by] = $arSqlOrder[$by][2]; } } //Add order by fields to the select list //in order to avoid sql errors if (is_array($arGroupBy) && count($arGroupBy) > 0) { if ($by == "STATUS") { $arGroupBy[] = "WF_STATUS_ID"; } elseif ($by == "CREATED") { $arGroupBy[] = "DATE_CREATE"; } else { $arGroupBy[] = $by; } } else { if ($by == "STATUS") { $arSelectFields[] = "WF_STATUS_ID"; } elseif ($by == "CREATED") { $arSelectFields[] = "DATE_CREATE"; } else { $arSelectFields[] = $by; } } } //*************************GROUP BY PART**************************** $sGroupBy = ""; if (is_array($arGroupBy) && count($arGroupBy) > 0) { $arSelectFields = $arGroupBy; $bWasGroup = true; foreach ($arSelectFields as $key => $val) { $val = strtoupper($val); if (array_key_exists($val, $arIblockElementFields)) { $sGroupBy .= "," . preg_replace("/(\\s+AS\\s+[A-Z_]+)/i", "", $arIblockElementFields[$val]); } elseif (substr($val, 0, 9) == "PROPERTY_") { $PR_ID = strtoupper(substr($val, 9)); if ($db_prop = CIBlockProperty::GetPropertyArray($PR_ID, CIBlock::_MergeIBArrays($arFilter["IBLOCK_ID"], $arFilter["IBLOCK_CODE"]))) { $sGroupBy .= CIBlockElement::MkPropertyGroup($db_prop, $arJoinProps); } } elseif (substr($val, 0, 13) == "PROPERTYSORT_") { $PR_ID = strtoupper(substr($val, 13)); if ($db_prop = CIBlockProperty::GetPropertyArray($PR_ID, CIBlock::_MergeIBArrays($arFilter["IBLOCK_ID"], $arFilter["IBLOCK_CODE"]))) { $sGroupBy .= CIBlockElement::MkPropertyGroup($db_prop, $arJoinProps, true); } } } if ($sGroupBy != "") { $sGroupBy = " GROUP BY " . substr($sGroupBy, 1) . " "; } } //*************************SELECT PART**************************** $arAddSelectFields = array(); if ($bOnlyCount) { $sSelect = "COUNT(%%_DISTINCT_%% BE.ID) as CNT "; } else { $sSelect = ""; $arDisplayedColumns = array(); $arProductFields = array("CATALOG_PURCHASING_PRICE" => true, "CATALOG_PURCHASING_CURRENCY" => true, "CATALOG_WEIGHT" => true, "CATALOG_QUANTITY" => true, "CATALOG_QUANTITY_RESERVED" => true, "CATALOG_VAT_INCLUDED" => true, "CATALOG_TYPE" => true, "CATALOG_MEASURE" => true, "CATALOG_AVAILABLE" => true); $bStar = false; foreach ($arSelectFields as $key => $val) { $val = strtoupper($val); if (array_key_exists($val, $arIblockElementFields)) { if (array_key_exists($val, $arDisplayedColumns)) { continue; } $arDisplayedColumns[$val] = true; $arSelectFields[$key] = $val; $sSelect .= "," . $arIblockElementFields[$val] . " as " . $val; } elseif ($val == "PROPERTY_*" && !$bWasGroup) { //We have to analyze arFilter IBLOCK_ID and IBLOCK_CODE //in a way to be shure we will get properties of the ONE IBLOCK ONLY! $arPropertyFilter = array("ACTIVE" => "Y", "VERSION" => 2); if (array_key_exists("IBLOCK_ID", $arFilter)) { if (is_array($arFilter["IBLOCK_ID"]) && count($arFilter["IBLOCK_ID"]) == 1) { $arPropertyFilter["IBLOCK_ID"] = $arFilter["IBLOCK_ID"][0]; } elseif (!is_array($arFilter["IBLOCK_ID"]) && intval($arFilter["IBLOCK_ID"]) > 0) { $arPropertyFilter["IBLOCK_ID"] = $arFilter["IBLOCK_ID"]; } } if (!array_key_exists("IBLOCK_ID", $arPropertyFilter)) { if (array_key_exists("IBLOCK_CODE", $arFilter)) { if (is_array($arFilter["IBLOCK_CODE"]) && count($arFilter["IBLOCK_CODE"]) == 1) { $arPropertyFilter["IBLOCK_CODE"] = $arFilter["IBLOCK_CODE"][0]; } elseif (!is_array($arFilter["IBLOCK_CODE"]) && strlen($arFilter["IBLOCK_CODE"]) > 0) { $arPropertyFilter["IBLOCK_CODE"] = $arFilter["IBLOCK_CODE"]; } else { continue; } } else { continue; } } $rs_prop = CIBlockProperty::GetList(array("sort" => "asc"), $arPropertyFilter); while ($db_prop = $rs_prop->Fetch()) { $arIBlockAllProps[] = $db_prop; } $iblock_id = false; foreach ($arIBlockAllProps as $db_prop) { if ($db_prop["USER_TYPE"] != "") { $arUserType = CIBlockProperty::GetUserType($db_prop["USER_TYPE"]); if (array_key_exists("ConvertFromDB", $arUserType)) { $arIBlockConvProps["PROPERTY_" . $db_prop["ID"]] = array("ConvertFromDB" => $arUserType["ConvertFromDB"], "PROPERTY" => $db_prop); } } $db_prop["ORIG_ID"] = $db_prop["ID"]; if ($db_prop["MULTIPLE"] == "Y") { $arIBlockMultProps["PROPERTY_" . $db_prop["ID"]] = $db_prop; } $iblock_id = $db_prop["IBLOCK_ID"]; } if ($iblock_id !== false) { if (!array_key_exists($iblock_id, $arJoinProps["FPS"])) { $arJoinProps["FPS"][$iblock_id] = count($arJoinProps["FPS"]); } $iPropCnt = $arJoinProps["FPS"][$iblock_id]; $sSelect .= ", FPS" . $iPropCnt . ".*"; } } elseif (substr($val, 0, 9) == "PROPERTY_") { $PR_ID = strtoupper($val); if (array_key_exists($PR_ID, $arDisplayedColumns)) { continue; } $arDisplayedColumns[$PR_ID] = true; $PR_ID = substr($PR_ID, 9); if (preg_match("/^([^.]+)\\.([^.]+)\$/", $PR_ID, $arMatch)) { $db_prop = CIBlockProperty::GetPropertyArray($arMatch[1], CIBlock::_MergeIBArrays($arFilter["IBLOCK_ID"], $arFilter["IBLOCK_CODE"])); if (is_array($db_prop) && $db_prop["PROPERTY_TYPE"] == "E") { CIBlockElement::MkPropertySelect($arMatch, $db_prop, $arIBlockLongProps, $arIBlockConvProps, $arJoinProps, $arIBlockMultProps, $arIBlockNumProps, $bWasGroup, $sGroupBy, $sSelect); } } else { if ($db_prop = CIBlockProperty::GetPropertyArray($PR_ID, CIBlock::_MergeIBArrays($arFilter["IBLOCK_ID"], $arFilter["IBLOCK_CODE"]))) { CIBlockElement::MkPropertySelect($PR_ID, $db_prop, $arIBlockLongProps, $arIBlockConvProps, $arJoinProps, $arIBlockMultProps, $arIBlockNumProps, $bWasGroup, $sGroupBy, $sSelect); } } } elseif (substr($val, 0, 13) == "PROPERTYSORT_") { $PR_ID = strtoupper($val); if (array_key_exists($PR_ID, $arDisplayedColumns)) { continue; } $arDisplayedColumns[$PR_ID] = true; $PR_ID = substr($PR_ID, 13); if (preg_match("/^([^.]+)\\.([^.]+)\$/", $PR_ID, $arMatch)) { $db_prop = CIBlockProperty::GetPropertyArray($arMatch[1], CIBlock::_MergeIBArrays($arFilter["IBLOCK_ID"], $arFilter["IBLOCK_CODE"])); if (is_array($db_prop) && $db_prop["PROPERTY_TYPE"] == "E") { CIBlockElement::MkPropertySelect($arMatch, $db_prop, $arIBlockLongProps, $arIBlockConvProps, $arJoinProps, $arIBlockMultProps, $arIBlockNumProps, $bWasGroup, $sGroupBy, $sSelect, true); } } else { if ($db_prop = CIBlockProperty::GetPropertyArray($PR_ID, CIBlock::_MergeIBArrays($arFilter["IBLOCK_ID"], $arFilter["IBLOCK_CODE"]))) { CIBlockElement::MkPropertySelect($PR_ID, $db_prop, $arIBlockLongProps, $arIBlockConvProps, $arJoinProps, $arIBlockMultProps, $arIBlockNumProps, $bWasGroup, $sGroupBy, $sSelect, true); } } } elseif ($val == "*") { $bStar = true; } elseif (substr($val, 0, 14) == "CATALOG_GROUP_") { $arAddSelectFields[] = $val; } elseif (substr($val, 0, 21) == "CATALOG_STORE_AMOUNT_") { $arAddSelectFields[] = $val; } elseif (isset($arProductFields[$val])) { $arAddSelectFields[] = $val; } elseif ($val == "RATING_TOTAL_VALUE" || $val == "RATING_TOTAL_VOTES" || $val == "RATING_TOTAL_POSITIVE_VOTES" || $val == "RATING_TOTAL_NEGATIVE_VOTES") { if (array_key_exists($val, $arDisplayedColumns)) { continue; } $arDisplayedColumns[$val] = true; $arSelectFields[$key] = $val; $sSelect .= "," . preg_replace("/^RATING_/", "RV.", $val) . " as " . $val; $arJoinProps["RV"] = true; } elseif ($val == "RATING_USER_VOTE_VALUE") { if (array_key_exists($val, $arDisplayedColumns)) { continue; } $arDisplayedColumns[$val] = true; $arSelectFields[$key] = $val; if (isset($USER) && is_object($USER)) { $sSelect .= "," . $DB->IsNull('RVU.VALUE', '0') . " as " . $val; $arJoinProps["RVU"] = true; } else { $sSelect .= ",0 as " . $val; } } } if ($bStar) { foreach ($arIblockElementFields as $key => $val) { if (array_key_exists($key, $arDisplayedColumns)) { continue; } $arDisplayedColumns[$key] = true; $arSelectFields[] = $key; $sSelect .= "," . $val . " as " . $key; } } elseif ($sGroupBy == "") { //Try to add missing fields for correct URL translation (only then no grouping) if (array_key_exists("DETAIL_PAGE_URL", $arDisplayedColumns)) { $arAddFields = array("LANG_DIR", "ID", "CODE", "EXTERNAL_ID", "IBLOCK_SECTION_ID", "IBLOCK_TYPE_ID", "IBLOCK_ID", "IBLOCK_CODE", "IBLOCK_EXTERNAL_ID", "LID"); } elseif (array_key_exists("CANONICAL_PAGE_URL", $arDisplayedColumns)) { $arAddFields = array("LANG_DIR", "ID", "CODE", "EXTERNAL_ID", "IBLOCK_SECTION_ID", "IBLOCK_TYPE_ID", "IBLOCK_ID", "IBLOCK_CODE", "IBLOCK_EXTERNAL_ID", "LID"); } elseif (array_key_exists("SECTION_PAGE_URL", $arDisplayedColumns)) { $arAddFields = array("LANG_DIR", "ID", "CODE", "EXTERNAL_ID", "IBLOCK_SECTION_ID", "IBLOCK_TYPE_ID", "IBLOCK_ID", "IBLOCK_CODE", "IBLOCK_EXTERNAL_ID", "LID"); } elseif (array_key_exists("LIST_PAGE_URL", $arDisplayedColumns)) { $arAddFields = array("LANG_DIR", "IBLOCK_TYPE_ID", "IBLOCK_ID", "IBLOCK_CODE", "IBLOCK_EXTERNAL_ID", "LID"); } else { $arAddFields = array(); } //Try to add missing fields for correct PREVIEW and DETAIL text formatting if (array_key_exists("DETAIL_TEXT", $arDisplayedColumns)) { $arAddFields[] = "DETAIL_TEXT_TYPE"; } if (array_key_exists("PREVIEW_TEXT", $arDisplayedColumns)) { $arAddFields[] = "PREVIEW_TEXT_TYPE"; } foreach ($arAddFields as $key) { if (array_key_exists($key, $arDisplayedColumns)) { continue; } $arDisplayedColumns[$key] = true; $arSelectFields[] = $key; $sSelect .= "," . $arIblockElementFields[$key] . " as " . $key; } } if ($sGroupBy != "") { $sSelect = substr($sSelect, 1) . ", COUNT(%%_DISTINCT_%% BE.ID) as CNT "; } elseif (strlen($sSelect) > 0) { $sSelect = "%%_DISTINCT_%% " . substr($sSelect, 1) . " "; } } //*********************WHERE PART********************* $arAddWhereFields = array(); if (is_array($arFilter) && array_key_exists("CATALOG", $arFilter)) { $arAddWhereFields = $arFilter["CATALOG"]; unset($arFilter["CATALOG"]); } $arSqlSearch = CIBlockElement::MkFilter($arFilter, $arJoinProps, $arAddWhereFields); $bDistinct = false; $sSectionWhere = ""; $sWhere = ""; foreach ($arSqlSearch as $condition) { if (strlen(trim($condition, "\n\t")) > 0) { $sWhere .= "\n\t\t\tAND (" . $condition . ")"; } } }