PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.25
Booking for Appointments and Events Calendar – Amelia v1.2.25
2.4.9 2.4.8 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
AddEventCommand.php 7 years ago AddEventCommandHandler.php 2 years ago DeleteEventBookingCommand.php 7 years ago DeleteEventBookingCommandHandler.php 1 year ago DeleteEventCommand.php 7 years ago DeleteEventCommandHandler.php 2 years ago GetCalendarEventsCommand.php 4 years 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 7 years ago GetEventDeleteEffectCommandHandler.php 2 years ago GetEventsCommand.php 7 years ago GetEventsCommandHandler.php 1 year ago UpdateEventBookingCommand.php 7 years ago UpdateEventBookingCommandHandler.php 1 year ago UpdateEventCommand.php 7 years ago UpdateEventCommandHandler.php 1 year ago UpdateEventStatusCommand.php 7 years ago UpdateEventStatusCommandHandler.php 2 years ago
UpdateEventCommandHandler.php
272 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 ($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 $event */
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 = apply_filters('amelia_before_event_updated_filter', $eventData, $oldEvent ? $oldEvent->toArray() : null, $command->getField('applyGlobally'));
118
119 do_action('amelia_before_event_updated', $eventData, $oldEvent ? $oldEvent->toArray() : null, $command->getField('applyGlobally'));
120
121 try {
122 /** @var Event $event */
123 $event = $eventApplicationService->build($eventData);
124 } catch (Exception $e) {
125 $result->setResult(CommandResult::RESULT_ERROR);
126 $result->setMessage($e->getMessage());
127 $result->setData(['message' => $e->getMessage()]);
128 return $result;
129 }
130
131 /** @var DateTimeValue $newUntil */
132 $newUntil = $event->getRecurring()
133 ? $event->getRecurring()->getUntil()->getValue()->setTime(0, 0, 0)
134 : null;
135
136 /** @var DateTimeValue $oldUntil */
137 $oldUntil = $oldEvent->getRecurring()
138 ? $oldEvent->getRecurring()->getUntil()->getValue()->setTime(0, 0, 0)
139 : null;
140
141 if ($oldEvent->getRecurring() &&
142 $event->getRecurring() &&
143 (
144 $newUntil < $oldUntil ||
145 $event->getRecurring()->getCycle()->getValue() !== $oldEvent->getRecurring()->getCycle()->getValue()
146 )
147 ) {
148 $result->setResult(CommandResult::RESULT_ERROR);
149 $result->setMessage('Could not update event');
150
151 return $result;
152 }
153
154 $event->setBookings($oldEvent->getBookings());
155
156 /** @var EventPeriod $oldEventPeriod */
157 foreach ($oldEvent->getPeriods()->getItems() as $oldEventPeriod) {
158 /** @var EventPeriod $eventPeriod */
159 foreach ($event->getPeriods()->getItems() as $eventPeriod) {
160 if ($eventPeriod->getId() &&
161 $oldEventPeriod->getId()->getValue() === $eventPeriod->getId()->getValue()
162 ) {
163 if ($oldEventPeriod->getZoomMeeting()) {
164 $eventPeriod->setZoomMeeting($oldEventPeriod->getZoomMeeting());
165 }
166 if ($oldEventPeriod->getLessonSpace()) {
167 $eventPeriod->setLessonSpace($oldEventPeriod->getLessonSpace());
168 }
169 }
170 }
171 }
172
173 $eventRepository->beginTransaction();
174
175 try {
176 $parsedEvents = $eventApplicationService->update(
177 $oldEvent,
178 $event,
179 $command->getField('applyGlobally')
180 );
181 } catch (QueryExecutionException $e) {
182 $eventRepository->rollback();
183 throw $e;
184 }
185
186 $eventRepository->commit();
187
188 do_action('amelia_after_event_updated', $event ? $event->toArray() : null, $oldEvent ? $oldEvent->toArray() : null, $command->getField('applyGlobally'));
189
190 $providersRemoved = array_udiff(
191 $oldEvent->getProviders()->getItems(),
192 $event->getProviders()->getItems(),
193 function ($a, $b) {
194 if ($a->getId()->getValue() == $b->getId()->getValue()) {
195 return 0;
196 } else {
197 return ($a->getId()->getValue() < $b->getId()->getValue() ? -1 : 1);
198 }
199 }
200 );
201
202 $providersAdded = array_udiff(
203 $event->getProviders()->getItems(),
204 $oldEvent->getProviders()->getItems(),
205 function ($a, $b) {
206 if ($a->getId()->getValue() == $b->getId()->getValue()) {
207 return 0;
208 } else {
209 return ($a->getId()->getValue() < $b->getId()->getValue() ? -1 : 1);
210 }
211 }
212 );
213
214 $newInfo = ($event->getDescription() ? $event->getDescription()->getValue() : null) !==
215 ($oldEvent->getDescription() ? $oldEvent->getDescription()->getValue() : null) ||
216 $event->getName()->getValue() !== $oldEvent->getName()->getValue();
217
218 $zoomUserChanged = ($event->getZoomUserId() ? $event->getZoomUserId()->getValue() : null) !==
219 ($oldEvent->getZoomUserId() ? $oldEvent->getZoomUserId()->getValue() : null);
220
221 $zoomUsersLicenced = false;
222
223 if ($oldEvent->getZoomUserId() && $event->getZoomUserId() && $zoomUserChanged) {
224 /** @var AbstractZoomApplicationService $zoomService */
225 $zoomService = $this->container->get('application.zoom.service');
226
227 $zoomUserType = 0;
228 $zoomOldUserType = 0;
229 $zoomResult = $zoomService->getUsers();
230 if (!(isset($zoomResult['code']) && $zoomResult['code'] === 124) &&
231 !($zoomResult['users'] === null && isset($zoomResult['message']))) {
232 $zoomUsers = $zoomResult['users'];
233 foreach ($zoomUsers as $key => $val) {
234 if ($val['id'] === $event->getZoomUserId()->getValue()) {
235 $zoomUserType = $val['type'];
236 }
237 if ($val['id'] === $oldEvent->getZoomUserId()->getValue()) {
238 $zoomOldUserType = $val['type'];
239 }
240 }
241 }
242 if ($zoomOldUserType > 1 && $zoomUserType > 1) {
243 $zoomUsersLicenced = true;
244 }
245 }
246
247 $organizerChanged = ($event->getOrganizerId() ? $event->getOrganizerId()->getValue() : null)
248 !== ($oldEvent->getOrganizerId() ? $oldEvent->getOrganizerId()->getValue() : null);
249
250 $result->setResult(CommandResult::RESULT_SUCCESS);
251 $result->setMessage('Successfully updated event.');
252 $result->setData(
253 [
254 Entities::EVENTS => $parsedEvents,
255 'zoomUserChanged' => $zoomUserChanged && $event->getZoomUserId() ? $event->getZoomUserId()->getValue() : null,
256 'zoomUsersLicenced' => $zoomUsersLicenced,
257 'newInfo' => $newInfo ? [
258 'name' => $event->getName(),
259 'description' => $event->getDescription()
260 ] : null,
261 'newProviders' => $providersAdded,
262 'removeProviders' => $providersRemoved,
263 'organizerChanged' => $organizerChanged,
264 'newOrganizer' => $event->getOrganizerId() ? $event->getOrganizerId()->getValue() : null,
265 'notifyParticipants' => $command->getField('notifyParticipants')
266 ]
267 );
268
269 return $result;
270 }
271 }
272