Генератор паролей

Функция поиска по сделкам, смарт-процессам, спискам

Функция поиска по полям сделки, смарт-процессов, списков
возвращает массив айдишников
SearchInEntyties($arParams);
где
$arParams = [
    "TYPE" => "deal", // "smart", "list"
    "ID" => 2, //для  deal id воронки (необ.), для смарт-процесса - ид типа (обязат.), для списка - ид списка (обязат.)
    "FIELDS" => ["NAME","UF_PROP"], //массив полей, по которым ищем
    "SEARCH_TEXT" => "stroka", //строка по которой ищем
    "SHOW_EXCEPTION" => "Y", // вывод ошибок для отладки, необ  
];
SEARCH_TEXT
формат даты
для сделок 16.08.2023 00:00:00, где дата БЕЗ времени время 00:00:00 указывать
для списка 2024-05-27 21:49:00


примеры $arParams
сделка
$arParams = [
  "TYPE" => "deal", 
    "ID" => 0, // ид воронки (необ.)
    "FIELDS" => ["TITLE","UF_CRM_DEAL_3804018097336"], //массив полей
    "SEARCH_TEXT" => "00Н1-000264", //строка по которой ищем
    "SHOW_EXCEPTION" => "Y", //необ, вывод ошибок
];
смарт-процессы
$arParams = [
    "TYPE" => "smart", 
    "ID" => 146, // ид типа (обязат.)
    "FIELDS" => ["TITLE","UF_CRM_3_1709319908868"], //массив полей
    "SEARCH_TEXT" => "КС-2 ИП Иванов И.И.", //строка по которой ищем
];
списки
$arParams = [
    "TYPE" => "list", 
    "ID" => 5, // ид списка (обязат.)
    "FIELDS" => ["PROPERTY_82"], //массив полей
    "SEARCH_TEXT" => "2024-05-27 21:49:00", //строка по которой ищем
];

сама функция, размещаем в init.php
function SearchInEntyties($arParams){ 
    $result = []; 
    if(isset($arParams["TYPE"]) && !empty($arParams["FIELDS"]) && is_array($arParams["FIELDS"]) && isset($arParams["SEARCH_TEXT"])){      
        try {  
            //смарт-процессы и сделки
            if(
                (
                    (($arParams["TYPE"]=="smart")  && !empty($arParams["ID"]) && (intval($arParams["ID"])==$arParams["ID"])) || ($arParams["TYPE"]=="deal")
                ) && CModule::IncludeModule("crm")
            ){
            

                if($arParams["TYPE"]=="smart"){
                    $factory = Bitrix\Crm\Service\Container::getInstance()->getFactory($arParams["ID"]);
                }    
                else{
                    $factory = Bitrix\Crm\Service\Container::getInstance()->getFactory(\CCrmOwnerType::Deal);
                }    

                if(!empty($factory)){
                    if(count($arParams["FIELDS"])==1){
                        if(($arParams["TYPE"]=="deal") && !empty($arParams["ID"]) && (intval($arParams["ID"])==$arParams["ID"])) {
                            $arFilterBase = [
                                "CATEGORY_ID" => $arParams["ID"]
                            ];
                        }
                        else{
                            $arFilterBase = [];
                        }    

                        foreach ($arParams["FIELDS"] as $field) {
                            $arFilterBase[$field] = $arParams["SEARCH_TEXT"];
                        } 
                        
                    }
                    else{
                        $arFilter = [ 
                            "LOGIC"=>"OR"
                        ];
                        foreach ($arParams["FIELDS"] as $field) {
                            $arFilter[] = [
                                $field => $arParams["SEARCH_TEXT"]
                            ];
                        } 
                        if(($arParams["TYPE"]=="deal") && !empty($arParams["ID"]) && (intval($arParams["ID"])==$arParams["ID"])){
                            $arFilterBase = [
                                "CATEGORY_ID" => $arParams["ID"]
                            ];
                            $arFilterBase[] = $arFilter;
                        }
                        else{
                            $arFilterBase = $arFilter;
                        }                   
                    }     
                    $items = $factory->getItems([
                       "filter" => $arFilterBase,
                       "select" => ["ID"], 
                    ]);

                    foreach($items as $item){
                        $result[] = $item->getId();
                    }    
                }
            }
            elseif(($arParams["TYPE"]=="list") && !empty($arParams["ID"]) && (intval($arParams["ID"])==$arParams["ID"]) && CModule::IncludeModule("iblock")){ //списки
                $arFilterBase = [
                  "IBLOCK_ID"=>$arParams["ID"],                
                ];
                if(count($arParams["FIELDS"])==1){
                    foreach ($arParams["FIELDS"] as $field) {
                        $arFilterBase[$field] = $arParams["SEARCH_TEXT"];
                    }                
                }
                else{
                    $arFilter = [ 
                        "LOGIC"=>"OR"
                    ];

                    foreach ($arParams["FIELDS"] as $field) {
                        $arFilter[] = [
                            $field => $arParams["SEARCH_TEXT"]
                        ];
                    }
                    $arFilterBase[] = $arFilter;
                }

                $arSelect = ["ID","IBLOCK_ID"];
                $res = CIBlockElement::GetList(["id"=>"asc"], $arFilterBase, false, ["nPageSize"=>3000,"iNumPage"=>1], $arSelect);
                while($arItem = $res->Fetch()){
                    $result[] = $arItem["ID"];
                }

            }  
        } 
        catch (Exception $e) {
            if(!empty($arParams["SHOW_EXCEPTION"]) && ($arParams["SHOW_EXCEPTION"]=="Y")){
                echo $e->getMessage();
            }   
        }      
    }

    return $result;
}