modules/CDev/SimpleCMS/src/Model/Menu.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 CDev\SimpleCMS\Model;
  7. use Includes\Utils\Operator;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use XLite\Core\Cache\ExecuteCachedTrait;
  10. /**
  11.  * Menu
  12.  *
  13.  * @ORM\Entity
  14.  * @ORM\Table  (name="menus",
  15.  *      indexes={
  16.  *          @ORM\Index (name="enabled", columns={"enabled", "type"}),
  17.  *          @ORM\Index (name="position", columns={"position"})
  18.  *      }
  19.  * )
  20.  */
  21. class Menu extends \XLite\Model\Base\I18n
  22. {
  23.     use ExecuteCachedTrait;
  24.     /**
  25.      * Menu types
  26.      */
  27.     public const MENU_TYPE_PRIMARY 'P';
  28.     public const MENU_TYPE_FOOTER  'F';
  29.     public const DEFAULT_HOME_PAGE       '{home}';
  30.     public const DEFAULT_MY_ACCOUNT      '{my account}';
  31.     public const DEFAULT_CATEGORIES_PAGE '?target=categories';
  32.     public const DEFAULT_PAGES           '?target=pages';
  33.     /**
  34.      * Unique ID
  35.      *
  36.      * @var integer
  37.      *
  38.      * @ORM\Id
  39.      * @ORM\GeneratedValue (strategy="AUTO")
  40.      * @ORM\Column         (type="integer", options={ "unsigned": true })
  41.      */
  42.     protected $id;
  43.     /**
  44.      * Link
  45.      *
  46.      * @var   string
  47.      *
  48.      * @ORM\Column (type="string", nullable=true)
  49.      */
  50.     protected $link;
  51.     /**
  52.      * Node left value
  53.      *
  54.      * @var integer
  55.      *
  56.      * @ORM\Column (type="integer")
  57.      */
  58.     protected $lpos 0;
  59.     /**
  60.      * Node right value
  61.      *
  62.      * @var integer
  63.      *
  64.      * @ORM\Column (type="integer")
  65.      */
  66.     protected $rpos 0;
  67.     /**
  68.      * Menu "depth" in the tree
  69.      *
  70.      * @var integer
  71.      *
  72.      * @ORM\Column (type="integer")
  73.      */
  74.     protected $depth = -1;
  75.     /**
  76.      * Some cached flags
  77.      *
  78.      * @var \CDev\SimpleCMS\Model\Menu\QuickFlags
  79.      *
  80.      * @ORM\OneToOne (targetEntity="CDev\SimpleCMS\Model\Menu\QuickFlags", mappedBy="menu", cascade={"all"})
  81.      */
  82.     protected $quickFlags;
  83.     /**
  84.      * Child menus
  85.      *
  86.      * @var \Doctrine\Common\Collections\ArrayCollection
  87.      *
  88.      * @ORM\OneToMany (targetEntity="CDev\SimpleCMS\Model\Menu", mappedBy="parent", cascade={"all"})
  89.      * @ORM\OrderBy ({"id"="ASC"})
  90.      */
  91.     protected $children;
  92.     /**
  93.      * Parent menu
  94.      *
  95.      * @var \CDev\SimpleCMS\Model\Menu
  96.      *
  97.      * @ORM\ManyToOne  (targetEntity="CDev\SimpleCMS\Model\Menu", inversedBy="children")
  98.      * @ORM\JoinColumn (name="parent_id", referencedColumnName="id", onDelete="SET NULL")
  99.      */
  100.     protected $parent;
  101.     /**
  102.      * Type
  103.      *
  104.      * @var string
  105.      *
  106.      * @ORM\Column (type="string", length=1)
  107.      */
  108.     protected $type;
  109.     /**
  110.      * Position
  111.      *
  112.      * @var integer
  113.      *
  114.      * @ORM\Column (type="integer")
  115.      */
  116.     protected $position 0;
  117.     /**
  118.      * Is menu enabled or not
  119.      *
  120.      * @var boolean
  121.      *
  122.      * @ORM\Column (type="boolean")
  123.      */
  124.     protected $enabled true;
  125.     /**
  126.      * Visible for anonymous only (A), logged in only (L), for all visitors (AL)
  127.      *
  128.      * @var string
  129.      *
  130.      * @ORM\Column (type="string", length=2)
  131.      */
  132.     protected $visibleFor 'AL';
  133.     /**
  134.      * @var \Doctrine\Common\Collections\Collection
  135.      *
  136.      * @ORM\OneToMany (targetEntity="CDev\SimpleCMS\Model\MenuTranslation", mappedBy="owner", cascade={"all"})
  137.      */
  138.     protected $translations;
  139.     /**
  140.      * Get menu types
  141.      *
  142.      * @return array
  143.      */
  144.     public static function getTypes()
  145.     {
  146.         return [
  147.             static::MENU_TYPE_PRIMARY => static::t('Primary menu'),
  148.             static::MENU_TYPE_FOOTER  => static::t('Footer menu'),
  149.         ];
  150.     }
  151.     /**
  152.      * Set type
  153.      *
  154.      * @param string $type Type
  155.      *
  156.      * @return void
  157.      */
  158.     public function setType($type)
  159.     {
  160.         $types = static::getTypes();
  161.         if (isset($types[$type])) {
  162.             $this->type $type;
  163.         }
  164.     }
  165.     /**
  166.      * Defines the resulting link values for the specific link values
  167.      * for example: {home}
  168.      *
  169.      * @return array
  170.      */
  171.     protected function defineLinkURLs()
  172.     {
  173.         return $this->executeCachedRuntime(static function () {
  174.             return [
  175.                 static::DEFAULT_HOME_PAGE  => ' ',
  176.                 static::DEFAULT_MY_ACCOUNT => \XLite\Core\Converter::buildURL('order_list'),
  177.             ];
  178.         });
  179.     }
  180.     /**
  181.      * Defines the link controller class names for the specific link values
  182.      * for example: {home}
  183.      *
  184.      * @return array
  185.      */
  186.     protected function defineLinkControllers()
  187.     {
  188.         return [
  189.             static::DEFAULT_HOME_PAGE => '\XLite\Controller\Customer\Main',
  190.             static::DEFAULT_MY_ACCOUNT => [
  191.                 '\XLite\Controller\Customer\OrderList',
  192.                 '\XLite\Controller\Customer\AddressBook',
  193.                 '\XLite\Controller\Customer\Profile',
  194.             ],
  195.         ];
  196.     }
  197.     /**
  198.      * Link value for home page is defined in static::DEFAULT_HOME_PAGE constant
  199.      *
  200.      * @see static::DEFAULT_HOME_PAGE
  201.      *
  202.      * @return string
  203.      */
  204.     public function getURL()
  205.     {
  206.         $list $this->defineLinkURLs();
  207.         $link $this->getLink();
  208.         return $list[$link] ?? $link;
  209.     }
  210.     /**
  211.      * Defines the link controller class name
  212.      * or FALSE if there is no specific link value
  213.      *
  214.      * @return string | false
  215.      */
  216.     public function getLinkController()
  217.     {
  218.         $list $this->defineLinkControllers();
  219.         $link $this->getLink();
  220.         return $list[$link] ?? false;
  221.     }
  222.     /**
  223.      * Get object unique id
  224.      *
  225.      * @return integer
  226.      */
  227.     public function getMenuId()
  228.     {
  229.         return $this->id;
  230.     }
  231.     /**
  232.      * Translation getter. AUTOGENERATED
  233.      *
  234.      * @return string
  235.      */
  236.     public function getName()
  237.     {
  238.         return $this->getSoftTranslation()->getName();
  239.     }
  240.     /**
  241.      * Get depth
  242.      *
  243.      * @return string
  244.      */
  245.     public function getDepth()
  246.     {
  247.         return $this->depth;
  248.     }
  249.     /**
  250.      * Get link
  251.      *
  252.      * @return string
  253.      */
  254.     public function getLink()
  255.     {
  256.         return $this->link;
  257.     }
  258.     /**
  259.      * Set parent
  260.      *
  261.      * @param \CDev\SimpleCMS\Model\Menu $parent Parent menu OPTIONAL
  262.      *
  263.      * @return void
  264.      */
  265.     public function setParent(\CDev\SimpleCMS\Model\Menu $parent null)
  266.     {
  267.         $this->parent $parent;
  268.     }
  269.     /**
  270.      * Return parent menu ID
  271.      *
  272.      * @return integer
  273.      */
  274.     public function getParentId()
  275.     {
  276.         return $this->getParent() ? $this->getParent()->getMenuId() : 0;
  277.     }
  278.     /**
  279.      * Return submenus list
  280.      *
  281.      * @return \Doctrine\Common\Collections\ArrayCollection
  282.      */
  283.     public function getSubmenus()
  284.     {
  285.         return $this->getChildren();
  286.     }
  287.     /**
  288.      * Get menu path
  289.      *
  290.      * @return array
  291.      */
  292.     public function getPath()
  293.     {
  294.         return $this->getRepository()->getMenuPath($this->getMenuId());
  295.     }
  296.     /**
  297.      * Get the number of submenus
  298.      *
  299.      * @return integer
  300.      */
  301.     public function getSubmenusCount()
  302.     {
  303.         $result 0;
  304.         $enabledCondition $this->getRepository()->getEnabledCondition();
  305.         $quickFlags $this->getQuickFlags();
  306.         if ($quickFlags) {
  307.             $result $enabledCondition
  308.                 $quickFlags->getSubmenusCountEnabled()
  309.                 : $quickFlags->getSubmenusCountAll();
  310.         }
  311.         return $result;
  312.     }
  313.     /**
  314.      * Get the number of submenus
  315.      *
  316.      * @return integer
  317.      */
  318.     public function getSubmenusCountConditional()
  319.     {
  320.         $currentState \XLite\Core\Auth::getInstance()->isLogged() && !\XLite\Core\Auth::getInstance()->isAnonymous()
  321.             ? 'L'
  322.             'A';
  323.         return $this->getSubmenus()->filter(static function (\CDev\SimpleCMS\Model\Menu $submenu) use ($currentState) {
  324.             return $submenu->getEnabled() && ($submenu->getVisibleFor() === 'AL' || $submenu->getVisibleFor() === $currentState);
  325.         })->count();
  326.     }
  327.     /**
  328.      * Get id
  329.      *
  330.      * @return integer
  331.      */
  332.     public function getId()
  333.     {
  334.         return $this->id;
  335.     }
  336.     /**
  337.      * Set link
  338.      *
  339.      * @param string $link
  340.      * @return Menu
  341.      */
  342.     public function setLink($link)
  343.     {
  344.         $this->link Operator::purifyLink($link);
  345.         return $this;
  346.     }
  347.     /**
  348.      * Set lpos
  349.      *
  350.      * @param integer $lpos
  351.      * @return Menu
  352.      */
  353.     public function setLpos($lpos)
  354.     {
  355.         $this->lpos $lpos;
  356.         return $this;
  357.     }
  358.     /**
  359.      * Get lpos
  360.      *
  361.      * @return integer
  362.      */
  363.     public function getLpos()
  364.     {
  365.         return $this->lpos;
  366.     }
  367.     /**
  368.      * Set rpos
  369.      *
  370.      * @param integer $rpos
  371.      * @return Menu
  372.      */
  373.     public function setRpos($rpos)
  374.     {
  375.         $this->rpos $rpos;
  376.         return $this;
  377.     }
  378.     /**
  379.      * Get rpos
  380.      *
  381.      * @return integer
  382.      */
  383.     public function getRpos()
  384.     {
  385.         return $this->rpos;
  386.     }
  387.     /**
  388.      * Set depth
  389.      *
  390.      * @param integer $depth
  391.      * @return Menu
  392.      */
  393.     public function setDepth($depth)
  394.     {
  395.         $this->depth $depth;
  396.         return $this;
  397.     }
  398.     /**
  399.      * Get type
  400.      *
  401.      * @return string
  402.      */
  403.     public function getType()
  404.     {
  405.         return $this->type;
  406.     }
  407.     /**
  408.      * Set position
  409.      *
  410.      * @param integer $position
  411.      * @return Menu
  412.      */
  413.     public function setPosition($position)
  414.     {
  415.         $this->position $position;
  416.         return $this;
  417.     }
  418.     /**
  419.      * Get position
  420.      *
  421.      * @return integer
  422.      */
  423.     public function getPosition()
  424.     {
  425.         return $this->position;
  426.     }
  427.     /**
  428.      * Set enabled
  429.      *
  430.      * @param boolean $enabled
  431.      * @return Menu
  432.      */
  433.     public function setEnabled($enabled)
  434.     {
  435.         $this->enabled = (bool)$enabled;
  436.         return $this;
  437.     }
  438.     /**
  439.      * Get enabled
  440.      *
  441.      * @return boolean
  442.      */
  443.     public function getEnabled()
  444.     {
  445.         return $this->enabled;
  446.     }
  447.     /**
  448.      * Set visibleFor
  449.      *
  450.      * @param string $visibleFor
  451.      * @return Menu
  452.      */
  453.     public function setVisibleFor($visibleFor)
  454.     {
  455.         $this->visibleFor $visibleFor;
  456.         return $this;
  457.     }
  458.     /**
  459.      * Get visibleFor
  460.      *
  461.      * @return string
  462.      */
  463.     public function getVisibleFor()
  464.     {
  465.         return $this->visibleFor;
  466.     }
  467.     /**
  468.      * Set quickFlags
  469.      *
  470.      * @param \CDev\SimpleCMS\Model\Menu\QuickFlags $quickFlags
  471.      * @return Menu
  472.      */
  473.     public function setQuickFlags(\CDev\SimpleCMS\Model\Menu\QuickFlags $quickFlags null)
  474.     {
  475.         $this->quickFlags $quickFlags;
  476.         return $this;
  477.     }
  478.     /**
  479.      * Get quickFlags
  480.      *
  481.      * @return \CDev\SimpleCMS\Model\Menu\QuickFlags
  482.      */
  483.     public function getQuickFlags()
  484.     {
  485.         return $this->quickFlags;
  486.     }
  487.     /**
  488.      * Add children
  489.      *
  490.      * @param \CDev\SimpleCMS\Model\Menu $children
  491.      * @return Menu
  492.      */
  493.     public function addChildren(\CDev\SimpleCMS\Model\Menu $children)
  494.     {
  495.         $this->children[] = $children;
  496.         return $this;
  497.     }
  498.     /**
  499.      * Get children
  500.      *
  501.      * @return \Doctrine\Common\Collections\Collection
  502.      */
  503.     public function getChildren()
  504.     {
  505.         return $this->children;
  506.     }
  507.     /**
  508.      * Get parent
  509.      *
  510.      * @return \CDev\SimpleCMS\Model\Menu
  511.      */
  512.     public function getParent()
  513.     {
  514.         return $this->parent;
  515.     }
  516. }