<?php
namespace App\Controller;
use App\Helpers\HomePageFetcher;
use App\Model\Product;
use Pimcore\Model\Site;
use Pimcore\Web2Print\Processor;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Process\Process;
use Symfony\Component\Routing\Annotation\Route;
class SpecificationSheetController extends DefaultFrontendController
{
/**
* @param Request $request
* @Route("/{_locale}/product-download-sheet/{product}", name="product_spec_sheet")
*/
public function singlePdfAction(Request $request, string $_locale): Response
{
$relatedObject = Product::getById((int)$request->get('product', 0)) ?? false;
if (!$relatedObject) {
throw $this->createNotFoundException('Product not found');
}
$homePages = HomePageFetcher::homePages();
$homePage = end($homePages);
foreach ($homePages as $key => $value) {
if (strtolower($key) === strtolower($_locale)) {
$homePage = $value;
break;
}
}
$currentSite = Site::getByRootId($homePage->getId());
$domainName = $currentSite === null
? $request->getHttpHost()
: ($currentSite->getDomains()[0] ?: $currentSite->getMainDomain());
$html = $this->renderView('pdfs/product-specification-sheet/product-specification-sheet_new.html.twig', [
'document' => $this->document,
'editmode' => $this->editmode,
'lang' => $_locale,
'domainName' => $domainName,
'productCategory' => $relatedObject->getCategory(),
'product' => $relatedObject,
//'rootCategory' => $relatedObject->getCategory()->getTopCategory(),
//'specifications' => [],
//'specifications' => array_slice($relatedObject->getSpecifications(), 0, 8),
'specifications' => $relatedObject->getSpecifications(), // array_slice($relatedObject->getSpecifications(), 0, 15),
//'printmode' => true,
]);
if ($request->query->has('html')) {
return new Response($html);
}
return new Response(
Processor::getInstance()->getPdfFromString($html),
200,
['Content-Type' => 'application/pdf']
);
}
/**
* @param Request $request
* @return string
* @Route("/{_locale}/product-catalog", name="product_catalog")
*/
public function productCatalogAction(Request $request, string $_locale)
{
$type = $request->query->has('html') ? 'html' : 'pdf';
/** @see GenerateProductCatalogCommand::execute() */
$filePath = PIMCORE_SYSTEM_TEMP_DIRECTORY . DIRECTORY_SEPARATOR . "product-catalog_$_locale.$type";
if (!in_array($type, ['pdf', 'html']) || !file_exists($filePath)) {
throw $this->createNotFoundException();
}
$headers = $type === 'pdf' ? ['Content-Type' => 'application/pdf'] : [];
return new Response(file_get_contents($filePath), 200, $headers);
}
/**
* @param Request $request
* @param LoggerInterface $backgroundTasksLogger
* @return RedirectResponse
* @Route("/admin/generate-product-catalog", name="pimcore_admin_generate_product_catalog")
*/
public function generateProductCatalogAction(Request $request, LoggerInterface $backgroundTasksLogger)
{
// Only allow admins to do this
if (!$user = $this->getUser()) {
throw $this->createNotFoundException();
}
// See https://symfony.com/doc/5.4/components/process.html
$process = new Process(['php', 'bin/console', 'app:generate-product-catalog', '--users=' . $user->getId()], PIMCORE_PROJECT_ROOT, timeout: 60 * 10);
// See https://stackoverflow.com/a/69602727/3017716
$process->setOptions(['create_new_console' => true]);
$process->start(/*function ($type, $buffer) use($backgroundTasksLogger) {
//if ($type === Process::ERR) {
$backgroundTasksLogger->debug("PRODCAT ($type): " . $buffer);
}*/);
$route = $request->headers->get('referer');
$fallback = $this->generateUrl('pimcore_admin_index');
//TODO flash success
return $this->redirect($route ?: $fallback);
}
}