src/Entity/Content/Menus/MenuItem.php line 15

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