Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
avatarmanager.php
1<?php
3
6
8{
9 private $currentUserId;
10
11 public function __construct($currentUserId = null)
12 {
13 $this->currentUserId = $currentUserId;
14 }
15
16 public static function getAvatarKeyByString($string): ?string
17 {
18 $parts = explode(",", $string);
19 $firstContact = trim($parts[0]);
20
21 $address = new Main\Mail\Address($firstContact);
22 if ($address->validate())
23 {
24 return $address->getEmail();
25 }
26
27 return null;
28 }
29
30 public function getAvatarParamsFromEmails($filedFromList)
31 {
32 $fileds = [];
33
34 foreach ($filedFromList as $filed)
35 {
36 $fileds = array_merge($fileds, $this->getAvatarParamsFromMessagesHeaders([['FIELD_FROM' => $filed]]));
37 }
38
39 return $fileds;
40 }
41
42 public function getAvatarParamsFromMessagesHeaders($messages)
43 {
44 $mailsNames = $this->getEmailsNames($messages);
45
46 $mailContacts = $this->fetchMailContacts(array_map(
47 function ($item)
48 {
49 return $item['email'];
50 },
51 $mailsNames
52 ));
53 $mailContacts = array_combine(array_column($mailContacts, 'EMAIL'), array_values($mailContacts));
54 $mailContacts = $this->fillFileIdColumn($mailContacts);
55
56 foreach ($mailsNames as $email => $data)
57 {
58 if (!empty($mailContacts[$email]))
59 {
60 if ((!$data['name'] || $data['name'] === $data['email']) &&
61 ($mailContacts[$email]['NAME'] && $mailContacts[$email]['NAME'] !== $mailContacts[$email]['EMAIL']))
62 {
63 $mailsNames[$email]['name'] = $mailContacts[$email]['NAME'];
64 }
65 $mailsNames[$email]['mailContact'] = $mailContacts[$email];
66 }
67 else
68 {
69 $mailsNames[$email]['mailContact'] = [
70 'EMAIL' => $data['email'],
71 'NAME' => $data['name'],
72 ];
73 }
74 }
75 return $mailsNames;
76 }
77
78 private function getEmailsNames($messages, $fieldList = false)
79 {
80 $emailNames = [];
81
82 if($fieldList)
83 {
84 $messages = $fieldList;
85 }
86
87 foreach ($messages as $index => $message)
88 {
89 $emailNames = array_merge($emailNames, $this->extractMailsNamesFrom($message['FIELD_FROM']));
90 $emailNames = array_merge($emailNames, $this->extractMailsNamesFrom($message['FIELD_TO']));
91 $emailNames = array_merge($emailNames, $this->extractMailsNamesFrom($message['FIELD_CC']));
92 $emailNames = array_merge($emailNames, $this->extractMailsNamesFrom($message['FIELD_BCC']));
93 }
94 $emailNames = $this->getBestNameChoices($emailNames);
95
96 return $emailNames;
97 }
98
99 private function getBestNameChoices($emailNames)
100 {
101 $results = [];
102 $bestNames = [];
103 foreach ($emailNames as $index => $data)
104 {
105 if (!isset($bestNames[$data['email']]))
106 {
107 $bestNames[$data['email']] = $data['name'];
108 $results[$data['email']] = [
109 'email' => $data['email'],
110 'name' => $data['name'],
111 ];
112 continue;
113 }
114 $newName = $data['name'];
115 $oldName = $bestNames[$data['email']];
116 if (!$oldName || $oldName == $data['email'])
117 {
118 $bestNames[$data['email']] = $newName;
119 $results[$data['email']] = [
120 'email' => $data['email'],
121 'name' => $newName,
122 ];
123 }
124 }
125
126 return $results;
127 }
128
129 private function extractMailsNamesFrom($parsedListOfEmails)
130 {
131 $emailNames = [];
132
133 if ($parsedListOfEmails)
134 {
135 foreach (\Bitrix\Mail\Helper\Message::parseAddressList($parsedListOfEmails) as $mailCopy)
136 {
137 $avatarKey = static::getAvatarKeyByString($mailCopy);
138 if ($avatarKey)
139 {
140 $address = new Main\Mail\Address($avatarKey);
141 if ($address->validate())
142 {
143 $emailNames[] = [
144 'email' => $address->getEmail(),
145 'name' => $address->getName() ?: $address->getEmail(),
146 ];
147 }
148 }
149 }
150 }
151 return $emailNames;
152 }
153
154 private function fillFileIdColumn($mailContacts)
155 {
156 foreach ($mailContacts as $mail => $mailContact)
157 {
158 if (!empty($mailContacts[$mail]['AVATAR_ID']))
159 {
160 $mailContacts[$mail]['FILE_ID'] = $mailContacts[$mail]['AVATAR_ID'];
161 }
162 }
163 return $mailContacts;
164 }
165
166 protected function fetchMailContacts($emails)
167 {
168 if (empty($emails))
169 {
170 return [];
171 }
172
173 return MailContactTable::getList(array(
174 'runtime' => array(
175 new Main\ORM\Fields\Relations\Reference(
176 'USER',
177 Main\UserTable::class,
178 array(
179 '=this.EMAIL' => 'ref.EMAIL',
180 '=ref.ACTIVE' => new Main\DB\SqlExpression('?', 'Y'),
181 )
182 )
183 ),
184 'select' => array(
185 'NAME', 'EMAIL', 'ICON', 'FILE_ID',
186 'AVATAR_ID' => 'USER.PERSONAL_PHOTO',
187 ),
188 'filter' => array(
189 '=USER_ID' => $this->currentUserId,
190 '@EMAIL' => $emails,
191 ),
192 ))->fetchAll();
193 }
194}