modules/XC/Upselling/src/Model/UpsellingProduct.php line 98

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 XC\Upselling\Model;
  7. use ApiPlatform\Core\Annotation as ApiPlatform;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use XC\Upselling\API\Endpoint\ProductUpsellingProduct\DTO\ProductUpsellingProductInput as Input;
  10. use XC\Upselling\API\Endpoint\ProductUpsellingProduct\DTO\ProductUpsellingProductOutput as Output;
  11. /**
  12.  * Upselling Product
  13.  *
  14.  * #'parent_child_product' index is used for Controller\Admin\UProductSelections::isExcludedProductId() and
  15.  * # API\Endpoint\ProductUpsellingProduct\Transformer\InputTransformer::checkUniqueness()
  16.  * # parent_child_product will be converted to unique in UniqueConstraintsEventListener::loadClassMetadata() XCB-2655?focusedCommentId=236581
  17.  * # parent_child_product_unique name cannot be used while we support mariaDB 10.4.x #XCB-2808
  18.  * @ORM\Entity
  19.  * @ORM\Table (name="upselling_products",
  20.  *      indexes={
  21.  *          @ORM\Index (name="parent_product_index", columns={"parent_product_id"}),
  22.  *          @ORM\Index (name="parent_child_product", columns={"parent_product_id", "product_id"})
  23.  *      }
  24.  * )
  25.  * @ApiPlatform\ApiResource(
  26.  *     shortName="Related Product",
  27.  *     input=Input::class,
  28.  *     output=Output::class,
  29.  *     itemOperations={
  30.  *          "get"={
  31.  *              "method"="GET",
  32.  *              "path"="/products/{product_id}/related_products/{id}.{_format}",
  33.  *              "identifiers"={"product_id", "id"},
  34.  *              "requirements"={"product_id"="\d+", "id"="\d+"},
  35.  *              "openapi_context"={
  36.  *                  "parameters"={
  37.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  38.  *                      {"name"="id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  39.  *                  }
  40.  *              }
  41.  *          },
  42.  *          "put"={
  43.  *              "method"="PUT",
  44.  *              "path"="/products/{product_id}/related_products/{id}.{_format}",
  45.  *              "identifiers"={"product_id", "id"},
  46.  *              "requirements"={"product_id"="\d+", "id"="\d+"},
  47.  *              "openapi_context"={
  48.  *                  "parameters"={
  49.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  50.  *                      {"name"="id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  51.  *                  }
  52.  *              }
  53.  *          },
  54.  *          "delete"={
  55.  *              "method"="DELETE",
  56.  *              "path"="/products/{product_id}/related_products/{id}.{_format}",
  57.  *              "identifiers"={"product_id", "id"},
  58.  *              "requirements"={"product_id"="\d+", "id"="\d+"},
  59.  *              "openapi_context"={
  60.  *                  "parameters"={
  61.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  62.  *                      {"name"="id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  63.  *                  }
  64.  *              }
  65.  *          }
  66.  *     },
  67.  *     collectionOperations={
  68.  *          "get"={
  69.  *              "method"="GET",
  70.  *              "path"="/products/{product_id}/related_products.{_format}",
  71.  *              "identifiers"={"product_id"},
  72.  *              "requirements"={"product_id"="\d+"},
  73.  *              "openapi_context"={
  74.  *                  "parameters"={
  75.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  76.  *                  },
  77.  *              }
  78.  *          },
  79.  *          "post"={
  80.  *              "method"="POST",
  81.  *              "path"="/products/{product_id}/related_products.{_format}",
  82.  *              "controller"="xcart.api.xc.upselling.product_upselling_product.controller",
  83.  *              "identifiers"={"product_id"},
  84.  *              "requirements"={"product_id"="\d+"},
  85.  *              "openapi_context"={
  86.  *                  "parameters"={
  87.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  88.  *                  }
  89.  *              }
  90.  *          }
  91.  *     }
  92.  * )
  93.  */
  94. class UpsellingProduct extends \XLite\Model\AEntity
  95. {
  96.     /**
  97.      * Session cell name
  98.      */
  99.     public const           SESSION_CELL_NAME      'upsellingProductsSearch';
  100.     protected const        KEEP_ORDERBY_ON_RELINK true;
  101.     /**
  102.      * Unique id
  103.      *
  104.      * @var integer
  105.      *
  106.      * @ORM\Id
  107.      * @ORM\GeneratedValue (strategy="AUTO")
  108.      * @ORM\Column         (type="integer")
  109.      */
  110.     protected $id;
  111.     /**
  112.      * Sort position
  113.      *
  114.      * @var integer
  115.      *
  116.      * @ORM\Column (type="integer")
  117.      */
  118.     protected $orderBy 0;
  119.     /**
  120.      * Product (relation)
  121.      *
  122.      * @var \XLite\Model\Product
  123.      *
  124.      * @ORM\ManyToOne  (targetEntity="XLite\Model\Product", inversedBy="upsellingProducts")
  125.      * @ORM\JoinColumn (name="product_id", referencedColumnName="product_id", onDelete="CASCADE")
  126.      */
  127.     protected $product;
  128.     /**
  129.      * Parent product (relation)
  130.      *
  131.      * @var \XLite\Model\Product
  132.      *
  133.      * @ORM\ManyToOne  (targetEntity="XLite\Model\Product", inversedBy="upsellingParentProducts")
  134.      * @ORM\JoinColumn (name="parent_product_id", referencedColumnName="product_id", onDelete="CASCADE")
  135.      */
  136.     protected $parentProduct;
  137.     /**
  138.      * SKU getter
  139.      *
  140.      * @return string
  141.      */
  142.     public function getSku()
  143.     {
  144.         return $this->getProduct()->getSku();
  145.     }
  146.     /**
  147.      * Price getter
  148.      *
  149.      * @return float
  150.      */
  151.     public function getPrice()
  152.     {
  153.         return $this->getProduct()->getPrice();
  154.     }
  155.     /**
  156.      * Check if the bi-directional link is needed
  157.      *
  158.      * @return boolean
  159.      */
  160.     public function getBidirectional()
  161.     {
  162.         $linkData = [
  163.             'parentProduct' => $this->getProduct(),
  164.             'product'       => $this->getParentProduct(),
  165.         ];
  166.         return (bool)$this->getRepository()->findOneBy($linkData);
  167.     }
  168.     /**
  169.      * Is called during rearrange, therefore the old orderBy has to be preserved
  170.      * @return boolean
  171.      */
  172.     public function setBidirectional($newValue)
  173.     {
  174.         $newValue
  175.             $this->getRepository()->addBidirectionalLink($this, static::KEEP_ORDERBY_ON_RELINK)
  176.             : $this->getRepository()->deleteBidirectionalLink($this);
  177.     }
  178.     /**
  179.      * Amount getter
  180.      *
  181.      * @return integer
  182.      */
  183.     public function getAmount()
  184.     {
  185.         return $this->getProduct()->getPublicAmount();
  186.     }
  187.     /**
  188.      * Get position
  189.      *
  190.      * @return integer
  191.      */
  192.     public function getPosition()
  193.     {
  194.         return $this->getOrderBy();
  195.     }
  196.     /**
  197.      * Set position
  198.      *
  199.      * @param integer $position Upselling link position
  200.      *
  201.      * @return void
  202.      */
  203.     public function setPosition($position)
  204.     {
  205.         return $this->setOrderBy($position);
  206.     }
  207.     /**
  208.      * Get id
  209.      *
  210.      * @return integer
  211.      */
  212.     public function getId()
  213.     {
  214.         return $this->id;
  215.     }
  216.     /**
  217.      * Set orderBy
  218.      *
  219.      * @param integer $orderBy
  220.      * @return UpsellingProduct
  221.      */
  222.     public function setOrderBy($orderBy)
  223.     {
  224.         $this->orderBy $orderBy;
  225.         return $this;
  226.     }
  227.     /**
  228.      * Get orderBy
  229.      *
  230.      * @return integer
  231.      */
  232.     public function getOrderBy()
  233.     {
  234.         return $this->orderBy;
  235.     }
  236.     /**
  237.      * Set product
  238.      *
  239.      * @param \XLite\Model\Product $product
  240.      * @return UpsellingProduct
  241.      */
  242.     public function setProduct(\XLite\Model\Product $product null)
  243.     {
  244.         $this->product $product;
  245.         return $this;
  246.     }
  247.     /**
  248.      * Get product
  249.      *
  250.      * @return \XLite\Model\Product
  251.      */
  252.     public function getProduct()
  253.     {
  254.         return $this->product;
  255.     }
  256.     /**
  257.      * Set parentProduct
  258.      *
  259.      * @param \XLite\Model\Product $parentProduct
  260.      * @return UpsellingProduct
  261.      */
  262.     public function setParentProduct(\XLite\Model\Product $parentProduct null)
  263.     {
  264.         $this->parentProduct $parentProduct;
  265.         return $this;
  266.     }
  267.     /**
  268.      * Get parentProduct
  269.      *
  270.      * @return \XLite\Model\Product
  271.      */
  272.     public function getParentProduct()
  273.     {
  274.         return $this->parentProduct;
  275.     }
  276. }