EventDomainService.php
526 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Domain\Services\Booking; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Collection\Collection; |
| 6 | use AmeliaBooking\Domain\Entity\Booking\Event\Event; |
| 7 | use AmeliaBooking\Domain\Entity\Booking\Event\EventPeriod; |
| 8 | use AmeliaBooking\Domain\Entity\Booking\Event\EventTicket; |
| 9 | use AmeliaBooking\Domain\Entity\Gallery\GalleryImage; |
| 10 | use AmeliaBooking\Domain\Factory\Booking\Event\EventTicketFactory; |
| 11 | use AmeliaBooking\Domain\Factory\Gallery\GalleryImageFactory; |
| 12 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 13 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 14 | use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue; |
| 15 | use AmeliaBooking\Domain\ValueObjects\Json; |
| 16 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 17 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\IntegerValue; |
| 18 | use AmeliaBooking\Domain\ValueObjects\Recurring; |
| 19 | use AmeliaBooking\Domain\ValueObjects\String\Cycle; |
| 20 | use \DateTime as DateTime; |
| 21 | |
| 22 | /** |
| 23 | * Class EventDomainService |
| 24 | * |
| 25 | * @package AmeliaBooking\Domain\Services\Booking |
| 26 | */ |
| 27 | class EventDomainService |
| 28 | { |
| 29 | /** |
| 30 | * @param DateTime $periodStart |
| 31 | * @param DateTime $periodEnd |
| 32 | * |
| 33 | * @return void |
| 34 | */ |
| 35 | public function fixPeriod($periodStart, $periodEnd) |
| 36 | { |
| 37 | if ($periodStart->format('Y-m-d H:i') === $periodEnd->format('Y-m-d H:i')) { |
| 38 | $periodEnd->modify('60 minutes'); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param Recurring $recurring |
| 44 | * @param Collection $eventPeriods |
| 45 | * |
| 46 | * @return array |
| 47 | * |
| 48 | * @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException |
| 49 | */ |
| 50 | public function getRecurringEventsPeriods($recurring, $eventPeriods) |
| 51 | { |
| 52 | $recurringPeriods = []; |
| 53 | |
| 54 | if (!($recurring && $recurring->getCycle() && $recurring->getUntil())) { |
| 55 | return $recurringPeriods; |
| 56 | } |
| 57 | |
| 58 | $dateNew = null; |
| 59 | $modifyCycle = 'days'; |
| 60 | $modifyBaseValue = $recurring->getCycleInterval()->getValue(); |
| 61 | |
| 62 | switch ($recurring->getCycle()->getValue()) { |
| 63 | case (Cycle::DAILY): |
| 64 | $modifyCycle = 'days'; |
| 65 | break; |
| 66 | |
| 67 | case (Cycle::WEEKLY): |
| 68 | $modifyCycle = 'days'; |
| 69 | $modifyBaseValue = 7*$modifyBaseValue; |
| 70 | break; |
| 71 | |
| 72 | case (Cycle::MONTHLY): |
| 73 | if ($recurring->getMonthlyRepeat() === 'on') { |
| 74 | $repeatPeriod = $recurring->getMonthlyOnRepeat() . ' ' . $recurring->getMonthlyOnDay(); |
| 75 | } else { |
| 76 | $dateNew = $recurring->getMonthDate(); |
| 77 | } |
| 78 | $modifyCycle = 'months'; |
| 79 | break; |
| 80 | |
| 81 | case (Cycle::YEARLY): |
| 82 | $modifyCycle = 'years'; |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | $hasMoreRecurringPeriods = true; |
| 87 | |
| 88 | $recurringOrder = 1; |
| 89 | |
| 90 | $recurringOrderEvent = 2; |
| 91 | |
| 92 | while ($hasMoreRecurringPeriods) { |
| 93 | $periods = new Collection(); |
| 94 | |
| 95 | $modifyValue = $recurringOrder * $modifyBaseValue; |
| 96 | |
| 97 | $periodStartDate0 = DateTimeService::getCustomDateTimeObject($eventPeriods->getItem(0)->getPeriodStart()->getValue()->format('Y-m-d H:i:s')); |
| 98 | $periodEndDate0 = DateTimeService::getCustomDateTimeObject($eventPeriods->getItem(0)->getPeriodEnd()->getValue()->format('Y-m-d H:i:s')); |
| 99 | |
| 100 | if (isset($repeatPeriod)) { |
| 101 | $periodStart0 = $this->getNextPeriodStartDate($periodStartDate0, $modifyValue, $repeatPeriod); |
| 102 | if ($periodStart0 === null) { |
| 103 | if ($periodStart0 > $recurring->getUntil()->getValue()) { |
| 104 | break; |
| 105 | } |
| 106 | $recurringOrder++; |
| 107 | $recurringOrderEvent++; |
| 108 | continue; |
| 109 | } |
| 110 | $dayDifference = $periodStartDate0->diff($periodStart0); |
| 111 | } else if ($dateNew) { |
| 112 | $periodStartNew = DateTimeService::getCustomDateTimeObject($dateNew->getValue()->format('Y-m-d H:i:s')); |
| 113 | $dayDifference = $periodStartDate0->diff($periodStartNew); |
| 114 | } |
| 115 | |
| 116 | for ($i = 0; $i < count($eventPeriods->getItems()); $i++) { |
| 117 | $periodStartDate = DateTimeService::getCustomDateTimeObject($eventPeriods->getItem($i)->getPeriodStart()->getValue()->format('Y-m-d H:i:s')); |
| 118 | $periodEndDate = DateTimeService::getCustomDateTimeObject($eventPeriods->getItem($i)->getPeriodEnd()->getValue()->format('Y-m-d H:i:s')); |
| 119 | |
| 120 | if (isset($repeatPeriod)) { |
| 121 | if ($i === 0) { |
| 122 | $periodStart = $periodStart0; |
| 123 | $periodEnd = $periodEndDate0->add($dayDifference); |
| 124 | } else { |
| 125 | $periodStart = $periodStartDate->add($dayDifference); |
| 126 | $periodEnd = $periodEndDate->add($dayDifference); |
| 127 | } |
| 128 | } else { |
| 129 | $periodStart = $periodStartDate->modify("+{$modifyValue} {$modifyCycle}"); |
| 130 | $periodEnd = $periodEndDate->modify("+{$modifyValue} {$modifyCycle}"); |
| 131 | if ($dateNew) { |
| 132 | $periodStart = $periodStart->add($dayDifference); |
| 133 | $periodEnd = $periodEnd->add($dayDifference); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | $newEventPeriod = new EventPeriod(); |
| 138 | |
| 139 | $newEventPeriod->setPeriodStart(new DateTimeValue($periodStart)); |
| 140 | $newEventPeriod->setPeriodEnd(new DateTimeValue($periodEnd)); |
| 141 | |
| 142 | $periods->addItem($newEventPeriod); |
| 143 | |
| 144 | if ($periodStart > $recurring->getUntil()->getValue()) { |
| 145 | $hasMoreRecurringPeriods = false; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if ($hasMoreRecurringPeriods) { |
| 150 | $recurringPeriods[] = ['order' => $recurringOrderEvent, 'periods' => $periods]; |
| 151 | $recurringOrderEvent++; |
| 152 | $recurringOrder++; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return $recurringPeriods; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @param Collection $eventPeriods |
| 161 | * @param bool $setId |
| 162 | * |
| 163 | * @return Collection |
| 164 | * |
| 165 | * @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException |
| 166 | */ |
| 167 | public function getClonedEventPeriods($eventPeriods, $setId = false) |
| 168 | { |
| 169 | $clonedPeriods = new Collection(); |
| 170 | |
| 171 | /** @var EventPeriod $eventPeriod **/ |
| 172 | foreach ($eventPeriods->getItems() as $eventPeriod) { |
| 173 | $periodStart = DateTimeService::getCustomDateTimeObject( |
| 174 | $eventPeriod->getPeriodStart()->getValue()->format('Y-m-d H:i:s') |
| 175 | ); |
| 176 | |
| 177 | $periodEnd = DateTimeService::getCustomDateTimeObject( |
| 178 | $eventPeriod->getPeriodEnd()->getValue()->format('Y-m-d H:i:s') |
| 179 | ); |
| 180 | |
| 181 | $newEventPeriod = new EventPeriod(); |
| 182 | |
| 183 | $newEventPeriod->setPeriodStart(new DateTimeValue($periodStart)); |
| 184 | $newEventPeriod->setPeriodEnd(new DateTimeValue($periodEnd)); |
| 185 | |
| 186 | if ($eventPeriod->getZoomMeeting()) { |
| 187 | $newEventPeriod->setZoomMeeting($eventPeriod->getZoomMeeting()); |
| 188 | } |
| 189 | |
| 190 | if ($setId) { |
| 191 | $newEventPeriod->setId(new Id($eventPeriod->getId()->getValue())); |
| 192 | } |
| 193 | |
| 194 | $clonedPeriods->addItem($newEventPeriod); |
| 195 | } |
| 196 | |
| 197 | return $clonedPeriods; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /** |
| 202 | * @param Collection $eventTickets |
| 203 | * |
| 204 | * @return Collection |
| 205 | * |
| 206 | * @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException |
| 207 | */ |
| 208 | public function getClonedEventTickets($eventTickets) |
| 209 | { |
| 210 | $clonedTickets = new Collection(); |
| 211 | |
| 212 | /** @var EventTicket $eventTicket **/ |
| 213 | foreach ($eventTickets->getItems() as $eventTicket) { |
| 214 | $newEventTicket = EventTicketFactory::create($eventTicket->toArray()); |
| 215 | $clonedTickets->addItem($newEventTicket); |
| 216 | } |
| 217 | |
| 218 | return $clonedTickets; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | /** |
| 223 | * @param Event $followingEvent |
| 224 | * @param Event $originEvent |
| 225 | * @param Collection $clonedOriginEventPeriods |
| 226 | * |
| 227 | * @return boolean |
| 228 | * |
| 229 | * @throws \Slim\Exception\ContainerValueNotFoundException |
| 230 | * @throws \Interop\Container\Exception\ContainerException |
| 231 | * @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException |
| 232 | */ |
| 233 | public function buildFollowingEvent($followingEvent, $originEvent, $clonedOriginEventPeriods) |
| 234 | { |
| 235 | $followingEvent->setName($originEvent->getName()); |
| 236 | $followingEvent->setPrice($originEvent->getPrice()); |
| 237 | $followingEvent->setMaxCapacity($originEvent->getMaxCapacity()); |
| 238 | $followingEvent->setTags($originEvent->getTags()); |
| 239 | $followingEvent->setProviders($originEvent->getProviders()); |
| 240 | $followingEvent->setBringingAnyone($originEvent->getBringingAnyone()); |
| 241 | $followingEvent->setBookMultipleTimes($originEvent->getBookMultipleTimes()); |
| 242 | $followingEvent->setOrganizerId($originEvent->getOrganizerId()); |
| 243 | $followingEvent->setCustomPricing($originEvent->getCustomPricing()); |
| 244 | $followingEvent->setCloseAfterMin($originEvent->getCloseAfterMin()); |
| 245 | $followingEvent->setMaxCustomCapacity($originEvent->getMaxCustomCapacity()); |
| 246 | $followingEvent->setCloseAfterMinBookings($originEvent->getCloseAfterMinBookings()); |
| 247 | $followingEvent->setAggregatedPrice($originEvent->getAggregatedPrice()); |
| 248 | $followingEvent->setMaxExtraPeople($originEvent->getMaxExtraPeople()); |
| 249 | $followingEvent->setNotifyParticipants($originEvent->isNotifyParticipants()); |
| 250 | |
| 251 | if ($originEvent->getCustomPricing() && $originEvent->getCustomPricing()->getValue() && $followingEvent->getBookings()->length() === 0) { |
| 252 | $newEventTickets = new Collection(); |
| 253 | /** @var EventTicket $eventTicket */ |
| 254 | foreach ($originEvent->getCustomTickets()->getItems() as $ticketIndex => $eventTicket) { |
| 255 | if (!empty($followingEvent->getCustomTickets()->toArray()[$ticketIndex])) { |
| 256 | $editedTicket = EventTicketFactory::create($followingEvent->getCustomTickets()->toArray()[$ticketIndex]); |
| 257 | $editedTicket->setName($eventTicket->getName()); |
| 258 | $editedTicket->setEnabled($eventTicket->getEnabled()); |
| 259 | $editedTicket->setPrice($eventTicket->getPrice()); |
| 260 | if ($eventTicket->getTranslations()) { |
| 261 | $editedTicket->setTranslations($eventTicket->getTranslations()); |
| 262 | } |
| 263 | $editedTicket->setSpots($eventTicket->getSpots()); |
| 264 | $newEventTickets->addItem($editedTicket); |
| 265 | } else { |
| 266 | $newTicket = EventTicketFactory::create($eventTicket->toArray()); |
| 267 | $newTicket->setId(new Id(0)); |
| 268 | $newTicket->setEventId($followingEvent->getId() ? new Id($followingEvent->getId()->getValue()) : null); |
| 269 | $newTicket->setSold(new IntegerValue(0)); |
| 270 | $newTicket->setDateRanges(new Json('[]')); |
| 271 | $newEventTickets->addItem($newTicket); |
| 272 | } |
| 273 | } |
| 274 | $followingEvent->setCustomTickets($newEventTickets); |
| 275 | } |
| 276 | |
| 277 | |
| 278 | |
| 279 | if ($originEvent->getTranslations()) { |
| 280 | $followingEvent->setTranslations($originEvent->getTranslations()); |
| 281 | } |
| 282 | |
| 283 | if ($originEvent->getDeposit()) { |
| 284 | $followingEvent->setDeposit($originEvent->getDeposit()); |
| 285 | } |
| 286 | |
| 287 | if ($originEvent->getDepositPayment()) { |
| 288 | $followingEvent->setDepositPayment($originEvent->getDepositPayment()); |
| 289 | } |
| 290 | |
| 291 | if ($originEvent->getDepositPerPerson()) { |
| 292 | $followingEvent->setDepositPerPerson($originEvent->getDepositPerPerson()); |
| 293 | } |
| 294 | |
| 295 | $followingEventGallery = new Collection(); |
| 296 | |
| 297 | /** @var GalleryImage $image **/ |
| 298 | foreach ($originEvent->getGallery()->getItems() as $image) { |
| 299 | $followingEventGallery->addItem( |
| 300 | GalleryImageFactory::create( |
| 301 | [ |
| 302 | 'id' => null, |
| 303 | 'entityId' => $followingEvent->getId() ? $followingEvent->getId()->getValue() : null, |
| 304 | 'entityType' => $image->getEntityType()->getValue(), |
| 305 | 'pictureFullPath' => $image->getPicture()->getFullPath(), |
| 306 | 'pictureThumbPath' => $image->getPicture()->getThumbPath(), |
| 307 | 'position' => $image->getPosition()->getValue(), |
| 308 | ] |
| 309 | ) |
| 310 | ); |
| 311 | } |
| 312 | |
| 313 | $followingEvent->setGallery($followingEventGallery); |
| 314 | |
| 315 | if ($originEvent->getSettings()) { |
| 316 | $followingEvent->setSettings($originEvent->getSettings()); |
| 317 | } |
| 318 | |
| 319 | $followingEvent->setBookingOpens($originEvent->getBookingOpens()); |
| 320 | |
| 321 | $followingEvent->setBookingCloses($originEvent->getBookingCloses()); |
| 322 | |
| 323 | $followingEvent->setBookingOpensRec($originEvent->getBookingOpensRec()); |
| 324 | |
| 325 | $followingEvent->setBookingClosesRec($originEvent->getBookingClosesRec()); |
| 326 | |
| 327 | if ($originEvent->getLocationId()) { |
| 328 | $followingEvent->setLocationId($originEvent->getLocationId()); |
| 329 | } |
| 330 | |
| 331 | if ($originEvent->getCustomLocation()) { |
| 332 | $followingEvent->setCustomLocation($originEvent->getCustomLocation()); |
| 333 | } |
| 334 | |
| 335 | if ($originEvent->getTags()) { |
| 336 | $followingEvent->setTags($originEvent->getTags()); |
| 337 | } |
| 338 | |
| 339 | if ($originEvent->getDescription()) { |
| 340 | $followingEvent->setDescription($originEvent->getDescription()); |
| 341 | } |
| 342 | |
| 343 | if ($originEvent->getColor()) { |
| 344 | $followingEvent->setColor($originEvent->getColor()); |
| 345 | } |
| 346 | |
| 347 | if ($originEvent->getShow()) { |
| 348 | $followingEvent->setShow($originEvent->getShow()); |
| 349 | } |
| 350 | |
| 351 | if ($originEvent->getZoomUserId()) { |
| 352 | $followingEvent->setZoomUserId($originEvent->getZoomUserId()); |
| 353 | } |
| 354 | |
| 355 | $modifyCycle = 'days'; |
| 356 | $modifyBaseValue = $originEvent->getRecurring()->getCycleInterval()->getValue(); |
| 357 | $dateNew = null; |
| 358 | |
| 359 | switch ($originEvent->getRecurring()->getCycle()->getValue()) { |
| 360 | case (Cycle::DAILY): |
| 361 | $modifyCycle = 'days'; |
| 362 | break; |
| 363 | |
| 364 | case (Cycle::WEEKLY): |
| 365 | $modifyCycle = 'days'; |
| 366 | $modifyBaseValue = 7*$modifyBaseValue; |
| 367 | break; |
| 368 | |
| 369 | case (Cycle::MONTHLY): |
| 370 | if ($followingEvent->getRecurring()->getMonthlyRepeat() === 'on') { |
| 371 | $repeatPeriod = $followingEvent->getRecurring()->getMonthlyOnRepeat() . ' ' . $followingEvent->getRecurring()->getMonthlyOnDay(); |
| 372 | } else { |
| 373 | $dateNew = $followingEvent->getRecurring()->getMonthDate(); |
| 374 | } |
| 375 | $modifyCycle = 'months'; |
| 376 | break; |
| 377 | |
| 378 | case (Cycle::YEARLY): |
| 379 | $modifyCycle = 'years'; |
| 380 | break; |
| 381 | } |
| 382 | |
| 383 | $modifyValue = $modifyBaseValue * ($followingEvent->getRecurring()->getOrder()->getValue() - 1); |
| 384 | |
| 385 | $periodStartDate0 = DateTimeService::getCustomDateTimeObject($clonedOriginEventPeriods->getItem(0)->getPeriodStart()->getValue()->format('Y-m-d H:i:s')); |
| 386 | $periodEndDate0 = DateTimeService::getCustomDateTimeObject($clonedOriginEventPeriods->getItem(0)->getPeriodEnd()->getValue()->format('Y-m-d H:i:s')); |
| 387 | |
| 388 | if (isset($repeatPeriod)) { |
| 389 | $periodStart0 = $this->getNextPeriodStartDate($periodStartDate0, $modifyValue, $repeatPeriod); |
| 390 | if ($periodStart0 === null) { |
| 391 | // for added events |
| 392 | return false; |
| 393 | } |
| 394 | $dayDifference = $periodStartDate0->diff($periodStart0); |
| 395 | } else if ($dateNew) { |
| 396 | $thisPeriodStart = DateTimeService::getCustomDateTimeObject($originEvent->getPeriods()->getItem(0)->getPeriodStart()->getValue()->format('Y-m-d H:i:s')); |
| 397 | $periodStartNew = DateTimeService::getCustomDateTimeObject($dateNew->getValue()->format('Y-m-d H:i:s')); |
| 398 | $dayDifference = $thisPeriodStart->diff($periodStartNew); |
| 399 | } |
| 400 | |
| 401 | |
| 402 | /** @var EventPeriod $followingEventPeriod */ |
| 403 | foreach ($followingEvent->getPeriods()->getItems() as $key => $followingEventPeriod) { |
| 404 | if ($clonedOriginEventPeriods->keyExists($key)) { |
| 405 | /** @var EventPeriod $clonedOriginEventPeriod */ |
| 406 | $clonedOriginEventPeriod = $clonedOriginEventPeriods->getItem($key); |
| 407 | |
| 408 | $periodStartDate = DateTimeService::getCustomDateTimeObject($clonedOriginEventPeriod->getPeriodStart()->getValue()->format('Y-m-d H:i:s')); |
| 409 | $periodEndDate = DateTimeService::getCustomDateTimeObject($clonedOriginEventPeriod->getPeriodEnd()->getValue()->format('Y-m-d H:i:s')); |
| 410 | |
| 411 | if (isset($repeatPeriod)) { |
| 412 | if ($key === 0) { |
| 413 | $periodStart = $periodStart0; |
| 414 | $periodEnd = $periodEndDate0->add($dayDifference); |
| 415 | } else { |
| 416 | $periodStart = $periodStartDate->add($dayDifference); |
| 417 | $periodEnd = $periodEndDate->add($dayDifference); |
| 418 | } |
| 419 | } else { |
| 420 | $periodStart = $periodStartDate->modify("+{$modifyValue} {$modifyCycle}"); |
| 421 | $periodEnd = $periodEndDate->modify("+{$modifyValue} {$modifyCycle}"); |
| 422 | $toEndTime = $periodStart->diff($periodEnd); |
| 423 | if ($dateNew) { |
| 424 | $periodStart = DateTimeService::getCustomDateTimeObject($periodStart->format('Y-m-'. $periodStartNew->format('d') .' H:i:s')); |
| 425 | $periodStartClone = clone $periodStart; |
| 426 | $periodEnd = $periodStartClone->add($toEndTime); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | $followingEventPeriod->setPeriodStart(new DateTimeValue($periodStart)); |
| 431 | $followingEventPeriod->setPeriodEnd(new DateTimeValue($periodEnd)); |
| 432 | } else { |
| 433 | $followingEvent->getPeriods()->deleteItem($key); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /** @var EventPeriod $originEventPeriod */ |
| 438 | foreach ($originEvent->getPeriods()->getItems() as $key => $originEventPeriod) { |
| 439 | if (!$followingEvent->getPeriods()->keyExists($key)) { |
| 440 | |
| 441 | /** @var EventPeriod $followingEventPeriod */ |
| 442 | $newFollowingEventPeriod = new EventPeriod(); |
| 443 | |
| 444 | $newPeriodStart = DateTimeService::getCustomDateTimeObject( |
| 445 | $originEventPeriod->getPeriodStart()->getValue()->format('Y-m-d H:i:s') |
| 446 | ); |
| 447 | |
| 448 | $newPeriodEnd = DateTimeService::getCustomDateTimeObject( |
| 449 | $originEventPeriod->getPeriodEnd()->getValue()->format('Y-m-d H:i:s') |
| 450 | ); |
| 451 | |
| 452 | if (isset($repeatPeriod)) { |
| 453 | $periodStart = $newPeriodStart->add($dayDifference); |
| 454 | $periodEnd = $newPeriodEnd->add($dayDifference); |
| 455 | } else { |
| 456 | $periodStart = $newPeriodStart->modify("+{$modifyValue} {$modifyCycle}"); |
| 457 | $periodEnd = $newPeriodEnd->modify("+{$modifyValue} {$modifyCycle}"); |
| 458 | $toEndTime = $periodStart->diff($periodEnd); |
| 459 | if ($dateNew && $dayDifference && $dayDifference->format('%a') !== '0') { |
| 460 | $periodStart = $periodStart->add($dayDifference); |
| 461 | $periodStartClone = clone $periodStart; |
| 462 | $periodEnd = $periodStartClone->add($toEndTime); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | $newFollowingEventPeriod->setPeriodStart(new DateTimeValue($periodStart)); |
| 467 | |
| 468 | $newFollowingEventPeriod->setPeriodEnd(new DateTimeValue($periodEnd)); |
| 469 | |
| 470 | $newFollowingEventPeriod->setEventId(new Id($followingEvent->getId()->getValue())); |
| 471 | |
| 472 | $followingEvent->getPeriods()->addItem($newFollowingEventPeriod); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | return true; |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * @param Collection $eventPeriods |
| 481 | * @param bool $setId |
| 482 | * |
| 483 | * @return Collection |
| 484 | * |
| 485 | * @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException |
| 486 | */ |
| 487 | public function getShortcodeForEventList($container, $events) |
| 488 | { |
| 489 | |
| 490 | /** @var SettingsService $settingsService */ |
| 491 | $settingsService = $container->get('domain.settings.service'); |
| 492 | $dateFormat = $settingsService->getSetting('wordpress', 'dateFormat'); |
| 493 | |
| 494 | for ($i=0; $i < count($events); $i++) { |
| 495 | $dateString = explode(" ", $events[$i]['periods'][0]['periodStart'])[0]; |
| 496 | $newDate = date_i18n($dateFormat, strtotime($dateString)); |
| 497 | $events[$i]['formattedPeriodStart'] = $newDate; |
| 498 | } |
| 499 | return $events; |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * @param \DateTime $periodStartDate0 |
| 504 | * @param int $modifyValue |
| 505 | * @param string $repeatPeriod |
| 506 | * |
| 507 | * @return \DateTime |
| 508 | * |
| 509 | * @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException |
| 510 | * @throws \Exception |
| 511 | */ |
| 512 | public function getNextPeriodStartDate($periodStartDate0, $modifyValue, $repeatPeriod) |
| 513 | { |
| 514 | $month = (int)$periodStartDate0->format('m') + $modifyValue; |
| 515 | $year = (int)$periodStartDate0->format('Y'); |
| 516 | |
| 517 | $year += floor($month / 12); |
| 518 | $month -= 12 * floor($month / 12); |
| 519 | $time = (int)$periodStartDate0->format('H')*60 + (int)$periodStartDate0->format('i'); |
| 520 | |
| 521 | $periodStart0 = DateTimeService::getCustomDateTimeObject($repeatPeriod . " of $year-$month"); |
| 522 | return explode(' ', $repeatPeriod)[0] === 'fifth' |
| 523 | && (int)$periodStart0->format('m') !== (int)$month ? null : $periodStart0->add(new \DateInterval('PT' . $time . 'M')); |
| 524 | } |
| 525 | } |
| 526 |