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.      * Check if the brand has logo.
  119.      *
  120.      * @return bool
  121.      */
  122.     public function hasImage()
  123.     {
  124.         return is_object($this->getImage());
  125.     }
  126.     /**
  127.      * Get brand_id
  128.      *
  129.      * @return int
  130.      */
  131.     public function getBrandId()
  132.     {
  133.         return $this->brand_id;
  134.     }
  135.     /**
  136.      * Get position
  137.      *
  138.      * @return int
  139.      */
  140.     public function getPosition()
  141.     {
  142.         return $this->position;
  143.     }
  144.     /**
  145.      * Set position
  146.      *
  147.      * @param int $position
  148.      *
  149.      * @return Brand
  150.      */
  151.     public function setPosition($position)
  152.     {
  153.         $this->position $position;
  154.         return $this;
  155.     }
  156.     /**
  157.      * Get option
  158.      *
  159.      * @return \XLite\Model\AttributeOption
  160.      */
  161.     public function getOption()
  162.     {
  163.         return $this->option;
  164.     }
  165.     /**
  166.      * Set option
  167.      *
  168.      * @param \XLite\Model\AttributeOption $option
  169.      *
  170.      * @return Brand
  171.      */
  172.     public function setOption(\XLite\Model\AttributeOption $option null)
  173.     {
  174.         $this->option $option;
  175.         $this->setCleanUrlIfEmpty();
  176.         return $this;
  177.     }
  178.     protected function setCleanUrlIfEmpty()
  179.     {
  180.         if (\XLite\Core\Converter::isEmptyString($this->getCleanURL())) {
  181.             $cleanUrl Database::getRepo('XLite\Model\CleanURL')->generateCleanURL($this);
  182.             $this->setCleanURL($cleanUrl);
  183.         }
  184.     }
  185.     /**
  186.      * Get image
  187.      *
  188.      * @return \QSL\ShopByBrand\Model\Image\Brand\Image
  189.      */
  190.     public function getImage()
  191.     {
  192.         return $this->image;
  193.     }
  194.     /**
  195.      * Set image
  196.      *
  197.      * @param \QSL\ShopByBrand\Model\Image\Brand\Image $image
  198.      *
  199.      * @return Brand
  200.      */
  201.     public function setImage(\QSL\ShopByBrand\Model\Image\Brand\Image $image null)
  202.     {
  203.         $this->image $image;
  204.         return $this;
  205.     }
  206.     /**
  207.      * Add clean URL
  208.      *
  209.      * @param \XLite\Model\CleanURL $cleanURLs
  210.      *
  211.      * @return Brand
  212.      */
  213.     public function addCleanURLs(\XLite\Model\CleanURL $cleanURLs)
  214.     {
  215.         $this->cleanURLs[] = $cleanURLs;
  216.         return $this;
  217.     }
  218.     /**
  219.      * Get clean URLs
  220.      *
  221.      * @return \Doctrine\Common\Collections\Collection
  222.      */
  223.     public function getCleanURLs()
  224.     {
  225.         return $this->cleanURLs;
  226.     }
  227.     // {{{ Translation Getters / setters
  228.     /**
  229.      * @return string
  230.      */
  231.     public function getDescription()
  232.     {
  233.         return $this->getTranslationField(__FUNCTION__);
  234.     }
  235.     /**
  236.      * @param string $description
  237.      *
  238.      * @return \XLite\Model\Base\Translation
  239.      */
  240.     public function setDescription($description)
  241.     {
  242.         return $this->setTranslationField(__FUNCTION__$description);
  243.     }
  244.     /**
  245.      * @return string
  246.      */
  247.     public function getMetaTitle()
  248.     {
  249.         return $this->getTranslationField(__FUNCTION__);
  250.     }
  251.     /**
  252.      * @param string $metaTitle
  253.      *
  254.      * @return \XLite\Model\Base\Translation
  255.      */
  256.     public function setMetaTitle($metaTitle)
  257.     {
  258.         return $this->setTranslationField(__FUNCTION__$metaTitle);
  259.     }
  260.     /**
  261.      * @return string
  262.      */
  263.     public function getMetaDescription()
  264.     {
  265.         return $this->getTranslationField(__FUNCTION__);
  266.     }
  267.     /**
  268.      * @param string $metaDescription
  269.      *
  270.      * @return \XLite\Model\Base\Translation
  271.      */
  272.     public function setMetaDescription($metaDescription)
  273.     {
  274.         return $this->setTranslationField(__FUNCTION__$metaDescription);
  275.     }
  276.     /**
  277.      * @return string
  278.      */
  279.     public function getMetaKeywords()
  280.     {
  281.         return $this->getTranslationField(__FUNCTION__);
  282.     }
  283.     /**
  284.      * @param string $metaKeywords
  285.      *
  286.      * @return \XLite\Model\Base\Translation
  287.      */
  288.     public function setMetaKeywords($metaKeywords)
  289.     {
  290.         return $this->setTranslationField(__FUNCTION__$metaKeywords);
  291.     }
  292.     /**
  293.      * Get enabled flag
  294.      *
  295.      * @return bool
  296.      */
  297.     public function getEnabled()
  298.     {
  299.         return $this->enabled;
  300.     }
  301.     /**
  302.      * Set enabled flag
  303.      *
  304.      * @param bool $enabled
  305.      *
  306.      * @return Brand
  307.      */
  308.     public function setEnabled(bool $enabled true)
  309.     {
  310.         $this->enabled $enabled;
  311.         return $this;
  312.     }
  313.     // }}}
  314.     public function getViewDescription(): string
  315.     {
  316.         return static::getPreprocessedValue($this->getDescription())
  317.             ?: $this->getDescription();
  318.     }
  319. }