classes/XLite/Model/ViewList.php line 21

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 Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Includes\Utils\Module\Module;
  10. use XCart\Domain\ModuleManagerDomain;
  11. /**
  12.  * View list
  13.  *
  14.  * @ORM\Entity
  15.  * @ORM\Table  (name="view_lists",
  16.  *          indexes={
  17.  *              @ORM\Index (name="forGenerateOneBannerPerViewList", columns={"entityId"}),
  18.  *              @ORM\Index (name="tl", columns={"tpl", "list"}),
  19.  *              @ORM\Index (name="lzv", columns={"list", "zone", "version"}),
  20.  *              @ORM\Index (name="tclz", columns={"tpl", "child", "list", "zone"})
  21.  *          }
  22.  * )
  23.  */
  24. class ViewList extends \XLite\Model\AEntity
  25. {
  26.     /**
  27.      * Predefined weights
  28.      */
  29.     public const POSITION_FIRST 0;
  30.     public const POSITION_LAST  16777215;
  31.     /**
  32.      * @deprecated since CDev-Core 5.5.0, use \XLite\Model\ViewList::INTERFACE_ and \XLite\Model\ViewList::ZONE_ constants
  33.      */
  34.     public const INTERFACE_CUSTOMER 'customer';
  35.     /**
  36.      * @deprecated since CDev-Core 5.5.0, use \XLite\Model\ViewList::INTERFACE_ and \XLite\Model\ViewList::ZONE_ constants
  37.      */
  38.     public const INTERFACE_ADMIN    'admin';
  39.     /**
  40.      * Predefined interfaces
  41.      */
  42.     public const INTERFACE_WEB  'web';
  43.     public const INTERFACE_MAIL 'mail';
  44.     public const INTERFACE_PDF  'pdf';
  45.     /**
  46.      * Predefined zones
  47.      */
  48.     public const ZONE_CUSTOMER 'customer';
  49.     public const ZONE_ADMIN    'admin';
  50.     /**
  51.      * Override modes
  52.      */
  53.     public const OVERRIDE_OFF 0;
  54.     public const OVERRIDE_MOVE 1;
  55.     public const OVERRIDE_HIDE 2;
  56.     public const OVERRIDE_DISABLE_PRESET 3;
  57.     /**
  58.      * Layout preset key
  59.      */
  60.     public const PRESET_ONE_COLUMN 'one';
  61.     /**
  62.      * Version key
  63.      *
  64.      * @var string
  65.      */
  66.     protected static $versionKey;
  67.     /**
  68.      * List id
  69.      *
  70.      * @var integer
  71.      *
  72.      * @ORM\Id
  73.      * @ORM\GeneratedValue (strategy="AUTO")
  74.      * @ORM\Column         (type="integer", length=11)
  75.      */
  76.     protected $list_id;
  77.     /**
  78.      * Parent view list item
  79.      *
  80.      * @var \XLite\Model\ViewList
  81.      *
  82.      * @ORM\ManyToOne  (targetEntity="XLite\Model\ViewList", inversedBy="variants")
  83.      * @ORM\JoinColumn (name="parent_id", referencedColumnName="list_id", onDelete="SET NULL")
  84.      */
  85.     protected $parent;
  86.     /**
  87.      * Variants of view list item
  88.      *
  89.      * @ORM\OneToMany (targetEntity="XLite\Model\ViewList", mappedBy="parent")
  90.      * @ORM\OrderBy ({"list_id"="ASC"})
  91.      */
  92.     protected $variants;
  93.     /**
  94.      * Class list name
  95.      *
  96.      * @var string
  97.      *
  98.      * @ORM\Column (type="string", options={"charset"="latin1"})
  99.      */
  100.     protected $list;
  101.     /**
  102.      * List interface
  103.      *
  104.      * @var string
  105.      *
  106.      * @ORM\Column (type="string", length=16, options={"charset"="latin1"})
  107.      */
  108.     protected $interface self::INTERFACE_WEB;
  109.     /**
  110.      * List zone
  111.      *
  112.      * @var string
  113.      *
  114.      * @ORM\Column (type="string", length=16, options={"charset"="latin1"})
  115.      */
  116.     protected $zone self::ZONE_CUSTOMER;
  117.     /**
  118.      * Child class name
  119.      *
  120.      * @var string
  121.      *
  122.      * @ORM\Column (type="string", length=512, options={"charset"="latin1"})
  123.      */
  124.     protected $child '';
  125.     /**
  126.      * Child weight
  127.      *
  128.      * @var integer
  129.      *
  130.      * @ORM\Column (type="integer", length=11)
  131.      */
  132.     protected $weight 0;
  133.     /**
  134.      * Template relative path
  135.      *
  136.      * @var string
  137.      *
  138.      * @ORM\Column (type="string", length=512, options={"charset"="latin1"})
  139.      */
  140.     protected $tpl '';
  141.     /**
  142.      * Template relative path
  143.      *
  144.      * @var string
  145.      *
  146.      * @ORM\Column (type="string", length=32, nullable=true)
  147.      */
  148.     protected $version;
  149.     /**
  150.      * Template relative path
  151.      *
  152.      * @var string
  153.      *
  154.      * @ORM\Column (type="string", length=32, nullable=true)
  155.      */
  156.     protected $preset;
  157.     /**
  158.      * Class list name
  159.      *
  160.      * @var string
  161.      *
  162.      * @ORM\Column (type="string")
  163.      */
  164.     protected $list_override '';
  165.     /**
  166.      * Child weight
  167.      *
  168.      * @var integer
  169.      *
  170.      * @ORM\Column (type="integer", length=11)
  171.      */
  172.     protected $weight_override 0;
  173.     /**
  174.      * Override mode
  175.      *
  176.      * @var boolean
  177.      *
  178.      * @ORM\Column (type="integer")
  179.      */
  180.     protected $override_mode 0;
  181.     /**
  182.      * Is class or template is deleted
  183.      *
  184.      * @var boolean
  185.      *
  186.      * @ORM\Column (type="boolean")
  187.      */
  188.     protected $deleted false;
  189.     /**
  190.      * Contains additional conditions
  191.      *
  192.      * @var integer
  193.      *
  194.      * @ORM\Column (type="integer", nullable=true)
  195.      */
  196.     protected $entityId;
  197.     public function __construct(array $data = [])
  198.     {
  199.         $this->variants = new ArrayCollection();
  200.         parent::__construct($data);
  201.     }
  202.     /**
  203.      * Get in zone hash
  204.      *
  205.      * @return string
  206.      */
  207.     public function getHashWithoutZone()
  208.     {
  209.         $prefix \XLite::ZONE_COMMON '/';
  210.         $pattern '/^' preg_quote($prefix'/') . '/uS';
  211.         $hashValues = [
  212.             $this->getList(),
  213.             $this->getChild(),
  214.             $this->getWeight(),
  215.             preg_replace($pattern''$this->getTpl()),
  216.         ];
  217.         return md5(serialize($hashValues));
  218.     }
  219.     /**
  220.      * Set version key
  221.      *
  222.      * @param string $key Key
  223.      *
  224.      * @return void
  225.      */
  226.     public static function setVersionKey($key)
  227.     {
  228.         static::$versionKey $key;
  229.     }
  230.     /**
  231.      * Get list_id
  232.      *
  233.      * @return integer
  234.      */
  235.     public function getListId()
  236.     {
  237.         return $this->list_id;
  238.     }
  239.     /**
  240.      * Set parent view list item
  241.      *
  242.      * @param ViewList $parent
  243.      *
  244.      * @return ViewList
  245.      */
  246.     public function setParent(ViewList $parent)
  247.     {
  248.         $parent->addVariant($this);
  249.         $this->parent $parent;
  250.         return $this;
  251.     }
  252.     /**
  253.      * Get parent view list
  254.      *
  255.      * @return ViewList
  256.      */
  257.     public function getParent()
  258.     {
  259.         return $this->parent;
  260.     }
  261.     /**
  262.      * Delete parent
  263.      *
  264.      * @return $this
  265.      */
  266.     public function deleteParent()
  267.     {
  268.         $this->parent null;
  269.         return $this;
  270.     }
  271.     /**
  272.      * Set list
  273.      *
  274.      * @param string $list
  275.      * @return ViewList
  276.      */
  277.     public function setList($list)
  278.     {
  279.         $this->list $list;
  280.         return $this;
  281.     }
  282.     /**
  283.      * Get list
  284.      *
  285.      * @return string
  286.      */
  287.     public function getList()
  288.     {
  289.         return $this->list;
  290.     }
  291.     /**
  292.      * Set interface
  293.      *
  294.      * @param string $interface
  295.      *
  296.      * @return ViewList
  297.      */
  298.     public function setInterface($interface)
  299.     {
  300.         $this->interface $interface;
  301.         return $this;
  302.     }
  303.     /**
  304.      * Get zone
  305.      *
  306.      * @return string
  307.      */
  308.     public function getInterface()
  309.     {
  310.         return $this->interface;
  311.     }
  312.     /**
  313.      * Set zone
  314.      *
  315.      * @param string $zone
  316.      * @return ViewList
  317.      */
  318.     public function setZone($zone)
  319.     {
  320.         $this->zone $zone;
  321.         return $this;
  322.     }
  323.     /**
  324.      * Get zone
  325.      *
  326.      * @return string
  327.      */
  328.     public function getZone()
  329.     {
  330.         return $this->zone;
  331.     }
  332.     /**
  333.      * Set child
  334.      *
  335.      * @param string $child
  336.      * @return ViewList
  337.      */
  338.     public function setChild($child)
  339.     {
  340.         $this->child $child;
  341.         return $this;
  342.     }
  343.     /**
  344.      * Get child
  345.      *
  346.      * @return string
  347.      */
  348.     public function getChild()
  349.     {
  350.         return $this->child;
  351.     }
  352.     /**
  353.      * Set weight
  354.      *
  355.      * @param integer $weight
  356.      * @return ViewList
  357.      */
  358.     public function setWeight($weight)
  359.     {
  360.         $this->weight $weight;
  361.         return $this;
  362.     }
  363.     /**
  364.      * Get weight
  365.      *
  366.      * @return integer
  367.      */
  368.     public function getWeight()
  369.     {
  370.         return $this->weight;
  371.     }
  372.     /**
  373.      * Set tpl
  374.      *
  375.      * @param string $tpl
  376.      * @return ViewList
  377.      */
  378.     public function setTpl($tpl)
  379.     {
  380.         $this->tpl $tpl;
  381.         return $this;
  382.     }
  383.     /**
  384.      * Get tpl
  385.      *
  386.      * @return string
  387.      */
  388.     public function getTpl()
  389.     {
  390.         return $this->tpl;
  391.     }
  392.     /**
  393.      * Set version
  394.      *
  395.      * @param string $version
  396.      * @return ViewList
  397.      */
  398.     public function setVersion($version)
  399.     {
  400.         $this->version $version;
  401.         return $this;
  402.     }
  403.     /**
  404.      * Get version
  405.      *
  406.      * @return string
  407.      */
  408.     public function getVersion()
  409.     {
  410.         return $this->version;
  411.     }
  412.     public static function getVersionKey(): ?string
  413.     {
  414.         return static::$versionKey;
  415.     }
  416.     /**
  417.      * Set list_override
  418.      *
  419.      * @param string $listOverride
  420.      * @return ViewList
  421.      */
  422.     public function setListOverride($listOverride)
  423.     {
  424.         $this->list_override $listOverride;
  425.         return $this;
  426.     }
  427.     /**
  428.      * Get list_override
  429.      *
  430.      * @return string
  431.      */
  432.     public function getListOverride()
  433.     {
  434.         return $this->list_override;
  435.     }
  436.     /**
  437.      * Set weight_override
  438.      *
  439.      * @param integer $weightOverride
  440.      * @return ViewList
  441.      */
  442.     public function setWeightOverride($weightOverride)
  443.     {
  444.         $this->weight_override $weightOverride;
  445.         return $this;
  446.     }
  447.     /**
  448.      * Get weight_override
  449.      *
  450.      * @return integer
  451.      */
  452.     public function getWeightOverride()
  453.     {
  454.         return $this->weight_override;
  455.     }
  456.     /**
  457.      * Set preset
  458.      *
  459.      * @param string $preset
  460.      * @return ViewList
  461.      */
  462.     public function setPreset($preset)
  463.     {
  464.         $this->preset $preset;
  465.         return $this;
  466.     }
  467.     /**
  468.      * Get preset
  469.      *
  470.      * @return string
  471.      */
  472.     public function getPreset()
  473.     {
  474.         return $this->preset;
  475.     }
  476.     /**
  477.      * @return bool
  478.      */
  479.     public function getDeleted()
  480.     {
  481.         return $this->deleted;
  482.     }
  483.     /**
  484.      * @param bool $deleted
  485.      * @return ViewList
  486.      */
  487.     public function setDeleted($deleted)
  488.     {
  489.         $this->deleted $deleted;
  490.         return $this;
  491.     }
  492.     /**
  493.      * @param integer $entityId
  494.      *
  495.      * @return ViewList
  496.      */
  497.     public function setEntityId($entityId)
  498.     {
  499.         $this->entityId $entityId;
  500.         return $this;
  501.     }
  502.     /**
  503.      * @return integer
  504.      */
  505.     public function getEntityId()
  506.     {
  507.         return $this->entityId;
  508.     }
  509.     /**
  510.      * Get view list variants
  511.      *
  512.      * @return ArrayCollection
  513.      */
  514.     public function getVariants()
  515.     {
  516.         return $this->variants;
  517.     }
  518.     /**
  519.      * Add view list variant
  520.      *
  521.      * @param ViewList $entity
  522.      * @return $this
  523.      */
  524.     public function addVariant(ViewList $entity)
  525.     {
  526.         $this->variants[] = $entity;
  527.         return $this;
  528.     }
  529.     /**
  530.      * Remove view list variant
  531.      *
  532.      * @param ViewList $entity
  533.      * @return bool
  534.      */
  535.     public function removeVariant(ViewList $entity)
  536.     {
  537.         return $this->variants->removeElement($entity);
  538.     }
  539.     /**
  540.      * Returns name of view list where this item will be actually displayed (takes overrides into account)
  541.      *
  542.      * @return string
  543.      */
  544.     public function getListActual()
  545.     {
  546.         if ($this->isDisplayed()) {
  547.             if ($this->getOverrideMode() > static::OVERRIDE_OFF) {
  548.                 return $this->getListOverride();
  549.             }
  550.             return $this->getList();
  551.         }
  552.         return 'hidden';
  553.     }
  554.     /**
  555.      * Returns view list item weight considering overrides
  556.      *
  557.      * @return integer
  558.      */
  559.     public function getWeightActual()
  560.     {
  561.         return $this->getOverrideMode() ? $this->getWeightOverride() : $this->getWeight();
  562.     }
  563.     /**
  564.      * Check if this view list item will be rendered
  565.      *
  566.      * @return boolean
  567.      */
  568.     public function isDisplayed()
  569.     {
  570.         return !$this->isHidden();
  571.     }
  572.     /**
  573.      * Check if this view list item is in hidden mode (not rendered in customer area and rendered invisible in layout editor)
  574.      * @return boolean
  575.      */
  576.     public function isHidden()
  577.     {
  578.         return $this->getOverrideMode() === static::OVERRIDE_HIDE;
  579.     }
  580.     /**
  581.      * Apply override settings
  582.      *
  583.      * @param integer $mode
  584.      * @param string $list
  585.      * @param integer $weight
  586.      */
  587.     public function applyOverrides($mode$list null$weight null)
  588.     {
  589.         $this->setOverrideMode($mode);
  590.         if ($list !== null) {
  591.             $this->setListOverride($list);
  592.         }
  593.         if ($weight !== null) {
  594.             $this->setWeightOverride($weight);
  595.         }
  596.     }
  597.     /**
  598.      * Transfer override settings from another view list item
  599.      *
  600.      * @param  \XLite\Model\ViewList $other Value source
  601.      */
  602.     public function mapOverrides(\XLite\Model\ViewList $other)
  603.     {
  604.         if ($other) {
  605.             $this->setListOverride($other->getListOverride());
  606.             $this->setWeightOverride($other->getWeightOverride());
  607.             $this->setOverrideMode($other->getOverrideMode());
  608.         }
  609.     }
  610.     /**
  611.      * Check if module for list item is enabled
  612.      *
  613.      * @return bool
  614.      */
  615.     public function isViewListModuleEnabled()
  616.     {
  617.         $class $this->getChild();
  618.         $tpl $this->getTpl();
  619.         /** @var ModuleManagerDomain $moduleManagerDomain */
  620.         $moduleManagerDomain \XCart\Container::getContainer()?->get(ModuleManagerDomain::class);
  621.         if (
  622.             $class
  623.             && ($moduleId Module::getModuleIdByClassName($class))
  624.         ) {
  625.             return $moduleManagerDomain->isEnabled($moduleId);
  626.         }
  627.         if (
  628.             $tpl
  629.             && preg_match('#modules/(\w+)/(\w+)/#S'$tpl$match)
  630.             && $moduleManagerDomain->isEnabled("{$match[1]}-{$match[2]}")
  631.         ) {
  632.             return false;
  633.         }
  634.         return true;
  635.     }
  636.     /**
  637.      * Set override_mode
  638.      *
  639.      * @param integer $overrideMode
  640.      * @return ViewList
  641.      */
  642.     public function setOverrideMode($overrideMode)
  643.     {
  644.         $this->override_mode $overrideMode;
  645.         return $this;
  646.     }
  647.     /**
  648.      * Get override_mode
  649.      *
  650.      * @return integer
  651.      */
  652.     public function getOverrideMode()
  653.     {
  654.         return $this->override_mode;
  655.     }
  656. }