src/Entity/Content/MenuItem.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Content;
  3. use Symfony\Component\Validator\Constraints AS Constraints;
  4. use Symfony\Component\Validator\Constraints AS Assert;
  5. use Doctrine\ORM\Mapping AS ORM;
  6. use Ramsey\Uuid\Uuid;
  7. /**
  8.  * @ORM\Entity(repositoryClass="App\Repository\Content\MenuItemRepository")
  9.  * @ORM\Table(name="content_menuitem")
  10.  */
  11. class MenuItem
  12. {
  13.     /**
  14.     * @ORM\Id
  15.     * @ORM\GeneratedValue(strategy="AUTO")
  16.     * @ORM\Column(type="integer")
  17.     */
  18.     private $id;
  19.     /**
  20.      * @ORM\Column(type="uuid")
  21.      * @Assert\Uuid
  22.      */
  23.      protected $uuid;
  24.     /**
  25.      * @var \DateTime
  26.      *
  27.      * @ORM\Column(type="datetime", nullable=false)
  28.      */
  29.     private $created;
  30.     /**
  31.      * @var \DateTime
  32.      *
  33.      * @ORM\Column(type="datetime", nullable=false)
  34.      */
  35.     private $modified;
  36.     /**
  37.      * @ORM\Column(type="string", length=60, nullable=true)
  38.      */
  39.     private $label;
  40.     
  41.     /**
  42.      * @ORM\Column(type="string", length=255, nullable=true)
  43.      */
  44.     private $url;
  45.     /**
  46.      * @ORM\Column(type="boolean", nullable=false)
  47.      */
  48.     private $autoUpdate;
  49.     
  50.     /**
  51.      * @ORM\Column(type="boolean", nullable=false)
  52.      */
  53.     private $openNewWindow;
  54.     /**
  55.      * @ORM\Column(type="integer", length=10, nullable=false)
  56.      */
  57.     private $displayOrder;
  58.     /**
  59.      * @ORM\ManyToOne(targetEntity="Menu", inversedBy="menuItems")
  60.      * @ORM\JoinColumn(name="menu_id", referencedColumnName="id")
  61.      **/
  62.     private $menu;
  63.     /**
  64.      * @ORM\ManyToOne(targetEntity="Item")
  65.      * @ORM\JoinColumn(name="item_id", referencedColumnName="id", onDelete="cascade")
  66.      **/
  67.     private $item;
  68.     /**
  69.      * @ORM\ManyToOne(targetEntity="MenuItem", inversedBy="children")
  70.      * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
  71.      **/
  72.     private $parent;
  73.     /**
  74.      * @ORM\OneToMany(targetEntity="MenuItem", mappedBy="parent", cascade={"all"})
  75.      * @ORM\OrderBy({"displayOrder" = "ASC"})
  76.      **/
  77.     private $children;
  78.     public function __construct()
  79.     {
  80.         // UUID
  81.         if(!$this->getUuid())
  82.         {
  83.             $uuid Uuid::uuid1();
  84.             $this->setUuid($uuid->toString());
  85.         }
  86.         // Set some defaults
  87.         $this->setCreated(new \DateTime());
  88.         $this->setModified(new \DateTime());
  89.         if(!$this->getOpenNewWindow())
  90.             $this->setOpenNewWindow(false);
  91.         if(!$this->getDisplayOrder())
  92.             $this->setDisplayOrder(time());
  93.         $this->children = array();
  94.         $this->autoUpdate true;
  95.     }
  96.     public function __toString()
  97.     {
  98.         return strval($this->getId());
  99.     }
  100.     public function getFriendlyLabel()
  101.     {
  102.         // Already got a label?
  103.         if($this->getLabel())
  104.             return $this->getLabel();
  105.         // Got a page?
  106.         if($this->getItem())
  107.             return $this->getItem()->getName();
  108.         return "Unknown";
  109.     }
  110.     public function getFriendlyTarget()
  111.     {
  112.         // Got a page?
  113.         if($this->getItem())
  114.         {
  115.             if($this->getItem()->getLanguage())
  116.                 $locale $this->getItem()->getLanguage()->getLocale();
  117.             else 
  118.                 $locale null;
  119.             // Temp menu fix for single language
  120.             if($this->getItem()->getLanguage()->getId() == 2)
  121.                 $locale null;
  122.             // Is it the index?
  123.             if($this->getItem()->getPath() == "index")
  124.                 return '/' .  ($locale $locale '');
  125.             else
  126.                 return '/' . ($locale $locale '/' '') . $this->getItem()->getPath();
  127.         }
  128.         elseif($this->getUrl())
  129.             return $this->getUrl();
  130.         return "#";
  131.     }
  132.     public function getId()
  133.     {
  134.         return $this->id;
  135.     }
  136.     public function setUuid($uuid)
  137.     {
  138.         $this->uuid $uuid;
  139.         return $this;
  140.     }
  141.     public function getUuid()
  142.     {
  143.         return $this->uuid;
  144.     }
  145.     public function setCreated(\DateTime $created)
  146.     {
  147.         $this->created $created;
  148.     }
  149.     public function getCreated()
  150.     {
  151.         return $this->created;
  152.     }
  153.     public function setModified(\DateTime $modified)
  154.     {
  155.         $this->modified $modified;
  156.     }
  157.     public function getModified()
  158.     {
  159.         return $this->modified;
  160.     }
  161.     public function getLabel()
  162.     {
  163.         return $this->label;
  164.     }
  165.     public function setLabel(string $label null)
  166.     {
  167.         $this->label $label;
  168.     }
  169.     
  170.     public function getUrl()
  171.     {
  172.         return $this->url;
  173.     }
  174.     public function setUrl(string $url null)
  175.     {
  176.         $this->url $url;
  177.     }
  178.     public function setMenu(\App\Entity\Content\Menu $menu null)
  179.     {
  180.         $this->menu $menu;
  181.         $menu->addMenuItem($this);
  182.         return $this;
  183.     }
  184.     public function getMenu()
  185.     {
  186.         return $this->menu;
  187.     }
  188.     public function getDisplayOrder()
  189.     {
  190.         return $this->displayOrder;
  191.     }
  192.     public function setDisplayOrder(int $displayOrder)
  193.     {
  194.         $this->displayOrder $displayOrder;
  195.     }
  196.     public function setItem(\App\Entity\Content\Item $item null)
  197.     {
  198.         $this->item $item;
  199.         return $this;
  200.     }
  201.     public function getItem()
  202.     {
  203.         return $this->item;
  204.     }
  205.     public function setParent(\App\Entity\Content\MenuItem $menuItem null)
  206.     {
  207.         $this->parent $menuItem;
  208.         return $this;
  209.     }
  210.     public function getParent()
  211.     {
  212.         return $this->parent;
  213.     }
  214.     public function addChild(\App\Entity\Content\MenuItem $menuItem)
  215.     {
  216.         if(!in_array($menuItem, (array)$this->children))
  217.             $this->children[] = $menuItem;
  218.         if($menuItem->getParent() != $this)
  219.             $menuItem->setParent($this);
  220.         return $this;
  221.     }
  222.     public function removeChild(\App\Entity\Content\MenuItem $menuItem)
  223.     {
  224.         $this->children->removeElement($menuItem);
  225.         $menuItem->setMenu(null);
  226.     }
  227.     public function getChildren()
  228.     {
  229.         return $this->children;
  230.     }
  231.     public function getAutoUpdate()
  232.     {
  233.         return $this->autoUpdate;
  234.     }
  235.     public function setAutoUpdate($autoUpdate)
  236.     {
  237.         $this->autoUpdate $autoUpdate;
  238.         return $this;
  239.     }
  240.     public function getOpenNewWindow()
  241.     {
  242.         return $this->openNewWindow;
  243.     }
  244.     public function setOpenNewWindow($openNewWindow)
  245.     {
  246.         $this->openNewWindow $openNewWindow;
  247.         return $this;
  248.     }
  249. }