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