vendor/score/cms/src/Entity/Menu.php line 15

Open in your IDE?
  1. <?php
  2. namespace Score\CmsBundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Score\BaseBundle\Entity\BaseEntity;
  6. use Score\CmsBundle\Entity\Page\Page;
  7. /**
  8.  * @ORM\Entity
  9.  * @ORM\Table(name="cms_menu")
  10.  * @ORM\HasLifecycleCallbacks()
  11.  */
  12. class Menu extends BaseEntity {
  13.     /**
  14.      * @ORM\Id
  15.      * @ORM\Column(type="integer")
  16.      * @ORM\GeneratedValue(strategy="AUTO")
  17.      */
  18.     protected $id;
  19.     /**
  20.      * @ORM\Column(type="string", length=200)
  21.      */
  22.     protected $name;
  23.     /**
  24.      * @ORM\Column(type="string", length=1000 , nullable = true)
  25.      */
  26.     protected $link;
  27.     /**
  28.      * @ORM\Column(type="integer", nullable = true)
  29.      */
  30.     protected $lvl;
  31.     /**
  32.      * @ORM\Column(type="integer", nullable = true)
  33.      */
  34.     protected $sortOrder;
  35.     /**
  36.      * @ORM\Column(name="m_type", type="string", length=250, nullable = true)
  37.      */
  38.     protected $type;
  39.     /**
  40.      * @ORM\Column(type="string", nullable = true)
  41.      */
  42.     protected $lang;
  43.     /**
  44.      * @ORM\Column(type="integer", nullable = true)
  45.      */
  46.     protected $rootId;
  47.     /**
  48.      * @ORM\Column(type="string", nullable = true)
  49.      */
  50.     protected $linkType;
  51.     /**
  52.      * @ORM\Column(type="string", nullable = true)
  53.      */
  54.     protected $linkTypeData;
  55.     /**
  56.      * @ORM\OneToMany(targetEntity="\Score\CmsBundle\Entity\Multisite\SiteItemMenu", mappedBy="menu",cascade={"persist","remove"})
  57.      */
  58.     protected $siteItemMenu = [];
  59.     protected $multisite;
  60.     /**
  61.      * @ORM\ManyToOne(targetEntity="\Score\CmsBundle\Entity\Page\Page", inversedBy="menus")
  62.      * @ORM\JoinColumn(name="m_page_id", referencedColumnName="id", nullable=true)
  63.      */
  64.     protected $page = null;
  65.     /**
  66.      * @ORM\Column(name="parent_id", type="integer", nullable = true)
  67.      */
  68.     protected $parentId;
  69.     /**
  70.      * @ORM\ManyToOne(targetEntity="Menu", inversedBy="children")
  71.      * @ORM\JoinColumn(name="parent_id2", referencedColumnName="id")
  72.      */
  73.     protected $parent;
  74.     /**
  75.      * @ORM\OneToMany(targetEntity="Menu", mappedBy="parent")
  76.      * @ORM\OrderBy({"sortOrder"="ASC"})
  77.      */
  78.     protected $children;
  79.     /**
  80.      * Constructor
  81.      */
  82.     public function __construct() {
  83.         $this->children = new ArrayCollection();
  84.     }
  85.     public function getId() {
  86.         return $this->id;
  87.     }
  88.     public function getName() {
  89.         if ($this->getPage()) {
  90.             return $this->getPage()->getMenuName();
  91.         }
  92.         return $this->name;
  93.     }
  94.     public function getLink() {
  95.         if ($this->getPage()) {
  96.             return $this->getPage()->getSeoId();
  97.         }
  98.         return $this->link;
  99.     }
  100.     public function getParentId() {
  101.         return $this->parentId;
  102.     }
  103.     public function getLvl() {
  104.         return $this->lvl;
  105.     }
  106.     public function getSortOrder() {
  107.         return $this->sortOrder;
  108.     }
  109.     public function getLang() {
  110.         return $this->lang;
  111.     }
  112.     public function setName($name) {
  113.         $this->name = $name;
  114.     }
  115.     public function setLink($link) {
  116.         $this->link = $link;
  117.     }
  118.     public function setParentId($parentId) {
  119.         $this->parentId = $parentId;
  120.     }
  121.     public function setLvl($lvl) {
  122.         $this->lvl = $lvl;
  123.     }
  124.     public function setSortOrder($sortOrder) {
  125.         $this->sortOrder = $sortOrder;
  126.     }
  127.     public function setLang($lang) {
  128.         $this->lang = $lang;
  129.     }
  130.     public function getRootId() {
  131.         return $this->rootId;
  132.     }
  133.     public function setRootId($rootId) {
  134.         $this->rootId = $rootId;
  135.     }
  136.     public function getLinkType() {
  137.         return $this->linkType;
  138.     }
  139.     public function getLinkTypeData() {
  140.         return $this->linkTypeData;
  141.     }
  142.     public function setLinkType($linkType) {
  143.         $this->linkType = $linkType;
  144.     }
  145.     public function setLinkTypeData($linkTypeData) {
  146.         $this->linkTypeData = $linkTypeData;
  147.     }
  148.     public function getLinkTypeId() {
  149.         if ($this->getPage()) {
  150.             return $this->getPage()->getId();
  151.         }
  152.         if ('page' == $this->getLinkType()) {
  153.             $data = json_decode($this->getLinkTypeData(), true);
  154.             return $data['id'];
  155.         } else {
  156.             return null;
  157.         }
  158.     }
  159.     public function getPublicLink() {
  160.         if ($this->getPage()) {
  161.             return $this->getPage()->getSeoId();
  162.         }
  163.         if ('page' == $this->getLinkType()) {
  164.             //return  $this->getLink();
  165.             $routeInfo = json_decode($this->getLinkTypeData(), true);
  166.             return $routeInfo['seoId'];
  167.         } else {
  168.             return $this->getLink();
  169.         }
  170.     }
  171.     /**
  172.      * Get the value of siteItemMenu
  173.      */
  174.     public function getSiteItemMenu() {
  175.         return $this->siteItemMenu;
  176.     }
  177.     /**
  178.      * Set the value of siteItemMenu
  179.      *
  180.      * @return  self
  181.      */
  182.     public function setSiteItemMenu($siteItemMenu) {
  183.         $this->siteItemMenu = $siteItemMenu;
  184.         return $this;
  185.     }
  186.     public function getType() {
  187.         return $this->type;
  188.     }
  189.     public function setType($type) {
  190.         $this->type = $type;
  191.         return $this;
  192.     }
  193.     public function setPage(Page $page = null) {
  194.         $this->page = $page;
  195.         return $this;
  196.     }
  197.     /**
  198.      * @return Page
  199.      */
  200.     public function getPage() {
  201.         return $this->page;
  202.     }
  203.     public function setParent(Menu $parent = null) {
  204.         $this->parent = $parent;
  205.         return $this;
  206.     }
  207.     public function getParent(): ?Menu {
  208.         return $this->parent;
  209.     }
  210.     public function addChild(Menu $child): self {
  211.         if (!$this->children->contains($child)) {
  212.             $this->children[] = $child;
  213.             $child->setParent($this);
  214.         }
  215.         return $this;
  216.     }
  217.     public function removeChild(Menu $child): self {
  218.         $this->children->removeElement($child);
  219.         $child->setParent(null);
  220.         return $this;
  221.     }
  222.     public function getChildren() {
  223.         return $this->children;
  224.     }
  225.     public function getAllChildren() {
  226.         $children = $this->getChildren()->toArray();
  227.         $subChildren = [];
  228.         foreach ($this->getChildren() as $ch) {
  229.             foreach ($ch->getAllChildren() as $sub) {
  230.                 $subChildren[] = $sub;
  231.             }
  232.         }
  233.         $colection = new ArrayCollection(
  234.             array_merge($children, $subChildren)
  235.         );
  236.         return $colection;
  237.     }
  238.     public function getAllParents($withRoot = false) {
  239.         $parent = $this->getParent();
  240.         $parents = [];
  241.         while ($parent) {
  242.             if ($parent->getRootId() || $withRoot) {
  243.                 $parents[] = $parent;
  244.             }
  245.             $parent = $parent->getParent();
  246.         }
  247.         $colection = new ArrayCollection($parents);
  248.         return $colection;
  249.     }
  250. }