Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
repository.php
1<?php
3
6use Bitrix\Main\Entity\ReferenceField;
7use \Bitrix\Mail\Helper\MessageFolder;
8
10{
11 private $mailboxId;
12 private $messagesIds;
13
14 public function __construct($mailboxId, $messagesIds)
15 {
16 $this->mailboxId = $mailboxId;
17 $this->messagesIds = $messagesIds;
18 }
19
20 public function getMailbox($mailboxUserId = null)
21 {
22 return Mail\MailboxTable::getUserMailbox($this->mailboxId, $mailboxUserId);
23 }
24
25 public function deleteOldMessages($folderCurrentName)
26 {
27 $connection = Main\Application::getInstance()->getConnection();
28 $sqlHelper = $connection->getSqlHelper();
29 $sql = 'DELETE from ' . Mail\MailMessageUidTable::getTableName() .
30 ' WHERE MAILBOX_ID = ' . intval($this->mailboxId) .
31 " AND DIR_MD5 = '" . $sqlHelper->forSql(md5($folderCurrentName)) . "'" .
32 ' AND MSG_UID = 0;';
33 $connection->query($sql);
34 return $connection->getAffectedRowsCount();
35 }
36
37 public function markMessagesUnseen($messages, $mailbox)
38 {
39 $this->setMessagesSeen('N', $messages, $mailbox);
40 }
41
42 public function markMessagesSeen($messages, $mailbox)
43 {
44 $this->setMessagesSeen('Y', $messages, $mailbox);
45 }
46
47 protected function setMessagesSeen($isSeen, $messages, $mailbox)
48 {
49 $messagesIds = [];
50
51 foreach ($this->messagesIds as $index => $messageId)
52 {
53 $messagesIds[$index] = $messageId;
54 }
55
56 if (empty($messagesIds) || empty($messages) || empty($mailbox))
57 {
58 return;
59 }
60
61 $mailsData = [];
62
63 foreach ($messages as $messageData)
64 {
65 $mailsData[] = [
66 'HEADER_MD5' => $messageData['HEADER_MD5'],
67 'MAILBOX_USER_ID' => $mailbox['USER_ID'],
68 'IS_SEEN' => $isSeen,
69 ];
70 }
71
72 $mailboxId = intval($this->mailboxId);
73
74 Mail\MailMessageUidTable::updateList(
75 [
76 '=MAILBOX_ID' => $mailboxId,
77 '@ID' => $messagesIds,
78 ],
79 [
80 'IS_SEEN' => $isSeen,
81 ],
82 $mailsData
83 );
84
85 $dirWithMessagesId = MessageFolder::getDirIdForMessages($mailboxId,$messagesIds);
86
87 if($isSeen === 'Y')
88 {
89 MessageFolder::decreaseDirCounter($mailboxId, $dirWithMessagesId, count($messagesIds));
90 }
91 else
92 {
93 MessageFolder::increaseDirCounter($mailboxId, false, $dirWithMessagesId, count($messagesIds));
94 }
95
96 \Bitrix\Mail\Helper::updateMailboxUnseenCounter($mailboxId);
97 }
98
99 public function updateMessageFieldsAfterMove($messages, $folderNewName, $mailbox)
100 {
101 $messagesIds = [];
102 foreach ($messages as $message)
103 {
104 $messagesIds[] = $message['ID'];
105 }
106 if (empty($messagesIds))
107 {
108 return;
109 }
110
111 $mailsData = [];
112 foreach ($messages as $messageData)
113 {
114 $mailsData[] = [
115 'HEADER_MD5' => $messageData['HEADER_MD5'],
116 'MAILBOX_USER_ID' => $mailbox['USER_ID']
117 ];
118 }
119
120 Mail\MailMessageUidTable::updateList(
121 [
122 '=MAILBOX_ID' => intval($this->mailboxId),
123 '@ID' => $messagesIds,
124 ],
125 [
126 'MSG_UID' => 0,
127 'DIR_MD5' => md5($folderNewName),
128 ],
129 $mailsData
130 );
131 }
132
133 public function addMailsToBlacklist($blacklistMails, $userId)
134 {
135 $result = new Main\Result();
136 $result->setData([Mail\BlacklistTable::addMailsBatch($blacklistMails, $userId)]);
137 return $result;
138 }
139
148 public function deleteMailsCompletely($messagesToDelete, $mailboxUserId)
149 {
150 // @TODO: make a log optional
151 /*$messageToLog = [
152 'cause' => 'deleteMailsCompletely',
153 'filter' => 'manual deletion of messages',
154 'removedMessages'=>$messagesToDelete,
155 ];
156 AddMessage2Log($messageToLog);*/
157
158 $ids = array_map(
159 function ($mail)
160 {
161 return intval($mail['MESSAGE_ID']);
162 },
163 $messagesToDelete
164 );
165 if (empty($ids))
166 {
167 return;
168 }
169 $mailFieldsForEvent = [];
170
171 foreach ($messagesToDelete as $index => $item)
172 {
173 $mailFieldsForEvent[] = [
174 'HEADER_MD5' => $item['HEADER_MD5'],
175 'MESSAGE_ID' => $item['MESSAGE_ID'],
176 'MAILBOX_USER_ID' => $mailboxUserId,
177 ];
178 }
179 Mail\MailMessageUidTable::deleteList(
180 [
181 '=MAILBOX_ID' => $this->mailboxId,
182 '@MESSAGE_ID' => $ids,
183 ],
184 $mailFieldsForEvent
185 );
186
187 // @TODO: use API
188 $connection = Main\Application::getInstance()->getConnection();
189 $connection->query(
190 'DELETE from ' . Mail\MailMessageTable::getTableName() .
191 ' WHERE ID IN (' . implode(',', $ids) . ');'
192 );
193 }
194
195 public function getMessages()
196 {
197 if (empty($this->messagesIds))
198 {
199 return [];
200 }
201 $messages = [];
202 $messagesSelected = Mail\MailMessageUidTable::query()
203 ->addSelect('MESSAGE_ID')
204 ->where('MAILBOX_ID', $this->mailboxId)
205 ->whereIn('ID', $this->messagesIds)
206 ->whereNot('MSG_UID', 0)
207 ->where('MESSAGE_ID', '>', 0)
208 ->addFilter('==DELETE_TIME', 0)
209 ->exec()
210 ->fetchAll();
211 if ($messagesSelected)
212 {
213 $messagesSelectedIds = array_map(
214 function ($item)
215 {
216 return $item['MESSAGE_ID'];
217 },
218 $messagesSelected
219 );
220 if (empty($messagesSelectedIds))
221 {
222 return [];
223 }
224 $messages = Mail\MailMessageUidTable::query()
225 ->registerRuntimeField(
226 '',
227 new ReferenceField(
228 'ref',
229 Mail\MailMessageTable::class,
230 ['=this.MESSAGE_ID' => 'ref.ID']
231 )
232 )
233 ->addSelect('ID')
234 ->addSelect('MAILBOX_ID')
235 ->addSelect('DIR_MD5')
236 ->addSelect('DIR_UIDV')
237 ->addSelect('MSG_UID')
238 ->addSelect('HEADER_MD5')
239 ->addSelect('IS_SEEN')
240 ->addSelect('SESSION_ID')
241 ->addSelect('MESSAGE_ID')
242 ->addSelect('ref.FIELD_FROM', 'FIELD_FROM')
243 ->whereIn('MESSAGE_ID', $messagesSelectedIds)
244 ->where('MAILBOX_ID', $this->mailboxId)
245 ->whereNot('MSG_UID', 0)
246 ->exec()
247 ->fetchAll();
248 }
249
250 return $messages;
251 }
252}
static addMailsBatch(array $list, $userId=null)
static decreaseDirCounter($mailboxId, $dirWithMessagesId, $idsUnseenCount)
static getDirIdForMessages($mailboxId, $messagesIds)
static increaseDirCounter($mailboxId, $dirForMoveMessages=false, $dirForMoveMessagesId, $idsUnseenCount)
setMessagesSeen($isSeen, $messages, $mailbox)
updateMessageFieldsAfterMove($messages, $folderNewName, $mailbox)
markMessagesUnseen($messages, $mailbox)
addMailsToBlacklist($blacklistMails, $userId)
deleteOldMessages($folderCurrentName)
getMailbox($mailboxUserId=null)
deleteMailsCompletely($messagesToDelete, $mailboxUserId)
__construct($mailboxId, $messagesIds)
markMessagesSeen($messages, $mailbox)