<?php
/**
* Elements DeMI
*
* This source file is available under the elements DeMI license version 1
*
* @copyright Copyright (c) elements.at New Media Solutions GmbH (https://www.elements.at/)
* @license elements DeMI Lizenz Version 1 (https://www.elements.at/de/demi-lizenz)
*/
namespace Elements\Bundle\DemiFrontendBundle\Controller;
use Carbon\Carbon;
use Elements\Bundle\CmsToolsBundle\Tool\Helper\ElementsCustomDateFormat;
use Elements\Demi\Frontend\Service\Configuration;
use Elements\Demi\Accommodation\Search\Parameter;
use Elements\Bundle\DemiFrontendBundle\Service\DemiUrl;
use Elements\Demi\Model\AccommodationServiceProvider;
use Elements\Demi\Model\Event;
use Elements\Demi\Model\Infrastructure;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\DemiAccommodationServiceProvider;
use Pimcore\Model\DataObject\DemiConfig;
use Pimcore\Model\DataObject\DemiEvent;
use Pimcore\Model\DataObject\DemiInfrastructure;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
abstract class AbstractDemiController extends FrontendController
{
protected ?ElementsCustomDateFormat $elementsCustomDateFormat;
protected ?Configuration $configuration;
protected string $testIp = '89.26.34.69';
public function redirectViaFeratelIdProperty(Request $request,Concrete $object, DemiUrl $demiUrl){
$feratelId = $object->getProperty("feratelId");
if(empty($feratelId)){
throw new NotFoundHttpException("feratel id missing for redirect");
}
$feratelId = trim($feratelId);
$targetRoute = "";
$configObject = $this->document->getProperty('demi_config');
$target=null;
$params=[];
if($object instanceof AccommodationServiceProvider){
$target = DemiAccommodationServiceProvider::getByFid($feratelId,1);
$targetRoute = "demi_acco_detail_page";
$params = [
'accommodation' => $target
];
} else if ($object instanceof Event){
$target = DemiEvent::getByFid($feratelId,1);
$targetRoute = "demi_event_detail";
$params = [
'path' => $configObject->getEventDetailPath(),
'name' => $target->getName(),
'id' => $target->getId()
];
} else if ($object instanceof Infrastructure) {
$target = DemiInfrastructure::getByFid($feratelId, 1);
$targetRoute = "demi_infrastructure_detail";
$params = [
'path' => $configObject->getInfrastructureDetailPath(),
'name' => $target->getName(),
'id' => $target->getId()
];
}
if($target != null && !$target->isPublished()){
$target = null;
}
if($target != null){
$correctPath = $demiUrl->__invoke($params, $targetRoute, true);
return $this->redirect($correctPath, 301);
} else {
throw new NotFoundHttpException("could not find a published target object with feratel fid ".$feratelId);
}
}
public function unavailableAction(Request $request, $unavailableType = null): Response
{
if ($this->document->getProperty('demi_unavailable_button')) {
$viewParams['buttonTarget'] = $this->document->getProperty('demi_unavailable_button');
}
if ($this->document->getProperty('demi_unavailable_button_text')) {
$viewParams['buttonText'] = $this->document->getProperty('demi_unavailable_button_text');
}
$viewParams['type'] = $unavailableType;
$this->addResponseHeader('X-Robots-Tag', 'noindex,nofollow');
$response = $this->renderTemplate('@ElementsDemiFrontend/Error/unavailable.html.twig', $viewParams);
$response->setStatusCode(404);
return $response;
}
public function forwardingAction(Request $request): Response
{
$forwardController = 'Elements\\Bundle\\DemiFrontendBundle\\Controller\\' . ucfirst($request->get('controller')) . 'Controller::' . $request->get('action') . 'Action';
return $this->forward($forwardController, $request->attributes->all(), $request->query->all());
}
public function setElementsCustomDateFormat(?ElementsCustomDateFormat $elementsCustomDateFormat): void
{
$this->elementsCustomDateFormat = $elementsCustomDateFormat;
}
public function setConfiguration(?Configuration $configuration): void
{
$this->configuration = $configuration;
}
protected function needsTimeStringRedirect(Request $request): array|bool|string
{
$from = $request->get('from');
$to = $request->get('to');
if (($from && is_numeric($from)) || ($to && is_numeric($to))) {
$uri = $request->getUri();
if ($from) {
$date = Carbon::createFromTimestamp((int) ($from / 1000))->setTime(0, 0);
$uri = str_replace('&from='.$from, '&from='.$this->elementsCustomDateFormat->dateToString($date), $uri);
}
if ($to) {
$date = Carbon::createFromTimestamp((int) ($to / 1000))->setTime(0, 0);
$uri = str_replace('&to='.$to, '&to='.$this->elementsCustomDateFormat->dateToString($date), $uri);
}
return $uri;
}
return false;
}
protected function getAlternativeSearchParams(Parameter $params, int $extendFrom = 3, int $extendTo = 3): Parameter
{
$alternativeParams = clone $params;
$to = Carbon::createFromTimestamp($params->getDateTo()?->getTimestamp());
$from = Carbon::createFromTimestamp($params->getDateFrom()?->getTimestamp());
$diffInDays = $to->diffInDays($from);
$diffFromToday = Carbon::now()->diffInDays($from, false);
// if date is in the past
if ($diffFromToday < 0) {
$diffFromToday = abs($diffFromToday);
$from->addDays($diffFromToday);
$to->addDays($diffFromToday);
}
// Subtract 3 days max from check-in date and add 3 days to check-out date
$subDaysFrom = min($extendFrom, $diffFromToday);
$addDaysTo = $extendTo;
$newFrom = $from->subDays($subDaysFrom);
$newTo = $to->addDays($addDaysTo);
$mappingArray = explode(',', $this->configuration->getVariableNightMappings());
$mappings = [];
foreach ($mappingArray as $item) {
$mappings[] = array_map(static function($element) {
return (int)$element;
}, explode('-', $item));
}
$nearestNights = -1;
$mappingsCount = count($mappings);
// Find nearest
foreach ($mappings as $i => $iValue) {
if ($iValue[0] <= $diffInDays && $iValue[1] >= $diffInDays) {
$nearestNights = $i;
break;
}
}
if ($nearestNights >= $mappingsCount || $nearestNights === -1) {
$startNight = ($diffInDays - 1);
$endNight = ($diffInDays + 1);
} else {
[$startNight, $endNight] = $mappings[$nearestNights];
}
$nights = [];
for ($i = $startNight; $i <= $endNight; $i++) {
$nights[] = $i;
}
$alternativeParams->setDateFrom($newFrom);
$alternativeParams->setDateTo($newTo);
$alternativeParams->setNights($nights);
return $alternativeParams;
}
}