PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.31
Booking for Appointments and Events Calendar – Amelia v1.2.31
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
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 2 years ago GetCalendarEventsCommand.php 1 year ago GetCalendarEventsCommandHandler.php 1 year ago GetEventBookingsCommand.php 1 year ago GetEventBookingsCommandHandler.php 1 year ago GetEventCommand.php 7 years ago GetEventCommandHandler.php 1 year ago GetEventDeleteEffectCommand.php 1 year ago GetEventDeleteEffectCommandHandler.php 1 year ago GetEventsCommand.php 1 year ago GetEventsCommandHandler.php 1 year ago UpdateEventBookingCommand.php 7 years ago UpdateEventBookingCommandHandler.php 1 year ago UpdateEventCommand.php 1 year ago UpdateEventCommandHandler.php 1 year ago UpdateEventStatusCommand.php 1 year ago UpdateEventStatusCommandHandler.php 1 year ago
UpdateEventCommandHandler.php
283 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 'applyGlobally'
41 ];
42
43 /**
44 * @param UpdateEventCommand $command
45 *
46 * @return CommandResult
47 * @throws ContainerValueNotFoundException
48 * @throws QueryExecutionException
49 * @throws InvalidArgumentException
50 * @throws AccessDeniedException
51 * @throws ContainerException
52 * @throws Exception
53 */
54 public function handle(UpdateEventCommand $command)
55 {
56 $result = new CommandResult();
57
58 $this->checkMandatoryFields($command);
59
60 $eventData = $command->getFields();
61
62 /** @var EventRepository $eventRepository */
63 $eventRepository = $this->container->get('domain.booking.event.repository');
64 /** @var EventApplicationService $eventApplicationService */
65 $eventApplicationService = $this->container->get('application.booking.event.service');
66 /** @var UserApplicationService $userAS */
67 $userAS = $this->getContainer()->get('application.user.service');
68 /** @var SettingsService $settingsDS */
69 $settingsDS = $this->container->get('domain.settings.service');
70 /** @var EntityApplicationService $entityService */
71 $entityService = $this->container->get('application.entity.service');
72
73 try {
74 /** @var AbstractUser $user */
75 $user = $command->getUserApplicationService()->authorization(
76 $command->getPage() === 'cabinet' ? $command->getToken() : null,
77 $command->getCabinetType()
78 );
79 } catch (AuthorizationException $e) {
80 $result->setResult(CommandResult::RESULT_ERROR);
81 $result->setData(
82 [
83 'reauthorize' => true
84 ]
85 );
86
87 return $result;
88 }
89
90 if (
91 $userAS->isCustomer($user) ||
92 (
93 $userAS->isProvider($user) && !$settingsDS->getSetting('roles', 'allowWriteEvents')
94 )
95 ) {
96 throw new AccessDeniedException('You are not allowed to update an event');
97 }
98
99 $entityService->removeMissingEntitiesForEvent($eventData);
100
101
102 /** @var Event $oldEvent */
103 $oldEvent = $eventApplicationService->getEventById(
104 $eventData['id'],
105 [
106 'fetchEventsPeriods' => true,
107 'fetchEventsTickets' => true,
108 'fetchEventsTags' => true,
109 'fetchEventsProviders' => true,
110 'fetchEventsImages' => true,
111 'fetchBookings' => true,
112 'fetchBookingsTickets' => true,
113 'fetchBookingsUsers' => true,
114 'fetchBookingsPayments' => true,
115 ]
116 );
117
118 $eventData =
119 apply_filters('amelia_before_event_updated_filter', $eventData, $oldEvent ? $oldEvent->toArray() : null, $command->getField('applyGlobally'));
120
121 do_action('amelia_before_event_updated', $eventData, $oldEvent ? $oldEvent->toArray() : null, $command->getField('applyGlobally'));
122
123 try {
124 /** @var Event $event */
125 $event = $eventApplicationService->build($eventData);
126 } catch (Exception $e) {
127 $result->setResult(CommandResult::RESULT_ERROR);
128 $result->setMessage($e->getMessage());
129 $result->setData(['message' => $e->getMessage()]);
130 return $result;
131 }
132
133 /** @var DateTimeValue $newUntil */
134 $newUntil = $event->getRecurring()
135 ? $event->getRecurring()->getUntil()->getValue()->setTime(0, 0, 0)
136 : null;
137
138 /** @var DateTimeValue $oldUntil */
139 $oldUntil = $oldEvent->getRecurring()
140 ? $oldEvent->getRecurring()->getUntil()->getValue()->setTime(0, 0, 0)
141 : null;
142
143 if (
144 $oldEvent->getRecurring() &&
145 $event->getRecurring() &&
146 (
147 $newUntil < $oldUntil ||
148 $event->getRecurring()->getCycle()->getValue() !== $oldEvent->getRecurring()->getCycle()->getValue()
149 )
150 ) {
151 $result->setResult(CommandResult::RESULT_ERROR);
152 $result->setMessage('Could not update event');
153
154 return $result;
155 }
156
157 $event->setBookings($oldEvent->getBookings());
158
159 /** @var EventPeriod $oldEventPeriod */
160 foreach ($oldEvent->getPeriods()->getItems() as $oldEventPeriod) {
161 /** @var EventPeriod $eventPeriod */
162 foreach ($event->getPeriods()->getItems() as $eventPeriod) {
163 if (
164 $eventPeriod->getId() &&
165 $oldEventPeriod->getId()->getValue() === $eventPeriod->getId()->getValue()
166 ) {
167 if ($oldEventPeriod->getZoomMeeting()) {
168 $eventPeriod->setZoomMeeting($oldEventPeriod->getZoomMeeting());
169 }
170 if ($oldEventPeriod->getLessonSpace()) {
171 $eventPeriod->setLessonSpace($oldEventPeriod->getLessonSpace());
172 }
173 }
174 }
175 }
176
177 $eventRepository->beginTransaction();
178
179 try {
180 $parsedEvents = $eventApplicationService->update(
181 $oldEvent,
182 $event,
183 $command->getField('applyGlobally')
184 );
185 } catch (QueryExecutionException $e) {
186 $eventRepository->rollback();
187 throw $e;
188 }
189
190 $eventRepository->commit();
191
192 do_action(
193 'amelia_after_event_updated',
194 $event ? $event->toArray() : null,
195 $oldEvent ? $oldEvent->toArray() : null,
196 $command->getField('applyGlobally')
197 );
198
199 $providersRemoved = array_udiff(
200 $oldEvent->getProviders()->getItems(),
201 $event->getProviders()->getItems(),
202 function ($a, $b) {
203 if ($a->getId()->getValue() == $b->getId()->getValue()) {
204 return 0;
205 } else {
206 return ($a->getId()->getValue() < $b->getId()->getValue() ? -1 : 1);
207 }
208 }
209 );
210
211 $providersAdded = array_udiff(
212 $event->getProviders()->getItems(),
213 $oldEvent->getProviders()->getItems(),
214 function ($a, $b) {
215 if ($a->getId()->getValue() == $b->getId()->getValue()) {
216 return 0;
217 } else {
218 return ($a->getId()->getValue() < $b->getId()->getValue() ? -1 : 1);
219 }
220 }
221 );
222
223 $newInfo = ($event->getDescription() ? $event->getDescription()->getValue() : null) !==
224 ($oldEvent->getDescription() ? $oldEvent->getDescription()->getValue() : null) ||
225 $event->getName()->getValue() !== $oldEvent->getName()->getValue();
226
227 $zoomUserChanged = ($event->getZoomUserId() ? $event->getZoomUserId()->getValue() : null) !==
228 ($oldEvent->getZoomUserId() ? $oldEvent->getZoomUserId()->getValue() : null);
229
230 $zoomUsersLicenced = false;
231
232 if ($oldEvent->getZoomUserId() && $event->getZoomUserId() && $zoomUserChanged) {
233 /** @var AbstractZoomApplicationService $zoomService */
234 $zoomService = $this->container->get('application.zoom.service');
235
236 $zoomUserType = 0;
237 $zoomOldUserType = 0;
238 $zoomResult = $zoomService->getUsers();
239 if (
240 !(isset($zoomResult['code']) && $zoomResult['code'] === 124) &&
241 !($zoomResult['users'] === null && isset($zoomResult['message']))
242 ) {
243 $zoomUsers = $zoomResult['users'];
244 foreach ($zoomUsers as $key => $val) {
245 if ($val['id'] === $event->getZoomUserId()->getValue()) {
246 $zoomUserType = $val['type'];
247 }
248 if ($val['id'] === $oldEvent->getZoomUserId()->getValue()) {
249 $zoomOldUserType = $val['type'];
250 }
251 }
252 }
253 if ($zoomOldUserType > 1 && $zoomUserType > 1) {
254 $zoomUsersLicenced = true;
255 }
256 }
257
258 $organizerChanged = ($event->getOrganizerId() ? $event->getOrganizerId()->getValue() : null)
259 !== ($oldEvent->getOrganizerId() ? $oldEvent->getOrganizerId()->getValue() : null);
260
261 $result->setResult(CommandResult::RESULT_SUCCESS);
262 $result->setMessage('Successfully updated event.');
263 $result->setData(
264 [
265 Entities::EVENTS => $parsedEvents,
266 'zoomUserChanged' => $zoomUserChanged && $event->getZoomUserId() ? $event->getZoomUserId()->getValue() : null,
267 'zoomUsersLicenced' => $zoomUsersLicenced,
268 'newInfo' => $newInfo ? [
269 'name' => $event->getName(),
270 'description' => $event->getDescription()
271 ] : null,
272 'newProviders' => $providersAdded,
273 'removeProviders' => $providersRemoved,
274 'organizerChanged' => $organizerChanged,
275 'newOrganizer' => $event->getOrganizerId() ? $event->getOrganizerId()->getValue() : null,
276 'notifyParticipants' => $command->getField('notifyParticipants')
277 ]
278 );
279
280 return $result;
281 }
282 }
283