src/Controller/SpecificationSheetController.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Helpers\HomePageFetcher;
  4. use App\Model\Product;
  5. use Pimcore\Model\Site;
  6. use Pimcore\Web2Print\Processor;
  7. use Psr\Log\LoggerInterface;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Process\Process;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class SpecificationSheetController extends DefaultFrontendController
  14. {
  15.     /**
  16.      * @param Request $request
  17.      * @Route("/{_locale}/product-download-sheet/{product}", name="product_spec_sheet")
  18.      */
  19.     public function singlePdfAction(Request $requeststring $_locale): Response
  20.     {
  21.         $relatedObject Product::getById((int)$request->get('product'0)) ?? false;
  22.         if (!$relatedObject) {
  23.             throw $this->createNotFoundException('Product not found');
  24.         }
  25.         $homePages HomePageFetcher::homePages();
  26.         $homePage end($homePages);
  27.         foreach ($homePages as $key => $value) {
  28.             if (strtolower($key) === strtolower($_locale)) {
  29.                 $homePage $value;
  30.                 break;
  31.             }
  32.         }
  33.         $currentSite Site::getByRootId($homePage->getId());
  34.         $domainName $currentSite === null
  35.          $request->getHttpHost()
  36.          : ($currentSite->getDomains()[0] ?: $currentSite->getMainDomain());
  37.         $html $this->renderView('pdfs/product-specification-sheet/product-specification-sheet_new.html.twig', [
  38.             'document' => $this->document,
  39.             'editmode' => $this->editmode,
  40.             'lang' => $_locale,
  41.             'domainName' => $domainName,
  42.             'productCategory' => $relatedObject->getCategory(),
  43.             'product' => $relatedObject,
  44.             //'rootCategory' => $relatedObject->getCategory()->getTopCategory(),
  45.             //'specifications' => [],
  46.             //'specifications' => array_slice($relatedObject->getSpecifications(), 0, 8),
  47.             'specifications' => $relatedObject->getSpecifications(), // array_slice($relatedObject->getSpecifications(), 0, 15),
  48.             //'printmode' => true,
  49.         ]);
  50.         if ($request->query->has('html')) {
  51.             return new Response($html);
  52.         }
  53.         return new Response(
  54.             Processor::getInstance()->getPdfFromString($html),
  55.             200,
  56.             ['Content-Type' => 'application/pdf']
  57.         );
  58.     }
  59.     /**
  60.      * @param Request $request
  61.      * @return string
  62.      * @Route("/{_locale}/product-catalog", name="product_catalog")
  63.      */
  64.     public function productCatalogAction(Request $requeststring $_locale)
  65.     {
  66.         $type $request->query->has('html') ? 'html' 'pdf';
  67.         /** @see GenerateProductCatalogCommand::execute() */
  68.         $filePath PIMCORE_SYSTEM_TEMP_DIRECTORY DIRECTORY_SEPARATOR "product-catalog_$_locale.$type";
  69.         if (!in_array($type, ['pdf''html']) || !file_exists($filePath)) {
  70.             throw $this->createNotFoundException();
  71.         }
  72.         $headers $type === 'pdf' ? ['Content-Type' => 'application/pdf'] : [];
  73.         return new Response(file_get_contents($filePath), 200$headers);
  74.     }
  75.     /**
  76.      * @param Request $request
  77.      * @param LoggerInterface $backgroundTasksLogger
  78.      * @return RedirectResponse
  79.      * @Route("/admin/generate-product-catalog", name="pimcore_admin_generate_product_catalog")
  80.      */
  81.     public function generateProductCatalogAction(Request $requestLoggerInterface $backgroundTasksLogger)
  82.     {
  83.         // Only allow admins to do this
  84.         if (!$user $this->getUser()) {
  85.             throw $this->createNotFoundException();
  86.         }
  87.         // See https://symfony.com/doc/5.4/components/process.html
  88.         $process = new Process(['php''bin/console''app:generate-product-catalog''--users=' $user->getId()], PIMCORE_PROJECT_ROOTtimeout60 10);
  89.         // See https://stackoverflow.com/a/69602727/3017716
  90.         $process->setOptions(['create_new_console' => true]);
  91.         $process->start(/*function ($type, $buffer) use($backgroundTasksLogger) {
  92.             //if ($type === Process::ERR) {
  93.             $backgroundTasksLogger->debug("PRODCAT ($type): " . $buffer);
  94.         }*/);
  95.         $route $request->headers->get('referer');
  96.         $fallback $this->generateUrl('pimcore_admin_index');
  97.         //TODO flash success
  98.         return $this->redirect($route ?: $fallback);
  99.     }
  100. }