src/Controller/ProductMascusController.php line 26

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by Elements.at New Media Solutions GmbH
  4.  *
  5.  */
  6. namespace App\Controller;
  7. use App\Twig\ConfigHelper;
  8. use Pimcore\Model\DataObject\MascusProduct;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. class ProductMascusController extends AbstractController
  13. {
  14.     /**
  15.      *
  16.      * @param Request $request
  17.      *
  18.      * @return JsonResponse|Response
  19.      *
  20.      * @throws \Exception
  21.      */
  22.     public function overviewAction(Request $requestConfigHelper $configHelper)
  23.     {
  24.         $siteConfig $configHelper->getSiteConfig();
  25.         $products = new MascusProduct\Listing();
  26.         $products->addConditionParam("name != '' AND name IS NOT NULL");
  27.         if ($siteConfig) {
  28.             $products->addConditionParam('config LIKE :config', ['config' => '%,' $siteConfig->getId() . ',%']);
  29.         }
  30.         $folderPath PIMCORE_PRIVATE_VAR '/cache-product-filter-data/mascus';
  31.         $filePath $folderPath '/document-' $this->document->getId() . '.json';
  32.         if (!file_exists($folderPath)) {
  33.             if (!mkdir($folderPath0777true) && !is_dir($folderPath)) {
  34.                 throw new \RuntimeException(sprintf('Directory "%s" was not created'$folderPath));
  35.             }
  36.         }
  37.         if (file_exists($filePath) && filemtime($filePath) > strtotime('-1 hour')) {
  38.             $cacheData json_decode(file_get_contents($filePath), true);
  39.         }
  40.         else {
  41.             $categories = [];
  42.             $brands = [];
  43.             $countries = [];
  44.             $workingHourMin 1000000000;
  45.             $workingHourMax 1;
  46.             $yearOfManufactureMin 1950;
  47.             $yearOfManufactureMax 1;
  48.             foreach ($products as $product) {
  49.                 $categories[$product->getCategory()->getId()] = [
  50.                     'id' => $product->getCategory()->getId(),
  51.                     'title' => $product->getCategory()->getTitle(),
  52.                     'displayTitle' => $product->getCategory()->getDisplayTitle(),
  53.                 ];
  54.                 $brands[$product->getBrand()->getId()] = [
  55.                     'id' => $product->getBrand()->getId(),
  56.                     'title' => $product->getBrand()->getInternalBrandName(),
  57.                     'displayTitle' => $product->getBrand()->getTitle(),
  58.                 ];
  59.                 $countries[$product->getCountry()] = $product->getCountry();
  60.                 if ($product->getMeterReadOutHours() < $workingHourMin) {
  61.                     $workingHourMin $product->getMeterReadOutHours();
  62.                 }
  63.                 if ($product->getMeterReadOutHours() > $workingHourMax) {
  64.                     $workingHourMax $product->getMeterReadOutHours();
  65.                 }
  66.                 if ($product->getYearOfManufacture() < $yearOfManufactureMin) {
  67.                     $yearOfManufactureMin $product->getYearOfManufacture();
  68.                 }
  69.                 if ($product->getYearOfManufacture() > $yearOfManufactureMax) {
  70.                     $yearOfManufactureMax $product->getYearOfManufacture();
  71.                 }
  72.             }
  73.             uasort($categories, function ($a$b) {
  74.                 if ($a['displayTitle'] && $b['displayTitle']) {
  75.                     return strcasecmp($a['displayTitle'], $b['displayTitle']);
  76.                 } elseif ($a['title'] && $b['title']) {
  77.                     return strcasecmp($a['title'], $b['title']);
  78.                 } else {
  79.                     return 0;
  80.                 }
  81.             });
  82.             uasort($brands, function ($a$b) {
  83.                 if (strpos($a['title'], '[') === && strpos($b['title'], '[') !== 0) {
  84.                     return 1;
  85.                 } elseif (strpos($b['title'], '[') === && strpos($a['title'], '[') !== 0) {
  86.                     return -1;
  87.                 } elseif ($a['displayTitle'] && $b['displayTitle']) {
  88.                     return strcasecmp($a['displayTitle'], $b['displayTitle']);
  89.                 } elseif ($a['title'] && $b['title']) {
  90.                     return strcasecmp($a['title'], $b['title']);
  91.                 } else {
  92.                     return 0;
  93.                 }
  94.             });
  95.             asort($countries);
  96.             $cacheData = [
  97.                 'categories' => $categories,
  98.                 'brands' => $brands,
  99.                 'countries' => $countries,
  100.                 'workingHourMin' => $workingHourMin,
  101.                 'workingHourMax' => $workingHourMax,
  102.                 'yearOfManufactureMin' => $yearOfManufactureMin,
  103.                 'yearOfManufactureMax' => $yearOfManufactureMax,
  104.             ];
  105.             file_put_contents($filePathjson_encode($cacheData));
  106.         }
  107.         $preselectCountry '';
  108.         $countryFromConfig $siteConfig->getCountry();
  109.         if ($request->get('categories', [])) {
  110.             $products->addConditionParam('category__id IN (' implode(','$request->get('categories', [])) . ')');
  111.         }
  112.         if ($request->get('brands', [])) {
  113.             $products->addConditionParam('brand__id IN (' implode(','$request->get('brands', [])) . ')');
  114.         }
  115.         if ($request->get('countries', [])) {
  116.             $orCondition = [];
  117.             foreach ($request->get('countries', []) as $country) {
  118.                 $orCondition[] = 'country = "' $country '"';
  119.             }
  120.             if (!empty($orCondition)) {
  121.                 $products->addConditionParam('(' implode(' OR '$orCondition) . ')');
  122.             }
  123.         } else {
  124.             $productsClone = clone $products;
  125.             $productsClone->addConditionParam('country = "' $countryFromConfig '"');
  126.             if (count($productsClone->getData())) {
  127.                 $products->addConditionParam('country = "' $countryFromConfig '"');
  128.                 $preselectCountry $countryFromConfig;
  129.             }
  130.         }
  131.         if (!is_null($request->get('workingHoursMin'))) {
  132.             $products->addConditionParam('meterReadOutHours >= :workingHoursMin OR meterReadOutHours IS NULL', ['workingHoursMin' => $request->get('workingHoursMin')]);
  133.         }
  134.         if (!is_null($request->get('workingHoursMax'))) {
  135.             $products->addConditionParam('meterReadOutHours <= :workingHoursMax OR meterReadOutHours IS NULL', ['workingHoursMax' => $request->get('workingHoursMax')]);
  136.         }
  137.         if (!is_null($request->get('yearOfManufactureMin'))) {
  138.             $products->addConditionParam('yearOfManufacture >= :yearOfManufactureMin OR yearOfManufacture IS NULL', ['yearOfManufactureMin' => $request->get('yearOfManufactureMin')]);
  139.         }
  140.         if (!is_null($request->get('yearOfManufactureMax'))) {
  141.             $products->addConditionParam('yearOfManufacture <= :yearOfManufactureMax OR yearOfManufacture IS NULL', ['yearOfManufactureMax' => $request->get('yearOfManufactureMax')]);
  142.         }
  143.         if ($request->get('q')) {
  144.             $products->addConditionParam('name LIKE :search OR model LIKE :search OR otherInformation LIKE :search', ['search' => '%' $request->get('q') . '%']);
  145.         }
  146.         $returnArray = [
  147.             'products' => $products,
  148.             'categories' => $cacheData['categories'],
  149.             'brands' => $cacheData['brands'],
  150.             'countries' => $cacheData['countries'],
  151.             'workingHourMin' => $cacheData['workingHourMin'],
  152.             'workingHourMax' => $cacheData['workingHourMax'],
  153.             'yearOfManufactureMin' => $cacheData['yearOfManufactureMin'],
  154.             'yearOfManufactureMax' => $cacheData['yearOfManufactureMax'],
  155.             'preselectCountry' => $preselectCountry,
  156.         ];
  157.         if ($request->isXmlHttpRequest() && $request->get('ajax')) {
  158.             return $this->json([
  159.                 'success' => true,
  160.                 'html' => $this->render('includes/product/product-content.html.twig'$returnArray)->getContent(),
  161.             ]);
  162.         }
  163.         return $this->renderTemplate('productMascus/overview.html.twig'$returnArray);
  164.     }
  165.     /**
  166.      *
  167.      * @param Request $request
  168.      *
  169.      * @return Response
  170.      */
  171.     public function detailAction(Request $requestMascusProduct $id): Response
  172.     {
  173.         $product $id;
  174.         return $this->renderTemplate('productMascus/detail.html.twig', [
  175.             'product' => $product,
  176.         ]);
  177.     }
  178. }