<?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\Demi\Frontend\Service\Configuration;
use Elements\Bundle\DemiFrontendBundle\Service\DemiFrontendHelper;
use Elements\Demi\AbstractObject;
use Elements\Demi\Model\AccommodationServiceProvider;
use Elements\Demi\Model\HousePackageContainer;
use Elements\Demi\Model\HousePackageMaster;
use Elements\Demi\Model\Package;
use Elements\Demi\Model\ShopItem;
use Exception;
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject\DemiShopItem;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class ImageController extends AbstractDemiController
{
public function __construct(protected DemiFrontendHelper $frontendHelper, Configuration $demiConfigurationHelper)
{
$this->setConfiguration($demiConfigurationHelper);
}
public function getImageAction(Request $request): BinaryFileResponse
{
$fromDate = $image = null;
$id = $request->get('objectId');
$object = AbstractObject::getById($id);
if ($request->get('fallbackId')) {
$fallback = AbstractObject::getById($request->get('fallbackId'));
}
$thumbnail = $request->get('thumbnail');
$type = null;
if(!empty($object)){
$type = $this->frontendHelper->getAccoImagesTypesAsArray($object);
}
if (!empty($dateFromTimestamp = $request->get('dateFrom'))) {
$fromDate = new Carbon();
$fromDate->setTimestamp($dateFromTimestamp);
}
if ($imageId = $request->get('imageId')) {
$image = Asset\Image::getById($imageId);
} elseif ($object instanceof AccommodationServiceProvider || $object instanceof HousePackageMaster || $object instanceof HousePackageContainer ||
$object instanceof ShopItem) {
$image = $object->getFirstImage($type, $fromDate);
}
if (!$image instanceof Asset\Image) {
if ($fallback && $fallback->getFirstImage($type, $fromDate)) {
$image = $fallback->getFirstImage($type, $fromDate);
} else {
$image = $this->configuration->getTeaserFallbackImage();
}
}
if (!$image instanceof Asset\Image) {
throw new Exception('Fallback Image is not set or not type of Asset\Image');
}
$thumbnail = $image->getThumbnail($thumbnail, false);
$response = new BinaryFileResponse($thumbnail->exists() ? $thumbnail->getLocalFile() : $image->getLocalFile());
$response->setMaxAge(60 * 60 * 24);
$response->setExpires(Carbon::now()->addDay());
$response->headers->set('Pragma', '');
return $response;
}
public function getImagesAction(Request $request) : JsonResponse
{
$fromDate = $images = null;
$result = [];
$id = $request->get('objectId');
$object = AbstractObject::getById($id);
$thumbnail = $request->get('thumbnail');
$thumbSmall = $request->get('thumbSmall');
$type = $this->frontendHelper->getAccoImagesTypesAsArray($object);
if (!empty($dateFromTimestamp = $request->get('dateFrom'))) {
$fromDate = new Carbon();
$fromDate->setTimestamp($dateFromTimestamp);
}
if ($object instanceof AccommodationServiceProvider
|| $object instanceof HousePackageMaster
|| $object instanceof HousePackageContainer
|| $object instanceof Package) {
$images = $object->getImages($type, $fromDate, true);
} elseif ($object instanceof DemiShopItem) {
$images = $object->getImages();
}
if ($request->get('fallbackId')) {
$fallback = AbstractObject::getById($request->get('fallbackId'));
$fallbackImages = $fallback->getImages($type, $fromDate);
$images = array_merge($images, $fallbackImages);
}
if (is_array($images)) {
foreach ($images as $image) {
$result[] = [
'src' => $image->getThumbnail($thumbnail)->getPath(),
'thumb' => $image->getThumbnail($thumbSmall)->getPath(),
'subHtml' => $this->frontendHelper->getNameAndCopyrightForImage($image)
];
}
}
if (!is_array($images) || count($images) == 0) {
$result[] = [
'src' => $this->configuration->getTeaserFallbackImage()->getThumbnail($thumbnail)->getPath(),
'thumb' => $this->configuration->getTeaserFallbackImage()->getThumbnail($thumbSmall)->getPath()
];
}
return new JsonResponse($result);
}
}