Skip to content

Instantly share code, notes, and snippets.

@helios-ag
Created December 26, 2013 01:52
Show Gist options
  • Save helios-ag/8128775 to your computer and use it in GitHub Desktop.
Save helios-ag/8128775 to your computer and use it in GitHub Desktop.
<?php
namespace FM\MenuBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Knp\Menu\NodeInterface;
use Knp\Menu\MenuItem as BaseMenuItem;
use Knp\Menu\ItemInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Table(name="fm_menu")
* @Gedmo\Tree(type="nested")
* @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")*
*/
class MenuItem extends BaseMenuItem implements NodeInterface
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
protected $id;
/**
* @ORM\Column(name="name", type="string", length=255)
*/
protected $name;
/**
* @Gedmo\TreeLeft
* @ORM\Column(name="lft", type="integer")
*/
protected $lft;
/**
* @Gedmo\TreeLevel
* @ORM\Column(name="lvl", type="integer")
*/
protected $lvl;
/**
* @Gedmo\TreeRight
* @ORM\Column(name="rgt", type="integer")
*/
protected $rgt;
/**
* @Gedmo\TreeRoot
* @ORM\Column(name="root", type="integer", nullable=true)
*/
protected $root;
/**
* @Gedmo\TreeParent
* @ORM\ManyToOne(targetEntity="MenuItem", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $parent;
/**
* @ORM\OneToMany(targetEntity="MenuItem", mappedBy="parent")
* @ORM\OrderBy({"lft" = "ASC"})
*/
protected $children;
/**
* @var
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $uri;
/**
* @var
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Length(max=90)
*/
protected $label;
/**
* @ORM\ManyToOne(targetEntity="FM\PageBundle\Entity\Page", inversedBy="links")
* @ORM\JoinColumn(name="page_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
protected $page;
/**
* @var
* @ORM\Column(type="boolean")
*/
protected $internal;
/**
* @var \Doctrine\Common\Collections\ArrayCollection
* @ORM\OneToMany(targetEntity="FM\PostBundle\Entity\Category", mappedBy="menu")
*/
protected $categories;
/**
* @return string
*/
public function __toString()
{
$prefix = "";
for ($i=2; $i<= $this->lvl; $i++){
$prefix .= "-";
}
return $prefix . $this->name;
}
/**
* @return string
*/
public function getLaveledTitle()
{
return (string)$this;
}
/**
*
*/
public function __construct()
{
$this->children = new ArrayCollection();
$this->internal = false;
$this->categories = new ArrayCollection();
}
/**
* @return \Doctrine\Common\Collections\ArrayCollection
*/
public function getChildren()
{
return $this->children;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param \FM\MenuBundle\Entity\MenuItem|\Knp\Menu\ItemInterface|null $parent
* @return \Knp\Menu\ItemInterface|void
*/
public function setParent(ItemInterface $parent = null)
{
$this->parent = $parent;
}
/**
* @return array
*/
public function getOptions()
{
$options =
array(
'uri' => $this->uri,
'label' => $this->label,
'attributes' => array('lvl' => $this->lvl, 'title' =>$this->name),
'linkAttributes' => array(),
'childrenAttributes' => array(),
'labelAttributes' => array(),
'display' => true,
'displayChildren' => true,
);
if($this->getInternal() && is_object($this->getPage()))
{
$slug = $this->getPage()->getSlug();
$route = array('route' => 'page',
'routeParameters' => array('slug' => $slug));
$options = array_merge($options,$route);
}
return $options;
}
/**
* @return mixed
*/
public function getParent()
{
return $this->parent;
}
/**
* @param $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @return
*/
public function getLabel()
{
return $this->label;
}
/**
* @param $label
*/
public function setLabel($label)
{
$this->label = $label;
}
/**
* @return
*/
public function getUri()
{
return $this->uri;
}
/**
* @param MenuItem $children
*/
public function addChildren(MenuItem $children)
{
$this->children[] = $children;
$children->setParent($this);
}
/**
* @param $pos
*/
public function removeChildren($pos)
{
unset($this->children[$pos]);
}
/**
* @param $uri
*/
public function setUri($uri)
{
$this->uri = $uri;
}
/**
* @param $children
*/
public function setChildren(array $children)
{
$this->children = $children;
foreach($children as $child)
$child->setParent($this);
}
/**
* @return
*/
public function getPriority()
{
return $this->priority;
}
/**
* @param $priority
*/
public function setPriority($priority)
{
$this->priority = $priority;
}
/**
* @return
*/
public function getPage()
{
return $this->page;
}
/**
* @param $page
*/
public function setPage($page)
{
$this->page = $page;
}
/**
* @return bool
*/
public function getInternal()
{
return $this->internal;
}
/**
* @param $internal
*/
public function setInternal($internal)
{
$this->internal = $internal;
}
/**
* @return \Doctrine\Common\Collections\ArrayCollection
*/
public function getCategories()
{
return $this->categories;
}
/**
* @param \Doctrine\Common\Collections\ArrayCollection $categories
*/
public function setCategories($categories)
{
$this->categories = $categories;
}
/**
* @param null $isCurrent
*/
public function setIsCurrent($isCurrent)
{
$this->isCurrent = $isCurrent;
}
/**
* @return null
*/
public function getIsCurrent()
{
return $this->isCurrent;
}
/**
* @param mixed $lft
*/
public function setLft($lft)
{
$this->lft = $lft;
}
/**
* @return mixed
*/
public function getLft()
{
return $this->lft;
}
/**
* @param mixed $lvl
*/
public function setLvl($lvl)
{
$this->lvl = $lvl;
}
/**
* @return mixed
*/
public function getLvl()
{
return $this->lvl;
}
/**
* @param mixed $rgt
*/
public function setRgt($rgt)
{
$this->rgt = $rgt;
}
/**
* @return mixed
*/
public function getRgt()
{
return $this->rgt;
}
/**
* @param mixed $root
*/
public function setRoot($root)
{
$this->root = $root;
}
/**
* @return mixed
*/
public function getRoot()
{
return $this->root;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment