vendor/kunstmaan/menu-bundle/Entity/BaseMenuItem.php line 16

Open in your IDE?
  1. <?php
  2. namespace Kunstmaan\MenuBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Gedmo\Mapping\Annotation as Gedmo;
  5. use Kunstmaan\AdminBundle\Entity\EntityInterface;
  6. use Kunstmaan\NodeBundle\Entity\NodeTranslation;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  9. /**
  10.  * @ORM\MappedSuperclass()
  11.  */
  12. #[ORM\MappedSuperclass]
  13. abstract class BaseMenuItem implements EntityInterface
  14. {
  15.     const TYPE_PAGE_LINK 'page_link';
  16.     const TYPE_URL_LINK 'url_link';
  17.     /**
  18.      * @var array
  19.      */
  20.     public static $types = [
  21.         self::TYPE_PAGE_LINK,
  22.         self::TYPE_URL_LINK,
  23.     ];
  24.     /**
  25.      * @ORM\Id
  26.      * @ORM\Column(type="bigint")
  27.      * @ORM\GeneratedValue(strategy="AUTO")
  28.      */
  29.     #[ORM\Id]
  30.     #[ORM\Column(name'id'type'bigint')]
  31.     #[ORM\GeneratedValue('AUTO')]
  32.     protected $id;
  33.     /**
  34.      * @var Menu
  35.      *
  36.      * @ORM\ManyToOne(targetEntity="Kunstmaan\MenuBundle\Entity\Menu", inversedBy="items")
  37.      * @ORM\JoinColumn(name="menu_id", referencedColumnName="id")
  38.      * @Gedmo\TreeRoot(identifierMethod="getMenu")
  39.      */
  40.     #[ORM\ManyToOne(targetEntityMenu::class, inversedBy'items')]
  41.     #[ORM\JoinColumn(name'menu_id'referencedColumnName'id')]
  42.     #[Gedmo\TreeRoot(identifierMethod'getMenu')]
  43.     #[Assert\NotNull]
  44.     protected $menu;
  45.     /**
  46.      * @var string
  47.      *
  48.      * @ORM\Column(name="type", type="string", length=15, nullable=true)
  49.      */
  50.     #[ORM\Column(name'type'type'string'length15nullabletrue)]
  51.     #[Assert\NotBlank]
  52.     #[Assert\Length(max15)]
  53.     protected $type;
  54.     /**
  55.      * @var NodeTranslation
  56.      *
  57.      * @ORM\ManyToOne(targetEntity="Kunstmaan\NodeBundle\Entity\NodeTranslation")
  58.      * @ORM\JoinColumn(name="node_translation_id", referencedColumnName="id")
  59.      */
  60.     #[ORM\ManyToOne(targetEntityNodeTranslation::class)]
  61.     #[ORM\JoinColumn(name'node_translation_id'referencedColumnName'id')]
  62.     protected $nodeTranslation;
  63.     /**
  64.      * @var string
  65.      *
  66.      * @ORM\Column(name="title", type="string", nullable=true)
  67.      */
  68.     #[ORM\Column(name'title'type'string'nullabletrue)]
  69.     #[Assert\Length(max255)]
  70.     protected $title;
  71.     /**
  72.      * @var string
  73.      *
  74.      * @ORM\Column(name="url", type="string", nullable=true)
  75.      */
  76.     #[ORM\Column(name'url'type'string'nullabletrue)]
  77.     #[Assert\Length(max255)]
  78.     protected $url;
  79.     /**
  80.      * @var bool
  81.      *
  82.      * @ORM\Column(name="new_window", type="boolean", nullable=true)
  83.      */
  84.     #[ORM\Column(name'new_window'type'boolean'nullabletrue)]
  85.     protected $newWindow;
  86.     /**
  87.      * @var int
  88.      *
  89.      * @Gedmo\TreeLeft
  90.      * @ORM\Column(name="lft", type="integer")
  91.      */
  92.     #[Gedmo\TreeLeft]
  93.     #[ORM\Column(name'lft'type'integer')]
  94.     protected $lft;
  95.     /**
  96.      * @var int
  97.      *
  98.      * @Gedmo\TreeLevel
  99.      * @ORM\Column(name="lvl", type="integer")
  100.      */
  101.     #[Gedmo\TreeLevel]
  102.     #[ORM\Column(name'lvl'type'integer')]
  103.     protected $lvl;
  104.     /**
  105.      * @var int
  106.      *
  107.      * @Gedmo\TreeRight
  108.      * @ORM\Column(name="rgt", type="integer")
  109.      */
  110.     #[Gedmo\TreeRight]
  111.     #[ORM\Column(name'rgt'type'integer')]
  112.     protected $rgt;
  113.     /**
  114.      * @return int
  115.      */
  116.     public function getId()
  117.     {
  118.         return $this->id;
  119.     }
  120.     /**
  121.      * @param int $id The unique identifier
  122.      *
  123.      * @return $this
  124.      */
  125.     public function setId($id)
  126.     {
  127.         $this->id $id;
  128.         return $this;
  129.     }
  130.     /**
  131.      * @return Menu
  132.      */
  133.     public function getMenu()
  134.     {
  135.         return $this->menu;
  136.     }
  137.     /**
  138.      * @param Menu $menu
  139.      *
  140.      * @return MenuItem
  141.      */
  142.     public function setMenu($menu)
  143.     {
  144.         $this->menu $menu;
  145.         return $this;
  146.     }
  147.     /**
  148.      * @return string
  149.      */
  150.     public function getType()
  151.     {
  152.         return $this->type;
  153.     }
  154.     /**
  155.      * @param string $type
  156.      *
  157.      * @return MenuItem
  158.      */
  159.     public function setType($type)
  160.     {
  161.         $this->type $type;
  162.         return $this;
  163.     }
  164.     /**
  165.      * @return NodeTranslation
  166.      */
  167.     public function getNodeTranslation()
  168.     {
  169.         return $this->nodeTranslation;
  170.     }
  171.     /**
  172.      * @param NodeTranslation $nodeTranslation
  173.      *
  174.      * @return MenuItem
  175.      */
  176.     public function setNodeTranslation($nodeTranslation)
  177.     {
  178.         $this->nodeTranslation $nodeTranslation;
  179.         return $this;
  180.     }
  181.     /**
  182.      * @return string
  183.      */
  184.     public function getTitle()
  185.     {
  186.         return $this->title;
  187.     }
  188.     /**
  189.      * @param string $title
  190.      *
  191.      * @return MenuItem
  192.      */
  193.     public function setTitle($title)
  194.     {
  195.         $this->title $title;
  196.         return $this;
  197.     }
  198.     /**
  199.      * @return string
  200.      */
  201.     public function getUrl()
  202.     {
  203.         return $this->url;
  204.     }
  205.     /**
  206.      * @param string $url
  207.      *
  208.      * @return MenuItem
  209.      */
  210.     public function setUrl($url)
  211.     {
  212.         $this->url $url;
  213.         return $this;
  214.     }
  215.     /**
  216.      * @return bool
  217.      */
  218.     public function isNewWindow()
  219.     {
  220.         return $this->newWindow;
  221.     }
  222.     /**
  223.      * @param bool $newWindow
  224.      *
  225.      * @return MenuItem
  226.      */
  227.     public function setNewWindow($newWindow)
  228.     {
  229.         $this->newWindow $newWindow;
  230.         return $this;
  231.     }
  232.     /**
  233.      * @return int
  234.      */
  235.     public function getLft()
  236.     {
  237.         return $this->lft;
  238.     }
  239.     /**
  240.      * @param int $lft
  241.      *
  242.      * @return MenuItem
  243.      */
  244.     public function setLft($lft)
  245.     {
  246.         $this->lft $lft;
  247.         return $this;
  248.     }
  249.     /**
  250.      * @return int
  251.      */
  252.     public function getLvl()
  253.     {
  254.         return $this->lvl;
  255.     }
  256.     /**
  257.      * @param int $lvl
  258.      *
  259.      * @return MenuItem
  260.      */
  261.     public function setLvl($lvl)
  262.     {
  263.         $this->lvl $lvl;
  264.         return $this;
  265.     }
  266.     /**
  267.      * @return int
  268.      */
  269.     public function getRgt()
  270.     {
  271.         return $this->rgt;
  272.     }
  273.     /**
  274.      * @param int $rgt
  275.      *
  276.      * @return MenuItem
  277.      */
  278.     public function setRgt($rgt)
  279.     {
  280.         $this->rgt $rgt;
  281.         return $this;
  282.     }
  283.     /**
  284.      * @return string
  285.      */
  286.     public function getDisplayTitle()
  287.     {
  288.         if ($this->getType() == self::TYPE_PAGE_LINK) {
  289.             if (!\is_null($this->getTitle())) {
  290.                 return $this->getTitle();
  291.             }
  292.             return $this->getNodeTranslation()->getTitle();
  293.         }
  294.         return $this->getTitle();
  295.     }
  296.     /**
  297.      * @return string
  298.      */
  299.     public function getDisplayUrl()
  300.     {
  301.         if ($this->getType() == self::TYPE_PAGE_LINK) {
  302.             return $this->getNodeTranslation()->getUrl();
  303.         }
  304.         return $this->getUrl();
  305.     }
  306.     /**
  307.      * @return bool
  308.      */
  309.     public function isOnline()
  310.     {
  311.         if ($this->getType() == self::TYPE_URL_LINK) {
  312.             return true;
  313.         }
  314.         if ($this->getNodeTranslation()->isOnline() && !$this->getNodeTranslation()->getNode()->isDeleted()) {
  315.             return true;
  316.         }
  317.         return false;
  318.     }
  319.     #[Assert\Callback]
  320.     public function validateEntity(ExecutionContextInterface $context)
  321.     {
  322.         if ($this->getType() == self::TYPE_PAGE_LINK && !$this->getNodeTranslation()) {
  323.             $context->buildViolation('Please select a page')
  324.               ->atPath('nodeTranslation')
  325.               ->addViolation();
  326.         } elseif ($this->getType() == self::TYPE_URL_LINK) {
  327.             if ($this->getTitle() === '') {
  328.                 $context->buildViolation('Please set the link title')
  329.                   ->atPath('title')
  330.                   ->addViolation();
  331.             }
  332.             if ($this->getUrl() === '') {
  333.                 $context->buildViolation('Please set the link URL')
  334.                   ->atPath('url')
  335.                   ->addViolation();
  336.             }
  337.         }
  338.     }
  339.     /**
  340.      * Return string representation of entity
  341.      *
  342.      * @return string
  343.      */
  344.     public function __toString()
  345.     {
  346.         return (string) $this->getId();
  347.     }
  348. }