18 $result = $this->
initData(MailboxDirectoryTable::TYPE_TRASH);
19 if (!$result->isSuccess())
24 return $this->processDelete($this->
getDirPathByType(MailboxDirectoryTable::TYPE_TRASH),$deleteImmediately);
30 if (!$result->isSuccess())
35 foreach ($this->messages as $index => $message)
37 if (in_array($message[
'ID'], $this->messagesIds,
true))
39 $folders[$message[
'DIR_MD5']] = $message[
'DIR_MD5'];
42 foreach ($folders as $index => $folderHash)
44 if ($folderHash === md5($folderToMoveName))
46 return $result->addError(
new Main\
Error(
Loc::getMessage(
'MAIL_CLIENT_MOVE_TO_SELF_FOLDER', [
'#FOLDER#' => $folderToMoveName]),
47 'MAIL_CLIENT_MOVE_TO_SELF_FOLDER'));
56 'MAIL_CLIENT_MAILBOX_NOT_FOUND'));
59 if ($dir->isDisabled())
61 return $result->addError(
new Main\
Error(
Loc::getMessage(
'MAIL_CLIENT_FOLDER_IS_DISABLED', [
'#FOLDER#' => $folderToMoveName]),
62 'MAIL_CLIENT_FOLDER_IS_DISABLED'));
69 elseif ($dir->isTrash())
73 elseif ($dir->isIncome())
78 $result = $this->moveMailsToFolder($folderToMoveName);
79 if (!$result->isSuccess())
84 return (
new Main\
Result());
89 $result = $this->
initData(MailboxDirectoryTable::TYPE_SPAM);
90 if (!$result->isSuccess())
95 $result = $this->moveMailsToFolder($this->
getDirPathByType(MailboxDirectoryTable::TYPE_INCOME));
96 if (!$result->isSuccess())
100 $filter = Mail\BlacklistTable::getUserAddressesListQuery($this->userId,
false)->getFilter();
101 $filter[] = [
'@ITEM_VALUE' => array_column($this->messages,
'EMAIL')];
102 $filter[] = [
'=ITEM_TYPE' => Mail\Blacklist\ItemType::EMAIL];
103 \Bitrix\Mail\BlacklistTable::deleteList($filter);
116 $result = $this->
initData(MailboxDirectoryTable::TYPE_SPAM);
117 if (!$result->isSuccess())
122 return $this->processSpam($this->
getDirPathByType(MailboxDirectoryTable::TYPE_SPAM));
125 private function processDelete($folderTrashName, $deleteImmediately =
false)
127 $messagesToMove = $messagesToDelete = [];
129 foreach ($this->messages as $messageUid)
131 if ($this->isMailToBeDeleted($messageUid) || $deleteImmediately)
133 $messagesToDelete[] = $messageUid;
137 $messagesToMove[] = $messageUid;
141 $result = $this->processMoving($messagesToMove, $folderTrashName);
142 if (!$result->isSuccess())
144 return (
new Main\Result())->addError(
new Main\Error(
Loc::getMessage(
'MAIL_CLIENT_SYNC_ERROR'),
'MAIL_CLIENT_SYNC_ERROR'));
147 return $this->deleteMessages($messagesToDelete, $this->mailbox);
150 private function processMoving($messagesToMove, $destinationDirectory)
152 if (!$messagesToMove)
154 return new Main\Result();
156 $result = $this->moveMailsByImap($messagesToMove, $destinationDirectory);
157 if ($result->isSuccess())
159 $this->repository->updateMessageFieldsAfterMove($this->messages, $destinationDirectory, $this->mailbox);
161 return (
new Main\Result())->setData($messagesToMove);
165 return (
new Main\Result())->addError(
new Main\
Error(
Loc::getMessage(
'MAIL_CLIENT_SYNC_ERROR'),
'MAIL_CLIENT_SYNC_ERROR'));
169 private function processSpam($folderSpamName)
171 $result = $this->moveMailsToFolder($folderSpamName);
172 if (!$result->isSuccess())
174 return (
new Main\Result())->addError(
new Main\
Error(
Loc::getMessage(
'MAIL_CLIENT_SYNC_ERROR'),
'MAIL_CLIENT_SYNC_ERROR'));
176 if ($result->getData())
178 $mailsToBlacklist = [];
179 foreach ($this->messages as $messageUid)
181 if ($messageUid[
'EMAIL'] !== $this->mailbox[
'EMAIL'])
183 $mailsToBlacklist[] = $messageUid[
'EMAIL'];
186 return $this->repository->addMailsToBlacklist($mailsToBlacklist, $this->userId);
191 private function deleteMessages($messagesToDelete,
$mailbox)
193 if (empty($messagesToDelete))
195 return new Main\Result();
198 $result = $this->mailboxHelper->deleteMails($messagesToDelete);
200 if ($result->isSuccess())
202 $this->repository->deleteMailsCompletely($messagesToDelete, $this->mailbox[
'USER_ID']);
203 return new Main\Result();
206 return (
new Main\Result())->addError(
new Main\
Error(
Loc::getMessage(
'MAIL_CLIENT_SYNC_ERROR'),
'MAIL_CLIENT_SYNC_ERROR'));
209 private function isMailToBeDeleted($messageUid)
212 return md5($trashFolder) === $messageUid[
'DIR_MD5'];
215 private function moveMailsToFolder($folderToName)
218 foreach ($this->messages as $messageUid)
220 if (md5($folderToName) !== $messageUid[
'DIR_MD5'])
222 $mailsToMove[] = $messageUid;
225 return $this->processMoving($mailsToMove, $folderToName);
228 private function moveMailsByImap($messagesToMove, $folder)
230 if (empty($messagesToMove))
232 return new Main\Result();
235 return $this->mailboxHelper->moveMailsToFolder($messagesToMove, $folder);
238 private function processSyncMovedMessages($folderCurrentNameEncoded)
240 $folderCurrentName = base64_decode($folderCurrentNameEncoded);
241 $this->mailboxHelper->syncDir($folderCurrentName);
243 Mail\MailMessageUidTable::updateList(
245 '=MAILBOX_ID' => $this->mailboxId,
246 '=DIR_MD5' => md5($folderCurrentName),
248 '!@IS_OLD' => [
'D',
'R'],
262 $mailManager->processSyncMovedMessages($folderName);
264 catch (\Exception $e)
273 $messIds = array_map(
283 static::class .
"::syncMovedMessages(%u, %u, '%s');",
284 $this->mailbox[
'ID'],
285 $this->mailbox[
'USER_ID'],
286 base64_encode($folderName)
static getMessage($code, $replace=null, $language=null)