Skip to content

Instantly share code, notes, and snippets.

@BekNaji
Last active August 31, 2022 10:33
Show Gist options
  • Save BekNaji/312a5e9f1a09caacca7c7320322057c0 to your computer and use it in GitHub Desktop.
Save BekNaji/312a5e9f1a09caacca7c7320322057c0 to your computer and use it in GitHub Desktop.
This function is changing response of live search contacts inside deal form (Bitrix24 CRM)
<?php
// Developer: Bekzod
$crm_api_entity_search = isset($_REQUEST['action']) && $_REQUEST['action'] == 'crm.api.entity.search' ? true : false;
if($crm_api_entity_search)
{
$request = $_REQUEST;
if(isset($request['options']['types'][0]) && $request['options']['types'][0] == 'CONTACT')
{
changeContactRespone($request);
}
}
// This function is changing response of live search contacts inside deal form
function changeContactRespone($request)
{
CModule::IncludeModule('crm');
$searchedQuery = $request['searchQuery'];
$customField = 'UF_CRM_1660812537038';
// get contact ids by phone number
$arFilter['n1']['%VALUE'] = $searchedQuery;
$contactRes = CCrmFieldMulti::GetList(array(), [
'ENTITY_ID' =>'CONTACT',
'TYPE_ID'=>'PHONE', // email
'FILTER' => $arFilter
]);
$contactIds = [];
while ($c = $contactRes->Fetch()){
$contactIds[] = $c['ELEMENT_ID'];
}
// get contacts by IDs and Full Name
$arFilter = [
'CHECK_PERMISSIONS' => 'N',
'LOGIC' => 'OR',
'ID' => $contactIds,
'%FULL_NAME' => trim($searchedQuery),
];
$res = CCrmContact::getList([$customField => 'ASC'],$arFilter,['ID','NAME','FULL_NAME',$customField]);
$contactIds = [];
while($arRes = $res->Fetch()){
$contactIds[] = $arRes['ID'];
$arContacts[$arRes['ID']] = $arRes;
}
// I tried to put this to above query "LOGIC OR" but it did not work!
// get contacts by custom input field
$arFilter = [
'CHECK_PERMISSIONS' => 'N',
$customField => trim($searchedQuery),
];
$res = CCrmContact::getList([$customField => 'ASC'],$arFilter,['ID','NAME','FULL_NAME',$customField]);
while($arRes = $res->Fetch()){
$contactIds[] = $arRes['ID'];
$arContacts[$arRes['ID']] = $arRes;
}
// get phone numbers all of contacts
$contactRes = CCrmFieldMulti::GetList(array(), [
'ENTITY_ID' =>'CONTACT',
'ELEMENT_ID'=> $contactIds,
]);
while ($c = $contactRes->Fetch()){
$phones[$c['ELEMENT_ID']] = $c['VALUE'];
}
$items = [];
// prepare items
foreach($arContacts as $item)
{
$items[] =
[
'id' => $item['ID'],
'type' => 'CONTACT',
'title' => $item['NAME'].' - '.($item[$customField] ? $item[$customField] : 'unknown'),
'module' => 'crm',
'subTitle' => '',
'actions' => [],
'links' => ['show' => "/crm/contact/details/{$item['ID']}/"],
'attributes' =>
[
'phone' =>
[
[
'type' => 'WORK',
'value' => isset($phones[$item['ID']]) ? $phones[$item['ID']] : NULL,
]
],
],
];
}
$data = [
'status' => 'success',
'data' =>
[
'items' => $items,
'limits' => []
],
'errors' => []
];
if(empty($items)){
return false;
}
header('Content-Type: application/json; charset=utf-8');
die(json_encode($data));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment