1: <?php
2: /**
3: * This code is licensed under Afterlogic Software License.
4: * For full statements of the license see LICENSE file.
5: */
6:
7: namespace Aurora\Modules\CorporateCalendar;
8:
9: /**
10: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
11: * @copyright Copyright (c) 2023, Afterlogic Corp.
12: *
13: * @property Settings $oModuleSettings
14: *
15: * @package Modules
16: */
17: class Module extends \Aurora\System\Module\AbstractLicensedModule
18: {
19: public $oManager = null;
20:
21: public function getManager()
22: {
23: if ($this->oManager === null) {
24: $this->oManager = new \Aurora\Modules\Calendar\Manager($this);
25: }
26:
27: return $this->oManager;
28: }
29:
30: public function init()
31: {
32: $this->subscribeEvent('Calendar::GetCalendars::after', array($this, 'onAfterGetCalendars'));
33: }
34:
35: /**
36: * @return Module
37: */
38: public static function getInstance()
39: {
40: return parent::getInstance();
41: }
42:
43: /**
44: * @return Module
45: */
46: public static function Decorator()
47: {
48: return parent::Decorator();
49: }
50:
51: /**
52: * @return Settings
53: */
54: public function getModuleSettings()
55: {
56: return $this->oModuleSettings;
57: }
58:
59: public function GetSettings()
60: {
61: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
62:
63: return array(
64: 'AllowShare' => $this->oModuleSettings->AllowShare
65: );
66: }
67:
68: /**
69: *
70: * @param int $UserId
71: * @param string $Id
72: * @param boolean $IsPublic
73: * @param array $Shares
74: * @param boolean $ShareToAll
75: * @param int $ShareToAllAccess
76: * @return array|boolean
77: */
78: public function UpdateCalendarShare($UserId, $Id, $IsPublic, $Shares, $ShareToAll = false, $ShareToAllAccess = \Aurora\Modules\Calendar\Enums\Permission::Read)
79: {
80: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
81: $sUserPublicId = \Aurora\System\Api::getUserPublicIdById($UserId);
82: $aShares = json_decode($Shares, true) ;
83:
84: // Share calendar to all users
85: if ($ShareToAll) {
86: $aShares[] = array(
87: 'email' => $this->getManager()->getTenantUser(),
88: 'access' => $ShareToAllAccess
89: );
90: } else {
91: $aShares[] = array(
92: 'email' => $this->getManager()->getTenantUser(),
93: 'access' => \Aurora\Modules\Calendar\Enums\Permission::RemovePermission
94: );
95: }
96:
97: // Public calendar
98: if ($IsPublic) {
99: $aShares[] = array(
100: 'email' => $this->getManager()->getPublicUser(),
101: 'access' => \Aurora\Modules\Calendar\Enums\Permission::Read
102: );
103: }
104: return $this->getManager()->updateCalendarShares($sUserPublicId, $Id, $aShares);
105: }
106:
107: public function onAfterGetCalendars($aData, &$oResult)
108: {
109: if (isset($aData['UserId']) && isset($oResult['Calendars'])) {
110: $oUser = \Aurora\System\Api::getUserById($aData['UserId']);
111: if ($oUser) {
112: $mCalendars = $this->getManager()->getSharedCalendars($oUser->PublicId);
113: if (is_array($mCalendars)) {
114: foreach ($mCalendars as $CalendarKey => $oCalendar) {
115: foreach ($oCalendar->Shares as $ShareKey => $aShare) {
116: if ($aShare['email'] === $this->getManager()->getTenantUser()) {
117: if (!$oCalendar->SharedToAll) {
118: $mCalendars[$CalendarKey]->Shared = true;
119: $mCalendars[$CalendarKey]->SharedToAll = true;
120: } elseif ($oUser->PublicId === $oCalendar->Owner) {
121: unset($mCalendars[$CalendarKey]);
122: }
123: unset($oCalendar->Shares[$ShareKey]);
124: }
125: }
126: }
127: $oResult['Calendars'] = array_merge($oResult['Calendars'], $mCalendars);
128: }
129: }
130: }
131: }
132: }
133: