1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | namespace Aurora\Modules\MailNotesPlugin; |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | class Module extends \Aurora\System\Module\AbstractModule |
20: | { |
21: | public function init() |
22: | { |
23: | $this->subscribeEvent('Mail::GetFolders::before', array($this, 'onBeforeGetFolders')); |
24: | $this->subscribeEvent('Mail::GetMessages::before', array($this, 'onBeforeGetMessages')); |
25: | $this->subscribeEvent('Mail::GetMessages::after', array($this, 'onAfterGetMessages')); |
26: | } |
27: | |
28: | |
29: | |
30: | |
31: | public static function getInstance() |
32: | { |
33: | return parent::getInstance(); |
34: | } |
35: | |
36: | |
37: | |
38: | |
39: | public static function Decorator() |
40: | { |
41: | return parent::Decorator(); |
42: | } |
43: | |
44: | |
45: | |
46: | |
47: | public function getModuleSettings() |
48: | { |
49: | return $this->oModuleSettings; |
50: | } |
51: | |
52: | public function onBeforeGetFolders(&$aArgs, &$mResult) |
53: | { |
54: | $oMailModule = \Aurora\Modules\Mail\Module::getInstance(); |
55: | $oApiAccountsManager = $oMailModule->getAccountsManager(); |
56: | $oApiMailManager = $oMailModule->getMailManager(); |
57: | |
58: | $iAccountID = $aArgs['AccountID']; |
59: | $oAccount = $oApiAccountsManager->getAccountById($iAccountID); |
60: | if ($oAccount) { |
61: | $oNamespace = $oApiMailManager->getFoldersNamespace($oAccount); |
62: | $sNamespace = $oNamespace ? $oNamespace->GetPersonalNamespace() : ''; |
63: | $aResult = \Aurora\Modules\Mail\Module::Decorator()->GetRelevantFoldersInformation($iAccountID, array($sNamespace . 'Notes'), false); |
64: | if (empty($aResult['Counts'])) { |
65: | try { |
66: | \Aurora\Modules\Mail\Module::Decorator()->CreateFolder($iAccountID, $sNamespace . 'Notes', '', '/'); |
67: | } catch (\Exception $oException) { |
68: | } |
69: | } |
70: | } |
71: | } |
72: | |
73: | public function onBeforeGetMessages(&$aArgs, &$mResult) |
74: | { |
75: | if (isset($aArgs['Folder']) && $aArgs['Folder'] === 'Notes' && isset($aArgs['Search']) && !empty($aArgs['Search'])) { |
76: | $aArgs['ActualSearch'] = $aArgs['Search']; |
77: | $aArgs['Search'] = sprintf('text:%s', $aArgs['Search']); |
78: | } |
79: | } |
80: | |
81: | public function onAfterGetMessages(&$aArgs, &$mResult) |
82: | { |
83: | if (isset($aArgs['Folder']) && $aArgs['Folder'] === 'Notes' && isset($aArgs['ActualSearch'])) { |
84: | $aArgs['Search'] = $aArgs['ActualSearch']; |
85: | unset($aArgs['ActualSearch']); |
86: | $mResult->Search = $aArgs['Search']; |
87: | } |
88: | } |
89: | |
90: | protected function populateFromOrigMessage($iAccountId, $FolderFullName, $MessageUid, &$oMessage) |
91: | { |
92: | $oOrigMessage = \Aurora\Modules\Mail\Module::Decorator()->GetMessage($iAccountId, $FolderFullName, $MessageUid); |
93: | |
94: | if ($oOrigMessage) { |
95: | $oFromCollection = $oOrigMessage->getFrom(); |
96: | if ($oFromCollection && $oFromCollection->Count() > 0) { |
97: | $oMessage->SetFrom($oFromCollection->GetByIndex(0)); |
98: | } |
99: | $oToCollection = $oOrigMessage->getTo(); |
100: | if ($oToCollection && $oToCollection->Count() > 0) { |
101: | $oMessage->SetTo($oToCollection); |
102: | } |
103: | } |
104: | } |
105: | |
106: | |
107: | |
108: | |
109: | |
110: | |
111: | |
112: | |
113: | |
114: | |
115: | |
116: | |
117: | public function SaveNote($AccountID, $FolderFullName, $Text, $Subject, $MessageUid = null) |
118: | { |
119: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
120: | |
121: | $oMailModule = \Aurora\System\Api::GetModule('Mail'); |
122: | $oApiAccountsManager = $oMailModule->getAccountsManager(); |
123: | $oAccount = $oApiAccountsManager->getAccountById($AccountID); |
124: | |
125: | $oAuthenticatedUser = \Aurora\System\Api::getAuthenticatedUser(); |
126: | if ($oAccount->IdUser !== $oAuthenticatedUser->Id) { |
127: | throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::AccessDenied, null, 'AccessDenied'); |
128: | } |
129: | |
130: | $oApiMailManager = $oMailModule->getMailManager(); |
131: | $oMessage = \MailSo\Mime\Message::NewInstance(); |
132: | $oMessage->RegenerateMessageId(); |
133: | $oMessage->SetSubject($Subject); |
134: | $oMessage->AddText($Text, true); |
135: | $oMessage->SetCustomHeader('X-Uniform-Type-Identifier', 'com.apple.mail-note'); |
136: | $oMessage->SetCustomHeader('X-Universally-Unique-Identifier', uniqid()); |
137: | |
138: | if (!empty($MessageUid)) { |
139: | $this->populateFromOrigMessage($AccountID, $FolderFullName, $MessageUid, $oMessage); |
140: | $oApiMailManager->deleteMessage($oAccount, $FolderFullName, array($MessageUid)); |
141: | } |
142: | |
143: | $rMessageStream = \MailSo\Base\ResourceRegistry::CreateMemoryResource(); |
144: | $iMessageStreamSize = \MailSo\Base\Utils::MultipleStreamWriter($oMessage->ToStream(true), array($rMessageStream), 8192, true, true, true); |
145: | $iNewUid = 0; |
146: | $oApiMailManager->appendMessageFromStream($oAccount, $rMessageStream, $FolderFullName, $iMessageStreamSize, $iNewUid); |
147: | $oApiMailManager->setMessageFlag($oAccount, $FolderFullName, [$iNewUid], \MailSo\Imap\Enumerations\MessageFlag::SEEN, \Aurora\Modules\Mail\Enums\MessageStoreAction::Add); |
148: | |
149: | return $iNewUid; |
150: | } |
151: | |
152: | public function GetSettings() |
153: | { |
154: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
155: | |
156: | $aSettings = array( |
157: | 'DisplayNotesButton' => $this->oModuleSettings->DisplayNotesButton, |
158: | ); |
159: | |
160: | return $aSettings; |
161: | } |
162: | } |
163: | |