classes/XLite/Model/ProductClass.php line 50

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