src/Model/Product/Car.php line 21

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace App\Model\Product;
  15. use Pimcore\Model\DataObject\AccessoryPart\Listing;
  16. use Pimcore\Model\DataObject\Data\Hotspotimage;
  17. class Car extends \Pimcore\Model\DataObject\Car
  18. {
  19.     const OBJECT_TYPE_ACTUAL_CAR 'actual-car';
  20.     const OBJECT_TYPE_VIRTUAL_CAR 'virtual-car';
  21.     /**
  22.      * @return string
  23.      */
  24.     public function getOSName(): ?string
  25.     {
  26.         return ($this->getManufacturer() ? $this->getManufacturer()->getName() . ' ' '') . $this->getName();
  27.     }
  28.     public function getProductName($language null) {
  29.         return $this->getName($language);
  30.     }
  31.     /**
  32.      * @return string
  33.      */
  34.     public function getSubText(): string
  35.     {
  36.         $textParts = [];
  37.         $textParts[] = $this->getBodyStyle() ? $this->getBodyStyle()->getName() : '';
  38.         $textParts[] = $this->getProductionYear();
  39.         $textParts[] = $this->getAttributes()->getEngine() ? $this->getAttributes()->getEngine()->getPower() : '';
  40.         return "<span class='text-nowrap'>" implode("</span>, <span class='text-nowrap'>"array_filter($textParts)) . '</span>';
  41.     }
  42.     /**
  43.      * @return int|string
  44.      */
  45.     public function getOSProductNumber(): ?string
  46.     {
  47.         return $this->getId();
  48.     }
  49.     /**
  50.      * @return string
  51.      */
  52.     public function getOSIndexType(): ?string
  53.     {
  54.         return $this->getObjectType() === self::OBJECT_TYPE_ACTUAL_CAR self::OBJECT_TYPE_VARIANT self::OBJECT_TYPE_OBJECT;
  55.     }
  56.     /**
  57.      * @return int
  58.      */
  59.     public function getOSParentId()
  60.     {
  61.         if ($this->getObjectType() == self::OBJECT_TYPE_ACTUAL_CAR) {
  62.             $parent $this->getParent();
  63.             while ($parent->getParent() instanceof self) {
  64.                 $parent $parent->getParent();
  65.             }
  66.             return $parent->getId();
  67.         }
  68.         return parent::getOSParentId();
  69.     }
  70.     /**
  71.      * @return Hotspotimage|null
  72.      */
  73.     public function getMainImage(): ?Hotspotimage
  74.     {
  75.         $gallery $this->getGallery();
  76.         if ($gallery) {
  77.             $items $gallery->getItems();
  78.             if ($items) {
  79.                 return $items[0];
  80.             }
  81.         }
  82.         return null;
  83.     }
  84.     /**
  85.      * @return Hotspotimage[]
  86.      */
  87.     public function getAdditionalImages(): array
  88.     {
  89.         $gallery $this->getGallery();
  90.         $items $gallery->getItems();
  91.         if ($items) {
  92.             unset($items[0]);
  93.         } else {
  94.             $items = [];
  95.         }
  96.         $items array_filter($items, function ($item) {
  97.             return !empty($item) && !empty($item->getImage());
  98.         });
  99.         $generalImages $this->getGenericImages()->getItems();
  100.         if ($generalImages) {
  101.             $items array_merge($items$generalImages);
  102.         }
  103.         return $items;
  104.     }
  105.     /**
  106.      * @return Category|null
  107.      */
  108.     public function getMainCategory(): ?Category
  109.     {
  110.         $categories $this->getCategories();
  111.         $category reset($categories);
  112.         return $category ?: null;
  113.     }
  114.     /**
  115.      * @return Listing
  116.      *
  117.      * @throws \Exception
  118.      */
  119.     public function getAccessories(): Listing
  120.     {
  121.         // get all parent IDs
  122.         $filterIds = ['compatibleTo LIKE "%,' $this->getId() . ',%"'];
  123.         $parent $this->getParent();
  124.         while ($parent instanceof self) {
  125.             $filterIds[] = 'compatibleTo LIKE "%,' $parent->getId() . ',%"';
  126.             $parent $parent->getParent();
  127.         }
  128.         // create listing with OR statements
  129.         $listing = new Listing();
  130.         $listing->setCondition(implode(' OR '$filterIds));
  131.         return $listing;
  132.     }
  133.     /**
  134.      * @return Car[]
  135.      */
  136.     public function getColorVariants(): array
  137.     {
  138.         if ($this->getObjectType() == self::OBJECT_TYPE_ACTUAL_CAR) {
  139.             $parent $this->getParent();
  140.             $carSiblings = [];
  141.             foreach ($parent->getChildren() as $sibling) {
  142.                 if ($sibling instanceof self && $sibling->getObjectType() == self::OBJECT_TYPE_ACTUAL_CAR) {
  143.                     $carSiblings[] = $sibling;
  144.                 }
  145.             }
  146.             return $carSiblings;
  147.         }
  148.         return [];
  149.     }
  150. }