<?php
namespace MLDev\BaseBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Sluggable\Handler\TreeSlugHandler;
use Symfony\Component\Validator\Constraints as Assert;
use MLDev\BaseBundle\Annotation\Uploadable;
use MLDev\BaseBundle\Repository\PageRepository;
use MLDev\BaseBundle\Contract\SeoSite\SeoSiteInfoInterface;
/**
* @Gedmo\Tree(type="nested")
*
* @ORM\Table(name="MLDev_Page")
* @ORM\Entity(repositoryClass=PageRepository::class)
* @ORM\HasLifecycleCallbacks()
*/
class Page
{
const TYPE_LOCAL = 'LOCAL';
const TYPE_EXTERNAL = 'EXTERNAL';
const DEFAULT_PAGE_ID = 1;
use Traits\Timestampable;
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", nullable=false)
*/
private $type = self::TYPE_LOCAL;
/**
* @ORM\ManyToOne(targetEntity=Page::class, inversedBy="childrens", fetch="EXTRA_LAZY")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
* @Gedmo\TreeParent
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity=Page::class, mappedBy="parent", cascade={"persist","remove"}, fetch="EXTRA_LAZY")
* @ORM\OrderBy({"priority" = "ASC", "id" = "ASC"})
*/
private $childrens;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $externalLink;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
private $name;
/**
* @Gedmo\Slug(fields={"name"}, updatable=false, unique=true, unique_base="parent", separator="-")
*
* @ORM\Column(type="string", length=255, nullable=false)
*/
private $alias;
/**
* @Gedmo\Slug(handlers={
* @Gedmo\SlugHandler(class=TreeSlugHandler::class, options={
* @Gedmo\SlugHandlerOption(name="prefix", value="/"),
* @Gedmo\SlugHandlerOption(name="parentRelationField", value="parent"),
* @Gedmo\SlugHandlerOption(name="separator", value="/"),
* @Gedmo\SlugHandlerOption(name="urilize", value=true)
* })
* }, fields={"alias"})
*
* @ORM\Column(type="string", length=255, nullable=false, unique=true)
*/
private $route;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description = null;
/**
* @ORM\Column(type="integer", name="priority", options={"default": 100})
*/
private $priority;
/**
* @ORM\Column(name="is_active", type="boolean", options={"default": 1})
*/
private $isActive = true;
/**
* @ORM\Column(type="string", name="image", nullable=true)
*
* @Uploadable(directoryAlias="page")
* @Assert\File(mimeTypes={"image/jpeg", "image/jpg", "image/png", "image/svg", "image/svg+xml"})
*/
private $image = null;
/**
* @ORM\ManyToOne(targetEntity=PageTemplate::class, fetch="EXTRA_LAZY")
* @ORM\JoinColumn(name="template_id", referencedColumnName="id")
*/
private $template;
/**
* @ORM\ManyToOne(targetEntity=PageTemplate::class, fetch="EXTRA_LAZY")
* @ORM\JoinColumn(name="layout_template_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $layoutTemplate;
/**
* @ORM\ManyToMany(targetEntity=Menu::class, fetch="EXTRA_LAZY", inversedBy="pages")
* @ORM\JoinTable(
* name="MLDev_Menu_Page",
* joinColumns={
* @ORM\JoinColumn(name="page_id", referencedColumnName="id", onDelete="CASCADE")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="menu_id", referencedColumnName="id", onDelete="CASCADE")
* }
* )
*/
private $menu;
/**
* @ORM\OneToMany(targetEntity=PageContent::class, mappedBy="page", cascade={"persist"}, fetch="EXTRA_LAZY", orphanRemoval=true)
* @ORM\OrderBy({"priority" = "ASC", "id" = "ASC"})
*/
private $pageContents;
/**
* @Gedmo\TreeLeft
* @ORM\Column(type="integer")
*/
private $lft;
/**
* @Gedmo\TreeLevel
* @ORM\Column(type="integer")
*/
private $lvl;
/**
* @Gedmo\TreeRight
* @ORM\Column(type="integer")
*/
private $rgt;
/**
* @Gedmo\TreeRoot
* @ORM\ManyToOne(targetEntity=Page::class)
* @ORM\JoinColumn(referencedColumnName="id", onDelete="CASCADE")
*/
private $root;
/**
* @ORM\ManyToMany(targetEntity=SeoSiteInfoInterface::class, cascade={"persist"})
* @ORM\JoinTable(
* name="MLDev_Page_SeoInfo",
* joinColumns={
* @ORM\JoinColumn(name="page_id", referencedColumnName="id", onDelete="CASCADE")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="seo_info_id", referencedColumnName="id", onDelete="CASCADE")
* }
* )
*/
private $seoInfo;
/**
* @return ArrayCollection
*/
public function getSeoInfo()
{
return $this->seoInfo;
}
/**
* @param ArrayCollection $seoInfo
*/
public function setSeoInfo($seoInfo): void
{
$this->seoInfo = $seoInfo;
}
/**
* Constructor
*/
public function __construct()
{
$this->menu = new ArrayCollection();
$this->childrens = new ArrayCollection();
$this->pageContents = new ArrayCollection();
$this->seoInfo = new ArrayCollection();
}
/**
* Clone method
*/
public function __clone()
{
if ($this->id) {
$this->id = null;
$menu = clone $this->menu;
$seoInfo = clone $this->seoInfo;
$pageContents = clone $this->pageContents;
$this->menu = $menu;
$this->seoInfo = new ArrayCollection();
$this->childrens = new ArrayCollection();
$this->pageContents = new ArrayCollection();
if (!$pageContents->isEmpty()) {
foreach ($pageContents as $pageContent) {
$clonePageContent = clone $pageContent;
$clonePageContent->setPage($this);
$this->addPageContent($clonePageContent);
}
}
if (!$seoInfo->isEmpty()) {
foreach ($seoInfo as $seoItem) {
$cloned = clone $seoItem;
$this->seoInfo->add($cloned);
}
}
}
}
/**
* @param Menu $menu
* @return Page
*/
public function addMenu(Menu $menu)
{
$this->menu[] = $menu;
return $this;
}
/**
* @param Menu $menu
*/
public function removeMenu(Menu $menu)
{
$this->menu->removeElement($menu);
}
/**
* @return Collection
*/
public function getMenu()
{
return $this->menu;
}
/**
* @return string
*/
public function __toString()
{
return (string)$this->getParent();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getType(): string
{
return $this->type;
}
/**
* @param string $type
*/
public function setType(string $type): void
{
$this->type = $type;
}
/**
* Set parent
*
* @param Page $parent
*
* @return Page
*/
public function setParent(Page $parent = null)
{
$this->parent = $parent;
return $this;
}
/**
* Get parent
*
* @return Page
*/
public function getParent()
{
return $this->parent;
}
/**
* Add childrens
*
* @param Page $childrens
* @return Page
*/
public function addChildren(Page $childrens)
{
$this->childrens[] = $childrens;
return $this;
}
/**
* Remove childrens
*
* @param Page $childrens
*/
public function removeChildren(Page $childrens)
{
$this->childrens->removeElement($childrens);
}
/**
* Remove childrens
*
* @param Page[] $childrens
* @return $this;
*/
public function removeChildrens($childrens)
{
foreach ($childrens as $children) {
$this->childrens->removeElement($children);
}
return $this;
}
/**
* Adds childrens
*
* @param Page[] $childrens
* @return $this;
*/
public function addChildrens($childrens)
{
foreach ($childrens as $children) {
$this->childrens->add($children);
}
return $this;
}
/**
* Get childrens
*
* @return Collection
*/
public function getChildrens()
{
return $this->childrens;
}
/**
* @return mixed
*/
public function getExternalLink()
{
return $this->externalLink;
}
/**
* @param mixed $externalLink
*/
public function setExternalLink($externalLink): void
{
$this->externalLink = $externalLink;
}
/**
* Set name
*
* @param string $name
*
* @return Page
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set alias
*
* @param string $alias
*/
public function setAlias($alias)
{
$this->alias = $alias;
}
/**
* Get alias
*
* @return string
*/
public function getAlias()
{
return $this->alias;
}
/**
* @return null
*/
public function getDescription()
{
return $this->description;
}
/**
* @param null $description
*/
public function setDescription($description): void
{
$this->description = $description;
}
/**
* @return mixed
*/
public function getRoute()
{
return $this->route;
}
/**
* @param mixed $route
*/
public function setRoute($route): void
{
$this->route = $route;
}
/**
* @return mixed
*/
public function getUri()
{
global $kernel;
if ('AppCache' == get_class($kernel)) {
$kernel = $kernel->getKernel();
}
if ($this->type === self::TYPE_EXTERNAL) {
return $this->getExternalLink();
}
// get default locate
$locate = $kernel->getContainer()->getParameter('locale');
return ($this->route == '/' . $locate) ? '/' : preg_replace('/^\/' . $locate . '(.*?)$/', "\\1", $this->route);
}
/**
* Set priority
*
* @param int $priority
*
* @return Page
*/
public function setPriority($priority)
{
$this->priority = $priority;
return $this;
}
/**
* Get priority
*
* @return int
*/
public function getPriority()
{
return $this->priority;
}
/**
* @return bool
*/
public function isActive()
{
return $this->isActive;
}
/**
* @param bool $isActive
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
}
/**
* @return null
*/
public function getImage()
{
return $this->image;
}
/**
* @param null $image
*/
public function setImage($image): void
{
$this->image = $image;
}
/**
* @return PageTemplate
*/
public function getTemplate()
{
return $this->template;
}
/**
* Set template
*
* @param PageTemplate $template
* @return Page
*/
public function setTemplate(PageTemplate $template = null)
{
$this->template = $template;
return $this;
}
/**
* Set baseTemplate
*
* @param PageTemplate $layoutTemplate
* @return Page
*/
public function setLayoutTemplate(PageTemplate $layoutTemplate = null)
{
$this->layoutTemplate = $layoutTemplate;
return $this;
}
/**
* Get baseTemplate
*
* @return PageTemplate
*/
public function getLayoutTemplate()
{
return $this->layoutTemplate;
}
/**
* @return ArrayCollection
*/
public function getPageContents()
{
return $this->pageContents;
}
/**
* @param $pageContents
* @return $this
*/
public function addPageContents($pageContents)
{
foreach ($pageContents as $pageContent) {
if (!$this->pageContents->contains($pageContent)) {
$this->pageContents->add($pageContent);
}
}
return $this;
}
/**
* @param PageContent $pageContent
* @return $this
*/
public function addPageContent(PageContent $pageContent)
{
$this->pageContents[] = $pageContent;
return $this;
}
/**
* @param $pageContents
* @return $this
*/
public function removePageContents($pageContents)
{
foreach ($pageContents as $pageContent) {
$this->pageContents->removeElement($pageContent);
}
return $this;
}
/**
* @param PageContent $pageContent
* @return $this
*/
public function removePageContent(PageContent $pageContent)
{
$this->pageContents->removeElement($pageContent);
return $this;
}
/**
* @return ArrayCollection|Collection
*/
public function getWidgets()
{
$criteria = Criteria::create()->where(
Criteria::expr()->eq('isActive', true)
);
$criteria->orderBy([
'priority' => Criteria::ASC,
'id' => Criteria::ASC,
]);
return $this->getPageContents()->matching($criteria);
}
/**
* @return mixed
*/
public function getLft()
{
return $this->lft;
}
/**
* @param mixed $lft
*/
public function setLft($lft)
{
$this->lft = $lft;
}
/**
* @return mixed
*/
public function getLvl()
{
return $this->lvl;
}
/**
* @param mixed $lvl
*/
public function setLvl($lvl)
{
$this->lvl = $lvl;
}
/**
* @return mixed
*/
public function getRgt()
{
return $this->rgt;
}
/**
* @param mixed $rgt
*/
public function setRgt($rgt)
{
$this->rgt = $rgt;
}
/**
* @return mixed
*/
public function getRoot()
{
return $this->root;
}
/**
* @param mixed $root
*/
public function setRoot($root)
{
$this->root = $root;
}
}