PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.0.2
Booking for Appointments and Events Calendar – Amelia v2.0.2
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
AddEventCommand.php 1 year ago AddEventCommandHandler.php 1 year ago DeleteEventBookingCommand.php 7 years ago DeleteEventBookingCommandHandler.php 1 year ago DeleteEventCommand.php 1 year ago DeleteEventCommandHandler.php 6 months ago DeleteEventsCommand.php 6 months ago DeleteEventsCommandHandler.php 6 months ago GetCalendarEventsCommand.php 1 year ago GetCalendarEventsCommandHandler.php 1 year ago GetEventBookingCommand.php 6 months ago GetEventBookingCommandHandler.php 5 months ago GetEventBookingsCommand.php 1 year ago GetEventBookingsCommandHandler.php 6 months ago GetEventCommand.php 7 years ago GetEventCommandHandler.php 6 months ago GetEventDeleteEffectCommand.php 1 year ago GetEventDeleteEffectCommandHandler.php 6 months ago GetEventsCommand.php 1 year ago GetEventsCommandHandler.php 6 months ago UpdateEventBookingCommand.php 7 years ago UpdateEventBookingCommandHandler.php 6 months ago UpdateEventCommand.php 1 year ago UpdateEventCommandHandler.php 6 months ago UpdateEventStatusCommand.php 1 year ago UpdateEventStatusCommandHandler.php 6 months ago UpdateEventVisibilityCommand.php 6 months ago UpdateEventVisibilityCommandHandler.php 6 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\AuthorizationException;
13 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
14 use AmeliaBooking\Domain\Entity\Booking\Event\Event;
15 use AmeliaBooking\Domain\Entity\Booking\Event\EventPeriod;
16 use AmeliaBooking\Domain\Entity\Entities;
17 use AmeliaBooking\Domain\Entity\User\AbstractUser;
18 use AmeliaBooking\Domain\Services\Settings\SettingsService;
19 use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue;
20 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
21 use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository;
22 use Exception;
23 use Interop\Container\Exception\ContainerException;
24 use Slim\Exception\ContainerValueNotFoundException;
25
26 /**
27 * Class UpdateEventCommandHandler
28 *
29 * @package AmeliaBooking\Application\Commands\Booking\Event
30 */
31 class UpdateEventCommandHandler extends CommandHandler
32 {
33 /**
34 * @var array
35 */
36 public $mandatoryFields = [
37 'id',
38 'name',
39 'periods'
40 ];
41
42 /**
43 * @param UpdateEventCommand $command
44 *
45 * @return CommandResult
46 * @throws ContainerValueNotFoundException
47 * @throws QueryExecutionException
48 * @throws InvalidArgumentException
49 * @throws AccessDeniedException
50 * @throws ContainerException
51 * @throws Exception
52 */
53 public function handle(UpdateEventCommand $command)
54 {
55 $result = new CommandResult();
56
57 $this->checkMandatoryFields($command);
58
59 $eventData = $command->getFields();
60
61 /** @var EventRepository $eventRepository */
62 $eventRepository = $this->container->get('domain.booking.event.repository');
63 /** @var EventApplicationService $eventApplicationService */
64 $eventApplicationService = $this->container->get('application.booking.event.service');
65 /** @var UserApplicationService $userAS */
66 $userAS = $this->getContainer()->get('application.user.service');
67 /** @var SettingsService $settingsDS */
68 $settingsDS = $this->container->get('domain.settings.service');
69 /** @var EntityApplicationService $entityService */
70 $entityService = $this->container->get('application.entity.service');
71
72 try {
73 /** @var AbstractUser $user */
74 $user = $command->getUserApplicationService()->authorization(
75 $command->getPage() === 'cabinet' ? $command->getToken() : null,
76 $command->getCabinetType()
77 );
78 } catch (AuthorizationException $e) {
79 $result->setResult(CommandResult::RESULT_ERROR);
80 $result->setData(
81 [
82 'reauthorize' => true
83 ]
84 );
85
86 return $result;
87 }
88
89 if (
90 $userAS->isCustomer($user) ||
91 (
92 $userAS->isProvider($user) && !$settingsDS->getSetting('roles', 'allowWriteEvents')
93 )
94 ) {
95 throw new AccessDeniedException('You are not allowed to update an event');
96 }
97
98 $entityService->removeMissingEntitiesForEvent($eventData);
99
100
101 /** @var Event $oldEvent */
102 $oldEvent = $eventApplicationService->getEventById(
103 $eventData['id'],
104 [
105 'fetchEventsPeriods' => true,
106 'fetchEventsTickets' => true,
107 'fetchEventsTags' => true,
108 'fetchEventsProviders' => true,
109 'fetchEventsImages' => true,
110 'fetchBookings' => true,
111 'fetchBookingsTickets' => true,
112 'fetchBookingsUsers' => true,
113 'fetchBookingsPayments' => true,
114 ]
115 );
116
117 $eventData =
118 apply_filters('amelia_before_event_updated_filter', $eventData, $oldEvent ? $oldEvent->toArray() : null, $command->getField('applyGlobally'));
119
120 do_action('amelia_before_event_updated', $eventData, $oldEvent ? $oldEvent->toArray() : null, $command->getField('applyGlobally'));
121
122 try {
123 /** @var Event $event */
124 $event = $eventApplicationService->build($eventData);
125 } catch (Exception $e) {
126 $result->setResult(CommandResult::RESULT_ERROR);
127 $result->setMessage($e->getMessage());
128 $result->setData(['message' => $e->getMessage()]);
129 return $result;
130 }
131
132 /** @var DateTimeValue $newUntil */
133 $newUntil = $event->getRecurring()
134 ? $event->getRecurring()->getUntil()->getValue()->setTime(0, 0, 0)
135 : null;
136
137 /** @var DateTimeValue $oldUntil */
138 $oldUntil = $oldEvent->getRecurring()
139 ? $oldEvent->getRecurring()->getUntil()->getValue()->setTime(0, 0, 0)
140 : null;
141
142 if (
143 $oldEvent->getRecurring() &&
144 $event->getRecurring() &&
145 (
146 $newUntil < $oldUntil ||
147 $event->getRecurring()->getCycle()->getValue() !== $oldEvent->getRecurring()->getCycle()->getValue()
148 )
149 ) {
150 $result->setResult(CommandResult::RESULT_ERROR);
151 $result->setMessage('Could not update event');
152
153 return $result;
154 }
155
156 $event->setBookings($oldEvent->getBookings());
157
158 /** @var EventPeriod $oldEventPeriod */
159 foreach ($oldEvent->getPeriods()->getItems() as $oldEventPeriod) {
160 /** @var EventPeriod $eventPeriod */
161 foreach ($event->getPeriods()->getItems() as $eventPeriod) {
162 if (
163 $eventPeriod->getId() &&
164 $oldEventPeriod->getId()->getValue() === $eventPeriod->getId()->getValue()
165 ) {
166 if ($oldEventPeriod->getZoomMeeting()) {
167 $eventPeriod->setZoomMeeting($oldEventPeriod->getZoomMeeting());
168 }
169 if ($oldEventPeriod->getLessonSpace()) {
170 $eventPeriod->setLessonSpace($oldEventPeriod->getLessonSpace());
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