PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.6
Booking for Appointments and Events Calendar – Amelia v2.4.6
2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Application / Commands / Booking / Event / UpdateEventCommandHandler.php
ameliabooking / src / Application / Commands / Booking / Event Last commit date
Tag 3 months ago AddEventCommand.php 1 year ago AddEventCommandHandler.php 1 week ago DeleteEventBookingCommand.php 7 years ago DeleteEventBookingCommandHandler.php 1 week ago DeleteEventCommand.php 1 year ago DeleteEventCommandHandler.php 7 months ago DeleteEventsCommand.php 7 months ago DeleteEventsCommandHandler.php 4 months ago GenerateEventWpaCustomPostCommand.php 4 weeks ago GenerateEventWpaCustomPostCommandHandler.php 4 weeks ago GetCalendarEventsCommand.php 1 year ago GetCalendarEventsCommandHandler.php 1 week ago GetEventBookingCommand.php 7 months ago GetEventBookingCommandHandler.php 1 week ago GetEventBookingsCommand.php 1 year ago GetEventBookingsCommandHandler.php 1 week ago GetEventCommand.php 7 years ago GetEventCommandHandler.php 1 week ago GetEventsCommand.php 1 year ago GetEventsCommandHandler.php 1 week ago UpdateEventBookingCommand.php 7 years ago UpdateEventBookingCommandHandler.php 7 months ago UpdateEventCommand.php 1 year ago UpdateEventCommandHandler.php 1 week ago UpdateEventStatusCommand.php 1 year ago UpdateEventStatusCommandHandler.php 7 months ago UpdateEventVisibilityCommand.php 7 months ago UpdateEventVisibilityCommandHandler.php 7 months ago
UpdateEventCommandHandler.php
282 lines
1 <?php
2
3 namespace AmeliaBooking\Application\Commands\Booking\Event;
4
5 use AmeliaBooking\Application\Commands\CommandHandler;
6 use AmeliaBooking\Application\Commands\CommandResult;
7 use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException;
8 use AmeliaBooking\Application\Services\Booking\EventApplicationService;
9 use AmeliaBooking\Application\Services\Entity\EntityApplicationService;
10 use AmeliaBooking\Application\Services\User\UserApplicationService;
11 use AmeliaBooking\Application\Services\Zoom\AbstractZoomApplicationService;
12 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
13 use AmeliaBooking\Domain\Entity\Booking\Event\Event;
14 use AmeliaBooking\Domain\Entity\Booking\Event\EventPeriod;
15 use AmeliaBooking\Domain\Entity\Entities;
16 use AmeliaBooking\Domain\Entity\User\AbstractUser;
17 use AmeliaBooking\Domain\Services\Settings\SettingsService;
18 use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue;
19 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
20 use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository;
21 use Exception;
22
23 /**
24 * Class UpdateEventCommandHandler
25 *
26 * @package AmeliaBooking\Application\Commands\Booking\Event
27 */
28 class UpdateEventCommandHandler extends CommandHandler
29 {
30 /**
31 * @var array
32 */
33 public $mandatoryFields = [
34 'id',
35 'name',
36 'periods'
37 ];
38
39 /**
40 * @param UpdateEventCommand $command
41 *
42 * @return CommandResult
43 * @throws QueryExecutionException
44 * @throws InvalidArgumentException
45 * @throws AccessDeniedException
46 * @throws Exception
47 */
48 public function handle(UpdateEventCommand $command)
49 {
50 /** @var AbstractUser $user */
51 $user = $command->authorize();
52
53 $result = new CommandResult();
54
55 $this->checkMandatoryFields($command);
56
57 $eventData = $command->getFields();
58
59 /** @var EventRepository $eventRepository */
60 $eventRepository = $this->container->get('domain.booking.event.repository');
61 /** @var EventApplicationService $eventApplicationService */
62 $eventApplicationService = $this->container->get('application.booking.event.service');
63 /** @var UserApplicationService $userAS */
64 $userAS = $this->getContainer()->get('application.user.service');
65 /** @var SettingsService $settingsDS */
66 $settingsDS = $this->container->get('domain.settings.service');
67 /** @var EntityApplicationService $entityService */
68 $entityService = $this->container->get('application.entity.service');
69
70 if (
71 $userAS->isCustomer($user) ||
72 (
73 $userAS->isProvider($user) && !$settingsDS->getSetting('roles', 'allowWriteEvents')
74 )
75 ) {
76 throw new AccessDeniedException('You are not allowed to update an event');
77 }
78
79 $entityService->removeMissingEntitiesForEvent($eventData);
80
81
82 /** @var Event $oldEvent */
83 $oldEvent = $eventApplicationService->getEventById(
84 $eventData['id'],
85 [
86 'fetchEventsPeriods' => true,
87 'fetchEventsTickets' => true,
88 'fetchEventsTags' => true,
89 'fetchEventsProviders' => true,
90 'fetchEventsImages' => true,
91 'fetchBookings' => true,
92 'fetchBookingsTickets' => true,
93 'fetchBookingsUsers' => true,
94 'fetchBookingsPayments' => true,
95 ]
96 );
97
98 $eventData =
99 apply_filters('amelia_before_event_updated_filter', $eventData, $oldEvent ? $oldEvent->toArray() : null, $command->getField('applyGlobally'));
100
101 do_action('amelia_before_event_updated', $eventData, $oldEvent ? $oldEvent->toArray() : null, $command->getField('applyGlobally'));
102
103 try {
104 /** @var Event $event */
105 $event = $eventApplicationService->build($eventData);
106 } catch (Exception $e) {
107 $result->setResult(CommandResult::RESULT_ERROR);
108 $result->setMessage($e->getMessage());
109 $result->setData(['message' => $e->getMessage()]);
110 return $result;
111 }
112
113 if (!empty($oldEvent->getPicture()) && $command->getPage() === 'cabinet') {
114 $event->setPicture($oldEvent->getPicture());
115 }
116
117 /** @var DateTimeValue $newUntil */
118 $newUntil = $event->getRecurring()
119 ? $event->getRecurring()->getUntil()->getValue()->setTime(0, 0, 0)
120 : null;
121
122 /** @var DateTimeValue $oldUntil */
123 $oldUntil = $oldEvent->getRecurring()
124 ? $oldEvent->getRecurring()->getUntil()->getValue()->setTime(0, 0, 0)
125 : null;
126
127 if (
128 $oldEvent->getRecurring() &&
129 $event->getRecurring() &&
130 (
131 $newUntil < $oldUntil ||
132 $event->getRecurring()->getCycle()->getValue() !== $oldEvent->getRecurring()->getCycle()->getValue()
133 )
134 ) {
135 $result->setResult(CommandResult::RESULT_ERROR);
136 $result->setMessage('Could not update event');
137
138 return $result;
139 }
140
141 $event->setBookings($oldEvent->getBookings());
142
143 /** @var EventPeriod $oldEventPeriod */
144 foreach ($oldEvent->getPeriods()->getItems() as $oldEventPeriod) {
145 /** @var EventPeriod $eventPeriod */
146 foreach ($event->getPeriods()->getItems() as $eventPeriod) {
147 if (
148 $eventPeriod->getId() &&
149 $oldEventPeriod->getId()->getValue() === $eventPeriod->getId()->getValue()
150 ) {
151 if ($oldEventPeriod->getZoomMeeting()) {
152 $eventPeriod->setZoomMeeting($oldEventPeriod->getZoomMeeting());
153 }
154 if ($oldEventPeriod->getLessonSpace()) {
155 $eventPeriod->setLessonSpace($oldEventPeriod->getLessonSpace());
156 }
157 if ($oldEventPeriod->getGoogleCalendarEventId()) {
158 $eventPeriod->setGoogleCalendarEventId($oldEventPeriod->getGoogleCalendarEventId());
159 }
160 if ($oldEventPeriod->getGoogleMeetUrl()) {
161 $eventPeriod->setGoogleMeetUrl($oldEventPeriod->getGoogleMeetUrl());
162 }
163 if ($oldEventPeriod->getOutlookCalendarEventId()) {
164 $eventPeriod->setOutlookCalendarEventId($oldEventPeriod->getOutlookCalendarEventId());
165 }
166 if ($oldEventPeriod->getMicrosoftTeamsUrl()) {
167 $eventPeriod->setMicrosoftTeamsUrl($oldEventPeriod->getMicrosoftTeamsUrl());
168 }
169 if ($oldEventPeriod->getAppleCalendarEventId()) {
170 $eventPeriod->setAppleCalendarEventId($oldEventPeriod->getAppleCalendarEventId());
171 }
172 }
173 }
174 }
175
176 $eventRepository->beginTransaction();
177
178 try {
179 $parsedEvents = $eventApplicationService->update(
180 $oldEvent,
181 $event,
182 $command->getField('applyGlobally')
183 );
184 } catch (QueryExecutionException $e) {
185 $eventRepository->rollback();
186 throw $e;
187 }
188
189 $eventRepository->commit();
190
191 do_action(
192 'amelia_after_event_updated',
193 $event ? $event->toArray() : null,
194 $oldEvent ? $oldEvent->toArray() : null,
195 $command->getField('applyGlobally')
196 );
197
198 $providersRemoved = array_udiff(
199 $oldEvent->getProviders()->getItems(),
200 $event->getProviders()->getItems(),
201 function ($a, $b) {
202 if ($a->getId()->getValue() == $b->getId()->getValue()) {
203 return 0;
204 } else {
205 return ($a->getId()->getValue() < $b->getId()->getValue() ? -1 : 1);
206 }
207 }
208 );
209
210 $providersAdded = array_udiff(
211 $event->getProviders()->getItems(),
212 $oldEvent->getProviders()->getItems(),
213 function ($a, $b) {
214 if ($a->getId()->getValue() == $b->getId()->getValue()) {
215 return 0;
216 } else {
217 return ($a->getId()->getValue() < $b->getId()->getValue() ? -1 : 1);
218 }
219 }
220 );
221
222 $newInfo = ($event->getDescription() ? $event->getDescription()->getValue() : null) !==
223 ($oldEvent->getDescription() ? $oldEvent->getDescription()->getValue() : null) ||
224 $event->getName()->getValue() !== $oldEvent->getName()->getValue();
225
226 $zoomUserChanged = ($event->getZoomUserId() ? $event->getZoomUserId()->getValue() : null) !==
227 ($oldEvent->getZoomUserId() ? $oldEvent->getZoomUserId()->getValue() : null);
228
229 $zoomUsersLicenced = false;
230
231 if ($oldEvent->getZoomUserId() && $event->getZoomUserId() && $zoomUserChanged) {
232 /** @var AbstractZoomApplicationService $zoomService */
233 $zoomService = $this->container->get('application.zoom.service');
234
235 $zoomUserType = 0;
236 $zoomOldUserType = 0;
237 $zoomResult = $zoomService->getUsers();
238 if (
239 !(isset($zoomResult['code']) && $zoomResult['code'] === 124) &&
240 !($zoomResult['users'] === null && isset($zoomResult['message']))
241 ) {
242 $zoomUsers = $zoomResult['users'];
243 foreach ($zoomUsers as $key => $val) {
244 if ($val['id'] === $event->getZoomUserId()->getValue()) {
245 $zoomUserType = $val['type'];
246 }
247 if ($val['id'] === $oldEvent->getZoomUserId()->getValue()) {
248 $zoomOldUserType = $val['type'];
249 }
250 }
251 }
252 if ($zoomOldUserType > 1 && $zoomUserType > 1) {
253 $zoomUsersLicenced = true;
254 }
255 }
256
257 $organizerChanged = ($event->getOrganizerId() ? $event->getOrganizerId()->getValue() : null)
258 !== ($oldEvent->getOrganizerId() ? $oldEvent->getOrganizerId()->getValue() : null);
259
260 $result->setResult(CommandResult::RESULT_SUCCESS);
261 $result->setMessage('Successfully updated event.');
262 $result->setData(
263 [
264 Entities::EVENTS => $parsedEvents,
265 'zoomUserChanged' => $zoomUserChanged && $event->getZoomUserId() ? $event->getZoomUserId()->getValue() : null,
266 'zoomUsersLicenced' => $zoomUsersLicenced,
267 'newInfo' => $newInfo ? [
268 'name' => $event->getName(),
269 'description' => $event->getDescription()
270 ] : null,
271 'newProviders' => $providersAdded,
272 'removeProviders' => $providersRemoved,
273 'organizerChanged' => $organizerChanged,
274 'newOrganizer' => $event->getOrganizerId() ? $event->getOrganizerId()->getValue() : null,
275 'notifyParticipants' => $command->getField('notifyParticipants')
276 ]
277 );
278
279 return $result;
280 }
281 }
282