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