modules/QSL/Make/src/Model/Level3.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\Level3\DTO\Level3Input as Input;
  10. use QSL\Make\API\Endpoint\Level3\DTO\Level3Output as Output;
  11. /**
  12.  * @ORM\Entity (repositoryClass="\QSL\Make\Model\Repo\Level3")
  13.  * @ORM\Table  (name="level3",
  14.  *      indexes={
  15.  *          @ORM\Index (name="enabled", columns={"enabled"}),
  16.  *          @ORM\Index (name="level2", columns={"level2_id"}),
  17.  *          @ORM\Index (name="showOnFrontPage", columns={"showOnFrontPage"})
  18.  *      }
  19.  * )
  20.  * @ApiPlatform\ApiResource(
  21.  *     shortName="Make/Model/Year - Level3",
  22.  *     input=Input::class,
  23.  *     output=Output::class,
  24.  *     itemOperations={
  25.  *          "get"={
  26.  *              "method"="GET",
  27.  *              "path"="/qsl/make/level3/{id}.{_format}",
  28.  *              "identifiers"={"id"},
  29.  *              "requirements"={"id"="\d+"}
  30.  *          },
  31.  *          "put"={
  32.  *              "method"="PUT",
  33.  *              "path"="/qsl/make/level3/{id}.{_format}",
  34.  *              "identifiers"={"id"},
  35.  *              "requirements"={"id"="\d+"}
  36.  *          },
  37.  *          "delete"={
  38.  *              "method"="DELETE",
  39.  *              "path"="/qsl/make/level3/{id}.{_format}",
  40.  *              "identifiers"={"id"},
  41.  *              "requirements"={"id"="\d+"}
  42.  *          }
  43.  *     },
  44.  *     collectionOperations={
  45.  *          "get"={
  46.  *              "method"="GET",
  47.  *              "path"="/qsl/make/level3.{_format}",
  48.  *              "identifiers"={}
  49.  *          },
  50.  *          "post"={
  51.  *              "method"="POST",
  52.  *              "path"="/qsl/make/level3.{_format}",
  53.  *              "identifiers"={},
  54.  *              "controller"="xcart.api.qsl.make.level3.controller"
  55.  *          }
  56.  *     },
  57.  *     attributes={
  58.  *         "pagination_items_per_page" = 100
  59.  *     }
  60.  * )
  61.  */
  62. class Level3 extends ALevel
  63. {
  64.     /**
  65.      * Level2
  66.      *
  67.      * @var \QSL\Make\Model\Level2
  68.      *
  69.      * @ORM\ManyToOne (targetEntity="QSL\Make\Model\Level2", inversedBy="level3", cascade={"merge","detach"})
  70.      * @ORM\JoinColumn (name="level2_id", referencedColumnName="id", onDelete="CASCADE")
  71.      */
  72.     protected $level2;
  73.     /**
  74.      * Level4
  75.      *
  76.      * @var \Doctrine\Common\Collections\ArrayCollection
  77.      *
  78.      * @ORM\OneToMany (targetEntity="QSL\Make\Model\Level4", mappedBy="level3", cascade={"all"})
  79.      * @ORM\OrderBy   ({"value" = "ASC"})
  80.      */
  81.     protected $level4;
  82.     /**
  83.      * Relation to a ModelProduct entities
  84.      *
  85.      * @var \Doctrine\Common\Collections\ArrayCollection
  86.      *
  87.      * @ORM\OneToMany (targetEntity="QSL\Make\Model\Level3Product", mappedBy="level", cascade={"all"})
  88.      * @ORM\OrderBy   ({"position" = "ASC"})
  89.      */
  90.     protected $level3Product;
  91.     /**
  92.      * One-to-one relation with image
  93.      *
  94.      * @var \QSL\Make\Model\Image\Level3
  95.      *
  96.      * @ORM\OneToMany  (targetEntity="QSL\Make\Model\Image\Level3", mappedBy="level", cascade={"all"})
  97.      */
  98.     protected $images;
  99.     /**
  100.      * @var \QSL\Make\Model\Image\Level3Logo
  101.      *
  102.      * @ORM\OneToOne  (targetEntity="QSL\Make\Model\Image\Level3Logo", 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="level3", 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\Level3Translation", 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.      * @return $this
  138.      */
  139.     public function addCleanURLs(\XLite\Model\CleanURL $cleanURL)
  140.     {
  141.         $this->cleanURLs[] = $cleanURL;
  142.     }
  143.     /**
  144.      * @param $value
  145.      */
  146.     public function setImages($value)
  147.     {
  148.         $this->images $value;
  149.     }
  150.     /**
  151.      * @return Image\Level3
  152.      */
  153.     public function getImages()
  154.     {
  155.         return $this->images;
  156.     }
  157.     /**
  158.      * @return \QSL\Make\Model\Image\Level3Logo
  159.      */
  160.     public function getLogo()
  161.     {
  162.         return $this->logo;
  163.     }
  164.     /**
  165.      * @param \QSL\Make\Model\Image\Level3Logo $value
  166.      */
  167.     public function setLogo($value)
  168.     {
  169.         $this->logo $value;
  170.     }
  171.     /**
  172.      * @return string
  173.      */
  174.     public function getCustomerTitle()
  175.     {
  176.         return $this->getTitle() ?: $this->getLevel2()->getCustomerTitle();
  177.     }
  178.     /**
  179.      * @return string
  180.      */
  181.     public function getCustomerValue()
  182.     {
  183.         return $this->getLevel2()->getCustomerValue() . ' ' $this->getValue();
  184.     }
  185.     /**
  186.      * @return string
  187.      */
  188.     public function getDisplayDescription()
  189.     {
  190.         return $this->getDescription() ?: $this->getLevel2()->getDisplayDescription();
  191.     }
  192.     /**
  193.      * @return Image\Level3
  194.      */
  195.     public function getCustomerImage()
  196.     {
  197.         return $this->getImages()->count() ? $this->getImages() : $this->getLevel2()->getCustomerImage();
  198.     }
  199.     /**
  200.      * @return mixed
  201.      */
  202.     public function getLevel2()
  203.     {
  204.         return $this->level2;
  205.     }
  206.     /**
  207.      * @param \QSL\Make\Model\Level2 $value
  208.      *
  209.      * @return \QSL\Make\Model\Level3
  210.      */
  211.     public function setLevel2($value)
  212.     {
  213.         $this->level2 $value;
  214.     }
  215.     /**
  216.      * Level4 getter
  217.      *
  218.      * @return \QSL\Make\Model\Level4
  219.      */
  220.     public function getLevel4()
  221.     {
  222.         return $this->level4;
  223.     }
  224.     /**
  225.      * @param \QSL\Make\Model\Level4 $value
  226.      */
  227.     public function addLevel4($value)
  228.     {
  229.         $this->level4[] = $value;
  230.     }
  231.     /**
  232.      * @return mixed
  233.      */
  234.     public function getLevel3Product()
  235.     {
  236.         return $this->level3Product;
  237.     }
  238.     /**
  239.      * @param \QSL\Make\Model\Level3Product $value
  240.      *
  241.      * @return \QSL\Make\Model\Level3
  242.      */
  243.     public function addLevel3Product($value)
  244.     {
  245.         $this->level3Product[] = $value;
  246.     }
  247.     /**
  248.      * @param \QSL\Make\Model\Level3Product $value
  249.      *
  250.      * @return \QSL\Make\Model\Level3
  251.      */
  252.     public function setLevelProduct($value)
  253.     {
  254.         return $this->addLevel3Product($value);
  255.     }
  256.     /**
  257.      * @return string
  258.      */
  259.     public function getLevelName($delimiter ' ')
  260.     {
  261.         return $this->getLevel2()->getLevelName($delimiter) . $delimiter $this->getValue();
  262.     }
  263.     public function getPathData(): array
  264.     {
  265.         $result $this->getLevel2()->getPathData();
  266.         $result[] = ['id' => $this->getId(), 'name' => $this->getValue()];
  267.         return $result;
  268.     }
  269.     /**
  270.      * @return array
  271.      */
  272.     public function getLevelSelectors()
  273.     {
  274.         return array_merge(
  275.             $this->getLevel2()->getLevelSelectors(),
  276.             [
  277.                 \XLite\Core\Config::getInstance()->QSL->Make->level3name => [
  278.                     'id'    => $this->getId(),
  279.                     'value' => $this->getValue(),
  280.                 ],
  281.             ]
  282.         );
  283.     }
  284.     /**
  285.      * Constructor
  286.      *
  287.      * @param array $data Entity properties OPTIONAL
  288.      */
  289.     public function __construct(array $data = [])
  290.     {
  291.         $this->level4        = new \Doctrine\Common\Collections\ArrayCollection();
  292.         $this->level3Product = new \Doctrine\Common\Collections\ArrayCollection();
  293.         $this->cleanURLs     = new \Doctrine\Common\Collections\ArrayCollection();
  294.         parent::__construct($data);
  295.     }
  296.     /**
  297.      * @return integer
  298.      */
  299.     public function getProductsCount()
  300.     {
  301.         return \XLite\Core\Database::getRepo('QSL\Make\Model\Level3Product')
  302.             ->search(new \XLite\Core\CommonCell(['level' => $this]), \XLite\Model\Repo\ARepo::SEARCH_MODE_COUNT);
  303.     }
  304.     /**
  305.      * @return array
  306.      */
  307.     public function getProducts()
  308.     {
  309.         return array_map(
  310.             static function ($modelProduct) {
  311.                 return $modelProduct->getProduct();
  312.             },
  313.             $this->getLevel3Product()->toArray()
  314.         );
  315.     }
  316.     /**
  317.      * @param integer $productId
  318.      *
  319.      * @return boolean
  320.      */
  321.     public function hasProduct($productId)
  322.     {
  323.         return in_array(
  324.             $productId,
  325.             array_map(
  326.                 static function ($product) {
  327.                     return $product->getProductId();
  328.                 },
  329.                 $this->getProducts()
  330.             ),
  331.             true
  332.         );
  333.     }
  334.     /**
  335.      * @return string
  336.      */
  337.     public function getLevelPath()
  338.     {
  339.         return $this->getLevel2()->getLevelPath() . '-' $this->getValue();
  340.     }
  341.     /**
  342.      * @return \QSL\Make\Model\Level2
  343.      */
  344.     public function getParentLevel()
  345.     {
  346.         return $this->getLevel2();
  347.     }
  348.     public function getDepth(): int
  349.     {
  350.         return 3;
  351.     }
  352. }