modules/QSL/MyWishlist/src/Model/Wishlist.php line 18

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\MyWishlist\Model;
  7. use Doctrine\ORM\Mapping as ORM;
  8. /**
  9.  * Wishlist item model
  10.  *
  11.  * @ORM\Entity
  12.  * @ORM\Table  (name="wishlists")
  13.  */
  14. class Wishlist extends \XLite\Model\AEntity
  15. {
  16.     public const FLAG_ALREADY_ADDED    1;
  17.     public const FLAG_ADDED            2;
  18.     public const FLAG_NOT_ADDED        3;
  19.     /**
  20.      * Wishlist identificator
  21.      *
  22.      * @var mixed
  23.      *
  24.      * @ORM\Id
  25.      * @ORM\GeneratedValue (strategy="AUTO")
  26.      * @ORM\Column         (type="integer")
  27.      */
  28.     protected $id;
  29.     /**
  30.      * Customer profile of wishlist
  31.      *
  32.      * @var \XLite\Model\Profile
  33.      *
  34.      * @ORM\ManyToOne  (targetEntity="XLite\Model\Profile", inversedBy="wishlists")
  35.      * @ORM\JoinColumn (name="profile_id", referencedColumnName="profile_id", onDelete="CASCADE")
  36.      */
  37.     protected $customer;
  38.     /**
  39.      * Wishlist name
  40.      *
  41.      * @var string
  42.      *
  43.      * @ORM\Column (type="string", length=255)
  44.      */
  45.     protected $wishlistName '';
  46.     /**
  47.      * Cell hash
  48.      *
  49.      * @var string
  50.      *
  51.      * @ORM\Column (type="string", nullable=true)
  52.      */
  53.     protected $hash;
  54.     /**
  55.      * Wishlist links relation
  56.      *
  57.      * @var \Doctrine\Common\Collections\ArrayCollection
  58.      *
  59.      * @ORM\OneToMany (targetEntity="QSL\MyWishlist\Model\WishlistLink", mappedBy="wishlist", cascade={"all"})
  60.      * @ORM\OrderBy ({"orderby" = "ASC"})
  61.      */
  62.     protected $wishlistLinks;
  63.     public function __construct(array $data = [])
  64.     {
  65.         $this->wishlistLinks = new \Doctrine\Common\Collections\ArrayCollection();
  66.         parent::__construct($data);
  67.     }
  68.     /**
  69.      * @return \XLite\Model\AEntity
  70.      */
  71.     public function cloneEntity()
  72.     {
  73.         $newList parent::cloneEntity();
  74.         $newList->setCustomer($this->getCustomer());
  75.         foreach ($this->getWishlistLinks() as $link) {
  76.             $cloned $link->cloneEntity();
  77.             $newList->addWishlistLinks($cloned);
  78.             $cloned->setParentProduct($link->getParentProduct());
  79.             $cloned->setWishlist($newList);
  80.         }
  81.         return $newList;
  82.     }
  83.     public function setCustomer($value)
  84.     {
  85.         $this->customer $value;
  86.         return $this;
  87.     }
  88.     public function getCustomer()
  89.     {
  90.         return $this->customer;
  91.     }
  92.     public function setWishlistName($value)
  93.     {
  94.         $this->wishlistName $value;
  95.         return $this;
  96.     }
  97.     public function getWishlistName()
  98.     {
  99.         return $this->wishlistName;
  100.     }
  101.     /**
  102.      * @param $hash
  103.      *
  104.      * @return $this
  105.      */
  106.     public function setHash($hash)
  107.     {
  108.         $this->hash $hash;
  109.         return $this;
  110.     }
  111.     /**
  112.      * @return string
  113.      */
  114.     public function getHash()
  115.     {
  116.         return $this->hash;
  117.     }
  118.     /**
  119.      * @return string
  120.      */
  121.     public function generateHash()
  122.     {
  123.         $hash substr(hash('md5'uniqid(''true)), 07);
  124.         $this->setHash($hash);
  125.         return $hash;
  126.     }
  127.     public function addWishlistLinks($link)
  128.     {
  129.         $this->wishlistLinks[] = $link;
  130.         return $this;
  131.     }
  132.     public function getWishlistLinks()
  133.     {
  134.         return $this->wishlistLinks;
  135.     }
  136.     /**
  137.      * Only the author of wishlist has access to it
  138.      *
  139.      * @param \XLite\Model\Profile $profile
  140.      *
  141.      * @return boolean
  142.      */
  143.     public function hasAccessToManage(\XLite\Model\Profile $profile)
  144.     {
  145.         return $profile->getProfileId() === $this->getCustomer()->getProfileId();
  146.     }
  147.     /**
  148.      * Process adding product to wishlist
  149.      *
  150.      * @param \XLite\Model\Product $product
  151.      *
  152.      * @return mixed
  153.      */
  154.     public function addItem(\XLite\Model\Product $product$orderby 0)
  155.     {
  156.         $repo \XLite\Core\Database::getRepo('QSL\MyWishlist\Model\WishlistLink');
  157.         $result = static::FLAG_NOT_ADDED;
  158.         $link $this->getWishlistLink($product);
  159.         if ($link) {
  160.             // The link is already here and we move the product to the top of wishlist
  161.             $link->setOrderby($repo->getMinimumOrderby($this) - 1);
  162.             $result = static::FLAG_ALREADY_ADDED;
  163.         } else {
  164.             $link = new \QSL\MyWishlist\Model\WishlistLink(['wishlist' => $this]);
  165.             \XLite\Core\Database::getEM()->persist($link);
  166.             \XLite\Core\Database::getEM()->flush($link);
  167.             $link->createSnapshot($product);
  168.             \XLite\Core\Database::getEM()->persist($link);
  169.             $this->addWishlistLinks($link);
  170.             $result = static::FLAG_ADDED;
  171.         }
  172.         \XLite\Core\Database::getEM()->flush();
  173.         return $result;
  174.     }
  175.     /**
  176.      * Remove wishlist item defined via snapshot product
  177.      *
  178.      * @param integer $linkId Wishlist link id
  179.      *
  180.      * @return void
  181.      */
  182.     public function removeWishlistLink($linkId)
  183.     {
  184.         $repo \XLite\Core\Database::getRepo('QSL\MyWishlist\Model\WishlistLink');
  185.         $link $repo->find($linkId);
  186.         if ($link) {
  187.             $repo->delete($link);
  188.         }
  189.     }
  190.     /**
  191.      * Check if current wishlist has any wishlist links in it
  192.      *
  193.      * @return boolean
  194.      */
  195.     public function hasProducts()
  196.     {
  197.         return $this->getWishlistLinks()->count() > 0;
  198.     }
  199.     /**
  200.      * Check if current wishlist has any wishlist links in it
  201.      *
  202.      * @return integer
  203.      */
  204.     public function getProductsCount()
  205.     {
  206.         $result 0;
  207.         foreach ($this->getWishlistLinks() as $link) {
  208.             $product $link->getParentProduct();
  209.             if ($product && $product->getEnabled()) {
  210.                 $result++;
  211.             }
  212.         }
  213.         return $result;
  214.     }
  215.     /**
  216.      * Check if current wishlist has visible products
  217.      *
  218.      * @return boolean
  219.      */
  220.     public function hasVisibleProducts()
  221.     {
  222.         $result false;
  223.         foreach ($this->getWishlistLinks() as $link) {
  224.             if ($link->getParentProduct()) {
  225.                 $result true;
  226.                 break;
  227.             }
  228.         }
  229.         return $result;
  230.     }
  231.     /**
  232.      * Check if the wishlist link is present in wishlist via its snapshot and return it
  233.      *
  234.      * @param \XLite\Model\Product $product Snapshot product model
  235.      *
  236.      * @return \QSL\MyWishlist\Model\WishlistLink|null
  237.      */
  238.     public function getWishlistLink(\XLite\Model\Product $product)
  239.     {
  240.         $result null;
  241.         foreach ($this->getWishlistLinks() as $link) {
  242.             if ($link->hasProduct($product)) {
  243.                 $result $link;
  244.                 break;
  245.             }
  246.         }
  247.         return $result;
  248.     }
  249. }