modules/QSL/ShopByBrand/src/Model/Brand.php line 16

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\ShopByBrand\Model;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use XLite\Core\Database;
  9. /**
  10.  * Class represents Brand model.
  11.  *
  12.  * @ORM\Entity (repositoryClass="\QSL\ShopByBrand\Model\Repo\Brand")
  13.  * @ORM\Table  (name="brands",
  14.  *      uniqueConstraints={
  15.  *          @ORM\UniqueConstraint(name="option_brand", columns={"attribute_option_id", "brand_id"})
  16.  *      }
  17.  * )
  18.  */
  19. class Brand extends \XLite\Model\Base\Catalog
  20. {
  21.     /**
  22.      * Brand ID.
  23.      *
  24.      * @var integer
  25.      *
  26.      * @ORM\Id
  27.      * @ORM\GeneratedValue (strategy="AUTO")
  28.      * @ORM\Column         (type="integer", options={ "unsigned": true })
  29.      */
  30.     protected $brand_id;
  31.     /**
  32.      * The attribute option used to indicate products of this brand.
  33.      *
  34.      * @var \XLite\Model\AttributeOption
  35.      *
  36.      * @ORM\OneToOne (targetEntity="XLite\Model\AttributeOption")
  37.      * @ORM\JoinColumn (name="attribute_option_id", referencedColumnName="id", onDelete="SET NULL", nullable=true)
  38.      */
  39.     protected $option;
  40.     /**
  41.      * One-to-one relation with brand_images table
  42.      *
  43.      * @var \QSL\ShopByBrand\Model\Image\Brand\Image
  44.      *
  45.      * @ORM\OneToOne  (targetEntity="QSL\ShopByBrand\Model\Image\Brand\Image", mappedBy="brand", cascade={"remove"})
  46.      */
  47.     protected $image;
  48.     /**
  49.      * Position of the brand among other ones in the list.
  50.      *
  51.      * @var integer
  52.      *
  53.      * @ORM\Column (type="integer")
  54.      */
  55.     protected $position 0;
  56.     /**
  57.      * Enabled flag
  58.      *
  59.      * @var boolean
  60.      *
  61.      * @ORM\Column (type="boolean")
  62.      */
  63.     protected $enabled true;
  64.     /**
  65.      * Clean URLs
  66.      *
  67.      * @var \Doctrine\Common\Collections\Collection
  68.      *
  69.      * @ORM\OneToMany (targetEntity="XLite\Model\CleanURL", mappedBy="brand", cascade={"all"})
  70.      * @ORM\OrderBy   ({"id" = "ASC"})
  71.      */
  72.     protected $cleanURLs;
  73.     /**
  74.      * @var \Doctrine\Common\Collections\Collection
  75.      *
  76.      * @ORM\OneToMany (targetEntity="QSL\ShopByBrand\Model\BrandTranslation", mappedBy="owner", cascade={"all"})
  77.      */
  78.     protected $translations;
  79.     /**
  80.      * Get the brand name stored in the associate attribute option.
  81.      *
  82.      * @return string
  83.      */
  84.     public function getName()
  85.     {
  86.         return $this->getOption() ? $this->getOption()->getName() : '';
  87.     }
  88.     /**
  89.      * Get the model ID.
  90.      *
  91.      * @return int
  92.      */
  93.     public function getId()
  94.     {
  95.         return $this->getBrandId();
  96.     }
  97.     /**
  98.      * Get brand products.
  99.      *
  100.      * @param \XLite\Core\CommonCell $cnd       Search condition OPTIONAL
  101.      * @param bool                   $countOnly Whether to count matching product, or return the list of them OPTIONAL
  102.      *
  103.      * @return array|integer
  104.      */
  105.     public function getProducts(\XLite\Core\CommonCell $cnd null$countOnly false)
  106.     {
  107.         if (!isset($cnd)) {
  108.             $cnd = new \XLite\Core\CommonCell();
  109.         }
  110.         // Main condition for this search
  111.         $cnd->{\XLite\Model\Repo\Product::P_BRAND_ID} = $this->getBrandId();
  112.         if (\XLite\Core\Config::getInstance()->General->show_out_of_stock_products === 'directLink') {
  113.             $cnd->{\XLite\Model\Repo\Product::P_INVENTORY} = \XLite\Model\Repo\Product::INV_IN;
  114.         }
  115.         return Database::getRepo('XLite\Model\Product')->search($cnd$countOnly);
  116.     }
  117.     /**
  118.      * Get brand products.
  119.      *
  120.      * @param \XLite\Core\CommonCell $cnd       Search condition OPTIONAL
  121.      * @param bool                   $countOnly Whether to count matching product, or return the list of them OPTIONAL
  122.      *
  123.      * @return array|integer
  124.      */
  125.     public function getAnyProducts(\XLite\Core\CommonCell $cnd null$countOnly false)
  126.     {
  127.         if (!isset($cnd)) {
  128.             $cnd = new \XLite\Core\CommonCell();
  129.         }
  130.         // Main condition for this search
  131.         $cnd->{\XLite\Model\Repo\Product::P_BRAND_ID} = $this->getBrandId();
  132.         return Database::getRepo('XLite\Model\Product')->search($cnd$countOnly);
  133.     }
  134.     /**
  135.      * Check if the brand has logo.
  136.      *
  137.      * @return bool
  138.      */
  139.     public function hasImage()
  140.     {
  141.         return is_object($this->getImage());
  142.     }
  143.     /**
  144.      * Get brand_id
  145.      *
  146.      * @return int
  147.      */
  148.     public function getBrandId()
  149.     {
  150.         return $this->brand_id;
  151.     }
  152.     /**
  153.      * Get position
  154.      *
  155.      * @return int
  156.      */
  157.     public function getPosition()
  158.     {
  159.         return $this->position;
  160.     }
  161.     /**
  162.      * Set position
  163.      *
  164.      * @param int $position
  165.      *
  166.      * @return Brand
  167.      */
  168.     public function setPosition($position)
  169.     {
  170.         $this->position $position;
  171.         return $this;
  172.     }
  173.     /**
  174.      * Get option
  175.      *
  176.      * @return \XLite\Model\AttributeOption
  177.      */
  178.     public function getOption()
  179.     {
  180.         return $this->option;
  181.     }
  182.     /**
  183.      * Set option
  184.      *
  185.      * @param \XLite\Model\AttributeOption $option
  186.      *
  187.      * @return Brand
  188.      */
  189.     public function setOption(\XLite\Model\AttributeOption $option null)
  190.     {
  191.         $this->option $option;
  192.         $this->setCleanUrlIfEmpty();
  193.         return $this;
  194.     }
  195.     protected function setCleanUrlIfEmpty()
  196.     {
  197.         if (\XLite\Core\Converter::isEmptyString($this->getCleanURL())) {
  198.             $cleanUrl Database::getRepo('XLite\Model\CleanURL')->generateCleanURL($this);
  199.             $this->setCleanURL($cleanUrl);
  200.         }
  201.     }
  202.     /**
  203.      * Get image
  204.      *
  205.      * @return \QSL\ShopByBrand\Model\Image\Brand\Image
  206.      */
  207.     public function getImage()
  208.     {
  209.         return $this->image;
  210.     }
  211.     /**
  212.      * Set image
  213.      *
  214.      * @param \QSL\ShopByBrand\Model\Image\Brand\Image $image
  215.      *
  216.      * @return Brand
  217.      */
  218.     public function setImage(\QSL\ShopByBrand\Model\Image\Brand\Image $image null)
  219.     {
  220.         $this->image $image;
  221.         return $this;
  222.     }
  223.     /**
  224.      * Add clean URL
  225.      *
  226.      * @param \XLite\Model\CleanURL $cleanURLs
  227.      *
  228.      * @return Brand
  229.      */
  230.     public function addCleanURLs(\XLite\Model\CleanURL $cleanURLs)
  231.     {
  232.         $this->cleanURLs[] = $cleanURLs;
  233.         return $this;
  234.     }
  235.     /**
  236.      * Get clean URLs
  237.      *
  238.      * @return \Doctrine\Common\Collections\Collection
  239.      */
  240.     public function getCleanURLs()
  241.     {
  242.         return $this->cleanURLs;
  243.     }
  244.     // {{{ Translation Getters / setters
  245.     /**
  246.      * @return string
  247.      */
  248.     public function getDescription()
  249.     {
  250.         return $this->getTranslationField(__FUNCTION__);
  251.     }
  252.     /**
  253.      * @param string $description
  254.      *
  255.      * @return \XLite\Model\Base\Translation
  256.      */
  257.     public function setDescription($description)
  258.     {
  259.         return $this->setTranslationField(__FUNCTION__$description);
  260.     }
  261.     /**
  262.      * @return string
  263.      */
  264.     public function getMetaTitle()
  265.     {
  266.         return $this->getTranslationField(__FUNCTION__);
  267.     }
  268.     /**
  269.      * @param string $metaTitle
  270.      *
  271.      * @return \XLite\Model\Base\Translation
  272.      */
  273.     public function setMetaTitle($metaTitle)
  274.     {
  275.         return $this->setTranslationField(__FUNCTION__$metaTitle);
  276.     }
  277.     /**
  278.      * @return string
  279.      */
  280.     public function getMetaDescription()
  281.     {
  282.         return $this->getTranslationField(__FUNCTION__);
  283.     }
  284.     /**
  285.      * @param string $metaDescription
  286.      *
  287.      * @return \XLite\Model\Base\Translation
  288.      */
  289.     public function setMetaDescription($metaDescription)
  290.     {
  291.         return $this->setTranslationField(__FUNCTION__$metaDescription);
  292.     }
  293.     /**
  294.      * @return string
  295.      */
  296.     public function getMetaKeywords()
  297.     {
  298.         return $this->getTranslationField(__FUNCTION__);
  299.     }
  300.     /**
  301.      * @param string $metaKeywords
  302.      *
  303.      * @return \XLite\Model\Base\Translation
  304.      */
  305.     public function setMetaKeywords($metaKeywords)
  306.     {
  307.         return $this->setTranslationField(__FUNCTION__$metaKeywords);
  308.     }
  309.     /**
  310.      * Get enabled flag
  311.      *
  312.      * @return bool
  313.      */
  314.     public function getEnabled()
  315.     {
  316.         return $this->enabled;
  317.     }
  318.     /**
  319.      * Set enabled flag
  320.      *
  321.      * @param bool $enabled
  322.      *
  323.      * @return Brand
  324.      */
  325.     public function setEnabled(bool $enabled true)
  326.     {
  327.         $this->enabled $enabled;
  328.         return $this;
  329.     }
  330.     // }}}
  331.     public function getViewDescription(): string
  332.     {
  333.         return static::getPreprocessedValue($this->getDescription())
  334.             ?: $this->getDescription();
  335.     }
  336. }