47 'ITEMS_LAST' => array(),
48 'ITEMS_HIDDEN' => array(),
49 'ADDITIONAL_INFO' => array(
50 'GROUPS_LIST' => array(
51 'mailcontacts' => array(
53 'TYPE_LIST' => [ $entityType ],
54 'DESC_LESS_MODE' =>
'N',
58 'SORT_SELECTED' => 100
62 $currentUser = \Bitrix\Main\Engine\CurrentUser::get();
63 if (is_null($currentUser->getId()))
68 $entityOptions = (!empty($params[
'options']) ? $params[
'options'] : array());
70 $lastItems = (!empty($params[
'lastItems']) ? $params[
'lastItems'] : array());
72 $selectedItems = (!empty($params[
'selectedItems']) ? $params[
'selectedItems'] : array());
73 $selectedItemsData = (!empty($entityOptions[
'selectedItemsData']) ? $entityOptions[
'selectedItemsData'] : array());
75 $lastMailContactsIdList = [];
76 if(!empty($lastItems[$entityType]))
78 $result[
"ITEMS_LAST"] = array_values($lastItems[$entityType]);
79 foreach ($lastItems[$entityType] as $value)
81 $lastMailContactsIdList[] = str_replace(self::PREFIX,
'', $value);
85 $selectedMailContactsIdList = [];
86 if(!empty($selectedItems[$entityType]))
88 foreach ($selectedItems[$entityType] as $value)
90 $selectedMailContactsIdList[] = str_replace(self::PREFIX,
'', $value);
94 $mailContactsIdList = array_merge($selectedMailContactsIdList, $lastMailContactsIdList);
95 $mailContactsIdList = array_slice($mailContactsIdList, 0, count($selectedMailContactsIdList) > 20 ? count($selectedMailContactsIdList) : 20);
96 $mailContactsIdList = array_unique($mailContactsIdList);
98 $mailContactsEmailList = [];
99 foreach($mailContactsIdList as $key => $contactId)
102 $contactId !== (
int) $contactId.
' '
103 && check_email($contactId,
true)
106 unset($mailContactsIdList[$key]);
107 $mailContactsEmailList[] = $contactId;
111 $mailContactsList = [];
114 '=USER_ID' => $currentUser->getId()
118 if (!empty($mailContactsIdList))
120 $filter[
'ID'] = $mailContactsIdList;
131 $mailContacts = \Bitrix\Mail\Internals\MailContactTable::getList([
134 'select' => [
'ID',
'NAME',
'EMAIL',
'ICON'],
138 foreach ($mailContacts as $mailContact)
140 $mailContactsList[self::PREFIX.$mailContact[
'ID']] = self::prepareEntity($mailContact, $entityOptions);
143 if (!empty($mailContactsEmailList))
145 foreach ($mailContactsEmailList as $mailContactEmail)
147 $mailContactsList[self::PREFIX.$mailContactEmail] = self::prepareEntity([
148 'ID' => $mailContactEmail,
150 isset($selectedItemsData[self::PREFIX.$mailContactEmail])
151 && isset($selectedItemsData[self::PREFIX.$mailContactEmail][
'name'])
152 ? $selectedItemsData[self::PREFIX.$mailContactEmail][
'name']
155 'EMAIL' => $mailContactEmail,
161 if (empty($lastMailContactsIdList))
163 $result[
"ITEMS_LAST"] = array_keys($mailContactsList);
166 $result[
'ITEMS'] = $mailContactsList;
194 public function search($params = array())
198 'ADDITIONAL_INFO' => array()
201 $entityOptions = (!empty($params[
'options']) ? $params[
'options'] : array());
202 $requestFields = (!empty($params[
'requestFields']) ? $params[
'requestFields'] : array());
203 $search = $requestFields[
'searchString'];
207 $currentUser = \Bitrix\Main\Engine\CurrentUser::get();
208 if (!is_null($currentUser->getId()))
210 $searchWords = preg_split(
'/\s+/', trim($search), ($wordsLimit = 10) + 1);
211 $searchWords = array_splice($searchWords, 0, $wordsLimit);
213 $sqlHelper = \Bitrix\Main\Application::getConnection()->getSqlHelper();
214 foreach ($searchWords as $word)
216 $word = str_replace(
'%',
'%%', $word);
217 $word = $sqlHelper->forSql($word);
218 $sortExpr .= sprintf(
219 '+(CASE WHEN %s THEN 2 WHEN %s THEN 1 ELSE 0 END)',
220 "(%1\$s LIKE '%%" . $word .
"%%')",
221 "(%2\$s LIKE '%%" . $word .
"%%')"
224 $sortWeight = new \Bitrix\Main\Entity\ExpressionField(
'SORT_WEIGHT', $sortExpr, [
'NAME',
'EMAIL']);
228 '%NAME' => $searchWords,
229 '%EMAIL' => $searchWords,
232 $queryFilter[] = [
'=USER_ID' => $currentUser->getId()];
233 $mailContacts = \Bitrix\Mail\Internals\MailContactTable::getList([
235 'SORT_WEIGHT' =>
'DESC',
238 'filter' => $queryFilter,
239 'select' => [
'ID',
'NAME',
'EMAIL',
'ICON', $sortWeight],
243 foreach ($mailContacts as $mailContact)
245 $result[
"ITEMS"][self::PREFIX.$mailContact[
'ID']] = self::prepareEntity($mailContact, $entityOptions);