src/Application/Controller/CompareController.php line 106

Open in your IDE?
  1. <?php
  2. namespace App\Application\Controller;
  3. use App\Admin\Document\Category;
  4. use App\Admin\Document\Offer;
  5. use App\Admin\Document\Page;
  6. use App\Admin\Document\Product;
  7. use App\Admin\Document\Property;
  8. use App\Admin\Document\PropertyValue;
  9. use App\Application\Cart\Cart;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use \Symfony\Component\Routing\Annotation\Route;
  14. class CompareController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/compare", name="compare")
  18.      */
  19.     public function index(Request $requestCart $cart)
  20.     {
  21.         if ($request->isMethod(Request::METHOD_POST)) {
  22.             $cart->clearCompareList($request->get('category'));
  23.         }
  24.         $products $this->getDocumentRepository(Product::class)->findBy([
  25.             'active' => true'_id' => ['$in' => $cart->getCompareList()]
  26.         ]);
  27.         $productsByCategory = ['' => $products];
  28.         $categories = [];
  29.         $groups = [];
  30.         $firstPropertyValueMap = [];
  31.         /**
  32.          * @var Property[] $properties
  33.          */
  34.         $properties $this->getDocumentRepository(Property::class)->findBy(['active' => true], ['name' => 'asc']);
  35.         foreach ($properties as $property) {
  36.             $groups[$property->getId()]['property'] = $property;
  37.         }
  38.         $values = [];
  39.         /**
  40.          * @var PropertyValue $value
  41.          */
  42.         foreach ($this->getDocumentRepository(PropertyValue::class)->findBy([]) as $value) {
  43.             $values[$value->getId1C()] = $value;
  44.         }
  45.         /**
  46.          * @var Product $product
  47.          */
  48.         foreach ($products as $product) {
  49.             foreach ($product->getCategories() as $category) {
  50.                 $productsByCategory[$category->getId()][] = $product;
  51.                 $categories[$category->getId()] = $category;
  52.                 break;
  53.             }
  54.         }
  55.         $differentColorMap = [];
  56.         foreach ($productsByCategory as $categoryId => $list) {
  57.             $differentColorMap[$categoryId] = false;
  58.             if (count($list)) {
  59.                 $product $list[0];
  60.                 $firstColorId $product->getColor() ? $product->getColor()->getId() : null;
  61.                 foreach ($list as $product) {
  62.                     $colorId $product->getColor() ? $product->getColor()->getId() : null;
  63.                     if ($colorId != $firstColorId) {
  64.                         $differentColorMap[$categoryId] = true;
  65.                         break;
  66.                     }
  67.                 }
  68.             }
  69.             foreach ($properties as $property) {
  70.                 $groups[$property->getId()]['different'][$categoryId] = false;
  71.                 $groups[$property->getId()]['empty'][$categoryId] = true;
  72.                 if (count($list)) {
  73.                     $product $list[0];
  74.                     $firstPropertyValueMap[$categoryId][$property->getId()] = $product->getProperty1CValueId($property) ?: '';
  75.                     foreach ($list as $product) {
  76.                         if ($firstPropertyValueMap[$categoryId][$property->getId()] != $product->getProperty1CValueId($property)) {
  77.                             $groups[$property->getId()]['different'][$categoryId] = true;
  78.                         }
  79.                         if ($product->hasProperty1C($property) && !empty($values[$product->getProperty1CValueId($property)])) {
  80.                             $groups[$property->getId()]['empty'][$categoryId] = false;
  81.                         }
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.         return $this->render(
  87.             'application/compare/index.html.twig', [
  88.                 'products' => $products,
  89.                 'productsByCategory' => $productsByCategory,
  90.                 'differentColor' => $differentColorMap,
  91.                 'categories' => $categories,
  92.                 'groups' => $groups,
  93.                 'values' => $values
  94.             ]
  95.         );
  96.     }
  97. }