Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
messageeventmanager.php
1<?php
2namespace Bitrix\Mail\Helper;
3
10
12{
13 const EVENT_DELETE_MESSAGES = 'onMailMessageDeleted';
14
21 public static function onMailMessageDeleted(Event $event)
22 {
23 $manager = new static();
24 $manager->processOnMailMessageDeletedEvent($event);
25 return $manager;
26 }
27
28 private function processOnMailMessageDeletedEvent(Event $event)
29 {
30 $params = $event->getParameters();
31 $filter = empty($params['DELETED_BY_FILTER']) ? [] : $params['DELETED_BY_FILTER'];
32 $fieldsData = empty($params['MAIL_FIELDS_DATA']) ? [] : $params['MAIL_FIELDS_DATA'];
33 $this->handleRemovedEvent($fieldsData, $filter);
34 }
35
42 public static function onMailMessageModified(Event $event)
43 {
44 $manager = new static();
45 $manager->processOnMailMessageModified($event);
46 return $manager;
47 }
48
49 private function processOnMailMessageModified(Event $event)
50 {
51 $params = $event->getParameters();
52 $updatedFieldValues = empty($params['UPDATED_FIELDS_VALUES']) ? [] : $params['UPDATED_FIELDS_VALUES'];
53 $fieldsData = empty($params['MAIL_FIELDS_DATA']) ? [] : $params['MAIL_FIELDS_DATA'];
54 $filter = empty($params['UPDATED_BY_FILTER']) ? [] : $params['UPDATED_BY_FILTER'];
55
56 if (!empty($updatedFieldValues) && isset($updatedFieldValues['IS_SEEN']))
57 {
58 $fieldsData = $this->getMailsFieldsData($fieldsData, ['HEADER_MD5', 'MAILBOX_USER_ID', 'IS_SEEN'], $filter);
59 $this->sendMessageModifiedEvent($fieldsData);
60 }
61
62 if (!empty($updatedFieldValues) && isset($updatedFieldValues['DIR_MD5']) && isset($filter['=MAILBOX_ID']))
63 {
64 $dirHash = empty($updatedFieldValues['DIR_MD5']) ? null : $updatedFieldValues['DIR_MD5'];
65 $dir = MailboxDirectory::fetchOneByHash($filter['=MAILBOX_ID'], $dirHash);
66
67 if (!empty($dirHash) && $dir != null)
68 {
69 if ($dir->isTrash() || $dir->isSpam() || $dir->isDisabled())
70 {
71 $this->handleRemovedEvent($fieldsData, $filter);
72 }
73 }
74 }
75 }
76
77 protected function sendMessageModifiedEvent($fieldsData)
78 {
79 foreach ($fieldsData as $fields)
80 {
81 $event = new Event(
82 'mail', 'OnMessageModified',
83 [
84 'user' => $fields['MAILBOX_USER_ID'],
85 'hash' => $fields['HEADER_MD5'],
86 'seen' => $fields['IS_SEEN'] === 'Y',
87 ]
88 );
89 $event->send();
90 }
91 }
92
93 private function handleRemovedEvent($fieldsData, $filter)
94 {
95 $this->sendMessageDeletedEvent($fieldsData);
96 }
97
98 protected function sendMessageDeletedEvent($fieldsData)
99 {
100 foreach ($fieldsData as $fields)
101 {
102 $event = new Event(
103 'mail', 'OnMessageObsolete',
104 [
105 'user' => $fields['MAILBOX_USER_ID'],
106 'hash' => $fields['HEADER_MD5'],
107 ]
108 );
109 $event->send();
110 }
111 }
112
113 private function getMailsFieldsData($eventData, $requiredKeys, $filter)
114 {
115 $fieldsData = $eventData;
116 $missingKeys = $requiredKeys;
117 $messagesCount = count($eventData);
118 if ($messagesCount)
119 {
120 foreach ($requiredKeys as $requiredKey)
121 {
122 if (count(array_column($eventData, $requiredKey)) === $messagesCount)
123 {
124 $missingKeys = array_diff($missingKeys, [$requiredKey]);
125 }
126 }
127 }
128
129 if (!empty($missingKeys) && !empty($filter))
130 {
131 $fieldsData = $this->getMailMessagesList($filter, $missingKeys);
132 }
133 $results = [];
134 foreach ($fieldsData as $index => $mailFieldsData)
135 {
136 $results[$mailFieldsData['HEADER_MD5']] = $mailFieldsData;
137 }
138 return $results;
139 }
140
141 protected function getMailMessagesList($filter, $selectingFields)
142 {
143 $dateLastMonth = new DateTime();
144 $dateLastMonth->add('-1 MONTH');
145 foreach ($selectingFields as $index => $selectingField)
146 {
147 if (strncmp('MAILBOX_', $selectingField, 8) === 0)
148 {
149 $selectingFields[$selectingField] = 'MAILBOX.'.mb_substr($selectingField, 8);
150 unset($selectingFields[$index]);
151 }
152 }
153
154 return MailMessageUidTable::getList([
155 'select' => $selectingFields,
156 'filter' => array_merge($filter, [
157 '>=INTERNALDATE' => $dateLastMonth,
158 '==DELETE_TIME' => 0,
159 ]),
160 ]
161 )->fetchAll();
162 }
163
170 public static function onMailEventMailRead(array $data)
171 {
172 $messageId = $data['msgid'];
173 if($messageId)
174 {
175 $message = MailMessageTable::getList([
176 'select' => [
177 'OPTIONS', 'ID', 'READ_CONFIRMED',
178 ],
179 'filter' => [
180 '=MSG_ID' => $messageId,
181 'READ_CONFIRMED' => null,
182 ]
183 ])->fetch();
184 if($message)
185 {
186 $readTime = new DateTime();
187 $result = MailMessageTable::update($message['ID'], [
188 'READ_CONFIRMED' => $readTime,
189 ]);
190 if($result->isSuccess())
191 {
192 if(Loader::includeModule("pull"))
193 {
194 \CPullWatch::addToStack(static::getPullTagName($message['ID']), [
195 'module_id' => 'mail',
196 'command' => 'onMessageRead',
197 'params' => [
198 'messageId' => $message['ID'],
199 'readTime' => $readTime->getTimestamp(),
200 ],
201 ]);
202 }
203 }
204 }
205 }
206
207 return $data;
208 }
209
210 public static function getPullTagName($messageId)
211 {
212 return 'MAILMESSAGEREADED'.$messageId;
213 }
214
215 public static function getRequiredFieldNamesForEvent($eventName)
216 {
217 if ($eventName === static::EVENT_DELETE_MESSAGES)
218 {
219 return array('HEADER_MD5', 'MESSAGE_ID', 'MAILBOX_USER_ID');
220 }
221
222 return [];
223 }
224}
getMailMessagesList($filter, $selectingFields)
static fetchOneByHash($mailboxId, $hash)
send($sender=null)
Definition event.php:139