Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mailcontacts.php
1<?
3
6
8{
9 const PREFIX = 'MC';
10
11 private static function prepareEntity($data, $options = [])
12 {
13 global $APPLICATION;
14
15 static $contactAvatars = [];
16
17 $email = $data['EMAIL'];
18 if ($contactAvatars[$email] === null)
19 {
20 ob_start();
21 $APPLICATION->IncludeComponent('bitrix:mail.contact.avatar', '',
22 [
23 'mailContact' => $data,
24 ]);
25 $contactAvatars[$email] = ob_get_clean();
26 }
27 $result = [
28 'id' => self::PREFIX.$data['ID'],
29 'entityType' => 'mailContacts',
30 'entityId' => $data['ID'],
31 'name' => htmlspecialcharsbx(Emoji::decode($data['NAME'])),
32 'iconCustom' => $contactAvatars[$email],
33 'email' => htmlspecialcharsbx($data['EMAIL']),
34 'desc' => htmlspecialcharsbx($data['EMAIL']),
35 'isEmail' => 'Y'
36 ];
37
38 return $result;
39 }
40
41 public function getData($params = [])
42 {
44
45 $result = array(
46 'ITEMS' => array(),
47 'ITEMS_LAST' => array(),
48 'ITEMS_HIDDEN' => array(),
49 'ADDITIONAL_INFO' => array(
50 'GROUPS_LIST' => array(
51 'mailcontacts' => array(
52 'TITLE' => Loc::getMessage('MAIN_UI_SELECTOR_TITLE_MAILCONTACTS'),
53 'TYPE_LIST' => [ $entityType ],
54 'DESC_LESS_MODE' => 'N',
55 'SORT' => 100
56 )
57 ),
58 'SORT_SELECTED' => 100
59 )
60 );
61
62 $currentUser = \Bitrix\Main\Engine\CurrentUser::get();
63 if (is_null($currentUser->getId()))
64 {
65 return $result;
66 }
67
68 $entityOptions = (!empty($params['options']) ? $params['options'] : array());
69
70 $lastItems = (!empty($params['lastItems']) ? $params['lastItems'] : array());
71
72 $selectedItems = (!empty($params['selectedItems']) ? $params['selectedItems'] : array());
73 $selectedItemsData = (!empty($entityOptions['selectedItemsData']) ? $entityOptions['selectedItemsData'] : array());
74
75 $lastMailContactsIdList = [];
76 if(!empty($lastItems[$entityType]))
77 {
78 $result["ITEMS_LAST"] = array_values($lastItems[$entityType]);
79 foreach ($lastItems[$entityType] as $value)
80 {
81 $lastMailContactsIdList[] = str_replace(self::PREFIX, '', $value);
82 }
83 }
84
85 $selectedMailContactsIdList = [];
86 if(!empty($selectedItems[$entityType]))
87 {
88 foreach ($selectedItems[$entityType] as $value)
89 {
90 $selectedMailContactsIdList[] = str_replace(self::PREFIX, '', $value);
91 }
92 }
93
94 $mailContactsIdList = array_merge($selectedMailContactsIdList, $lastMailContactsIdList);
95 $mailContactsIdList = array_slice($mailContactsIdList, 0, count($selectedMailContactsIdList) > 20 ? count($selectedMailContactsIdList) : 20);
96 $mailContactsIdList = array_unique($mailContactsIdList);
97
98 $mailContactsEmailList = [];
99 foreach($mailContactsIdList as $key => $contactId)
100 {
101 if (
102 $contactId !== (int) $contactId.' '
103 && check_email($contactId, true)
104 )
105 {
106 unset($mailContactsIdList[$key]);
107 $mailContactsEmailList[] = $contactId;
108 }
109 }
110
111 $mailContactsList = [];
112
113 $filter = [
114 '=USER_ID' => $currentUser->getId()
115 ];
116 $order = [];
117
118 if (!empty($mailContactsIdList))
119 {
120 $filter['ID'] = $mailContactsIdList;
121 $limit = false;
122 }
123 else
124 {
125 $order = [
126 'ID' => 'DESC'
127 ];
128 $limit = 10;
129 }
130
131 $mailContacts = \Bitrix\Mail\Internals\MailContactTable::getList([
132 'order' => $order,
133 'filter' => $filter,
134 'select' => ['ID', 'NAME', 'EMAIL', 'ICON'],
135 'limit' => $limit,
136 ])->fetchAll();
137
138 foreach ($mailContacts as $mailContact)
139 {
140 $mailContactsList[self::PREFIX.$mailContact['ID']] = self::prepareEntity($mailContact, $entityOptions);
141 }
142
143 if (!empty($mailContactsEmailList))
144 {
145 foreach ($mailContactsEmailList as $mailContactEmail)
146 {
147 $mailContactsList[self::PREFIX.$mailContactEmail] = self::prepareEntity([
148 'ID' => $mailContactEmail,
149 'NAME' => (
150 isset($selectedItemsData[self::PREFIX.$mailContactEmail])
151 && isset($selectedItemsData[self::PREFIX.$mailContactEmail]['name'])
152 ? $selectedItemsData[self::PREFIX.$mailContactEmail]['name']
153 : $mailContactEmail
154 ),
155 'EMAIL' => $mailContactEmail,
156 'ICON' => \Bitrix\Mail\Helper\MailContact::getIconData($mailContactEmail, '')
157 ], $entityOptions);
158 }
159 }
160
161 if (empty($lastMailContactsIdList))
162 {
163 $result["ITEMS_LAST"] = array_keys($mailContactsList);
164 }
165
166 $result['ITEMS'] = $mailContactsList;
167
168 return $result;
169 }
170
171 public function getTabList($params = [])
172 {
173 $result = [];
174
175 $options = (!empty($params['options']) ? $params['options'] : []);
176
177 if (
178 isset($options['addTab'])
179 && $options['addTab'] == 'Y'
180 )
181 {
182 $result = array(
183 array(
184 'id' => 'mailcontacts',
185 'name' => Loc::getMessage('MAIN_UI_SELECTOR_TAB_MAILCONTACTS2'),
186 'sort' => 1000
187 )
188 );
189 }
190
191 return $result;
192 }
193
194 public function search($params = array())
195 {
196 $result = array(
197 'ITEMS' => array(),
198 'ADDITIONAL_INFO' => array()
199 );
200
201 $entityOptions = (!empty($params['options']) ? $params['options'] : array());
202 $requestFields = (!empty($params['requestFields']) ? $params['requestFields'] : array());
203 $search = $requestFields['searchString'];
204
205 if ($search <> '')
206 {
207 $currentUser = \Bitrix\Main\Engine\CurrentUser::get();
208 if (!is_null($currentUser->getId()))
209 {
210 $searchWords = preg_split('/\s+/', trim($search), ($wordsLimit = 10) + 1);
211 $searchWords = array_splice($searchWords, 0, $wordsLimit);
212 $sortExpr = '0';
213 $sqlHelper = \Bitrix\Main\Application::getConnection()->getSqlHelper();
214 foreach ($searchWords as $word)
215 {
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 . "%%')"
222 );
223 }
224 $sortWeight = new \Bitrix\Main\Entity\ExpressionField('SORT_WEIGHT', $sortExpr, ['NAME', 'EMAIL']);
225 $queryFilter = [
226 [
227 'LOGIC' => 'OR',
228 '%NAME' => $searchWords,
229 '%EMAIL' => $searchWords,
230 ],
231 ];
232 $queryFilter[] = ['=USER_ID' => $currentUser->getId()];
233 $mailContacts = \Bitrix\Mail\Internals\MailContactTable::getList([
234 'order' => [
235 'SORT_WEIGHT' => 'DESC',
236 'NAME' => 'ASC',
237 ],
238 'filter' => $queryFilter,
239 'select' => ['ID', 'NAME', 'EMAIL', 'ICON', $sortWeight],
240 'limit' => 10,
241 ])->fetchAll();
242
243 foreach ($mailContacts as $mailContact)
244 {
245 $result["ITEMS"][self::PREFIX.$mailContact['ID']] = self::prepareEntity($mailContact, $entityOptions);
246 }
247 }
248 }
249
250 return $result;
251 }
252}
static getIconData($email, $name, $lastName=null)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29