modules/QSL/Make/src/Model/Level2.php line 66

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace QSL\Make\Model;
  7. use ApiPlatform\Core\Annotation as ApiPlatform;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use QSL\Make\API\Endpoint\Level2\DTO\Level2Input as Input;
  10. use QSL\Make\API\Endpoint\Level2\DTO\Level2Output as Output;
  11. /**
  12.  * @ORM\Entity (repositoryClass="\QSL\Make\Model\Repo\Level2")
  13.  * @ORM\Table  (name="level2",
  14.  *      indexes={
  15.  *          @ORM\Index (name="enabled", columns={"enabled"}),
  16.  *          @ORM\Index (name="level1", columns={"level1_id"}),
  17.  *          @ORM\Index (name="showOnFrontPage", columns={"showOnFrontPage"})
  18.  *      }
  19.  * )
  20.  * @ApiPlatform\ApiResource(
  21.  *     shortName="Make/Model/Year - Level2",
  22.  *     input=Input::class,
  23.  *     output=Output::class,
  24.  *     itemOperations={
  25.  *          "get"={
  26.  *              "method"="GET",
  27.  *              "path"="/qsl/make/level2/{id}.{_format}",
  28.  *              "identifiers"={"id"},
  29.  *              "requirements"={"id"="\d+"}
  30.  *          },
  31.  *          "put"={
  32.  *              "method"="PUT",
  33.  *              "path"="/qsl/make/level2/{id}.{_format}",
  34.  *              "identifiers"={"id"},
  35.  *              "requirements"={"id"="\d+"}
  36.  *          },
  37.  *          "delete"={
  38.  *              "method"="DELETE",
  39.  *              "path"="/qsl/make/level2/{id}.{_format}",
  40.  *              "identifiers"={"id"},
  41.  *              "requirements"={"id"="\d+"}
  42.  *          }
  43.  *     },
  44.  *     collectionOperations={
  45.  *          "get"={
  46.  *              "method"="GET",
  47.  *              "path"="/qsl/make/level2.{_format}",
  48.  *              "identifiers"={}
  49.  *          },
  50.  *          "post"={
  51.  *              "method"="POST",
  52.  *              "path"="/qsl/make/level2.{_format}",
  53.  *              "identifiers"={},
  54.  *              "controller"="xcart.api.qsl.make.level2.controller"
  55.  *          }
  56.  *     },
  57.  *     attributes={
  58.  *         "pagination_items_per_page" = 100
  59.  *     }
  60.  * )
  61.  */
  62. class Level2 extends ALevel
  63. {
  64.     /**
  65.      * Level1
  66.      *
  67.      * @var \QSL\Make\Model\Level1
  68.      *
  69.      * @ORM\ManyToOne (targetEntity="QSL\Make\Model\Level1", inversedBy="level2", cascade={"merge","detach"})
  70.      * @ORM\JoinColumn (name="level1_id", referencedColumnName="id", onDelete="CASCADE")
  71.      */
  72.     protected $level1;
  73.     /**
  74.      * Level3 list
  75.      *
  76.      * @var \Doctrine\Common\Collections\ArrayCollection
  77.      *
  78.      * @ORM\OneToMany (targetEntity="QSL\Make\Model\Level3", mappedBy="level2", cascade={"all"})
  79.      * @ORM\OrderBy   ({"value" = "ASC"})
  80.      */
  81.     protected $level3;
  82.     /**
  83.      * Relation to a ModelProduct entities
  84.      *
  85.      * @var \Doctrine\Common\Collections\ArrayCollection
  86.      *
  87.      * @ORM\OneToMany (targetEntity="QSL\Make\Model\Level2Product", mappedBy="level", cascade={"all"})
  88.      * @ORM\OrderBy   ({"position" = "ASC"})
  89.      */
  90.     protected $level2Product;
  91.     /**
  92.      * Banner images
  93.      *
  94.      * @var \QSL\Make\Model\Image\Level2
  95.      *
  96.      * @ORM\OneToMany  (targetEntity="QSL\Make\Model\Image\Level2", mappedBy="level", cascade={"all"})
  97.      */
  98.     protected $images;
  99.     /**
  100.      * @var \QSL\Make\Model\Image\Level2Logo
  101.      *
  102.      * @ORM\OneToOne  (targetEntity="QSL\Make\Model\Image\Level2Logo", mappedBy="level", cascade={"all"})
  103.      */
  104.     protected $logo;
  105.     /**
  106.      * Clean URLs
  107.      *
  108.      * @var \Doctrine\Common\Collections\ArrayCollection
  109.      *
  110.      * @ORM\OneToMany (targetEntity="XLite\Model\CleanURL", mappedBy="level2", cascade={"all"})
  111.      * @ORM\OrderBy   ({"id" = "ASC"})
  112.      */
  113.     protected $cleanURLs;
  114.     /**
  115.      * @var \Doctrine\Common\Collections\Collection
  116.      *
  117.      * @ORM\OneToMany (targetEntity="QSL\Make\Model\Level2Translation", mappedBy="owner", cascade={"all"})
  118.      */
  119.     protected $translations;
  120.     /**
  121.      * @return \Doctrine\Common\Collections\ArrayCollection
  122.      */
  123.     public function getCleanURLs()
  124.     {
  125.         return $this->cleanURLs;
  126.     }
  127.     /**
  128.      * @@param \Doctrine\Common\Collections\ArrayCollection
  129.      */
  130.     public function setCleanURLs($value)
  131.     {
  132.         $this->cleanURLs $value;
  133.     }
  134.     /**
  135.      * Add cleanURL
  136.      *
  137.      * @param \XLite\Model\CleanURL $cleanURL
  138.      */
  139.     public function addCleanURLs(\XLite\Model\CleanURL $cleanURL)
  140.     {
  141.         $this->cleanURLs[] = $cleanURL;
  142.     }
  143.     /**
  144.      * @param $value
  145.      *
  146.      * @return $this
  147.      */
  148.     public function setImages($value)
  149.     {
  150.         $this->images $value;
  151.         return $this;
  152.     }
  153.     /**
  154.      * @return Image\Level2
  155.      */
  156.     public function getImages()
  157.     {
  158.         return $this->images;
  159.     }
  160.     /**
  161.      * @return \QSL\Make\Model\Image\Level2Logo
  162.      */
  163.     public function getLogo()
  164.     {
  165.         return $this->logo;
  166.     }
  167.     /**
  168.      * @param \QSL\Make\Model\Image\Level2Logo $value
  169.      */
  170.     public function setLogo($value)
  171.     {
  172.         $this->logo $value;
  173.     }
  174.     /**
  175.      * @return string
  176.      */
  177.     public function getCustomerTitle()
  178.     {
  179.         return $this->getTitle() ?: $this->getLevel1()->getCustomerTitle();
  180.     }
  181.     /**
  182.      * @return string
  183.      */
  184.     public function getCustomerValue()
  185.     {
  186.         return $this->getLevel1()->getCustomerValue() . ' ' $this->getValue();
  187.     }
  188.     /**
  189.      * @return string
  190.      */
  191.     public function getDisplayDescription()
  192.     {
  193.         return $this->getDescription() ?: $this->getLevel1()->getDisplayDescription();
  194.     }
  195.     /**
  196.      * @return Image\Level1|Image\Level2
  197.      */
  198.     public function getCustomerImage()
  199.     {
  200.         return $this->getImages()->count() ? $this->getImages() : $this->getLevel1()->getCustomerImage();
  201.     }
  202.     /**
  203.      * @return \QSL\Make\Model\Level1
  204.      */
  205.     public function getLevel1()
  206.     {
  207.         return $this->level1;
  208.     }
  209.     /**
  210.      * @param \QSL\Make\Model\Level1 $value
  211.      *
  212.      * @return \QSL\Make\Model\Level2
  213.      */
  214.     public function setLevel1($value)
  215.     {
  216.         $this->level1 $value;
  217.         return $this;
  218.     }
  219.     /**
  220.      * @return \Doctrine\Common\Collections\ArrayCollection
  221.      */
  222.     public function getLevel3()
  223.     {
  224.         return $this->level3;
  225.     }
  226.     /**
  227.      * @param mixed $value
  228.      *
  229.      * @return \QSL\Make\Model\Level2
  230.      */
  231.     public function addLevel3($value)
  232.     {
  233.         $this->level3[] = $value;
  234.         return $this;
  235.     }
  236.     /**
  237.      * @return \Doctrine\Common\Collections\ArrayCollection
  238.      */
  239.     public function getLevel2Product()
  240.     {
  241.         return $this->level2Product;
  242.     }
  243.     /**
  244.      * @param mixed $value
  245.      *
  246.      * @return \QSL\Make\Model\Level2
  247.      */
  248.     public function addLevel2Product($value)
  249.     {
  250.         $this->level2Product[] = $value;
  251.         return $this;
  252.     }
  253.     /**
  254.      * @param mixed $value
  255.      *
  256.      * @return \QSL\Make\Model\Level2
  257.      */
  258.     public function setLevelProduct($value)
  259.     {
  260.         return $this->addLevel2Product($value);
  261.     }
  262.     /**
  263.      * @return string
  264.      */
  265.     public function getLevelName($delimiter ' ')
  266.     {
  267.         return $this->getLevel1()->getLevelName($delimiter) . $delimiter $this->getValue();
  268.     }
  269.     public function getPathData(): array
  270.     {
  271.         $result $this->getLevel1()->getPathData();
  272.         $result[] = ['id' => $this->getId(), 'name' => $this->getValue()];
  273.         return $result;
  274.     }
  275.     /**
  276.      * @return array
  277.      */
  278.     public function getLevelSelectors()
  279.     {
  280.         return array_merge(
  281.             $this->getLevel1()->getLevelSelectors(),
  282.             [
  283.                 \XLite\Core\Config::getInstance()->QSL->Make->level2name => [
  284.                     'id'    => $this->getId(),
  285.                     'value' => $this->getValue(),
  286.                 ],
  287.             ]
  288.         );
  289.     }
  290.     /**
  291.      * Constructor
  292.      *
  293.      * @param array $data Entity properties OPTIONAL
  294.      */
  295.     public function __construct(array $data = [])
  296.     {
  297.         $this->level3        = new \Doctrine\Common\Collections\ArrayCollection();
  298.         $this->level2Product = new \Doctrine\Common\Collections\ArrayCollection();
  299.         $this->cleanURLs     = new \Doctrine\Common\Collections\ArrayCollection();
  300.         parent::__construct($data);
  301.     }
  302.     /**
  303.      * @return integer
  304.      */
  305.     public function getProductsCount()
  306.     {
  307.         return \XLite\Core\Database::getRepo('QSL\Make\Model\Level2Product')
  308.             ->search(new \XLite\Core\CommonCell(['level' => $this]), \XLite\Model\Repo\ARepo::SEARCH_MODE_COUNT);
  309.     }
  310.     /**
  311.      * @return array
  312.      */
  313.     public function getProducts()
  314.     {
  315.         return array_map(
  316.             static function ($modelProduct) {
  317.                 return $modelProduct->getProduct();
  318.             },
  319.             $this->getLevel2Product()->toArray()
  320.         );
  321.     }
  322.     /**
  323.      * @param integer $productId
  324.      *
  325.      * @return boolean
  326.      */
  327.     public function hasProduct($productId)
  328.     {
  329.         return in_array(
  330.             $productId,
  331.             array_map(
  332.                 static function ($product) {
  333.                     return $product->getProductId();
  334.                 },
  335.                 $this->getProducts()
  336.             )
  337.         );
  338.     }
  339.     /**
  340.      * @return string
  341.      */
  342.     public function getLevelPath()
  343.     {
  344.         return $this->getLevel1()->getLevelPath() . '-' $this->getValue();
  345.     }
  346.     /**
  347.      * @return \QSL\Make\Model\Level1
  348.      */
  349.     public function getParentLevel()
  350.     {
  351.         return $this->getLevel1();
  352.     }
  353.     public function getDepth(): int
  354.     {
  355.         return 2;
  356.     }
  357. }