classes/XLite/Model/ProductClass.php line 51

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2001-present X-Cart Holdings LLC. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace XLite\Model;
  7. use ApiPlatform\Core\Annotation as ApiPlatform;
  8. use XLite\Core\CommonCell;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use XLite\API\Endpoint\ProductClass\DTO\ProductClassInput;
  11. use XLite\API\Endpoint\ProductClass\DTO\ProductClassOutput;
  12. use XLite\API\Filter\AlphabeticalOrderFilter;
  13. /**
  14.  * Product class
  15.  *
  16.  * @ORM\Entity
  17.  * @ORM\Table  (name="product_classes")
  18.  * @ApiPlatform\ApiResource(
  19.  *     shortName="Product Class",
  20.  *     input=ProductClassInput::class,
  21.  *     output=ProductClassOutput::class,
  22.  *     itemOperations={
  23.  *         "get"={
  24.  *             "method"="GET",
  25.  *             "path"="/product_classes/{id}",
  26.  *             "identifiers"={"id"},
  27.  *         },
  28.  *         "put"={
  29.  *             "method"="PUT",
  30.  *             "path"="/product_classes/{id}",
  31.  *             "identifiers"={"id"},
  32.  *         },
  33.  *         "delete"={
  34.  *             "method"="DELETE",
  35.  *             "path"="/product_classes/{id}",
  36.  *             "identifiers"={"id"},
  37.  *         }
  38.  *     },
  39.  *     collectionOperations={
  40.  *         "get"={
  41.  *             "method"="GET",
  42.  *             "path"="/product_classes",
  43.  *             "identifiers"={"id"},
  44.  *         },
  45.  *         "post"={
  46.  *             "method"="POST",
  47.  *             "path"="/product_classes",
  48.  *             "identifiers"={"id"},
  49.  *         }
  50.  *     }
  51.  * )
  52.  * @ApiPlatform\ApiFilter(AlphabeticalOrderFilter::class, properties={"name"="ASC"})
  53.  */
  54. class ProductClass extends \XLite\Model\Base\I18n
  55. {
  56.     /**
  57.      * ID
  58.      *
  59.      * @var integer
  60.      *
  61.      * @ORM\Id
  62.      * @ORM\GeneratedValue (strategy="AUTO")
  63.      * @ORM\Column         (type="integer", options={ "unsigned": true })
  64.      */
  65.     protected $id;
  66.     /**
  67.      * Position
  68.      *
  69.      * @var integer
  70.      *
  71.      * @ORM\Column (type="integer")
  72.      */
  73.     protected $position 0;
  74.     /**
  75.      * Attributes
  76.      *
  77.      * @var \Doctrine\Common\Collections\Collection
  78.      *
  79.      * @ORM\OneToMany (targetEntity="XLite\Model\Attribute", mappedBy="productClass", cascade={"all"})
  80.      */
  81.     protected $attributes;
  82.     /**
  83.      * Attribute groups
  84.      *
  85.      * @var \Doctrine\Common\Collections\Collection
  86.      *
  87.      * @ORM\OneToMany (targetEntity="XLite\Model\AttributeGroup", mappedBy="productClass", cascade={"all"})
  88.      * @ORM\OrderBy   ({"position" = "ASC"})
  89.      */
  90.     protected $attribute_groups;
  91.     /**
  92.      * @var \Doctrine\Common\Collections\Collection
  93.      *
  94.      * @ORM\OneToMany (targetEntity="XLite\Model\ProductClassTranslation", mappedBy="owner", cascade={"all"})
  95.      */
  96.     protected $translations;
  97.     /**
  98.      * Constructor
  99.      *
  100.      * @param array $data Entity properties OPTIONAL
  101.      *
  102.      * @return void
  103.      */
  104.     public function __construct(array $data = [])
  105.     {
  106.         $this->attributes = new \Doctrine\Common\Collections\ArrayCollection();
  107.         $this->attribute_groups = new \Doctrine\Common\Collections\ArrayCollection();
  108.         parent::__construct($data);
  109.     }
  110.     /**
  111.      * Return number of products associated with the category
  112.      *
  113.      * @return integer
  114.      */
  115.     public function getProductsCount()
  116.     {
  117.         return $this->getProducts(nulltrue);
  118.     }
  119.     /**
  120.      * Return products list
  121.      *
  122.      * @param CommonCell $cnd        Search condition OPTIONAL
  123.      * @param boolean                $countOnly Return items list or only its size OPTIONAL
  124.      *
  125.      * @return array|integer
  126.      */
  127.     public function getProducts(CommonCell $cnd null$countOnly false)
  128.     {
  129.         if ($this->isPersistent()) {
  130.             if (!isset($cnd)) {
  131.                 $cnd = new CommonCell();
  132.             }
  133.             $cnd $this->prepareCndProducts($cnd);
  134.             $result \XLite\Core\Database::getRepo('XLite\Model\Product')->search($cnd$countOnly);
  135.         } else {
  136.             $result $countOnly : [];
  137.         }
  138.         return $result;
  139.     }
  140.     protected function prepareCndProducts(CommonCell $cnd): CommonCell
  141.     {
  142.         // Main condition for this search
  143.         $cnd->{\XLite\Model\Repo\Product::P_PRODUCT_CLASS} = $this;
  144.         return $cnd;
  145.     }
  146.     /**
  147.      * Return number of attributes associated with this class
  148.      *
  149.      * @return integer
  150.      */
  151.     public function getAttributesCount()
  152.     {
  153.         return count($this->getAttributes());
  154.     }
  155.     /**
  156.      * Get id
  157.      *
  158.      * @return integer
  159.      */
  160.     public function getId()
  161.     {
  162.         return $this->id;
  163.     }
  164.     /**
  165.      * Set position
  166.      *
  167.      * @param integer $position
  168.      * @return ProductClass
  169.      */
  170.     public function setPosition($position)
  171.     {
  172.         $this->position $position;
  173.         return $this;
  174.     }
  175.     /**
  176.      * Get position
  177.      *
  178.      * @return integer
  179.      */
  180.     public function getPosition()
  181.     {
  182.         return $this->position;
  183.     }
  184.     /**
  185.      * Add attributes
  186.      *
  187.      * @param \XLite\Model\Attribute $attributes
  188.      * @return ProductClass
  189.      */
  190.     public function addAttributes(\XLite\Model\Attribute $attributes)
  191.     {
  192.         $this->attributes[] = $attributes;
  193.         return $this;
  194.     }
  195.     /**
  196.      * Get attributes
  197.      *
  198.      * @return \Doctrine\Common\Collections\Collection
  199.      */
  200.     public function getAttributes()
  201.     {
  202.         return $this->attributes;
  203.     }
  204.     /**
  205.      * Add attribute_groups
  206.      *
  207.      * @param \XLite\Model\AttributeGroup $attributeGroups
  208.      * @return ProductClass
  209.      */
  210.     public function addAttributeGroups(\XLite\Model\AttributeGroup $attributeGroups)
  211.     {
  212.         $this->attribute_groups[] = $attributeGroups;
  213.         return $this;
  214.     }
  215.     /**
  216.      * Get attribute_groups
  217.      *
  218.      * @return \Doctrine\Common\Collections\Collection
  219.      */
  220.     public function getAttributeGroups()
  221.     {
  222.         return $this->attribute_groups;
  223.     }
  224. }