src/Application/Twig/GlobalLoader.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\Application\Twig;
  3. use App\Admin\Document\Category;
  4. use App\Admin\Document\Config;
  5. use App\Admin\Document\Product;
  6. use App\Admin\Document\ProductProperty;
  7. use App\Admin\Document\Property;
  8. use App\Admin\Document\PropertyValue;
  9. use App\Application\Controller\CatalogController;
  10. use Doctrine\ODM\MongoDB\DocumentManager;
  11. use Symfony\Component\HttpFoundation\Request;
  12. class GlobalLoader
  13. {
  14.     /**
  15.      * @var DocumentManager
  16.      */
  17.     private $documentManager;
  18.     /**
  19.      * @var Config
  20.      */
  21.     private $config;
  22.     public function __construct(DocumentManager $documentManager)
  23.     {
  24.         $this->documentManager $documentManager;
  25.     }
  26.     /**
  27.      * @return object|null|Config
  28.      * @throws \Exception
  29.      */
  30.     public function getConfig()
  31.     {
  32.         if (!$this->config) {
  33.             $this->config $this->documentManager->getRepository(Config::class)->findOneBy([]);
  34.             if (!$this->config) {
  35.                 throw new \Exception('Config not found');
  36.             }
  37.         }
  38.         return $this->config;
  39.     }
  40.     private $menu;
  41.     public function getMenu() {
  42.         if (!isset($this->menu)) {
  43.             $this->menu $this->documentManager->getRepository(Category::class)->findBy(
  44.                 ['active' => true'parent' => null], ['orderNumber' => 'asc''name' => 'asc']);
  45.         }
  46.         return $this->menu;
  47.     }
  48.     private $propertyMap = [];
  49.     /**
  50.      * @param $id
  51.      * @return ProductProperty
  52.      */
  53.     public function getProperty(\App\Admin\Document\Embedded\Property $property) {
  54.         $result false;
  55.         if ($property->getValueId() !== null) {
  56.             $key $property->getId() . '_' $property->getValueId();
  57.             if (isset($this->propertyMap[$key])) {
  58.                 return $this->propertyMap[$key];
  59.             }
  60.             /**
  61.              * @var ProductProperty $productProperty
  62.              */
  63.             $productProperty $this->documentManager->getRepository(ProductProperty::class)->findOneBy([
  64.                 'id1C' => $property->getId(),
  65.                 'active' => true
  66.             ]);
  67.             if ($productProperty) {
  68.                 $value $this->getPropertyValue($property->getValueId());
  69.                 if ($value) {
  70.                     $productProperty->setValue($value);
  71.                     $result $productProperty;
  72.                 }
  73.             }
  74.             $this->propertyMap[$key] = $result;
  75.         }
  76.         return $result;
  77.     }
  78.     private $propertyValueMap = [];
  79.     private function getPropertyValue($id) {
  80.         if (isset($this->propertyValueMap[$id])) {
  81.             return $this->propertyValueMap[$id];
  82.         }
  83.         $value $this->documentManager->getRepository(PropertyValue::class)->findOneBy(['id1C' => $id]);
  84.         if (!$value) {
  85.             $value = new PropertyValue();
  86.             $value->setId1C($id);
  87.             $value->setName($id);
  88.         }
  89.         $this->propertyValueMap[$id] = $value;
  90.         return $value;
  91.     }
  92.     /**
  93.      * Получить отсортированные свойства товара с учетом orderNumber
  94.      */
  95.     public function getSortedProperties(Product $product) {
  96.         $properties $product->getProperties1C();
  97.         $sortedProperties = [];
  98.         
  99.         foreach ($properties as $property) {
  100.             $productProperty $this->getProperty($property);
  101.             if ($productProperty) {
  102.                 $sortedProperties[] = $productProperty;
  103.             }
  104.         }
  105.         
  106.         // Сортировка: сначала по orderNumber (если есть), затем в текущем порядке
  107.         usort($sortedProperties, function($a$b) {
  108.             $orderA $a->getOrderNumber();
  109.             $orderB $b->getOrderNumber();
  110.             
  111.             // Если у обеих есть orderNumber - сортируем по нему
  112.             if ($orderA !== null && $orderB !== null) {
  113.                 return $orderA <=> $orderB;
  114.             }
  115.             
  116.             // Если только у одной есть orderNumber - она идет первой
  117.             if ($orderA !== null && $orderB === null) {
  118.                 return -1;
  119.             }
  120.             if ($orderA === null && $orderB !== null) {
  121.                 return 1;
  122.             }
  123.             
  124.             // Если у обеих orderNumber = null - оставляем текущий порядок
  125.             return 0;
  126.         });
  127.         
  128.         return $sortedProperties;
  129.     }
  130.     private $watchedProducts null;
  131.     public function getRecentlyWatchedProducts($excludeProductId null) {
  132.         if ($this->watchedProducts === null) {
  133.             $request Request::createFromGlobals();
  134.             $ids explode(';'$request->cookies->get(CatalogController::RECENTLY_WATCHED_PRODUCTS_COOKIE_NAME''));
  135.             $productIds = [];
  136.             foreach ($ids as $id) {
  137.                 if ($id) {
  138.                     $productIds[] = new \MongoId($id);
  139.                 }
  140.             }
  141.             $this->watchedProducts $this->documentManager->getRepository(Product::class)->findBy(
  142.                 [
  143.                     '_id' => ['$in' => $productIds],
  144.                 ]
  145.             );
  146.             usort($this->watchedProducts, function (Product $aProduct $b) use ($productIds) {
  147.                 return array_search($a->getId(), $productIds) - array_search($b->getId(), $productIds);
  148.             });
  149.         }
  150.         $result = [];
  151.         /**
  152.          * @var Product $product
  153.          */
  154.         foreach ($this->watchedProducts as $product) {
  155.             if ($product->getId() != $excludeProductId && $product->getActive()) {
  156.                 $result[] = $product;
  157.             }
  158.         }
  159.         return $result;
  160.     }
  161. }