Поддержка серверов

CForumMessage::GetListEx: метод Битрикс

Постоянная ссылка: http://bxapi.ru/src/?id=162085

//    CForumMessage::GetListEx()
//    /bitrix/modules/forum/classes/mysql/message.php:489

    public static function GetListEx($arOrder = Array("ID"=>"ASC"), $arFilter = Array(), $bCount = false, $iNum = 0, $arAddParams = array())
    {
        global $DB;
        $arSqlSearch = array();
        $arSqlOrder = array();
        $arSqlFrom = array();
        $arSqlSelect = array();
        $arSqlGroup = array();
        $strSqlSearch = "";
        $strSqlOrder = "";
        $strSqlFrom = "";
        $strSqlSelect = "";
        $strSqlGroup = "";
        $UseGroup = false;
        $arFilter = (is_array($arFilter) ? $arFilter : array());
        $arAddParams = (is_array($arAddParams) ? $arAddParams : array());
        $arIndexFields = array();

        foreach ($arFilter as $key => $val)
        {
            $key_res = CForumNew::GetFilterOperation($key);
            $key = strtoupper($key_res["FIELD"]);
            $strNegative = $key_res["NEGATIVE"];
            $strOperation = $key_res["OPERATION"];

            switch ($key)
            {
                case "PARAM1":
                case "AUTHOR_NAME":
                case "POST_MESSAGE_CHECK":
                case "APPROVED":
                case "NEW_TOPIC":
                case "POST_MESSAGE":
                    $arIndexFields[] = $key;
                    if ($strOperation == "LIKE")
                        $val = "%".$val."%";
                    if (strlen($val)<=0)
                        $arSqlSearch[] = ($strNegative=="Y"?"NOT":"")."(FM.".$key." IS NULL OR LENGTH(FM.".$key.")<=0)";
                    else
                        $arSqlSearch[] = ($strNegative=="Y"?" FM.".$key." IS NULL OR NOT ":"")."(FM.".$key." ".$strOperation." '".$DB->ForSql($val)."' )";
                    break;
                case "APPROVED_AND_MINE":
                    $arIndexFields[] = "APPROVED";
                    if ($val >= 0)
                    {
                        $arSqlSearch[] = "(FM.APPROVED='Y' OR FM.AUTHOR_ID=".intval($val).")";
                        $arIndexFields[] = "AUTHOR_ID";
                    }
                    else
                    {
                        $arSqlSearch[] = "(FM.APPROVED='Y')";
                    }
                    break;
                case "PARAM2":
                case "ID":
                case "AUTHOR_ID":
                case "FORUM_ID":
                case "TOPIC_ID":
                case "ATTACH_IMG":
                    $arIndexFields[] = $key;
                    if ( ($strOperation == "IN") && (!is_array($val)) && (strpos($val,",")>0) )
                        $val = explode(",", $val);
                    if (($strOperation!="IN") && (intVal($val) > 0))
                        $arSqlSearch[] = ($strNegative=="Y"?" FM.".$key." IS NULL OR NOT ":"")."(FM.".$key." ".$strOperation." ".intVal($val)." )";
                    elseif (($strOperation =="IN") && ((is_array($val) && (array_sum($val) > 0)) || (strlen($val) > 0) ))
                    {
                        if (is_array($val))
                        {
                            $val_int = array();
                            foreach ($val as $v)
                                $val_int[] = intVal($v);
                            $val = implode(", ", $val_int);
                        }
                        else
                        {
                            $val = intval($val);
                        }
                        $arSqlSearch[] = ($strNegative=="Y"?" NOT ":"")."(FM.".$key." IN (".$DB->ForSql($val).") )";
                    }
                    else
                        $arSqlSearch[] = ($strNegative=="Y"?"NOT":"")."(FM.".$key." IS NULL OR FM.".$key."<=0)";
                    break;
                case "POINTS_TO_AUTHOR_ID":
                    if (intVal($val) > 0)
                    {
                        $arSqlSelect["FR.POINTS"] = "FR.POINTS";
                        $arSqlSelect["FR.DATE_UPDATE"] = "FR.DATE_UPDATE";
                        $arSqlFrom["FR"] = "LEFT JOIN b_forum_user_points FR ON ((FM.AUTHOR_ID = FR.TO_USER_ID) AND (FR.FROM_USER_ID=".intVal($val)."))";
                    }
                    break;
                case "POST_DATE":
                    $arIndexFields[] = $key;
                    if (strlen($val)<=0)
                        $arSqlSearch[] = ($strNegative=="Y"?"NOT":"")."(FM.".$key." IS NULL OR LENGTH(FM.".$key.")<=0)";
                    else
                        $arSqlSearch[] = ($strNegative=="Y"?" FM.".$key." IS NULL OR NOT ":"")."(FM.".$key." ".$strOperation." ".$DB->CharToDateFunction($DB->ForSql($val), "FULL").")";
                    break;
                case "USER_ID":
//                    $arSqlSelect["LAST_VISIT"] = $DB->DateToCharFunction("FUT.LAST_VISIT", "FULL");
                    if(intVal($val) > 0)
                    {
                        $arSqlFrom["FUT"] = "
                            LEFT JOIN b_forum_user_topic FUT ON (FM.TOPIC_ID = FUT.TOPIC_ID AND FUT.USER_ID=".intVal($val).")";
                    }
                    break;
                case "NEW_MESSAGE":
                    if (strLen($val) > 0 && intVal($arFilter["USER_ID"]) > 0)
                    {
                        $arSqlFrom["FUT"] = "
                        LEFT JOIN b_forum_user_topic FUT ON (FM.TOPIC_ID = FUT.TOPIC_ID AND FUT.USER_ID=".intVal($arFilter["USER_ID"]).")";
                        $arSqlSearch[] = "
                            (FUT.LAST_VISIT IS NOT NULL AND FM.POST_DATE > FUT.LAST_VISIT)
                            OR
                            (FUT.LAST_VISIT IS NULL AND FM.POST_DATE ".$strOperation." ".$DB->CharToDateFunction($DB->ForSql($val), "FULL").")
                            ";
                    }
                break;
                case "USER_GROUP":
                    if (!empty($val))
                    {
                        if (!is_array($val))
                            $val = explode(",", $val);
                        if (!in_array(2, $val))
                            $val[] = 2;
                        $val = implode(",", array_map("intval", $val));
                        $arIndexFields[] = "FP.GROUP_ID";
                        $arSqlFrom["FP"] = "LEFT JOIN b_forum_perms FP ON (FP.FORUM_ID=FM.FORUM_ID)";
                        $arSqlSearch[] = "FP.GROUP_ID IN (".$DB->ForSql($val).") AND ((FP.PERMISSION IN ('E','I','M') AND FM.APPROVED='Y') OR (FP.PERMISSION IN ('Q','U','Y')))";
                        $UseGroup = true;
                    }
                break;
                case "TOPIC_SOCNET_GROUP_ID":
                        $arIndexFields[] = "FT.SOCNET_GROUP_ID";
                        $arSqlFrom["FT"] = "
                            LEFT JOIN b_forum_topic FT ON (FT.ID = FM.TOPIC_ID)";
                        $arSqlSearch[] = "FT.SOCNET_GROUP_ID = ".IntVal($val);
                        $arSqlSelect[] = "FT.SOCNET_GROUP_ID as TOPIC_SOCNET_GROUP_ID";
                    break;
                case "TOPIC_OWNER_ID":
                        $arIndexFields[] = "FT.OWNER_ID";
                        $arSqlFrom["FT"] = "
                            LEFT JOIN b_forum_topic FT ON (FT.ID = FM.TOPIC_ID)";
                        $arSqlSearch[] = "FT.OWNER_ID = ".IntVal($val);
                        $arSqlSelect[] = "FT.OWNER_ID as TOPIC_OWNER_ID";
                    break;
                case "TOPIC":
                    $arSqlFrom["FT"] = "
                        LEFT JOIN b_forum_topic FT ON (FT.ID = FM.TOPIC_ID)";
                    $arSqlSelect[] = "FT.TITLE";
                    $arSqlSelect[] = CForumNew::Concat("-", array("FT.ID", "FT.TITLE_SEO"))." as TITLE_SEO";
                    $arSqlSelect[] = "FT.DESCRIPTION AS TOPIC_DESCRIPTION";
                    $arSqlSelect[] = $DB->DateToCharFunction("FT.START_DATE", "FULL")." as START_DATE";
                    $arSqlSelect[] = "FT.USER_START_NAME";
                    $arSqlSelect[] = "FT.USER_START_ID";
                    $arSqlSelect[] = "FT.XML_ID AS TOPIC_XML_ID";
                    $arSqlSelect[] = "FT.SOCNET_GROUP_ID AS TOPIC_SOCNET_GROUP_ID";
                    $arSqlSelect[] = "FT.OWNER_ID AS TOPIC_OWNER_ID";
                break;
                case "TOPIC_MESSAGE_ID":
                    if (is_array($val))
                    {
                        $val_int = array();
                        foreach ($val as $v)
                            $val_int[] = intval($v);
                        $val = implode(", ", $val_int);
                    }
                    else
                    {
                        $val = intval($val);
                    }
                    if (!empty($val))
                    {
                        $arSqlFrom["FT"] = "
                            LEFT JOIN b_forum_topic FT ON (FT.ID = FM.TOPIC_ID)";
                        $arIndexFields[] = "ID";
                        $arSqlSearch[] = "FT.ID IN (SELECT DISTINCT TOPIC_ID FROM b_forum_message WHERE ID IN (".$val."))";
                    }
                break;
                case "TOPIC_TITLE":
                case "TITLE":
                    $arSqlFrom["FT"] = "
                        LEFT JOIN b_forum_topic FT ON (FT.ID = FM.TOPIC_ID)";
                    $key = "TITLE";
                    $arIndexFields[] = $key;
                    if ($strOperation == "LIKE")
                        $val = "%".$val."%";
                    if (strlen($val)<=0)
                        $arSqlSearch[] = ($strNegative=="Y"?"NOT":"")."(FT.".$key." IS NULL OR LENGTH(FT.".$key.")<=0)";
                    else
                        $arSqlSearch[] = ($strNegative=="Y"?" FT.".$key." IS NULL OR NOT ":"")."(FT.".$key." ".$strOperation." '".$DB->ForSql($val)."' )";
                break;
            }
        }
        foreach ($arOrder as $by=>$order)
        {
            $by = strtoupper($by); $order = strtoupper($order);
            if ($order!="ASC") $order = "DESC";
            if (in_array($by, array("FORUM_ID", "TOPIC_ID", "USE_SMILES", "NEW_TOPIC", "APPROVED",
                "POST_DATE", "POST_MESSAGE", "ATTACH_IMG", "PARAM1", "PARAM2",
                "AUTHOR_ID", "AUTHOR_NAME", "AUTHOR_EMAIL", "AUTHOR_IP", "AUTHOR_REAL_IP", "GUEST_ID",
                "EDITOR_ID", "EDITOR_NAME", "EDITOR_EMAIL", "EDIT_REASON", "EDIT_DATE", "HTML"))):
                $arSqlOrder[] = "FM.".$by." ".$order;
            elseif ($by == "SORT" || $by == "NAME"):
                $arSqlFrom["F"] = "
                LEFT JOIN b_forum F ON (F.ID = FM.FORUM_ID)";
                $arSqlSelect["F.".$by] = "F.".$by;
                $arSqlOrder[] = "F_M.".$by." ".$order;
            else:
                $arSqlOrder[] = "FM.ID ".$order;
                $by = "ID";
            endif;
        }
        $arSqlOrder = array_unique($arSqlOrder);
        DelDuplicateSort($arSqlOrder);
        if(count($arSqlOrder) > 0)
            $strSqlOrder = " ORDER BY ".implode(", ", $arSqlOrder);

        $IX_FORUM_MESSAGE_TOPIC = false;
        if (count($arSqlSearch) > 0)
        {
            $strSqlSearch = " AND (".implode(") AND (", $arSqlSearch).") ";
            $arIndexFields = array_unique($arIndexFields);
            $IX_FORUM_MESSAGE_TOPIC = ($arIndexFields == array("TOPIC_ID", "APPROVED") || $arIndexFields == array("TOPIC_ID"));
        }
        if (count($arSqlSelect) > 0)
            $strSqlSelect = ",\n\t".implode(", ", $arSqlSelect);
        if (count($arSqlFrom) > 0)
            $strSqlFrom .= "\n\t".implode("\n\t", $arSqlFrom);
        if ($UseGroup)
        {
            foreach ($arSqlSelect as $key => $val)
            {
                if (substr($key, 0, 1) != "!")
                    $arSqlGroup[$key] = $val;
            }
            if (!empty($arSqlGroup)):
                $strSqlGroup = ", ".implode(", ", $arSqlGroup);
            endif;
        }

        if ($bCount || (is_set($arAddParams, "bDescPageNumbering") && intVal($arAddParams["nTopCount"]) <= 0))
        {
            $strSql =
                "SELECT
                    COUNT(FM.ID) as CNT,
                    MAX(FM.ID) AS LAST_MESSAGE_ID
                FROM b_forum_message FM
                    ".$strSqlFrom."
                WHERE 1 = 1
                    ".$strSqlSearch;
            if ($bCount === 3)
            {
                $strSql .= "GROUP BY FM.TOPIC_ID";
                return $DB->Query($strSql, false, "File: ".__FILE__."
Line: ".__LINE__);
            }
            // This code exists because of http://bugs.mysql.com/bug.php?id=64002
            $iCnt = 0; $iLAST_MESSAGE_ID = 0;
            if ((array_intersect_key($arFilter, array("TOPIC_ID" => null, "APPROVED" => null)) == $arFilter ||
                array_intersect_key($arFilter, array("TOPIC_ID" => null)) == $arFilter) && $arFilter["TOPIC_ID"] > 0) // high-usage
            {
                $strSql .= "GROUP BY FM.TOPIC_ID"; // explane the same as without "GROUP BY"
                $db_res = $DB->Query($strSql, false, "File: ".__FILE__."
Line: ".__LINE__);
                if ($ar_res = $db_res->Fetch())
                {
                    $iCnt = intVal($ar_res["CNT"]);
                    $iLAST_MESSAGE_ID = intVal($ar_res["LAST_MESSAGE_ID"]);
                }
            }
            else
            {
                $strSql = "SELECT COUNT(FM.ID) as CNT \nFROM b_forum_message FM ".$strSqlFrom."\nWHERE 1 = 1 ".$strSqlSearch;
                $db_res = $DB->Query($strSql, false, "File: ".__FILE__."
Line: ".__LINE__);
                if ($ar_res = $db_res->Fetch())
                {
                    $iCnt = intVal($ar_res["CNT"]);
                }
                if ($bCount === 4)
                {
                    $strSql = "SELECT MAX(FM.ID) AS LAST_MESSAGE_ID \nFROM b_forum_message FM ".$strSqlFrom."\nWHERE 1 = 1 ".$strSqlSearch;
                    $db_res = $DB->Query($strSql, false, "File: ".__FILE__."
Line: ".__LINE__);
                    if ($ar_res = $db_res->Fetch())
                    {
                        $iLAST_MESSAGE_ID = intVal($ar_res["LAST_MESSAGE_ID"]);
                    }
                }
            }
            // / This code exists because of http://bugs.mysql.com/bug.php?id=64002
            if ($bCount === 4)
                return array("CNT" => $iCnt, "LAST_MESSAGE_ID" => $iLAST_MESSAGE_ID);

            if ($bCount)
                return $iCnt;
        }

        if ($UseGroup)
        {
            $strSql =
                "SELECT F_M.*, FM.FORUM_ID, FM.TOPIC_ID, FM.USE_SMILES, FM.NEW_TOPIC, \n".
                "    FM.APPROVED, FM.SOURCE_ID, \n".
                "    ".$DB->DateToCharFunction("FM.POST_DATE", "FULL")." as POST_DATE, \n".
                "    FM.POST_MESSAGE, FM.POST_MESSAGE_HTML, FM.POST_MESSAGE_FILTER, \n".
                "    FM.ATTACH_IMG, FM.XML_ID, FM.PARAM1, FM.PARAM2, \n".
                "    FM.AUTHOR_ID, FM.AUTHOR_NAME, FM.AUTHOR_EMAIL, \n".
                "    FM.AUTHOR_IP, FM.AUTHOR_REAL_IP, FM.GUEST_ID, \n".
                "    FM.EDITOR_ID, FM.EDITOR_NAME, FM.EDITOR_EMAIL, FM.EDIT_REASON, \n".
                "    ".$DB->DateToCharFunction("FM.EDIT_DATE", "FULL")." as EDIT_DATE, \n".
                "    FM.HTML, FM.MAIL_HEADER, \n".
                "    FU.SHOW_NAME, FU.DESCRIPTION, FU.NUM_POSTS, FU.POINTS as NUM_POINTS, FU.SIGNATURE, FU.AVATAR, \n".
                "    ".$DB->DateToCharFunction("FU.DATE_REG", "SHORT")." as DATE_REG, \n".
                "    U.LOGIN, U.NAME, U.SECOND_NAME, U.LAST_NAME, U.PERSONAL_PHOTO, FU.RANK_ID, U.PERSONAL_WWW, U.PERSONAL_GENDER, \n".
                "    U.EMAIL, U.PERSONAL_ICQ, U.PERSONAL_CITY, U.PERSONAL_COUNTRY".
                (!empty($arAddParams["sNameTemplate"]) ?
                    ",\n\t".CForumUser::GetFormattedNameFieldsForSelect(array_merge(
                        $arAddParams, array(
                        "sUserTablePrefix" => "U.",
                        "sForumUserTablePrefix" => "FU.",
                        "sFieldName" => "AUTHOR_NAME_FRMT")), false) : "")." \n".
                "FROM ( \n".
                "        SELECT FM.ID".$strSqlSelect." \n".
                "        FROM b_forum_message FM \n".
                "            LEFT JOIN b_forum_user FU ON FM.AUTHOR_ID = FU.USER_ID \n".
                "            LEFT JOIN b_user U ON FM.AUTHOR_ID = U.ID \n".
                "            ".$strSqlFrom." \n".
                "        WHERE (1=1 ".$strSqlSearch.") \n".
                "        GROUP BY FM.ID".$strSqlGroup." \n".
                "    ) F_M \n".
                "    INNER JOIN b_forum_message FM ON (F_M.ID = FM.ID) \n".
                "    LEFT JOIN b_forum_user FU ON (FM.AUTHOR_ID = FU.USER_ID) \n".
                "    LEFT JOIN b_user U ON (FM.AUTHOR_ID = U.ID) \n".
                $strSqlOrder;
        }
        else
        {
            $strSql =
                "SELECT FM.ID, FM.FORUM_ID, FM.TOPIC_ID, FM.USE_SMILES, FM.NEW_TOPIC, \n".
                "    FM.APPROVED, FM.SOURCE_ID, \n".
                "    ".$DB->DateToCharFunction("FM.POST_DATE", "FULL")." as POST_DATE, \n".
                "    FM.POST_MESSAGE, FM.POST_MESSAGE_HTML, FM.POST_MESSAGE_FILTER, \n".
                "    FM.ATTACH_IMG, FM.XML_ID, FM.PARAM1, FM.PARAM2, \n".
                "    FM.AUTHOR_ID, FM.AUTHOR_NAME, FM.AUTHOR_EMAIL, \n".
                "    FM.AUTHOR_IP, FM.AUTHOR_REAL_IP, FM.GUEST_ID, \n".
                "    FM.EDITOR_ID, FM.EDITOR_NAME, FM.EDITOR_EMAIL, FM.EDIT_REASON, \n".
                "    ".$DB->DateToCharFunction("FM.EDIT_DATE", "FULL")." as EDIT_DATE, \n".
                "    FM.HTML, FM.MAIL_HEADER, \n".
                "    FU.SHOW_NAME, FU.DESCRIPTION, FU.NUM_POSTS, FU.POINTS as NUM_POINTS, FU.SIGNATURE, FU.AVATAR, \n".
                "    ".$DB->DateToCharFunction("FU.DATE_REG", "SHORT")." as DATE_REG, \n".
                "    U.LOGIN, U.NAME, U.SECOND_NAME, U.LAST_NAME, U.PERSONAL_PHOTO, FU.RANK_ID, U.PERSONAL_WWW, U.PERSONAL_GENDER, \n".
                "    U.EMAIL, U.PERSONAL_ICQ, U.PERSONAL_CITY, U.PERSONAL_COUNTRY".
                (!empty($arAddParams["sNameTemplate"]) ?
                    ",\n\t".CForumUser::GetFormattedNameFieldsForSelect(array_merge(
                        $arAddParams, array(
                        "sUserTablePrefix" => "U.",
                        "sForumUserTablePrefix" => "FU.",
                        "sFieldName" => "AUTHOR_NAME_FRMT")), false)."\n" : "").$strSqlSelect."\n".
                "FROM b_forum_message FM ".($IX_FORUM_MESSAGE_TOPIC ? "USE INDEX (IX_FORUM_MESSAGE_TOPIC)" : "")."\n".
                "    LEFT JOIN b_forum_user FU ON (FM.AUTHOR_ID = FU.USER_ID) \n".
                "    LEFT JOIN b_user U ON (FM.AUTHOR_ID = U.ID) \n".
                "    ".$strSqlFrom." \n".
                "WHERE 1 = 1 ".$strSqlSearch." \n".
                $strSqlOrder;
        }

        $iNum = intVal($iNum);
        if ($iNum > 0 || intVal($arAddParams["nTopCount"]) > 0):
            $iNum = ($iNum > 0) ? $iNum : intVal($arAddParams["nTopCount"]);
            $strSql .= "\nLIMIT 0,".$iNum;
        endif;

        if (!$iNum && is_array($arAddParams) && is_set($arAddParams, "bDescPageNumbering") && (intVal($arAddParams["nTopCount"])<=0))
        {
            $db_res = new CDBResult();
            $db_res->NavQuery($strSql, $iCnt, $arAddParams);
        }
        else
        {
            $db_res = $DB->Query($strSql, false, "File: ".__FILE__."
Line: ".__LINE__);
        }
        return new _CMessageDBResult($db_res, $arAddParams);
    }

Блог разработчика

9 Августа 2016 ResizeImageGet и простой водяной знак налету

Сегодня поговорим о том как проще всего воткнуть водяной знак на картинки в Битрикс. Например, на все изображения каталога.

21 Июля 2016 Добавление вкладки в CRM коробки Битрикс24

В этом посте я покажу, как легко и просто добавить вкладку в сущность CRM. И не просто добавить, а вывести там свой контент.

21 Июля 2016 Как просто обращаться из коробки Битрикс24 на сайт на базе Битрикс

К примеру вам хочется внутренним скриптом дернуть с сайта-магазина информацию, которая там, в свою очередь, закрыта некоторыми правами. Например, админскими. Можно конечно мутить свой REST, или иной формат общения, но проще всего воспользоваться штатным инструментом:

Текст сообщения*
Загрузить изображение