コード例 #1
0
ファイル: iblockelement.php プロジェクト: spas-viktor/books
 function GetList($arOrder = array("SORT" => "ASC"), $arFilter = array(), $arGroupBy = false, $arNavStartParams = false, $arSelectFields = array())
 {
     /*
     Filter combinations:
     CHECK_PERMISSIONS="N" - check permissions of the current user to the infoblock
     	MIN_PERMISSION="R" - when permissions check, then minimal access level
     SHOW_HISTORY="N" - add history items to list
     	SHOW_NEW="N" - if not add history items, then add new, but not published elements
     */
     global $DB, $USER, $APPLICATION;
     $MAX_LOCK = intval(COption::GetOptionString("workflow", "MAX_LOCK_TIME", "60"));
     $uid = is_object($USER) ? intval($USER->GetID()) : 0;
     $arIblockElementFields = array("ID" => "BE.ID", "TIMESTAMP_X" => $DB->DateToCharFunction("BE.TIMESTAMP_X"), "TIMESTAMP_X_UNIX" => 'UNIX_TIMESTAMP(BE.TIMESTAMP_X)', "MODIFIED_BY" => "BE.MODIFIED_BY", "DATE_CREATE" => $DB->DateToCharFunction("BE.DATE_CREATE"), "DATE_CREATE_UNIX" => 'UNIX_TIMESTAMP(BE.DATE_CREATE)', "CREATED_BY" => "BE.CREATED_BY", "IBLOCK_ID" => "BE.IBLOCK_ID", "IBLOCK_SECTION_ID" => "BE.IBLOCK_SECTION_ID", "ACTIVE" => "BE.ACTIVE", "ACTIVE_FROM" => CPageOption::GetOptionString("iblock", "FORMAT_ACTIVE_DATES", "-") != "-" ? $DB->DateToCharFunction("BE.ACTIVE_FROM", CPageOption::GetOptionString("iblock", "FORMAT_ACTIVE_DATES", "SHORT")) : "IF(EXTRACT(HOUR_SECOND FROM BE.ACTIVE_FROM)>0, " . $DB->DateToCharFunction("BE.ACTIVE_FROM", "FULL") . ", " . $DB->DateToCharFunction("BE.ACTIVE_FROM", "SHORT") . ")", "ACTIVE_TO" => CPageOption::GetOptionString("iblock", "FORMAT_ACTIVE_DATES", "-") != "-" ? $DB->DateToCharFunction("BE.ACTIVE_TO", CPageOption::GetOptionString("iblock", "FORMAT_ACTIVE_DATES", "SHORT")) : "IF(EXTRACT(HOUR_SECOND FROM BE.ACTIVE_TO)>0, " . $DB->DateToCharFunction("BE.ACTIVE_TO", "FULL") . ", " . $DB->DateToCharFunction("BE.ACTIVE_TO", "SHORT") . ")", "DATE_ACTIVE_FROM" => CPageOption::GetOptionString("iblock", "FORMAT_ACTIVE_DATES", "-") != "-" ? $DB->DateToCharFunction("BE.ACTIVE_FROM", CPageOption::GetOptionString("iblock", "FORMAT_ACTIVE_DATES", "SHORT")) : "IF(EXTRACT(HOUR_SECOND FROM BE.ACTIVE_FROM)>0, " . $DB->DateToCharFunction("BE.ACTIVE_FROM", "FULL") . ", " . $DB->DateToCharFunction("BE.ACTIVE_FROM", "SHORT") . ")", "DATE_ACTIVE_TO" => CPageOption::GetOptionString("iblock", "FORMAT_ACTIVE_DATES", "-") != "-" ? $DB->DateToCharFunction("BE.ACTIVE_TO", CPageOption::GetOptionString("iblock", "FORMAT_ACTIVE_DATES", "SHORT")) : "IF(EXTRACT(HOUR_SECOND FROM BE.ACTIVE_TO)>0, " . $DB->DateToCharFunction("BE.ACTIVE_TO", "FULL") . ", " . $DB->DateToCharFunction("BE.ACTIVE_TO", "SHORT") . ")", "SORT" => "BE.SORT", "NAME" => "BE.NAME", "PREVIEW_PICTURE" => "BE.PREVIEW_PICTURE", "PREVIEW_TEXT" => "BE.PREVIEW_TEXT", "PREVIEW_TEXT_TYPE" => "BE.PREVIEW_TEXT_TYPE", "DETAIL_PICTURE" => "BE.DETAIL_PICTURE", "DETAIL_TEXT" => "BE.DETAIL_TEXT", "DETAIL_TEXT_TYPE" => "BE.DETAIL_TEXT_TYPE", "SEARCHABLE_CONTENT" => "BE.SEARCHABLE_CONTENT", "WF_STATUS_ID" => "BE.WF_STATUS_ID", "WF_PARENT_ELEMENT_ID" => "BE.WF_PARENT_ELEMENT_ID", "WF_LAST_HISTORY_ID" => "BE.WF_LAST_HISTORY_ID", "WF_NEW" => "BE.WF_NEW", "LOCK_STATUS" => "if (BE.WF_DATE_LOCK is null, 'green', if(DATE_ADD(BE.WF_DATE_LOCK, interval " . $MAX_LOCK . " MINUTE)<now(), 'green', if(BE.WF_LOCKED_BY=" . $uid . ", 'yellow', 'red')))", "WF_LOCKED_BY" => "BE.WF_LOCKED_BY", "WF_DATE_LOCK" => $DB->DateToCharFunction("BE.WF_DATE_LOCK"), "WF_COMMENTS" => "BE.WF_COMMENTS", "IN_SECTIONS" => "BE.IN_SECTIONS", "SHOW_COUNTER" => "BE.SHOW_COUNTER", "SHOW_COUNTER_START" => "BE.SHOW_COUNTER_START", "CODE" => "BE.CODE", "TAGS" => "BE.TAGS", "XML_ID" => "BE.XML_ID", "EXTERNAL_ID" => "BE.XML_ID", "TMP_ID" => "BE.TMP_ID", "USER_NAME" => "concat('(',U.LOGIN,') ',ifnull(U.NAME,''),' ',ifnull(U.LAST_NAME,''))", "LOCKED_USER_NAME" => "concat('(',UL.LOGIN,') ',ifnull(UL.NAME,''),' ',ifnull(UL.LAST_NAME,''))", "CREATED_USER_NAME" => "concat('(',UC.LOGIN,') ',ifnull(UC.NAME,''),' ',ifnull(UC.LAST_NAME,''))", "LANG_DIR" => "L.DIR", "LID" => "B.LID", "IBLOCK_TYPE_ID" => "B.IBLOCK_TYPE_ID", "IBLOCK_CODE" => "B.CODE", "IBLOCK_NAME" => "B.NAME", "IBLOCK_EXTERNAL_ID" => "B.XML_ID", "DETAIL_PAGE_URL" => "B.DETAIL_PAGE_URL", "LIST_PAGE_URL" => "B.LIST_PAGE_URL", "CREATED_DATE" => $DB->DateFormatToDB("YYYY.MM.DD", "BE.DATE_CREATE"), "BP_PUBLISHED" => "if(BE.WF_STATUS_ID = 1, 'Y', 'N')");
     $bDistinct = false;
     CIBlockElement::PrepareGetList($arIblockElementFields, $arJoinProps, $bOnlyCount, $bDistinct, $arSelectFields, $sSelect, $arAddSelectFields, $arFilter, $sWhere, $sSectionWhere, $arAddWhereFields, $arGroupBy, $sGroupBy, $arOrder, $arSqlOrder, $arAddOrderByFields, $arIBlockFilter, $arIBlockMultProps, $arIBlockConvProps, $arIBlockAllProps, $arIBlockNumProps, $arIBlockLongProps);
     $arFilterIBlocks = isset($arFilter["IBLOCK_ID"]) ? array($arFilter["IBLOCK_ID"]) : array();
     //******************FROM PART********************************************
     $sFrom = "";
     foreach ($arJoinProps["FPS"] as $iblock_id => $iPropCnt) {
         $sFrom .= "\t\t\tINNER JOIN b_iblock_element_prop_s" . $iblock_id . " FPS" . $iPropCnt . " ON FPS" . $iPropCnt . ".IBLOCK_ELEMENT_ID = BE.ID\n";
         $arFilterIBlocks[$iblock_id] = $iblock_id;
     }
     foreach ($arJoinProps["FP"] as $propID => $db_prop) {
         $i = $db_prop["CNT"];
         if ($db_prop["bFullJoin"]) {
             $sFrom .= "\t\t\tINNER JOIN b_iblock_property FP" . $i . " ON FP" . $i . ".IBLOCK_ID = B.ID AND " . (IntVal($propID) > 0 ? " FP" . $i . ".ID=" . IntVal($propID) . "\n" : " FP" . $i . ".CODE='" . $DB->ForSQL($propID, 200) . "'\n");
         } else {
             $sFrom .= "\t\t\tLEFT JOIN b_iblock_property FP" . $i . " ON FP" . $i . ".IBLOCK_ID = B.ID AND " . (IntVal($propID) > 0 ? " FP" . $i . ".ID=" . IntVal($propID) . "\n" : " FP" . $i . ".CODE='" . $DB->ForSQL($propID, 200) . "'\n");
         }
         if ($db_prop["IBLOCK_ID"]) {
             $arFilterIBlocks[$db_prop["IBLOCK_ID"]] = $db_prop["IBLOCK_ID"];
         }
     }
     foreach ($arJoinProps["FPV"] as $propID => $db_prop) {
         $i = $db_prop["CNT"];
         if ($db_prop["MULTIPLE"] == "Y") {
             $bDistinct = true;
         }
         if ($db_prop["VERSION"] == 2) {
             $strTable = "b_iblock_element_prop_m" . $db_prop["IBLOCK_ID"];
         } else {
             $strTable = "b_iblock_element_property";
         }
         if ($db_prop["bFullJoin"]) {
             $sFrom .= "\t\t\tINNER JOIN " . $strTable . " FPV" . $i . " ON FPV" . $i . ".IBLOCK_PROPERTY_ID = FP" . $db_prop["JOIN"] . ".ID AND FPV" . $i . ".IBLOCK_ELEMENT_ID = BE.ID\n";
         } else {
             $sFrom .= "\t\t\tLEFT JOIN " . $strTable . " FPV" . $i . " ON FPV" . $i . ".IBLOCK_PROPERTY_ID = FP" . $db_prop["JOIN"] . ".ID AND FPV" . $i . ".IBLOCK_ELEMENT_ID = BE.ID\n";
         }
         if ($db_prop["IBLOCK_ID"]) {
             $arFilterIBlocks[$db_prop["IBLOCK_ID"]] = $db_prop["IBLOCK_ID"];
         }
     }
     foreach ($arJoinProps["FPEN"] as $propID => $db_prop) {
         $i = $db_prop["CNT"];
         if ($db_prop["VERSION"] == 2 && $db_prop["MULTIPLE"] == "N") {
             if ($db_prop["bFullJoin"]) {
                 $sFrom .= "\t\t\tINNER JOIN b_iblock_property_enum FPEN" . $i . " ON FPEN" . $i . ".PROPERTY_ID = " . $db_prop["ORIG_ID"] . " AND FPS" . $db_prop["JOIN"] . ".PROPERTY_" . $db_prop["ORIG_ID"] . " = FPEN" . $i . ".ID\n";
             } else {
                 $sFrom .= "\t\t\tLEFT JOIN b_iblock_property_enum FPEN" . $i . " ON FPEN" . $i . ".PROPERTY_ID = " . $db_prop["ORIG_ID"] . " AND FPS" . $db_prop["JOIN"] . ".PROPERTY_" . $db_prop["ORIG_ID"] . " = FPEN" . $i . ".ID\n";
             }
         } else {
             if ($db_prop["bFullJoin"]) {
                 $sFrom .= "\t\t\tINNER JOIN b_iblock_property_enum FPEN" . $i . " ON FPEN" . $i . ".PROPERTY_ID = FPV" . $db_prop["JOIN"] . ".IBLOCK_PROPERTY_ID AND FPV" . $db_prop["JOIN"] . ".VALUE_ENUM = FPEN" . $i . ".ID\n";
             } else {
                 $sFrom .= "\t\t\tLEFT JOIN b_iblock_property_enum FPEN" . $i . " ON FPEN" . $i . ".PROPERTY_ID = FPV" . $db_prop["JOIN"] . ".IBLOCK_PROPERTY_ID AND FPV" . $db_prop["JOIN"] . ".VALUE_ENUM = FPEN" . $i . ".ID\n";
             }
         }
         if ($db_prop["IBLOCK_ID"]) {
             $arFilterIBlocks[$db_prop["IBLOCK_ID"]] = $db_prop["IBLOCK_ID"];
         }
     }
     foreach ($arJoinProps["BE"] as $propID => $db_prop) {
         $i = $db_prop["CNT"];
         $sFrom .= "\t\t\tLEFT JOIN b_iblock_element BE" . $i . " ON BE" . $i . ".ID = " . ($db_prop["VERSION"] == 2 && $db_prop["MULTIPLE"] == "N" ? "FPS" . $db_prop["JOIN"] . ".PROPERTY_" . $db_prop["ORIG_ID"] : "FPV" . $db_prop["JOIN"] . ".VALUE_NUM") . ($arFilter["SHOW_HISTORY"] != "Y" ? " AND ((BE.WF_STATUS_ID=1 AND BE.WF_PARENT_ELEMENT_ID IS NULL)" . ($arFilter["SHOW_NEW"] == "Y" ? " OR BE.WF_NEW='Y'" : "") . ")" : "") . "\n";
         if ($db_prop["bJoinIBlock"]) {
             $sFrom .= "\t\t\tLEFT JOIN b_iblock B" . $i . " ON B" . $i . ".ID = BE" . $i . ".IBLOCK_ID\n";
         }
         if ($db_prop["bJoinSection"]) {
             $sFrom .= "\t\t\tLEFT JOIN b_iblock_section BS" . $i . " ON BS" . $i . ".ID = BE" . $i . ".IBLOCK_SECTION_ID\n";
         }
         if ($db_prop["IBLOCK_ID"]) {
             $arFilterIBlocks[$db_prop["IBLOCK_ID"]] = $db_prop["IBLOCK_ID"];
         }
     }
     foreach ($arJoinProps["BE_FPS"] as $iblock_id => $db_prop) {
         $sFrom .= "\t\t\tLEFT JOIN b_iblock_element_prop_s" . $iblock_id . " JFPS" . $db_prop["CNT"] . " ON JFPS" . $db_prop["CNT"] . ".IBLOCK_ELEMENT_ID = BE" . $db_prop["JOIN"] . ".ID\n";
         if ($db_prop["IBLOCK_ID"]) {
             $arFilterIBlocks[$db_prop["IBLOCK_ID"]] = $db_prop["IBLOCK_ID"];
         }
     }
     foreach ($arJoinProps["BE_FP"] as $propID => $db_prop) {
         $i = $db_prop["CNT"];
         list($propID, $link) = explode("~", $propID, 2);
         if ($db_prop["bFullJoin"]) {
             $sFrom .= "\t\t\tINNER JOIN b_iblock_property JFP" . $i . " ON JFP" . $i . ".IBLOCK_ID = BE" . $db_prop["JOIN"] . ".IBLOCK_ID AND " . (IntVal($propID) > 0 ? " JFP" . $i . ".ID=" . IntVal($propID) . "\n" : " JFP" . $i . ".CODE='" . $DB->ForSQL($propID, 200) . "'\n");
         } else {
             $sFrom .= "\t\t\tLEFT JOIN b_iblock_property JFP" . $i . " ON JFP" . $i . ".IBLOCK_ID = BE" . $db_prop["JOIN"] . ".IBLOCK_ID AND " . (IntVal($propID) > 0 ? " JFP" . $i . ".ID=" . IntVal($propID) . "\n" : " JFP" . $i . ".CODE='" . $DB->ForSQL($propID, 200) . "'\n");
         }
         if ($db_prop["IBLOCK_ID"]) {
             $arFilterIBlocks[$db_prop["IBLOCK_ID"]] = $db_prop["IBLOCK_ID"];
         }
     }
     foreach ($arJoinProps["BE_FPV"] as $propID => $db_prop) {
         $i = $db_prop["CNT"];
         list($propID, $link) = explode("~", $propID, 2);
         if ($db_prop["MULTIPLE"] == "Y") {
             $bDistinct = true;
         }
         if ($db_prop["VERSION"] == 2) {
             $strTable = "b_iblock_element_prop_m" . $db_prop["IBLOCK_ID"];
         } else {
             $strTable = "b_iblock_element_property";
         }
         if ($db_prop["bFullJoin"]) {
             $sFrom .= "\t\t\tINNER JOIN " . $strTable . " JFPV" . $i . " ON JFPV" . $i . ".IBLOCK_PROPERTY_ID = JFP" . $db_prop["JOIN"] . ".ID AND JFPV" . $i . ".IBLOCK_ELEMENT_ID = BE" . $db_prop["BE_JOIN"] . ".ID\n";
         } else {
             $sFrom .= "\t\t\tLEFT JOIN " . $strTable . " JFPV" . $i . " ON JFPV" . $i . ".IBLOCK_PROPERTY_ID = JFP" . $db_prop["JOIN"] . ".ID AND JFPV" . $i . ".IBLOCK_ELEMENT_ID = BE" . $db_prop["BE_JOIN"] . ".ID\n";
         }
         if ($db_prop["IBLOCK_ID"]) {
             $arFilterIBlocks[$db_prop["IBLOCK_ID"]] = $db_prop["IBLOCK_ID"];
         }
     }
     foreach ($arJoinProps["BE_FPEN"] as $propID => $db_prop) {
         $i = $db_prop["CNT"];
         list($propID, $link) = explode("~", $propID, 2);
         if ($db_prop["VERSION"] == 2 && $db_prop["MULTIPLE"] == "N") {
             if ($db_prop["bFullJoin"]) {
                 $sFrom .= "\t\t\tINNER JOIN b_iblock_property_enum JFPEN" . $i . " ON JFPEN" . $i . ".PROPERTY_ID = " . $db_prop["ORIG_ID"] . " AND JFPS" . $db_prop["JOIN"] . ".PROPERTY_" . $db_prop["ORIG_ID"] . " = JFPEN" . $i . ".ID\n";
             } else {
                 $sFrom .= "\t\t\tLEFT JOIN b_iblock_property_enum JFPEN" . $i . " ON JFPEN" . $i . ".PROPERTY_ID = " . $db_prop["ORIG_ID"] . " AND JFPS" . $db_prop["JOIN"] . ".PROPERTY_" . $db_prop["ORIG_ID"] . " = JFPEN" . $i . ".ID\n";
             }
         } else {
             if ($db_prop["bFullJoin"]) {
                 $sFrom .= "\t\t\tINNER JOIN b_iblock_property_enum JFPEN" . $i . " ON JFPEN" . $i . ".PROPERTY_ID = JFPV" . $db_prop["JOIN"] . ".IBLOCK_PROPERTY_ID AND JFPV" . $db_prop["JOIN"] . ".VALUE_ENUM = JFPEN" . $i . ".ID\n";
             } else {
                 $sFrom .= "\t\t\tLEFT JOIN b_iblock_property_enum JFPEN" . $i . " ON JFPEN" . $i . ".PROPERTY_ID = JFPV" . $db_prop["JOIN"] . ".IBLOCK_PROPERTY_ID AND JFPV" . $db_prop["JOIN"] . ".VALUE_ENUM = JFPEN" . $i . ".ID\n";
             }
         }
         if ($db_prop["IBLOCK_ID"]) {
             $arFilterIBlocks[$db_prop["IBLOCK_ID"]] = $db_prop["IBLOCK_ID"];
         }
     }
     if (strlen($arJoinProps["BES"])) {
         $sFrom .= "\t\t\t" . $arJoinProps["BES"] . "\n";
     }
     if ($arJoinProps["RV"]) {
         $sFrom .= "\t\t\tLEFT JOIN b_rating_voting RV ON RV.ENTITY_TYPE_ID = 'IBLOCK_ELEMENT' AND RV.ENTITY_ID = BE.ID\n";
     }
     if ($arJoinProps["RVU"]) {
         $sFrom .= "\t\t\tLEFT JOIN b_rating_vote RVU ON RVU.ENTITY_TYPE_ID = 'IBLOCK_ELEMENT' AND RVU.ENTITY_ID = BE.ID AND RVU.USER_ID = " . $uid . "\n";
     }
     if ($arJoinProps["RVV"]) {
         $sFrom .= "\t\t\t" . ($arJoinProps["RVV"]["bFullJoin"] ? "INNER" : "LEFT") . " JOIN b_rating_vote RVV ON RVV.ENTITY_TYPE_ID = 'IBLOCK_ELEMENT' AND RVV.ENTITY_ID = BE.ID\n";
     }
     //******************END OF FROM PART********************************************
     $bCatalogSort = false;
     if (count($arAddSelectFields) > 0 || count($arAddWhereFields) > 0 || count($arAddOrderByFields) > 0) {
         if (CModule::IncludeModule("catalog")) {
             $res_catalog = CCatalogProduct::GetQueryBuildArrays($arAddOrderByFields, $arAddWhereFields, $arAddSelectFields);
             if ($sGroupBy == "" && !$bOnlyCount && !(is_object($this) && isset($this->strField))) {
                 $sSelect .= $res_catalog["SELECT"] . " ";
             }
             $sFrom .= str_replace("LEFT JOIN", "\n\t\t\tLEFT JOIN", $res_catalog["FROM"]) . "\n";
             //$sWhere .= $res_catalog["WHERE"]." "; moved to MkFilter
             if (is_array($res_catalog["ORDER"]) && count($res_catalog["ORDER"])) {
                 $bCatalogSort = true;
                 foreach ($res_catalog["ORDER"] as $i => $val) {
                     $arSqlOrder[$i] = $val;
                 }
             }
         }
     }
     $i = array_search("CREATED_BY_FORMATTED", $arSelectFields);
     if ($i !== false) {
         if ($sSelect && $sGroupBy == "" && !$bOnlyCount && !(is_object($this) && isset($this->strField))) {
             $sSelect .= ",UC.NAME UC_NAME, UC.LAST_NAME UC_LAST_NAME, UC.SECOND_NAME UC_SECOND_NAME, UC.EMAIL UC_EMAIL, UC.ID UC_ID, UC.LOGIN UC_LOGIN";
         } else {
             unset($arSelectFields[$i]);
         }
     }
     $sOrderBy = "";
     foreach ($arSqlOrder as $i => $val) {
         if (strlen($val)) {
             if ($sOrderBy == "") {
                 $sOrderBy = " ORDER BY ";
             } else {
                 $sOrderBy .= ",";
             }
             $sOrderBy .= $val . " ";
         }
     }
     if (strlen(trim($sSelect)) <= 0) {
         $sSelect = "0 as NOP ";
     }
     $bDistinct = $bDistinct || isset($arFilter["INCLUDE_SUBSECTIONS"]) && $arFilter["INCLUDE_SUBSECTIONS"] == "Y";
     if ($bDistinct) {
         $sSelect = str_replace("%%_DISTINCT_%%", "DISTINCT", $sSelect);
     } else {
         $sSelect = str_replace("%%_DISTINCT_%%", "", $sSelect);
     }
     $sFrom = "\n\t\t\tb_iblock B\n\t\t\tINNER JOIN b_lang L ON B.LID=L.LID\n\t\t\tINNER JOIN b_iblock_element BE ON BE.IBLOCK_ID = B.ID\n\t\t\t" . ltrim($sFrom, "\t\n") . (in_array("USER_NAME", $arSelectFields) ? "\t\t\tLEFT JOIN b_user U ON U.ID=BE.MODIFIED_BY\n" : "") . (in_array("LOCKED_USER_NAME", $arSelectFields) ? "\t\t\tLEFT JOIN b_user UL ON UL.ID=BE.WF_LOCKED_BY\n" : "") . (in_array("CREATED_USER_NAME", $arSelectFields) || in_array("CREATED_BY_FORMATTED", $arSelectFields) ? "\t\t\tLEFT JOIN b_user UC ON UC.ID=BE.CREATED_BY\n" : "") . "\n\t\t";
     $strSql = "\n\t\t\tFROM " . $sFrom . "\n\t\t\tWHERE 1=1 " . $sWhere . "\n\t\t\t" . $sGroupBy . "\n\t\t";
     if (isset($this) && is_object($this) && isset($this->strField)) {
         $this->sFrom = $sFrom;
         $this->sWhere = $sWhere;
         return "SELECT " . $sSelect . $strSql;
     }
     if ($bOnlyCount) {
         $res = $DB->Query("SELECT " . $sSelect . $strSql, false, "FILE: " . __FILE__ . "<br> LINE: " . __LINE__);
         $res = $res->Fetch();
         return $res["CNT"];
     }
     if (is_array($arNavStartParams)) {
         $nTopCount = intval($arNavStartParams["nTopCount"]);
         $nElementID = intval($arNavStartParams["nElementID"]);
         if ($nTopCount > 0) {
             $strSql = "SELECT " . $sSelect . $strSql . $sOrderBy . " LIMIT " . $nTopCount;
             $res = $DB->Query($strSql);
         } elseif ($nElementID > 0 && $sGroupBy == "" && $sOrderBy != "" && strpos($sSelect, "BE.ID") !== false && !$bCatalogSort) {
             $nPageSize = intval($arNavStartParams["nPageSize"]);
             if ($nPageSize > 0) {
                 $DB->Query("SET @rank=0");
                 $DB->Query("\n\t\t\t\t\t\tSELECT @rank:=el1.rank\n\t\t\t\t\t\tFROM (\n\t\t\t\t\t\t\tSELECT @rank:=@rank+1 AS rank, el0.*\n\t\t\t\t\t\t\tFROM (\n\t\t\t\t\t\t\t\tSELECT " . $sSelect . $strSql . $sOrderBy . "\n\t\t\t\t\t\t\t) el0\n\t\t\t\t\t\t) el1\n\t\t\t\t\t\tWHERE el1.ID = " . $nElementID . "\n\t\t\t\t\t");
                 $DB->Query("SET @rank2=0");
                 $res = $DB->Query("\n\t\t\t\t\t\tSELECT *\n\t\t\t\t\t\tFROM (\n\t\t\t\t\t\t\tSELECT @rank2:=@rank2+1 AS RANK, el0.*\n\t\t\t\t\t\t\tFROM (\n\t\t\t\t\t\t\t\tSELECT " . $sSelect . $strSql . $sOrderBy . "\n\t\t\t\t\t\t\t) el0\n\t\t\t\t\t\t) el1\n\t\t\t\t\t\tWHERE el1.RANK between @rank-{$nPageSize} and @rank+{$nPageSize}\n\t\t\t\t\t");
             } else {
                 $DB->Query("SET @rank=0");
                 $res = $DB->Query("\n\t\t\t\t\t\tSELECT el1.*\n\t\t\t\t\t\tFROM (\n\t\t\t\t\t\t\tSELECT @rank:=@rank+1 AS RANK, el0.*\n\t\t\t\t\t\t\tFROM (\n\t\t\t\t\t\t\t\tSELECT " . $sSelect . $strSql . $sOrderBy . "\n\t\t\t\t\t\t\t) el0\n\t\t\t\t\t\t) el1\n\t\t\t\t\t\tWHERE el1.ID = " . $nElementID . "\n\t\t\t\t\t");
             }
         } else {
             if ($sGroupBy == "") {
                 $res_cnt = $DB->Query("SELECT COUNT(" . ($bDistinct ? "DISTINCT BE.ID" : "'x'") . ") as C " . $strSql);
                 $res_cnt = $res_cnt->Fetch();
                 $cnt = $res_cnt["C"];
             } else {
                 $res_cnt = $DB->Query("SELECT 'x' " . $strSql);
                 $cnt = $res_cnt->SelectedRowsCount();
             }
             $strSql = "SELECT " . $sSelect . $strSql . $sOrderBy;
             $res = new CDBResult();
             $res->NavQuery($strSql, $cnt, $arNavStartParams);
         }
     } else {
         $strSql = "SELECT " . $sSelect . $strSql . $sOrderBy;
         $res = $DB->Query($strSql, false, "FILE: " . __FILE__ . "<br> LINE: " . __LINE__);
     }
     $res = new CIBlockResult($res);
     $res->SetIBlockTag($arFilterIBlocks);
     $res->arIBlockMultProps = $arIBlockMultProps;
     $res->arIBlockConvProps = $arIBlockConvProps;
     $res->arIBlockAllProps = $arIBlockAllProps;
     $res->arIBlockNumProps = $arIBlockNumProps;
     $res->arIBlockLongProps = $arIBlockLongProps;
     return $res;
 }
コード例 #2
0
ファイル: iblockelement.php プロジェクト: DarneoStudio/bitrix
 function MkFilter($arFilter, &$arJoinProps, &$arAddWhereFields, $level = 0, $bPropertyLeftJoin = false)
 {
     global $DB, $USER;
     $arSqlSearch = array();
     $permSQL = "";
     $arSectionFilter = array("LOGIC" => "", "BE" => array(), "BS" => array());
     $strSqlSearch = "";
     if (!is_array($arFilter)) {
         $arFilter = array();
     }
     foreach ($arFilter as $key => $val) {
         $key = strtoupper($key);
         $p = strpos($key, "PROPERTY_");
         if ($p !== false && $p < 4) {
             $arFilter[substr($key, 0, $p) . "PROPERTY"][substr($key, $p + 9)] = $val;
             unset($arFilter[$key]);
         }
     }
     if (array_key_exists("LOGIC", $arFilter) && $arFilter["LOGIC"] == "OR") {
         $Logic = "OR";
         unset($arFilter["LOGIC"]);
         $bPropertyLeftJoin = true;
     } else {
         $Logic = "AND";
     }
     if ($Logic === "AND" && $level === 0) {
         $f = new \Bitrix\Iblock\PropertyIndex\QueryBuilder($arFilter["IBLOCK_ID"]);
         if ($f->isValid()) {
             $arJoinProps["FC"] = $f->getFilterSql($arFilter, $arSqlSearch);
             $arJoinProps["FC_DISTINCT"] = $f->getDistinct();
         }
     }
     foreach ($arFilter as $orig_key => $val) {
         $res = CIBlock::MkOperationFilter($orig_key);
         $key = $res["FIELD"];
         $cOperationType = $res["OPERATION"];
         //it was done before $key = strtoupper($key);
         switch ($key . "") {
             case "ACTIVE":
             case "DETAIL_TEXT_TYPE":
             case "PREVIEW_TEXT_TYPE":
                 $arSqlSearch[] = CIBlock::FilterCreateEx("BE." . $key, $val, "string_equal", $bFullJoinTmp, $cOperationType);
                 break;
             case "NAME":
             case "XML_ID":
             case "TMP_ID":
             case "DETAIL_TEXT":
             case "SEARCHABLE_CONTENT":
             case "PREVIEW_TEXT":
             case "CODE":
             case "TAGS":
             case "WF_COMMENTS":
                 $arSqlSearch[] = CIBlock::FilterCreateEx("BE." . $key, $val, "string", $bFullJoinTmp, $cOperationType);
                 break;
             case "ID":
                 if (is_object($val)) {
                     $arSqlSearch[] = $val->_sql_in("BE." . $key, $cOperationType);
                 } else {
                     $arSqlSearch[] = CIBlock::FilterCreateEx("BE." . $key, $val, "number", $bFullJoinTmp, $cOperationType);
                 }
                 break;
             case "SHOW_COUNTER":
             case "WF_PARENT_ELEMENT_ID":
             case "WF_STATUS_ID":
             case "SORT":
             case "CREATED_BY":
             case "PREVIEW_PICTURE":
             case "DETAIL_PICTURE":
                 $arSqlSearch[] = CIBlock::FilterCreateEx("BE." . $key, $val, "number", $bFullJoinTmp, $cOperationType);
                 break;
             case "IBLOCK_ID":
                 $arSqlSearch[] = CIBlock::FilterCreateEx("BE." . $key, $val, "number", $bFullJoinTmp, $cOperationType);
                 break;
             case "TIMESTAMP_X":
             case "DATE_CREATE":
             case "SHOW_COUNTER_START":
                 $arSqlSearch[] = CIBlock::FilterCreateEx("BE." . $key, $val, "date", $bFullJoinTmp, $cOperationType);
                 break;
             case "EXTERNAL_ID":
                 $arSqlSearch[] = CIBlock::FilterCreateEx("BE.XML_ID", $val, "string", $bFullJoinTmp, $cOperationType);
                 break;
             case "IBLOCK_TYPE":
                 $flt = CIBlock::FilterCreateEx("B.IBLOCK_TYPE_ID", $val, "string", $bFullJoinTmp, $cOperationType);
                 $arSqlSearch[] = $flt;
                 break;
             case "CHECK_PERMISSIONS":
                 if ($val == "Y" && (!is_object($USER) || !$USER->IsAdmin())) {
                     $permSQL = CIBlockElement::_check_rights_sql($arFilter["MIN_PERMISSION"]);
                 }
                 break;
             case "CHECK_BP_PERMISSIONS":
                 if (IsModuleInstalled('bizproc') && (!is_object($USER) || !$USER->IsAdmin())) {
                     if (is_array($val)) {
                         $MODULE_ID = $DB->ForSQL($val["MODULE_ID"]);
                         $ENTITY = $DB->ForSQL($val["ENTITY"]);
                         $PERMISSION = $DB->ForSQL($val["PERMISSION"]);
                         $arUserGroups = array();
                         if (is_array($val["GROUPS"])) {
                             $USER_ID = intval($val["USER_ID"]);
                             foreach ($val["GROUPS"] as $GROUP_ID) {
                                 $GROUP_ID = intval($GROUP_ID);
                                 if ($GROUP_ID) {
                                     $arUserGroups[$GROUP_ID] = $GROUP_ID;
                                 }
                             }
                         } else {
                             $USER_ID = 0;
                         }
                     } else {
                         $MODULE_ID = "iblock";
                         $ENTITY = "CIBlockDocument";
                         $PERMISSION = $val;
                         $arUserGroups = false;
                         $USER_ID = 0;
                     }
                     if ($PERMISSION == "read" || $PERMISSION == "write") {
                         if (!is_array($arUserGroups) && is_object($USER)) {
                             $USER_ID = intval($USER->GetID());
                             $arUserGroups = $USER->GetUserGroupArray();
                         }
                         if (!is_array($arUserGroups) || count($arUserGroups) <= 0) {
                             $arUserGroups = array(2);
                         }
                         $arSqlSearch[] = "EXISTS (\n\t\t\t\t\t\t\tSELECT S.DOCUMENT_ID_INT\n\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\tb_bp_workflow_state S\n\t\t\t\t\t\t\tINNER JOIN b_bp_workflow_permissions P ON S.ID = P.WORKFLOW_ID\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tS.DOCUMENT_ID_INT = BE.ID\n\t\t\t\t\t\t\t\tAND S.MODULE_ID = '{$MODULE_ID}'\n\t\t\t\t\t\t\t\tAND S.ENTITY = '{$ENTITY}'\n\t\t\t\t\t\t\t\tAND P.PERMISSION = '{$PERMISSION}'\n\t\t\t\t\t\t\t\tAND (\n\t\t\t\t\t\t\t\t\tP.OBJECT_ID IN ('" . implode("', '", $arUserGroups) . "')\n\t\t\t\t\t\t\t\t\tOR (P.OBJECT_ID = 'Author' AND BE.CREATED_BY = {$USER_ID})\n\t\t\t\t\t\t\t\t\tOR (P.OBJECT_ID = " . $DB->Concat("'USER_'", "'{$USER_ID}'") . ")\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t)";
                     }
                 }
                 break;
             case "CHECK_BP_TASKS_PERMISSIONS":
                 if (IsModuleInstalled('bizproc') && CModule::IncludeModule("socialnetwork") && (!is_object($USER) || !$USER->IsAdmin())) {
                     $val = explode("_", $val);
                     $taskType = $val[0];
                     if (!in_array($taskType, array("user", "group"))) {
                         $taskType = "user";
                     }
                     $ownerId = intval($val[1]);
                     $val = $val[2];
                     if (!in_array($val, array("read", "write", "comment"))) {
                         $val = "write";
                     }
                     $userId = is_object($USER) ? intval($USER->GetID()) : 0;
                     $arUserGroups = array();
                     if ($taskType == "group") {
                         $r = CSocNetFeaturesPerms::CanPerformOperation($userId, SONET_ENTITY_GROUP, $ownerId, "tasks", $val == "write" ? "edit_tasks" : "view_all");
                         if ($r) {
                             break;
                         }
                         $arUserGroups[] = SONET_ROLES_ALL;
                         $r = CSocNetUserToGroup::GetUserRole($userId, $ownerId);
                         if (strlen($r) > 0) {
                             $arUserGroups[] = $r;
                         }
                     } else {
                         //						$arUserGroups[] = SONET_RELATIONS_TYPE_ALL;
                         //						if (CSocNetUserRelations::IsFriends($userId, $ownerId))
                         //							$arUserGroups[] = SONET_RELATIONS_TYPE_FRIENDS;
                         //						elseif (CSocNetUserRelations::IsFriends2($userId, $ownerId))
                         //							$arUserGroups[] = SONET_RELATIONS_TYPE_FRIENDS2;
                     }
                     $arSqlSearch[] = "EXISTS (\n\t\t\t\t\t\tSELECT S.DOCUMENT_ID_INT\n\t\t\t\t\t\tFROM\n\t\t\t\t\t\tb_bp_workflow_state S\n\t\t\t\t\t\tINNER JOIN b_bp_workflow_permissions P ON S.ID = P.WORKFLOW_ID\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tS.DOCUMENT_ID_INT = BE.ID\n\t\t\t\t\t\t\tAND S.MODULE_ID = 'intranet'\n\t\t\t\t\t\t\tAND S.ENTITY = 'CIntranetTasksDocument'\n\t\t\t\t\t\t\tAND P.PERMISSION = '" . $val . "'\n\t\t\t\t\t\t\tAND (\n\t\t\t\t\t\t\t\t" . ($taskType == "group" ? "P.OBJECT_ID IN ('" . implode("', '", $arUserGroups) . "') OR" : "") . "\n\t\t\t\t\t\t\t\t(P.OBJECT_ID = 'author' AND BE.CREATED_BY = " . $userId . ")\n\t\t\t\t\t\t\t\tOR (P.OBJECT_ID = 'responsible' AND " . $userId . " IN (\n\t\t\t\t\t\t\t\t\tSELECT SFPV0.VALUE_NUM\n\t\t\t\t\t\t\t\t\tFROM b_iblock_element_property SFPV0\n\t\t\t\t\t\t\t\t\t\tINNER JOIN b_iblock_property SFP0 ON (SFPV0.IBLOCK_PROPERTY_ID = SFP0.ID)\n\t\t\t\t\t\t\t\t\tWHERE " . CIBlock::_Upper("SFP0.CODE") . "='TASKASSIGNEDTO'\n\t\t\t\t\t\t\t\t\t\tAND SFP0.IBLOCK_ID = BE.IBLOCK_ID\n\t\t\t\t\t\t\t\t\t\tAND SFPV0.IBLOCK_ELEMENT_ID = BE.ID\n\t\t\t\t\t\t\t\t))\n\t\t\t\t\t\t\t\tOR (P.OBJECT_ID = 'trackers' AND " . $userId . " IN (\n\t\t\t\t\t\t\t\t\tSELECT SFPV0.VALUE_NUM\n\t\t\t\t\t\t\t\t\tFROM b_iblock_element_property SFPV0\n\t\t\t\t\t\t\t\t\t\tINNER JOIN b_iblock_property SFP0 ON (SFPV0.IBLOCK_PROPERTY_ID = SFP0.ID)\n\t\t\t\t\t\t\t\t\tWHERE " . CIBlock::_Upper("SFP0.CODE") . "='TASKTRACKERS'\n\t\t\t\t\t\t\t\t\t\tAND SFP0.IBLOCK_ID = BE.IBLOCK_ID\n\t\t\t\t\t\t\t\t\t\tAND SFPV0.IBLOCK_ELEMENT_ID = BE.ID\n\t\t\t\t\t\t\t\t))\n\t\t\t\t\t\t\t\tOR (P.OBJECT_ID = '" . ("USER_" . $userId) . "')\n\t\t\t\t\t\t\t)\n\t\t\t\t\t)";
                 }
                 break;
             case "CHECK_BP_VIRTUAL_PERMISSIONS":
                 if (IsModuleInstalled('bizproc') && (!is_object($USER) || !$USER->IsAdmin())) {
                     if (!in_array($val, array("read", "create", "admin"))) {
                         $val = "admin";
                     }
                     $userId = is_object($USER) ? intval($USER->GetID()) : 0;
                     if (is_object($USER)) {
                         $arUserGroups = $USER->GetUserGroupArray();
                     }
                     if (!is_array($arUserGroups) || count($arUserGroups) <= 0) {
                         $arUserGroups = array(2);
                     }
                     $arSqlSearch[] = "EXISTS (\n\t\t\t\t\t\tSELECT S.DOCUMENT_ID_INT\n\t\t\t\t\t\tFROM b_bp_workflow_state S\n\t\t\t\t\t\t\tINNER JOIN b_bp_workflow_permissions P ON S.ID = P.WORKFLOW_ID\n\t\t\t\t\t\tWHERE S.DOCUMENT_ID_INT = BE.ID\n\t\t\t\t\t\t\tAND S.MODULE_ID = 'bizproc'\n\t\t\t\t\t\t\tAND S.ENTITY = 'CBPVirtualDocument'\n\t\t\t\t\t\t\tAND\n\t\t\t\t\t\t\t\t(P.PERMISSION = '" . $val . "'\n\t\t\t\t\t\t\t\tAND (\n\t\t\t\t\t\t\t\t\tP.OBJECT_ID IN ('" . implode("', '", $arUserGroups) . "')\n\t\t\t\t\t\t\t\t\tOR (P.OBJECT_ID = 'Author' AND BE.CREATED_BY = " . $userId . ")\n\t\t\t\t\t\t\t\t\tOR (P.OBJECT_ID = " . $DB->Concat("'USER_'", "'" . $userId . "'") . ")\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t)\n\t\t\t\t\t)";
                 }
                 break;
             case "TASKSTATUS":
                 if (IsModuleInstalled('bizproc')) {
                     $arSqlSearch[] = ($cOperationType == "N" ? "NOT " : "") . "EXISTS (\n\t\t\t\t\t\tSELECT S.DOCUMENT_ID_INT\n\t\t\t\t\t\tFROM\n\t\t\t\t\t\tb_bp_workflow_state S\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tS.DOCUMENT_ID_INT = BE.ID\n\t\t\t\t\t\t\tAND S.MODULE_ID = 'intranet'\n\t\t\t\t\t\t\tAND S.ENTITY = 'CIntranetTasksDocument'\n\t\t\t\t\t\t\tAND S.STATE = '" . $DB->ForSql($val) . "'\n\t\t\t\t\t)";
                 }
                 break;
             case "LID":
             case "SITE_ID":
             case "IBLOCK_LID":
             case "IBLOCK_SITE_ID":
                 $flt = CIBlock::FilterCreateEx("SITE_ID", $val, "string_equal", $bFullJoinTmp, $cOperationType);
                 if (strlen($flt)) {
                     $arSqlSearch[] = ($cOperationType == "N" ? "NOT " : "") . "EXISTS (\n\t\t\t\t\t\tSELECT IBLOCK_ID FROM b_iblock_site WHERE IBLOCK_ID = B.ID\n\t\t\t\t\t\tAND " . $flt . "\n\t\t\t\t\t)";
                 }
                 break;
             case "DATE_ACTIVE_FROM":
                 $arSqlSearch[] = CIBlock::FilterCreateEx("BE.ACTIVE_FROM", $val, "date", $bFullJoinTmp, $cOperationType);
                 break;
             case "DATE_ACTIVE_TO":
                 $arSqlSearch[] = CIBlock::FilterCreateEx("BE.ACTIVE_TO", $val, "date", $bFullJoinTmp, $cOperationType);
                 break;
             case "IBLOCK_ACTIVE":
                 $flt = CIBlock::FilterCreateEx("B.ACTIVE", $val, "string_equal", $bFullJoinTmp, $cOperationType);
                 $arSqlSearch[] = $flt;
                 break;
             case "IBLOCK_CODE":
                 $flt = CIBlock::FilterCreateEx("B.CODE", $val, "string", $bFullJoinTmp, $cOperationType);
                 $arSqlSearch[] = $flt;
                 break;
             case "ID_ABOVE":
                 $arSqlSearch[] = CIBlock::FilterCreateEx("BE.ID", $val, "number_above", $bFullJoinTmp, $cOperationType);
                 break;
             case "ID_LESS":
                 $arSqlSearch[] = CIBlock::FilterCreateEx("BE.ID", $val, "number_less", $bFullJoinTmp, $cOperationType);
                 break;
             case "ACTIVE_FROM":
                 if (strlen($val) > 0) {
                     $arSqlSearch[] = "(BE.ACTIVE_FROM " . ($cOperationType == "N" ? "<" : ">=") . $DB->CharToDateFunction($DB->ForSql($val), "FULL") . ($cOperationType == "N" ? "" : " OR BE.ACTIVE_FROM IS NULL") . ")";
                 }
                 break;
             case "ACTIVE_TO":
                 if (strlen($val) > 0) {
                     $arSqlSearch[] = "(BE.ACTIVE_TO " . ($cOperationType == "N" ? ">" : "<=") . $DB->CharToDateFunction($DB->ForSql($val), "FULL") . ($cOperationType == "N" ? "" : " OR BE.ACTIVE_TO IS NULL") . ")";
                 }
                 break;
             case "ACTIVE_DATE":
                 if (strlen($val) > 0) {
                     $arSqlSearch[] = ($cOperationType == "N" ? " NOT" : "") . "((BE.ACTIVE_TO >= " . $DB->GetNowFunction() . " OR BE.ACTIVE_TO IS NULL) AND (BE.ACTIVE_FROM <= " . $DB->GetNowFunction() . " OR BE.ACTIVE_FROM IS NULL))";
                 }
                 break;
             case "DATE_MODIFY_FROM":
                 if (strlen($val) > 0) {
                     $arSqlSearch[] = "(BE.TIMESTAMP_X " . ($cOperationType == "N" ? "<" : ">=") . $DB->CharToDateFunction($DB->ForSql($val), "FULL") . ($cOperationType == "N" ? "" : " OR BE.TIMESTAMP_X IS NULL") . ")";
                 }
                 break;
             case "DATE_MODIFY_TO":
                 if (strlen($val) > 0) {
                     $arSqlSearch[] = "(BE.TIMESTAMP_X " . ($cOperationType == "N" ? ">" : "<=") . $DB->CharToDateFunction($DB->ForSql($val), "FULL") . ($cOperationType == "N" ? "" : " OR BE.TIMESTAMP_X IS NULL") . ")";
                 }
                 break;
             case "WF_NEW":
                 if ($val == "Y" || $val == "N") {
                     $arSqlSearch[] = CIBlock::FilterCreateEx("BE.WF_NEW", "Y", "string_equal", $bFullJoinTmp, $val == "Y" ? false : true, false);
                 }
                 break;
             case "MODIFIED_USER_ID":
             case "MODIFIED_BY":
                 $arSqlSearch[] = CIBlock::FilterCreateEx("BE.MODIFIED_BY", $val, "number", $bFullJoinTmp, $cOperationType);
                 break;
             case "CREATED_USER_ID":
             case "CREATED_BY":
                 $arSqlSearch[] = CIBlock::FilterCreateEx("BE.CREATED_BY", $val, "number", $bFullJoinTmp, $cOperationType);
                 break;
             case "RATING_USER_ID":
                 $arSqlSearch[] = CIBlock::FilterCreateEx("RVV.USER_ID", $val, "number", $bFullJoinTmp, $cOperationType);
                 $arJoinProps["RVV"] = array("bFullJoin" => $bFullJoinTmp);
                 break;
             case "WF_STATUS":
                 $arSqlSearch[] = CIBlock::FilterCreateEx("BE.WF_STATUS_ID", $val, "number", $bFullJoinTmp, $cOperationType);
                 break;
             case "WF_LOCK_STATUS":
                 if (strlen($val) > 0) {
                     $USER_ID = is_object($USER) ? intval($USER->GetID()) : 0;
                     $arSqlSearch[] = " if(BE.WF_DATE_LOCK is null, 'green', if(DATE_ADD(BE.WF_DATE_LOCK, interval " . COption::GetOptionInt("workflow", "MAX_LOCK_TIME", 60) . " MINUTE)<now(), 'green', if(BE.WF_LOCKED_BY=" . $USER_ID . ", 'yellow', 'red'))) = '" . $DB->ForSql($val) . "'";
                 }
                 break;
             case "WF_LAST_STATUS_ID":
                 $arSqlSearch[] = "exists (\n\t\t\t\t\tselect\n\t\t\t\t\t\thistory.ID\n\t\t\t\t\tfrom\n\t\t\t\t\t\tb_iblock_element history\n\t\t\t\t\twhere\n\t\t\t\t\t\thistory.WF_PARENT_ELEMENT_ID = BE.ID\n\t\t\t\t\t\tand history.WF_STATUS_ID = " . intval($val) . "\n\t\t\t\t\t\tand history.ID = (\n\t\t\t\t\t\t\tselect max(history0.ID) LAST_ID\n\t\t\t\t\t\t\tfrom b_iblock_element history0\n\t\t\t\t\t\t\twhere history0.WF_PARENT_ELEMENT_ID = BE.ID\n\t\t\t\t\t\t)\n\t\t\t\t)\n\t\t\t\t";
                 break;
             case "SECTION_ACTIVE":
                 if ($arFilter["INCLUDE_SUBSECTIONS"] === "Y") {
                     $arSectionFilter["BS"][] = "BSubS.ACTIVE = 'Y'";
                 } else {
                     $arSectionFilter["BS"][] = "BS.ACTIVE = 'Y'";
                 }
                 break;
             case "SECTION_GLOBAL_ACTIVE":
                 if ($arFilter["INCLUDE_SUBSECTIONS"] === "Y") {
                     $arSectionFilter["BS"][] = "BSubS.GLOBAL_ACTIVE = 'Y'";
                 } else {
                     $arSectionFilter["BS"][] = "BS.GLOBAL_ACTIVE = 'Y'";
                 }
                 break;
             case "SUBSECTION":
                 if (!is_array($val)) {
                     $val = array($val);
                 }
                 //Find out margins of sections
                 $arUnknownMargins = array();
                 foreach ($val as $i => $section) {
                     if (!is_array($section)) {
                         $arUnknownMargins[intval($section)] = intval($section);
                     }
                 }
                 if (count($arUnknownMargins) > 0) {
                     $rs = $DB->Query("SELECT ID, LEFT_MARGIN, RIGHT_MARGIN FROM b_iblock_section WHERE ID in (" . implode(", ", $arUnknownMargins) . ")");
                     while ($ar = $rs->Fetch()) {
                         $arUnknownMargins[intval($ar["ID"])] = array(intval($ar["LEFT_MARGIN"]), intval($ar["RIGHT_MARGIN"]));
                     }
                     foreach ($val as $i => $section) {
                         if (!is_array($section)) {
                             $val[$i] = $arUnknownMargins[intval($section)];
                         }
                     }
                 }
                 //Now sort them out
                 $arMargins = array();
                 foreach ($val as $i => $section) {
                     if (is_array($section) && count($section) == 2) {
                         $left = intval($section[0]);
                         $right = intval($section[1]);
                         if ($left > 0 && $right > 0) {
                             $arMargins[$left] = $right;
                         }
                     }
                 }
                 ksort($arMargins);
                 //Remove subsubsections of the sections
                 $prev_right = 0;
                 foreach ($arMargins as $left => $right) {
                     if ($right <= $prev_right) {
                         unset($arMargins[$left]);
                     } else {
                         $prev_right = $right;
                     }
                 }
                 if (isset($arFilter["INCLUDE_SUBSECTIONS"]) && $arFilter["INCLUDE_SUBSECTIONS"] === "Y") {
                     $bsAlias = "BSubS";
                 } else {
                     $bsAlias = "BS";
                 }
                 $res = "";
                 foreach ($arMargins as $left => $right) {
                     if ($res != "") {
                         $res .= $cOperationType == "N" ? " AND " : " OR ";
                     }
                     $res .= ($cOperationType == "N" ? " NOT " : " ") . "({$bsAlias}.LEFT_MARGIN >= " . $left . " AND {$bsAlias}.RIGHT_MARGIN <= " . $right . ")\n";
                 }
                 if ($res != "") {
                     $arSectionFilter["BS"][] = "(" . $res . ")";
                 }
                 break;
             case "SECTION_ID":
                 if (!is_array($val)) {
                     $val = array($val);
                 }
                 $arSections = array();
                 foreach ($val as $section_id) {
                     $section_id = intval($section_id);
                     $arSections[$section_id] = $section_id;
                 }
                 if ($cOperationType == "N") {
                     if (array_key_exists(0, $arSections)) {
                         $arSectionFilter["BE"][] = "BE.IN_SECTIONS<>'N'";
                         $arSectionFilter["LOGIC"] = "AND";
                         unset($arSections[0]);
                         if (count($arSections) > 0) {
                             $arSectionFilter["BS"][] = "BS.ID NOT IN (" . implode(", ", $arSections) . ")";
                         }
                     } elseif (count($arSections) > 0) {
                         $arSectionFilter["BE"][] = "BE.IN_SECTIONS='N'";
                         $arSectionFilter["LOGIC"] = "OR";
                         $arSectionFilter["BS"][] = "BS.ID NOT IN (" . implode(", ", $arSections) . ")";
                     }
                 } else {
                     if (array_key_exists(0, $arSections)) {
                         $arSectionFilter["BE"][] = "BE.IN_SECTIONS='N'";
                         $arSectionFilter["LOGIC"] = "OR";
                         unset($arSections[0]);
                     }
                     if (count($arSections) > 0) {
                         $arSectionFilter["BS"][] = "BS.ID IN (" . implode(", ", $arSections) . ")";
                     }
                 }
                 break;
             case "SECTION_CODE":
                 if (!is_array($val)) {
                     $val = array($val);
                 }
                 $arSections = array();
                 foreach ($val as $section_code) {
                     $section_code = $DB->ForSql($section_code);
                     $arSections[$section_code] = $section_code;
                 }
                 if ($cOperationType == "N") {
                     if (array_key_exists("", $arSections)) {
                         $arSectionFilter["BE"][] = "BE.IN_SECTIONS<>'N'";
                         $arSectionFilter["LOGIC"] = "AND";
                         unset($arSections[""]);
                         if (count($arSections) > 0) {
                             $arSectionFilter["BS"][] = "BS.CODE NOT IN ('" . implode("', '", $arSections) . "')";
                         }
                     } elseif (count($arSections) > 0) {
                         $arSectionFilter["BE"][] = "BE.IN_SECTIONS='N'";
                         $arSectionFilter["LOGIC"] = "OR";
                         $arSectionFilter["BS"][] = "BS.CODE NOT IN ('" . implode("', '", $arSections) . "')";
                     }
                 } else {
                     if (array_key_exists("", $arSections)) {
                         $arSectionFilter["BE"][] = "BE.IN_SECTIONS='N'";
                         $arSectionFilter["LOGIC"] = "OR";
                         unset($arSections[""]);
                     }
                     if (count($arSections) > "") {
                         $arSectionFilter["BS"][] = "BS.CODE IN ('" . implode("', '", $arSections) . "')";
                     }
                 }
                 break;
             case "PROPERTY":
                 foreach ($val as $propID => $propVAL) {
                     $res = CIBlock::MkOperationFilter($propID);
                     $res["LOGIC"] = $Logic;
                     $res["LEFT_JOIN"] = $bPropertyLeftJoin;
                     if (preg_match("/^([^.]+)\\.([^.]+)\$/", $res["FIELD"], $arMatch)) {
                         $db_prop = CIBlockProperty::GetPropertyArray($arMatch[1], CIBlock::_MergeIBArrays($arFilter["IBLOCK_ID"], $arFilter["IBLOCK_CODE"], $arFilter["~IBLOCK_ID"], $arFilter["~IBLOCK_CODE"]));
                         if (is_array($db_prop) && $db_prop["PROPERTY_TYPE"] == "E") {
                             $res["FIELD"] = $arMatch;
                             CIBlockElement::MkPropertyFilter($res, $cOperationType, $propVAL, $db_prop, $arJoinProps, $arSqlSearch);
                         }
                     } else {
                         if ($db_prop = CIBlockProperty::GetPropertyArray($res["FIELD"], CIBlock::_MergeIBArrays($arFilter["IBLOCK_ID"], $arFilter["IBLOCK_CODE"], $arFilter["~IBLOCK_ID"], $arFilter["~IBLOCK_CODE"]))) {
                             CIBlockElement::MkPropertyFilter($res, $cOperationType, $propVAL, $db_prop, $arJoinProps, $arSqlSearch);
                         }
                     }
                 }
                 break;
             default:
                 if (is_numeric($orig_key)) {
                     //Here is hint for better property resolution:
                     if (!is_array($val) || !array_key_exists("~IBLOCK_ID", $val)) {
                         if (array_key_exists("IBLOCK_ID", $arFilter)) {
                             $val["~IBLOCK_ID"] = $arFilter["IBLOCK_ID"];
                         } elseif (array_key_exists("~IBLOCK_ID", $arFilter)) {
                             $val["~IBLOCK_ID"] = $arFilter["~IBLOCK_ID"];
                         }
                     }
                     if (!is_array($val) || !array_key_exists("~IBLOCK_CODE", $val)) {
                         if (array_key_exists("IBLOCK_CODE", $arFilter)) {
                             $val["~IBLOCK_CODE"] = $arFilter["IBLOCK_CODE"];
                         } elseif (array_key_exists("~IBLOCK_CODE", $arFilter)) {
                             $val["~IBLOCK_CODE"] = $arFilter["~IBLOCK_CODE"];
                         }
                     }
                     //Subfilter process
                     $arSubSqlSearch = CIBlockElement::MkFilter($val, $arJoinProps, $arAddWhereFields, $level + 1, $bPropertyLeftJoin);
                     if (strlen(trim($arSubSqlSearch[0], "\n\t"))) {
                         $arSqlSearch[] = str_replace("\n\t\t\t", "\n\t\t\t\t", $arSubSqlSearch[0]);
                     }
                 } elseif (strtoupper(substr($key, 0, 8)) == "CATALOG_" && CModule::IncludeModule("catalog")) {
                     $res_catalog = CCatalogProduct::GetQueryBuildArrays(array(), array($orig_key => $val), array());
                     if (strlen($res_catalog["WHERE"])) {
                         $arSqlSearch[] = substr($res_catalog["WHERE"], 5);
                         // " AND ".$res
                         $arAddWhereFields[$orig_key] = $val;
                     }
                 }
                 break;
         }
     }
     //SECTION sub filter
     $sWhere = "";
     foreach ($arSectionFilter["BS"] as $strFilter) {
         if (strlen($strFilter)) {
             if (strlen($sWhere)) {
                 $sWhere .= " " . $Logic . " ";
             }
             $sWhere .= "(" . $strFilter . ")";
         }
     }
     $bINCLUDE_SUBSECTIONS = isset($arFilter["INCLUDE_SUBSECTIONS"]) && $arFilter["INCLUDE_SUBSECTIONS"] === "Y";
     if (strlen($sWhere)) {
         $sectionScope = "";
         if (isset($arFilter["SECTION_SCOPE"])) {
             if ($arFilter["SECTION_SCOPE"] == "IBLOCK") {
                 $sectionScope = "AND BSE.ADDITIONAL_PROPERTY_ID IS NULL";
             } elseif ($arFilter["SECTION_SCOPE"] == "PROPERTY") {
                 $sectionScope = "AND BSE.ADDITIONAL_PROPERTY_ID IS NOT NULL";
             } elseif (preg_match("/^PROPERTY_(\\d+)\$/", $arFilter["SECTION_SCOPE"], $match)) {
                 $sectionScope = "AND BSE.ADDITIONAL_PROPERTY_ID = " . $match[1];
             }
         }
         //Try to convert correlated subquery to join subquery
         if ($level == 0 && $Logic == "AND" && !count($arSectionFilter["BE"])) {
             $arJoinProps["BES"] .= " INNER JOIN (\n\t\t\t\t\tSELECT DISTINCT BSE.IBLOCK_ELEMENT_ID\n\t\t\t\t\tFROM b_iblock_section_element BSE\n\t\t\t\t\t" . ($bINCLUDE_SUBSECTIONS ? "\n\t\t\t\t\tINNER JOIN b_iblock_section BSubS ON BSE.IBLOCK_SECTION_ID = BSubS.ID\n\t\t\t\t\tINNER JOIN b_iblock_section BS ON (BSubS.IBLOCK_ID=BS.IBLOCK_ID\n\t\t\t\t\t\tAND BSubS.LEFT_MARGIN>=BS.LEFT_MARGIN\n\t\t\t\t\t\tAND BSubS.RIGHT_MARGIN<=BS.RIGHT_MARGIN)\n\t\t\t\t\t" : "\n\t\t\t\t\tINNER JOIN b_iblock_section BS ON BSE.IBLOCK_SECTION_ID = BS.ID\n\t\t\t\t\t") . "\n\t\t\t\t\tWHERE (" . $sWhere . "){$sectionScope}\n\t\t\t\t\t) BES ON BES.IBLOCK_ELEMENT_ID = BE.ID\n";
         } else {
             $arSqlSearch[] = "(" . (count($arSectionFilter["BE"]) ? implode(" " . $arSectionFilter["LOGIC"] . " ", $arSectionFilter["BE"]) . " " . $arSectionFilter["LOGIC"] : "") . " EXISTS (\n\t\t\t\t\tSELECT BSE.IBLOCK_ELEMENT_ID\n\t\t\t\t\tFROM b_iblock_section_element BSE\n\t\t\t\t\t" . ($bINCLUDE_SUBSECTIONS ? "\n\t\t\t\t\tINNER JOIN b_iblock_section BSubS ON BSE.IBLOCK_SECTION_ID = BSubS.ID\n\t\t\t\t\tINNER JOIN b_iblock_section BS ON (BSubS.IBLOCK_ID=BS.IBLOCK_ID\n\t\t\t\t\t\tAND BSubS.LEFT_MARGIN>=BS.LEFT_MARGIN\n\t\t\t\t\t\tAND BSubS.RIGHT_MARGIN<=BS.RIGHT_MARGIN)\n\t\t\t\t\t" : "\n\t\t\t\t\tINNER JOIN b_iblock_section BS ON BSE.IBLOCK_SECTION_ID = BS.ID\n\t\t\t\t\t") . "\n\t\t\t\t\tWHERE BSE.IBLOCK_ELEMENT_ID = BE.ID\n\t\t\t\t\tAND (" . $sWhere . "){$sectionScope}\n\t\t\t\t\t))";
         }
     } elseif (count($arSectionFilter["BE"])) {
         foreach ($arSectionFilter["BE"] as $strFilter) {
             $arSqlSearch[] = $strFilter;
         }
     }
     $sWhere = "";
     foreach ($arSqlSearch as $strFilter) {
         if (strlen(trim($strFilter, "\n\t"))) {
             if (strlen($sWhere)) {
                 $sWhere .= "\n\t\t\t\t" . $Logic . " ";
             } else {
                 $sWhere .= "\n\t\t\t\t";
             }
             $sWhere .= "(" . $strFilter . ")";
         }
     }
     $arSqlSearch = array("\n\t\t\t" . $sWhere . "\n\t\t\t");
     $SHOW_BP_NEW = "";
     $SHOW_NEW = isset($arFilter["SHOW_NEW"]) && $arFilter["SHOW_NEW"] == "Y" ? "Y" : "N";
     if ($SHOW_NEW == "Y" && isset($arFilter["SHOW_BP_NEW"]) && is_array($arFilter["SHOW_BP_NEW"]) && IsModuleInstalled('bizproc') && (!is_object($USER) || !$USER->IsAdmin())) {
         $MODULE_ID = $DB->ForSQL($arFilter["SHOW_BP_NEW"]["MODULE_ID"]);
         $ENTITY = $DB->ForSQL($arFilter["SHOW_BP_NEW"]["ENTITY"]);
         $PERMISSION = $DB->ForSQL($arFilter["SHOW_BP_NEW"]["PERMISSION"]);
         $arUserGroups = array();
         if (is_array($arFilter["SHOW_BP_NEW"]["GROUPS"])) {
             $USER_ID = intval($arFilter["SHOW_BP_NEW"]["USER_ID"]);
             foreach ($arFilter["SHOW_BP_NEW"]["GROUPS"] as $GROUP_ID) {
                 $GROUP_ID = intval($GROUP_ID);
                 if ($GROUP_ID) {
                     $arUserGroups[$GROUP_ID] = $GROUP_ID;
                 }
             }
         } else {
             $USER_ID = false;
             $arUserGroups = false;
         }
         if ($PERMISSION == "read" || $PERMISSION == "write") {
             if (!is_array($arUserGroups)) {
                 $USER_ID = is_object($USER) ? intval($USER->GetID()) : 0;
                 if (is_object($USER)) {
                     $arUserGroups = $USER->GetUserGroupArray();
                 }
             }
             if (!is_array($arUserGroups) || count($arUserGroups) <= 0) {
                 $arUserGroups = array(2);
             }
             $SHOW_BP_NEW = " AND EXISTS (\n\t\t\t\t\tSELECT S.DOCUMENT_ID_INT\n\t\t\t\t\tFROM\n\t\t\t\t\tb_bp_workflow_state S\n\t\t\t\t\tINNER JOIN b_bp_workflow_permissions P ON S.ID = P.WORKFLOW_ID\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tS.DOCUMENT_ID_INT = BE.ID\n\t\t\t\t\t\tAND S.MODULE_ID = '{$MODULE_ID}'\n\t\t\t\t\t\tAND S.ENTITY = '{$ENTITY}'\n\t\t\t\t\t\tAND P.PERMISSION = '{$PERMISSION}'\n\t\t\t\t\t\tAND (\n\t\t\t\t\t\t\tP.OBJECT_ID IN ('" . implode("', '", $arUserGroups) . "')\n\t\t\t\t\t\t\tOR (P.OBJECT_ID = 'Author' AND BE.CREATED_BY = {$USER_ID})\n\t\t\t\t\t\t\tOR (P.OBJECT_ID = " . $DB->Concat("'USER_'", "'{$USER_ID}'") . ")\n\t\t\t\t\t\t)\n\t\t\t\t)";
         }
     }
     if (!isset($arFilter["SHOW_HISTORY"]) || $arFilter["SHOW_HISTORY"] != "Y") {
         $arSqlSearch[] = "((BE.WF_STATUS_ID=1 AND BE.WF_PARENT_ELEMENT_ID IS NULL)" . ($SHOW_NEW == "Y" ? " OR (BE.WF_NEW='Y'" . $SHOW_BP_NEW . ")" : "") . ")";
     }
     if ($permSQL) {
         $arSqlSearch[] = $permSQL;
     }
     if (isset($this) && is_object($this) && isset($this->subQueryProp)) {
         //Subquery list value should not be null
         $this->MkPropertyFilter(CIBlock::MkOperationFilter("!" . substr($this->strField, 9)), "NE", false, $this->subQueryProp, $arJoinProps, $arSqlSearch);
     }
     return $arSqlSearch;
 }