<?php
/**
* Created by Elements.at New Media Solutions GmbH
*
*/
namespace App\Controller;
use App\Twig\ConfigHelper;
use Pimcore\Model\DataObject\MascusProduct;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ProductMascusController extends AbstractController
{
/**
*
* @param Request $request
*
* @return JsonResponse|Response
*
* @throws \Exception
*/
public function overviewAction(Request $request, ConfigHelper $configHelper)
{
$siteConfig = $configHelper->getSiteConfig();
$products = new MascusProduct\Listing();
$products->addConditionParam("name != '' AND name IS NOT NULL");
if ($siteConfig) {
$products->addConditionParam('config LIKE :config', ['config' => '%,' . $siteConfig->getId() . ',%']);
}
$folderPath = PIMCORE_PRIVATE_VAR . '/cache-product-filter-data/mascus';
$filePath = $folderPath . '/document-' . $this->document->getId() . '.json';
if (!file_exists($folderPath)) {
if (!mkdir($folderPath, 0777, true) && !is_dir($folderPath)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $folderPath));
}
}
if (file_exists($filePath) && filemtime($filePath) > strtotime('-1 hour')) {
$cacheData = json_decode(file_get_contents($filePath), true);
}
else {
$categories = [];
$brands = [];
$countries = [];
$workingHourMin = 1000000000;
$workingHourMax = 1;
$yearOfManufactureMin = 1950;
$yearOfManufactureMax = 1;
foreach ($products as $product) {
$categories[$product->getCategory()->getId()] = [
'id' => $product->getCategory()->getId(),
'title' => $product->getCategory()->getTitle(),
'displayTitle' => $product->getCategory()->getDisplayTitle(),
];
$brands[$product->getBrand()->getId()] = [
'id' => $product->getBrand()->getId(),
'title' => $product->getBrand()->getInternalBrandName(),
'displayTitle' => $product->getBrand()->getTitle(),
];
$countries[$product->getCountry()] = $product->getCountry();
if ($product->getMeterReadOutHours() < $workingHourMin) {
$workingHourMin = $product->getMeterReadOutHours();
}
if ($product->getMeterReadOutHours() > $workingHourMax) {
$workingHourMax = $product->getMeterReadOutHours();
}
if ($product->getYearOfManufacture() < $yearOfManufactureMin) {
$yearOfManufactureMin = $product->getYearOfManufacture();
}
if ($product->getYearOfManufacture() > $yearOfManufactureMax) {
$yearOfManufactureMax = $product->getYearOfManufacture();
}
}
uasort($categories, function ($a, $b) {
if ($a['displayTitle'] && $b['displayTitle']) {
return strcasecmp($a['displayTitle'], $b['displayTitle']);
} elseif ($a['title'] && $b['title']) {
return strcasecmp($a['title'], $b['title']);
} else {
return 0;
}
});
uasort($brands, function ($a, $b) {
if (strpos($a['title'], '[') === 0 && strpos($b['title'], '[') !== 0) {
return 1;
} elseif (strpos($b['title'], '[') === 0 && strpos($a['title'], '[') !== 0) {
return -1;
} elseif ($a['displayTitle'] && $b['displayTitle']) {
return strcasecmp($a['displayTitle'], $b['displayTitle']);
} elseif ($a['title'] && $b['title']) {
return strcasecmp($a['title'], $b['title']);
} else {
return 0;
}
});
asort($countries);
$cacheData = [
'categories' => $categories,
'brands' => $brands,
'countries' => $countries,
'workingHourMin' => $workingHourMin,
'workingHourMax' => $workingHourMax,
'yearOfManufactureMin' => $yearOfManufactureMin,
'yearOfManufactureMax' => $yearOfManufactureMax,
];
file_put_contents($filePath, json_encode($cacheData));
}
$preselectCountry = '';
$countryFromConfig = $siteConfig->getCountry();
if ($request->get('categories', [])) {
$products->addConditionParam('category__id IN (' . implode(',', $request->get('categories', [])) . ')');
}
if ($request->get('brands', [])) {
$products->addConditionParam('brand__id IN (' . implode(',', $request->get('brands', [])) . ')');
}
if ($request->get('countries', [])) {
$orCondition = [];
foreach ($request->get('countries', []) as $country) {
$orCondition[] = 'country = "' . $country . '"';
}
if (!empty($orCondition)) {
$products->addConditionParam('(' . implode(' OR ', $orCondition) . ')');
}
} else {
$productsClone = clone $products;
$productsClone->addConditionParam('country = "' . $countryFromConfig . '"');
if (count($productsClone->getData())) {
$products->addConditionParam('country = "' . $countryFromConfig . '"');
$preselectCountry = $countryFromConfig;
}
}
if (!is_null($request->get('workingHoursMin'))) {
$products->addConditionParam('meterReadOutHours >= :workingHoursMin OR meterReadOutHours IS NULL', ['workingHoursMin' => $request->get('workingHoursMin')]);
}
if (!is_null($request->get('workingHoursMax'))) {
$products->addConditionParam('meterReadOutHours <= :workingHoursMax OR meterReadOutHours IS NULL', ['workingHoursMax' => $request->get('workingHoursMax')]);
}
if (!is_null($request->get('yearOfManufactureMin'))) {
$products->addConditionParam('yearOfManufacture >= :yearOfManufactureMin OR yearOfManufacture IS NULL', ['yearOfManufactureMin' => $request->get('yearOfManufactureMin')]);
}
if (!is_null($request->get('yearOfManufactureMax'))) {
$products->addConditionParam('yearOfManufacture <= :yearOfManufactureMax OR yearOfManufacture IS NULL', ['yearOfManufactureMax' => $request->get('yearOfManufactureMax')]);
}
if ($request->get('q')) {
$products->addConditionParam('name LIKE :search OR model LIKE :search OR otherInformation LIKE :search', ['search' => '%' . $request->get('q') . '%']);
}
$returnArray = [
'products' => $products,
'categories' => $cacheData['categories'],
'brands' => $cacheData['brands'],
'countries' => $cacheData['countries'],
'workingHourMin' => $cacheData['workingHourMin'],
'workingHourMax' => $cacheData['workingHourMax'],
'yearOfManufactureMin' => $cacheData['yearOfManufactureMin'],
'yearOfManufactureMax' => $cacheData['yearOfManufactureMax'],
'preselectCountry' => $preselectCountry,
];
if ($request->isXmlHttpRequest() && $request->get('ajax')) {
return $this->json([
'success' => true,
'html' => $this->render('includes/product/product-content.html.twig', $returnArray)->getContent(),
]);
}
return $this->renderTemplate('productMascus/overview.html.twig', $returnArray);
}
/**
*
* @param Request $request
*
* @return Response
*/
public function detailAction(Request $request, MascusProduct $id): Response
{
$product = $id;
return $this->renderTemplate('productMascus/detail.html.twig', [
'product' => $product,
]);
}
}