src/Elements/Bundle/DemiFrontendBundle/Controller/ImageController.php line 37

Open in your IDE?
  1. <?php
  2. /**
  3.  * Elements DeMI
  4.  *
  5.  * This source file is available under the elements DeMI license version 1
  6.  *
  7.  *  @copyright  Copyright (c) elements.at New Media Solutions GmbH (https://www.elements.at/)
  8.  *  @license    elements DeMI Lizenz Version 1 (https://www.elements.at/de/demi-lizenz)
  9.  */
  10. namespace Elements\Bundle\DemiFrontendBundle\Controller;
  11. use Carbon\Carbon;
  12. use Elements\Demi\Frontend\Service\Configuration;
  13. use Elements\Bundle\DemiFrontendBundle\Service\DemiFrontendHelper;
  14. use Elements\Demi\AbstractObject;
  15. use Elements\Demi\Model\AccommodationServiceProvider;
  16. use Elements\Demi\Model\HousePackageContainer;
  17. use Elements\Demi\Model\HousePackageMaster;
  18. use Elements\Demi\Model\Package;
  19. use Elements\Demi\Model\ShopItem;
  20. use Exception;
  21. use Pimcore\Model\Asset;
  22. use Pimcore\Model\DataObject\DemiShopItem;
  23. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  24. use Symfony\Component\HttpFoundation\JsonResponse;
  25. use Symfony\Component\HttpFoundation\Request;
  26. class ImageController extends AbstractDemiController
  27. {
  28.     public function __construct(protected DemiFrontendHelper $frontendHelperConfiguration $demiConfigurationHelper)
  29.     {
  30.         $this->setConfiguration($demiConfigurationHelper);
  31.     }
  32.     public function getImageAction(Request $request): BinaryFileResponse
  33.     {
  34.         $fromDate $image null;
  35.         $id $request->get('objectId');
  36.         $object AbstractObject::getById($id);
  37.         if ($request->get('fallbackId')) {
  38.             $fallback AbstractObject::getById($request->get('fallbackId'));
  39.         }
  40.         $thumbnail $request->get('thumbnail');
  41.         $type null;
  42.         if(!empty($object)){
  43.             $type $this->frontendHelper->getAccoImagesTypesAsArray($object);
  44.         }
  45.         if (!empty($dateFromTimestamp $request->get('dateFrom'))) {
  46.             $fromDate = new Carbon();
  47.             $fromDate->setTimestamp($dateFromTimestamp);
  48.         }
  49.         if ($imageId $request->get('imageId')) {
  50.             $image Asset\Image::getById($imageId);
  51.         } elseif ($object instanceof AccommodationServiceProvider || $object instanceof HousePackageMaster || $object instanceof HousePackageContainer ||
  52.             $object instanceof ShopItem) {
  53.             $image $object->getFirstImage($type$fromDate);
  54.         }
  55.         if (!$image instanceof Asset\Image) {
  56.             if ($fallback && $fallback->getFirstImage($type$fromDate)) {
  57.                 $image $fallback->getFirstImage($type$fromDate);
  58.             } else {
  59.                 $image $this->configuration->getTeaserFallbackImage();
  60.             }
  61.         }
  62.         if (!$image instanceof Asset\Image) {
  63.             throw new Exception('Fallback Image is not set or not type of Asset\Image');
  64.         }
  65.         $thumbnail $image->getThumbnail($thumbnailfalse);
  66.         $response = new BinaryFileResponse($thumbnail->exists() ? $thumbnail->getLocalFile() : $image->getLocalFile());
  67.         $response->setMaxAge(60 60 24);
  68.         $response->setExpires(Carbon::now()->addDay());
  69.         $response->headers->set('Pragma''');
  70.         return $response;
  71.     }
  72.     public function getImagesAction(Request $request) : JsonResponse
  73.     {
  74.         $fromDate $images null;
  75.         $result = [];
  76.         $id $request->get('objectId');
  77.         $object AbstractObject::getById($id);
  78.         $thumbnail $request->get('thumbnail');
  79.         $thumbSmall $request->get('thumbSmall');
  80.         $type $this->frontendHelper->getAccoImagesTypesAsArray($object);
  81.         if (!empty($dateFromTimestamp $request->get('dateFrom'))) {
  82.             $fromDate = new Carbon();
  83.             $fromDate->setTimestamp($dateFromTimestamp);
  84.         }
  85.         if ($object instanceof AccommodationServiceProvider
  86.             || $object instanceof HousePackageMaster
  87.             || $object instanceof HousePackageContainer
  88.             || $object instanceof Package) {
  89.             $images $object->getImages($type$fromDatetrue);
  90.         } elseif ($object instanceof  DemiShopItem) {
  91.             $images $object->getImages();
  92.         }
  93.         if ($request->get('fallbackId')) {
  94.             $fallback AbstractObject::getById($request->get('fallbackId'));
  95.             $fallbackImages $fallback->getImages($type$fromDate);
  96.             $images array_merge($images$fallbackImages);
  97.         }
  98.         if (is_array($images)) {
  99.             foreach ($images as $image) {
  100.                 $result[] = [
  101.                     'src' => $image->getThumbnail($thumbnail)->getPath(),
  102.                     'thumb' => $image->getThumbnail($thumbSmall)->getPath(),
  103.                     'subHtml' => $this->frontendHelper->getNameAndCopyrightForImage($image)
  104.                 ];
  105.             }
  106.         }
  107.         if (!is_array($images) || count($images) == 0) {
  108.             $result[] = [
  109.                 'src' => $this->configuration->getTeaserFallbackImage()->getThumbnail($thumbnail)->getPath(),
  110.                 'thumb' => $this->configuration->getTeaserFallbackImage()->getThumbnail($thumbSmall)->getPath()
  111.             ];
  112.         }
  113.         return new JsonResponse($result);
  114.     }
  115. }