1: <?php
2: /**
3: * This code is licensed under AGPLv3 license or Afterlogic Software License
4: * if commercial version of the product was purchased.
5: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
6: */
7:
8: namespace Aurora\Modules\MailSaveAttachmentsToFilesPlugin;
9:
10: /**
11: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
12: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
13: * @copyright Copyright (c) 2023, Afterlogic Corp.
14: *
15: * @property Settings $oModuleSettings
16: *
17: * @package Modules
18: */
19: class Module extends \Aurora\System\Module\AbstractModule
20: {
21: /*
22: * @var $getFilecacheManager()Manager \Aurora\System\Managers\Filecache
23: */
24: public $oApiFilecacheManager = null;
25:
26: public function getFilecacheManager()
27: {
28: if ($this->oApiFilecacheManager === null) {
29: $this->oApiFilecacheManager = new \Aurora\System\Managers\Filecache();
30: }
31:
32: return $this->oApiFilecacheManager;
33: }
34:
35: public function init() {}
36:
37: /**
38: * @return Module
39: */
40: public static function getInstance()
41: {
42: return parent::getInstance();
43: }
44:
45: /**
46: * @return Module
47: */
48: public static function Decorator()
49: {
50: return parent::Decorator();
51: }
52:
53: /**
54: * @return Settings
55: */
56: public function getModuleSettings()
57: {
58: return $this->oModuleSettings;
59: }
60:
61: /**
62: *
63: * @return boolean
64: */
65: public function Save($UserId, $AccountID, $Attachments = [], $Storage = 'personal', $Path = '')
66: {
67: $mResult = false;
68: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
69:
70: $oMailModuleDecorator = \Aurora\Modules\Mail\Module::Decorator();
71: if ($oMailModuleDecorator) {
72: $aTempFiles = $oMailModuleDecorator->SaveAttachmentsAsTempFiles($AccountID, $Attachments);
73: if (\is_array($aTempFiles)) {
74: $sUUID = \Aurora\System\Api::getUserUUIDById($UserId);
75: foreach ($aTempFiles as $sTempName => $sData) {
76: $aData = \Aurora\System\Api::DecodeKeyValues($sData);
77: if (\is_array($aData) && isset($aData['FileName'])) {
78: $sFileName = (string) $aData['FileName'];
79: $rResource = $this->getFilecacheManager()->getFile($sUUID, $sTempName);
80: if ($rResource) {
81: $aArgs = array(
82: 'UserId' => $UserId,
83: 'Type' => $Storage,
84: 'Path' => $Path,
85: 'Name' => $sFileName,
86: 'Data' => $rResource,
87: 'Overwrite' => false,
88: 'RangeType' => 0,
89: 'Offset' => 0,
90: 'ExtendedProps' => array()
91: );
92: \Aurora\System\Api::GetModuleManager()->broadcastEvent(
93: 'Files',
94: 'CreateFile',
95: $aArgs,
96: $mResult
97: );
98: }
99: }
100: }
101: }
102: }
103:
104: return $mResult;
105: }
106: }
107: