<?php
namespace App\Application\Controller;
use App\Admin\Document\Category;
use App\Admin\Document\Offer;
use App\Admin\Document\Page;
use App\Admin\Document\Product;
use App\Admin\Document\Property;
use App\Admin\Document\PropertyValue;
use App\Application\Cart\Cart;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use \Symfony\Component\Routing\Annotation\Route;
class CompareController extends AbstractController
{
/**
* @Route("/compare", name="compare")
*/
public function index(Request $request, Cart $cart)
{
if ($request->isMethod(Request::METHOD_POST)) {
$cart->clearCompareList($request->get('category'));
}
$products = $this->getDocumentRepository(Product::class)->findBy([
'active' => true, '_id' => ['$in' => $cart->getCompareList()]
]);
$productsByCategory = ['' => $products];
$categories = [];
$groups = [];
$firstPropertyValueMap = [];
/**
* @var Property[] $properties
*/
$properties = $this->getDocumentRepository(Property::class)->findBy(['active' => true], ['name' => 'asc']);
foreach ($properties as $property) {
$groups[$property->getId()]['property'] = $property;
}
$values = [];
/**
* @var PropertyValue $value
*/
foreach ($this->getDocumentRepository(PropertyValue::class)->findBy([]) as $value) {
$values[$value->getId1C()] = $value;
}
/**
* @var Product $product
*/
foreach ($products as $product) {
foreach ($product->getCategories() as $category) {
$productsByCategory[$category->getId()][] = $product;
$categories[$category->getId()] = $category;
break;
}
}
$differentColorMap = [];
foreach ($productsByCategory as $categoryId => $list) {
$differentColorMap[$categoryId] = false;
if (count($list)) {
$product = $list[0];
$firstColorId = $product->getColor() ? $product->getColor()->getId() : null;
foreach ($list as $product) {
$colorId = $product->getColor() ? $product->getColor()->getId() : null;
if ($colorId != $firstColorId) {
$differentColorMap[$categoryId] = true;
break;
}
}
}
foreach ($properties as $property) {
$groups[$property->getId()]['different'][$categoryId] = false;
$groups[$property->getId()]['empty'][$categoryId] = true;
if (count($list)) {
$product = $list[0];
$firstPropertyValueMap[$categoryId][$property->getId()] = $product->getProperty1CValueId($property) ?: '';
foreach ($list as $product) {
if ($firstPropertyValueMap[$categoryId][$property->getId()] != $product->getProperty1CValueId($property)) {
$groups[$property->getId()]['different'][$categoryId] = true;
}
if ($product->hasProperty1C($property) && !empty($values[$product->getProperty1CValueId($property)])) {
$groups[$property->getId()]['empty'][$categoryId] = false;
}
}
}
}
}
return $this->render(
'application/compare/index.html.twig', [
'products' => $products,
'productsByCategory' => $productsByCategory,
'differentColor' => $differentColorMap,
'categories' => $categories,
'groups' => $groups,
'values' => $values
]
);
}
}