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: * @package Modules
16: */
17: class Module extends \Aurora\System\Module\AbstractModule
18: {
19: /*
20: * @var $getFilecacheManager()Manager \Aurora\System\Managers\Filecache
21: */
22: public $oApiFilecacheManager = null;
23:
24: public function getFilecacheManager()
25: {
26: if ($this->oApiFilecacheManager === null) {
27: $this->oApiFilecacheManager = new \Aurora\System\Managers\Filecache();
28: }
29:
30: return $this->oApiFilecacheManager;
31: }
32:
33: public function init()
34: {
35: }
36:
37: /**
38: *
39: * @return boolean
40: */
41: public function Save($UserId, $AccountID, $Attachments = [], $Storage = 'personal', $Path = '')
42: {
43: $mResult = false;
44: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
45:
46: $oMailModuleDecorator = \Aurora\Modules\Mail\Module::Decorator();
47: if ($oMailModuleDecorator) {
48: $aTempFiles = $oMailModuleDecorator->SaveAttachmentsAsTempFiles($AccountID, $Attachments);
49: if (\is_array($aTempFiles)) {
50: $sUUID = \Aurora\System\Api::getUserUUIDById($UserId);
51: foreach ($aTempFiles as $sTempName => $sData) {
52: $aData = \Aurora\System\Api::DecodeKeyValues($sData);
53: if (\is_array($aData) && isset($aData['FileName'])) {
54: $sFileName = (string) $aData['FileName'];
55: $rResource = $this->getFilecacheManager()->getFile($sUUID, $sTempName);
56: if ($rResource) {
57: $aArgs = array(
58: 'UserId' => $UserId,
59: 'Type' => $Storage,
60: 'Path' => $Path,
61: 'Name' => $sFileName,
62: 'Data' => $rResource,
63: 'Overwrite' => false,
64: 'RangeType' => 0,
65: 'Offset' => 0,
66: 'ExtendedProps' => array()
67: );
68: \Aurora\System\Api::GetModuleManager()->broadcastEvent(
69: 'Files',
70: 'CreateFile',
71: $aArgs,
72: $mResult
73: );
74: }
75: }
76: }
77: }
78: }
79:
80: return $mResult;
81: }
82: }
83: