vendor/fri0z/mldev-base-bundle/src/Entity/Page.php line 25

Open in your IDE?
  1. <?php
  2. namespace MLDev\BaseBundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\Common\Collections\Criteria;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Gedmo\Mapping\Annotation as Gedmo;
  8. use Gedmo\Sluggable\Handler\TreeSlugHandler;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. use MLDev\BaseBundle\Annotation\Uploadable;
  11. use MLDev\BaseBundle\Repository\PageRepository;
  12. use MLDev\BaseBundle\Contract\SeoSite\SeoSiteInfoInterface;
  13. /**
  14.  * @Gedmo\Tree(type="nested")
  15.  *
  16.  * @ORM\Table(name="MLDev_Page")
  17.  * @ORM\Entity(repositoryClass=PageRepository::class)
  18.  * @ORM\HasLifecycleCallbacks()
  19.  */
  20. class Page
  21. {
  22.     const TYPE_LOCAL 'LOCAL';
  23.     const TYPE_EXTERNAL 'EXTERNAL';
  24.     const DEFAULT_PAGE_ID 1;
  25.     use Traits\Timestampable;
  26.     /**
  27.      * @ORM\Id
  28.      * @ORM\Column(name="id", type="integer")
  29.      * @ORM\GeneratedValue(strategy="AUTO")
  30.      */
  31.     private $id;
  32.     /**
  33.      * @ORM\Column(type="string", nullable=false)
  34.      */
  35.     private $type self::TYPE_LOCAL;
  36.     /**
  37.      * @ORM\ManyToOne(targetEntity=Page::class, inversedBy="childrens", fetch="EXTRA_LAZY")
  38.      * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
  39.      * @Gedmo\TreeParent
  40.      */
  41.     private $parent;
  42.     /**
  43.      * @ORM\OneToMany(targetEntity=Page::class, mappedBy="parent", cascade={"persist","remove"}, fetch="EXTRA_LAZY")
  44.      * @ORM\OrderBy({"priority" = "ASC", "id" = "ASC"})
  45.      */
  46.     private $childrens;
  47.     /**
  48.      * @ORM\Column(type="string", length=255, nullable=true)
  49.      */
  50.     private $externalLink;
  51.     /**
  52.      * @ORM\Column(type="string", length=255, nullable=false)
  53.      */
  54.     private $name;
  55.     /**
  56.      * @Gedmo\Slug(fields={"name"}, updatable=false, unique=true, unique_base="parent", separator="-")
  57.      *
  58.      * @ORM\Column(type="string", length=255, nullable=false)
  59.      */
  60.     private $alias;
  61.     /**
  62.      * @Gedmo\Slug(handlers={
  63.      *      @Gedmo\SlugHandler(class=TreeSlugHandler::class, options={
  64.      *          @Gedmo\SlugHandlerOption(name="prefix", value="/"),
  65.      *          @Gedmo\SlugHandlerOption(name="parentRelationField", value="parent"),
  66.      *          @Gedmo\SlugHandlerOption(name="separator", value="/"),
  67.      *          @Gedmo\SlugHandlerOption(name="urilize", value=true)
  68.      *      })
  69.      * }, fields={"alias"})
  70.      *
  71.      * @ORM\Column(type="string", length=255, nullable=false, unique=true)
  72.      */
  73.     private $route;
  74.     /**
  75.      * @ORM\Column(type="text", nullable=true)
  76.      */
  77.     private $description null;
  78.     /**
  79.      * @ORM\Column(type="integer", name="priority", options={"default": 100})
  80.      */
  81.     private $priority;
  82.     /**
  83.      * @ORM\Column(name="is_active", type="boolean", options={"default": 1})
  84.      */
  85.     private $isActive true;
  86.     /**
  87.      * @ORM\Column(type="string", name="image", nullable=true)
  88.      *
  89.      * @Uploadable(directoryAlias="page")
  90.      * @Assert\File(mimeTypes={"image/jpeg", "image/jpg", "image/png", "image/svg", "image/svg+xml"})
  91.      */
  92.     private $image null;
  93.     /**
  94.      * @ORM\ManyToOne(targetEntity=PageTemplate::class, fetch="EXTRA_LAZY")
  95.      * @ORM\JoinColumn(name="template_id", referencedColumnName="id")
  96.      */
  97.     private $template;
  98.     /**
  99.      * @ORM\ManyToOne(targetEntity=PageTemplate::class, fetch="EXTRA_LAZY")
  100.      * @ORM\JoinColumn(name="layout_template_id", referencedColumnName="id", onDelete="CASCADE")
  101.      */
  102.     private $layoutTemplate;
  103.     /**
  104.      * @ORM\ManyToMany(targetEntity=Menu::class, fetch="EXTRA_LAZY", inversedBy="pages")
  105.      * @ORM\JoinTable(
  106.      *      name="MLDev_Menu_Page",
  107.      *      joinColumns={
  108.      *          @ORM\JoinColumn(name="page_id", referencedColumnName="id", onDelete="CASCADE")
  109.      *      },
  110.      *      inverseJoinColumns={
  111.      *          @ORM\JoinColumn(name="menu_id", referencedColumnName="id", onDelete="CASCADE")
  112.      *      }
  113.      * )
  114.      */
  115.     private $menu;
  116.     /**
  117.      * @ORM\OneToMany(targetEntity=PageContent::class, mappedBy="page", cascade={"persist"}, fetch="EXTRA_LAZY", orphanRemoval=true)
  118.      * @ORM\OrderBy({"priority" = "ASC", "id" = "ASC"})
  119.      */
  120.     private $pageContents;
  121.     /**
  122.      * @Gedmo\TreeLeft
  123.      * @ORM\Column(type="integer")
  124.      */
  125.     private $lft;
  126.     /**
  127.      * @Gedmo\TreeLevel
  128.      * @ORM\Column(type="integer")
  129.      */
  130.     private $lvl;
  131.     /**
  132.      * @Gedmo\TreeRight
  133.      * @ORM\Column(type="integer")
  134.      */
  135.     private $rgt;
  136.     /**
  137.      * @Gedmo\TreeRoot
  138.      * @ORM\ManyToOne(targetEntity=Page::class)
  139.      * @ORM\JoinColumn(referencedColumnName="id", onDelete="CASCADE")
  140.      */
  141.     private $root;
  142.     /**
  143.      * @ORM\ManyToMany(targetEntity=SeoSiteInfoInterface::class, cascade={"persist"})
  144.      * @ORM\JoinTable(
  145.      *     name="MLDev_Page_SeoInfo",
  146.      *     joinColumns={
  147.      *          @ORM\JoinColumn(name="page_id", referencedColumnName="id", onDelete="CASCADE")
  148.      *     },
  149.      *     inverseJoinColumns={
  150.      *          @ORM\JoinColumn(name="seo_info_id", referencedColumnName="id", onDelete="CASCADE")
  151.      *     }
  152.      * )
  153.      */
  154.     private $seoInfo;
  155.     /**
  156.      * @return ArrayCollection
  157.      */
  158.     public function getSeoInfo()
  159.     {
  160.         return $this->seoInfo;
  161.     }
  162.     /**
  163.      * @param ArrayCollection $seoInfo
  164.      */
  165.     public function setSeoInfo($seoInfo): void
  166.     {
  167.         $this->seoInfo $seoInfo;
  168.     }
  169.     /**
  170.      * Constructor
  171.      */
  172.     public function __construct()
  173.     {
  174.         $this->menu = new ArrayCollection();
  175.         $this->childrens = new ArrayCollection();
  176.         $this->pageContents = new ArrayCollection();
  177.         $this->seoInfo = new ArrayCollection();
  178.     }
  179.     /**
  180.      * Clone method
  181.      */
  182.     public function __clone()
  183.     {
  184.         if ($this->id) {
  185.             $this->id null;
  186.             $menu = clone $this->menu;
  187.             $seoInfo = clone $this->seoInfo;
  188.             $pageContents = clone $this->pageContents;
  189.             $this->menu $menu;
  190.             $this->seoInfo = new ArrayCollection();
  191.             $this->childrens = new ArrayCollection();
  192.             $this->pageContents = new ArrayCollection();
  193.             if (!$pageContents->isEmpty()) {
  194.                 foreach ($pageContents as $pageContent) {
  195.                     $clonePageContent = clone $pageContent;
  196.                     $clonePageContent->setPage($this);
  197.                     $this->addPageContent($clonePageContent);
  198.                 }
  199.             }
  200.             if (!$seoInfo->isEmpty()) {
  201.                 foreach ($seoInfo as $seoItem) {
  202.                     $cloned = clone $seoItem;
  203.                     $this->seoInfo->add($cloned);
  204.                 }
  205.             }
  206.         }
  207.     }
  208.     /**
  209.      * @param Menu $menu
  210.      * @return Page
  211.      */
  212.     public function addMenu(Menu $menu)
  213.     {
  214.         $this->menu[] = $menu;
  215.         return $this;
  216.     }
  217.     /**
  218.      * @param Menu $menu
  219.      */
  220.     public function removeMenu(Menu $menu)
  221.     {
  222.         $this->menu->removeElement($menu);
  223.     }
  224.     /**
  225.      * @return Collection
  226.      */
  227.     public function getMenu()
  228.     {
  229.         return $this->menu;
  230.     }
  231.     /**
  232.      * @return string
  233.      */
  234.     public function __toString()
  235.     {
  236.         return (string)$this->getParent();
  237.     }
  238.     /**
  239.      * Get id
  240.      *
  241.      * @return int
  242.      */
  243.     public function getId()
  244.     {
  245.         return $this->id;
  246.     }
  247.     /**
  248.      * @return string
  249.      */
  250.     public function getType(): string
  251.     {
  252.         return $this->type;
  253.     }
  254.     /**
  255.      * @param string $type
  256.      */
  257.     public function setType(string $type): void
  258.     {
  259.         $this->type $type;
  260.     }
  261.     /**
  262.      * Set parent
  263.      *
  264.      * @param Page $parent
  265.      *
  266.      * @return Page
  267.      */
  268.     public function setParent(Page $parent null)
  269.     {
  270.         $this->parent $parent;
  271.         return $this;
  272.     }
  273.     /**
  274.      * Get parent
  275.      *
  276.      * @return Page
  277.      */
  278.     public function getParent()
  279.     {
  280.         return $this->parent;
  281.     }
  282.     /**
  283.      * Add childrens
  284.      *
  285.      * @param Page $childrens
  286.      * @return Page
  287.      */
  288.     public function addChildren(Page $childrens)
  289.     {
  290.         $this->childrens[] = $childrens;
  291.         return $this;
  292.     }
  293.     /**
  294.      * Remove childrens
  295.      *
  296.      * @param Page $childrens
  297.      */
  298.     public function removeChildren(Page $childrens)
  299.     {
  300.         $this->childrens->removeElement($childrens);
  301.     }
  302.     /**
  303.      * Remove childrens
  304.      *
  305.      * @param Page[] $childrens
  306.      * @return $this;
  307.      */
  308.     public function removeChildrens($childrens)
  309.     {
  310.         foreach ($childrens as $children) {
  311.             $this->childrens->removeElement($children);
  312.         }
  313.         return $this;
  314.     }
  315.     /**
  316.      * Adds childrens
  317.      *
  318.      * @param Page[] $childrens
  319.      * @return $this;
  320.      */
  321.     public function addChildrens($childrens)
  322.     {
  323.         foreach ($childrens as $children) {
  324.             $this->childrens->add($children);
  325.         }
  326.         return $this;
  327.     }
  328.     /**
  329.      * Get childrens
  330.      *
  331.      * @return Collection
  332.      */
  333.     public function getChildrens()
  334.     {
  335.         return $this->childrens;
  336.     }
  337.     /**
  338.      * @return mixed
  339.      */
  340.     public function getExternalLink()
  341.     {
  342.         return $this->externalLink;
  343.     }
  344.     /**
  345.      * @param mixed $externalLink
  346.      */
  347.     public function setExternalLink($externalLink): void
  348.     {
  349.         $this->externalLink $externalLink;
  350.     }
  351.     /**
  352.      * Set name
  353.      *
  354.      * @param string $name
  355.      *
  356.      * @return Page
  357.      */
  358.     public function setName($name)
  359.     {
  360.         $this->name $name;
  361.         return $this;
  362.     }
  363.     /**
  364.      * Get name
  365.      *
  366.      * @return string
  367.      */
  368.     public function getName()
  369.     {
  370.         return $this->name;
  371.     }
  372.     /**
  373.      * Set alias
  374.      *
  375.      * @param string $alias
  376.      */
  377.     public function setAlias($alias)
  378.     {
  379.         $this->alias $alias;
  380.     }
  381.     /**
  382.      * Get alias
  383.      *
  384.      * @return string
  385.      */
  386.     public function getAlias()
  387.     {
  388.         return $this->alias;
  389.     }
  390.     /**
  391.      * @return null
  392.      */
  393.     public function getDescription()
  394.     {
  395.         return $this->description;
  396.     }
  397.     /**
  398.      * @param null $description
  399.      */
  400.     public function setDescription($description): void
  401.     {
  402.         $this->description $description;
  403.     }
  404.     /**
  405.      * @return mixed
  406.      */
  407.     public function getRoute()
  408.     {
  409.         return $this->route;
  410.     }
  411.     /**
  412.      * @param mixed $route
  413.      */
  414.     public function setRoute($route): void
  415.     {
  416.         $this->route $route;
  417.     }
  418.     /**
  419.      * @return mixed
  420.      */
  421.     public function getUri()
  422.     {
  423.         global $kernel;
  424.         if ('AppCache' == get_class($kernel)) {
  425.             $kernel $kernel->getKernel();
  426.         }
  427.         if ($this->type === self::TYPE_EXTERNAL) {
  428.             return $this->getExternalLink();
  429.         }
  430.         // get default locate
  431.         $locate $kernel->getContainer()->getParameter('locale');
  432.         return ($this->route == '/' $locate) ? '/' preg_replace('/^\/' $locate '(.*?)$/'"\\1"$this->route);
  433.     }
  434.     /**
  435.      * Set priority
  436.      *
  437.      * @param int $priority
  438.      *
  439.      * @return Page
  440.      */
  441.     public function setPriority($priority)
  442.     {
  443.         $this->priority $priority;
  444.         return $this;
  445.     }
  446.     /**
  447.      * Get priority
  448.      *
  449.      * @return int
  450.      */
  451.     public function getPriority()
  452.     {
  453.         return $this->priority;
  454.     }
  455.     /**
  456.      * @return bool
  457.      */
  458.     public function isActive()
  459.     {
  460.         return $this->isActive;
  461.     }
  462.     /**
  463.      * @param bool $isActive
  464.      */
  465.     public function setIsActive($isActive)
  466.     {
  467.         $this->isActive $isActive;
  468.     }
  469.     /**
  470.      * @return null
  471.      */
  472.     public function getImage()
  473.     {
  474.         return $this->image;
  475.     }
  476.     /**
  477.      * @param null $image
  478.      */
  479.     public function setImage($image): void
  480.     {
  481.         $this->image $image;
  482.     }
  483.     /**
  484.      * @return PageTemplate
  485.      */
  486.     public function getTemplate()
  487.     {
  488.         return $this->template;
  489.     }
  490.     /**
  491.      * Set template
  492.      *
  493.      * @param PageTemplate $template
  494.      * @return Page
  495.      */
  496.     public function setTemplate(PageTemplate $template null)
  497.     {
  498.         $this->template $template;
  499.         return $this;
  500.     }
  501.     /**
  502.      * Set baseTemplate
  503.      *
  504.      * @param PageTemplate $layoutTemplate
  505.      * @return Page
  506.      */
  507.     public function setLayoutTemplate(PageTemplate $layoutTemplate null)
  508.     {
  509.         $this->layoutTemplate $layoutTemplate;
  510.         return $this;
  511.     }
  512.     /**
  513.      * Get baseTemplate
  514.      *
  515.      * @return PageTemplate
  516.      */
  517.     public function getLayoutTemplate()
  518.     {
  519.         return $this->layoutTemplate;
  520.     }
  521.     /**
  522.      * @return ArrayCollection
  523.      */
  524.     public function getPageContents()
  525.     {
  526.         return $this->pageContents;
  527.     }
  528.     /**
  529.      * @param $pageContents
  530.      * @return $this
  531.      */
  532.     public function addPageContents($pageContents)
  533.     {
  534.         foreach ($pageContents as $pageContent) {
  535.             if (!$this->pageContents->contains($pageContent)) {
  536.                 $this->pageContents->add($pageContent);
  537.             }
  538.         }
  539.         return $this;
  540.     }
  541.     /**
  542.      * @param PageContent $pageContent
  543.      * @return $this
  544.      */
  545.     public function addPageContent(PageContent $pageContent)
  546.     {
  547.         $this->pageContents[] = $pageContent;
  548.         return $this;
  549.     }
  550.     /**
  551.      * @param $pageContents
  552.      * @return $this
  553.      */
  554.     public function removePageContents($pageContents)
  555.     {
  556.         foreach ($pageContents as $pageContent) {
  557.             $this->pageContents->removeElement($pageContent);
  558.         }
  559.         return $this;
  560.     }
  561.     /**
  562.      * @param PageContent $pageContent
  563.      * @return $this
  564.      */
  565.     public function removePageContent(PageContent $pageContent)
  566.     {
  567.         $this->pageContents->removeElement($pageContent);
  568.         return $this;
  569.     }
  570.     /**
  571.      * @return ArrayCollection|Collection
  572.      */
  573.     public function getWidgets()
  574.     {
  575.         $criteria Criteria::create()->where(
  576.             Criteria::expr()->eq('isActive'true)
  577.         );
  578.         $criteria->orderBy([
  579.             'priority' => Criteria::ASC,
  580.             'id' => Criteria::ASC,
  581.         ]);
  582.         return $this->getPageContents()->matching($criteria);
  583.     }
  584.     /**
  585.      * @return mixed
  586.      */
  587.     public function getLft()
  588.     {
  589.         return $this->lft;
  590.     }
  591.     /**
  592.      * @param mixed $lft
  593.      */
  594.     public function setLft($lft)
  595.     {
  596.         $this->lft $lft;
  597.     }
  598.     /**
  599.      * @return mixed
  600.      */
  601.     public function getLvl()
  602.     {
  603.         return $this->lvl;
  604.     }
  605.     /**
  606.      * @param mixed $lvl
  607.      */
  608.     public function setLvl($lvl)
  609.     {
  610.         $this->lvl $lvl;
  611.     }
  612.     /**
  613.      * @return mixed
  614.      */
  615.     public function getRgt()
  616.     {
  617.         return $this->rgt;
  618.     }
  619.     /**
  620.      * @param mixed $rgt
  621.      */
  622.     public function setRgt($rgt)
  623.     {
  624.         $this->rgt $rgt;
  625.     }
  626.     /**
  627.      * @return mixed
  628.      */
  629.     public function getRoot()
  630.     {
  631.         return $this->root;
  632.     }
  633.     /**
  634.      * @param mixed $root
  635.      */
  636.     public function setRoot($root)
  637.     {
  638.         $this->root $root;
  639.     }
  640. }