vendor/millerdigital/eleq-pimcore/src/Controller/SanityController.php line 18

Open in your IDE?
  1. <?php
  2. namespace Millerdigital\EleqPimcore\Controller;
  3. use Millerdigital\EleqPimcore\Attribute\SanityApi;
  4. use Millerdigital\EleqPimcore\Formatter\SanityProductFormatter;
  5. use Pimcore\Model\Asset;
  6. use Pimcore\Model\DataObject\Product;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\StreamedResponse;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. use Symfony\Contracts\Translation\LocaleAwareInterface;
  14. #[SanityApi(validateApiKeytruerateLimitfalse)]
  15. class SanityController extends AbstractController
  16. {
  17.     private TranslatorInterface $translator;
  18.     public function __construct(
  19.         TranslatorInterface $translator
  20.     ) {
  21.         $this->translator $translator;
  22.     }
  23.     /**
  24.      * Fetch detailed information about a specific product by its slug.
  25.      * 
  26.      * Returns complete product details including environmental conditions, 
  27.      * application conditions, variants, documents, contact information, and related products.
  28.      */
  29.     #[Route('/api/sanity/product/{slug}'name'sanity_product'methods: ['GET'])]
  30.     public function fetchProduct(Request $requeststring $slug): Response
  31.     {
  32.         $language self::getRequestLanguage($request);
  33.         $request->setLocale($language);
  34.         if ($this->translator instanceof LocaleAwareInterface) {
  35.             $this->translator->setLocale($language);
  36.         }
  37.         $product Product::getBySlug($slug$language1);
  38.         if ($product && $product->getPath() == "/Products/" && $product->getShowOnWebsite($language)) {
  39.             $product SanityProductFormatter::formatProduct($product$this->translator$request);
  40.             return $this->json($product200, ["Content-Type" => "application/json"]);
  41.         }
  42.         throw $this->createNotFoundException();
  43.     }
  44.     
  45.     /**
  46.      * Fetch a list of products with pagination.
  47.      * 
  48.      * Returns a list of products with detailed information and total count.
  49.      */
  50.     #[Route('/api/sanity/products'name'sanity_products'methods: ['GET'])]
  51.     public function fetchProducts(Request $request): Response
  52.     {
  53.         // Get offset and limit from query parameters
  54.         $offset = (int)$request->query->get('offset'0);
  55.         $limit = (int)$request->query->get('limit'20);
  56.         $language self::getRequestLanguage($request);
  57.         $request->setLocale($language);
  58.         if ($this->translator instanceof LocaleAwareInterface) {
  59.             $this->translator->setLocale($language);
  60.         }
  61.         $productListing = new Product\Listing();
  62.         $productListing->setLocale($language);
  63.         $productListing->addConditionParam('o_path = ? AND ShowOnWebsite = ?', ['/Products/'1]);
  64.         // sort by Order product-property
  65.         $productListing->setOrderKey(
  66.             ['order''oo_id',]
  67.         );
  68.         $productListing->setOrder(
  69.             ['desc''asc']
  70.         );
  71.         
  72.         $products $productListing->getItems($offset$limit);
  73.         $products array_map(fn(Product $product) => SanityProductFormatter::formatProduct($product$this->translator$request), $products);
  74.         $count $productListing->count();
  75.         $result = [ 
  76.             'count' => $count,
  77.             'products' => $products
  78.         ];
  79.         
  80.         return $this->json($result200, ["Content-Type" => "application/json"]);
  81.     }
  82.     /**
  83.      * Get all document filenames.
  84.      * 
  85.      * Returns a JSON array of all available document filenames.
  86.      */
  87.     #[Route('/api/sanity/documents'name'sanity_documents'methods: ['GET'])]
  88.     public function fetchDocuments(): Response
  89.     {
  90.         $assetListing = new Asset\Listing();
  91.         $assetListing->addConditionParam('type = ?''document');
  92.         $assets $assetListing->load();
  93.         
  94.         $filenames array_map(fn(Asset $asset) => $asset->getFilename(), $assets);
  95.         
  96.         return $this->json($filenames200, ["Content-Type" => "application/json"]);
  97.     }
  98.     /**
  99.      * Download a document by filename.
  100.      * 
  101.      * Returns the document as a streamed response with appropriate headers.
  102.      */
  103.     #[Route('/api/sanity/document/{file}/download'name'sanity_document'methods: ['GET'])]
  104.     public function fetchDocument(string $file): Response
  105.     {
  106.         $assetListing = new Asset\Listing();
  107.         $assetListing->addConditionParam('filename = ?'$file);
  108.         $asset $assetListing->getItems(01)[0] ?? null;
  109.         
  110.         if (!$asset) {
  111.             throw $this->createNotFoundException();
  112.         }
  113.         $stream $asset->getStream();
  114.         return new StreamedResponse(function () use ($stream) {
  115.             fpassthru($stream);
  116.         }, 200, [
  117.             'Content-Type' => $asset->getMimeType(),
  118.             'Content-Disposition' => sprintf('attachment; filename="%s"'$asset->getFilename()),
  119.             'Content-Length' => $asset->getFileSize(),
  120.         ]);
  121.     }
  122.     private static function getRequestLanguage(Request $request): ?string
  123.     {
  124.         if ($language $request->query->get('lang')) {
  125.             return $language;
  126.         }
  127.         if ($language $request->getPreferredLanguage()) {
  128.             return substr($language0strrpos($language'_'));
  129.         }
  130.         return \Locale::getDefault();
  131.     }
  132. }