1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | namespace Aurora\Modules\MailNotesPlugin; |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | class Module extends \Aurora\System\Module\AbstractModule |
18: | { |
19: | public function init() |
20: | { |
21: | $this->subscribeEvent('Mail::GetFolders::before', array($this, 'onBeforeGetFolders')); |
22: | $this->subscribeEvent('Mail::GetMessages::before', array($this, 'onBeforeGetMessages')); |
23: | $this->subscribeEvent('Mail::GetMessages::after', array($this, 'onAfterGetMessages')); |
24: | } |
25: | |
26: | public function onBeforeGetFolders(&$aArgs, &$mResult) |
27: | { |
28: | $oMailModule = \Aurora\Modules\Mail\Module::getInstance(); |
29: | $oApiAccountsManager = $oMailModule->getAccountsManager(); |
30: | $oApiMailManager = $oMailModule->getMailManager(); |
31: | |
32: | $iAccountID = $aArgs['AccountID']; |
33: | $oAccount = $oApiAccountsManager->getAccountById($iAccountID); |
34: | if ($oAccount) { |
35: | $oNamespace = $oApiMailManager->getFoldersNamespace($oAccount); |
36: | $sNamespace = $oNamespace ? $oNamespace->GetPersonalNamespace() : ''; |
37: | $aResult = $oApiMailManager->getFolderListInformation($oAccount, array($sNamespace . 'Notes'), false); |
38: | if (empty($aResult)) { |
39: | try { |
40: | \Aurora\Modules\Mail\Module::Decorator()->CreateFolder($iAccountID, $sNamespace . 'Notes', '', '/'); |
41: | } catch (\Exception $oException) { |
42: | } |
43: | } |
44: | } |
45: | } |
46: | |
47: | public function onBeforeGetMessages(&$aArgs, &$mResult) |
48: | { |
49: | if (isset($aArgs['Folder']) && $aArgs['Folder'] === 'Notes' && isset($aArgs['Search']) && !empty($aArgs['Search'])) { |
50: | $aArgs['ActualSearch'] = $aArgs['Search']; |
51: | $aArgs['Search'] = sprintf('text:%s', $aArgs['Search']); |
52: | } |
53: | } |
54: | |
55: | public function onAfterGetMessages(&$aArgs, &$mResult) |
56: | { |
57: | if (isset($aArgs['Folder']) && $aArgs['Folder'] === 'Notes' && isset($aArgs['ActualSearch'])) { |
58: | $aArgs['Search'] = $aArgs['ActualSearch']; |
59: | unset($aArgs['ActualSearch']); |
60: | $mResult->Search = $aArgs['Search']; |
61: | } |
62: | } |
63: | |
64: | protected function populateFromOrigMessage($iAccountId, $FolderFullName, $MessageUid, &$oMessage) |
65: | { |
66: | $oOrigMessage = \Aurora\Modules\Mail\Module::Decorator()->GetMessage($iAccountId, $FolderFullName, $MessageUid); |
67: | |
68: | if ($oOrigMessage) { |
69: | $oFromCollection = $oOrigMessage->getFrom(); |
70: | if (isset($oFromCollection) && $oFromCollection->Count() > 0) { |
71: | $oMessage->SetFrom($oFromCollection->GetByIndex(0)); |
72: | } |
73: | $oToCollection = $oOrigMessage->getTo(); |
74: | if (isset($oToCollection) && $oToCollection->Count() > 0) { |
75: | $oMessage->SetTo($oToCollection); |
76: | } |
77: | } |
78: | } |
79: | |
80: | public function SaveNote($AccountID, $FolderFullName, $Text, $Subject, $MessageUid = null) |
81: | { |
82: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
83: | $oMailModule = \Aurora\System\Api::GetModule('Mail'); |
84: | $oApiAccountsManager = $oMailModule->getAccountsManager(); |
85: | $oAccount = $oApiAccountsManager->getAccountById($AccountID); |
86: | $oApiMailManager = $oMailModule->getMailManager(); |
87: | |
88: | $oMessage = \MailSo\Mime\Message::NewInstance(); |
89: | $oMessage->RegenerateMessageId(); |
90: | $oMessage->SetSubject($Subject); |
91: | $oMessage->AddText($Text, true); |
92: | $oMessage->SetCustomHeader('X-Uniform-Type-Identifier', 'com.apple.mail-note'); |
93: | $oMessage->SetCustomHeader('X-Universally-Unique-Identifier', uniqid()); |
94: | |
95: | if (!empty($MessageUid)) { |
96: | $this->populateFromOrigMessage($AccountID, $FolderFullName, $MessageUid, $oMessage); |
97: | $oApiMailManager->deleteMessage($oAccount, $FolderFullName, array($MessageUid)); |
98: | } |
99: | |
100: | $rMessageStream = \MailSo\Base\ResourceRegistry::CreateMemoryResource(); |
101: | $iMessageStreamSize = \MailSo\Base\Utils::MultipleStreamWriter($oMessage->ToStream(true), array($rMessageStream), 8192, true, true, true); |
102: | $iNewUid = 0; |
103: | $oApiMailManager->appendMessageFromStream($oAccount, $rMessageStream, $FolderFullName, $iMessageStreamSize, $iNewUid); |
104: | $oApiMailManager->setMessageFlag($oAccount, $FolderFullName, [$iNewUid], \MailSo\Imap\Enumerations\MessageFlag::SEEN, \Aurora\Modules\Mail\Enums\MessageStoreAction::Add); |
105: | |
106: | return $iNewUid; |
107: | } |
108: | } |
109: | |