<?php
namespace App\CmsBundle\Entity;
use App\CasinoBundle\Entity\Author;
use App\CmsBundle\Enum\LogActionEnum;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* @ORM\Entity(
* repositoryClass="App\CmsBundle\Repository\RouteRepository"
* )
* @ORM\Table(
* name="route",
* indexes={
* @ORM\Index(name="route_site_index", columns={"site_id"}),
* @ORM\Index(name="route_position_index", columns={"position"}),
* @ORM\Index(name="route_parent_route_index", columns={"parent_route_id"}),
* @ORM\Index(name="route_related_route_index", columns={"related_to_id"}),
* @ORM\Index(name="route_author_index", columns={"author_id"}),
* @ORM\Index(name="route_reviewer_index", columns={"reviewer_id"})
* }
* )
*/
class Route implements LogInterface
{
use ParametersTrait, PositionTrait, IdTrait, TitleTrait, SiteTrait, DescriptionTrait, LogTrait;
/**
* @ORM\ManyToOne(targetEntity="App\CmsBundle\Entity\Route", inversedBy="relates", fetch="EXTRA_LAZY")
* @ORM\JoinColumn(nullable=true)
*/
private $relatedTo;
/**
* @ORM\OneToMany(targetEntity="App\CmsBundle\Entity\Route", mappedBy="relatedTo", cascade={"persist"})
*/
private $relates;
/**
* @ORM\Column(type="string", length=255)
*/
private $urlPattern;
/**
* @ORM\ManyToOne(targetEntity="App\CmsBundle\Entity\Site", inversedBy="routes")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*/
private $site;
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="App\CmsBundle\Entity\Widget", mappedBy="route", cascade={"persist"}, orphanRemoval=true)
* @ORM\JoinColumn(nullable=false)
*/
protected $widgets;
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="App\CmsBundle\Entity\Page", mappedBy="route", cascade={"persist","remove"}, orphanRemoval=true)
* @ORM\JoinColumn(nullable=false)
*/
protected $pages;
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="App\CmsBundle\Entity\Template", mappedBy="route", cascade={"persist","remove"}, orphanRemoval=true)
* @ORM\JoinColumn(nullable=false)
*/
protected $templates;
/**
* @ORM\ManyToOne(targetEntity="App\CmsBundle\Entity\Route", inversedBy="childRoutes")
* @ORM\JoinColumn(nullable=true)
*/
private $parentRoute;
/**
* @ORM\OneToMany(targetEntity="App\CmsBundle\Entity\Route", mappedBy="parentRoute")
* @ORM\JoinColumn(nullable=true)
*/
private $childRoutes;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $parameters;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $defaults;
/**
* @ORM\OneToOne(targetEntity="App\CmsBundle\Entity\Directory", mappedBy="route")
* @ORM\JoinColumn(nullable=true)
*/
private $directory;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $pageTitle;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $h1;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $breadcrumbsTitle;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $summaryTitle;
private $canonical;
private $robots;
/**
*
* @ORM\OneToMany(targetEntity="App\CmsBundle\Entity\Bullet", mappedBy="route", cascade={"persist"}, orphanRemoval=true)
* @ORM\JoinColumn(nullable=true)
*/
private Collection $bullet;
/**
* @ORM\ManyToOne(targetEntity="App\CasinoBundle\Entity\Author")
* @ORM\JoinColumn(name="author_id", referencedColumnName="id", nullable=true)
*/
private ?Author $author = null;
/**
* @ORM\ManyToOne(targetEntity="App\CasinoBundle\Entity\Author")
* @ORM\JoinColumn(name="reviewer_id", referencedColumnName="id", nullable=true)
*/
private ?Author $reviewer = null;
public function __construct()
{
$this->widgets = new ArrayCollection();
$this->childRoutes = new ArrayCollection();
$this->parameters = json_encode([]);
$this->defaults = json_encode([]);
$this->pages = new ArrayCollection();
$this->templates = new ArrayCollection();
$this->bullet = new ArrayCollection();
$this->relates = new ArrayCollection();
}
/**
* @return string
*/
public function __toString(): string
{
return $this->title ?? '';
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return string|null
*/
public function getTitle(): ?string
{
return $this->title;
}
/**
* @param string $title
* @return $this
*/
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
/**
* @return string|null
*/
public function getUrlPattern(): ?string
{
return $this->urlPattern;
}
/**
* @param string $urlPattern
* @return $this
*/
public function setUrlPattern(string $urlPattern): self
{
$this->urlPattern = $urlPattern;
return $this;
}
/**
* @return array
*/
public function getDefaults(): array
{
return json_decode($this->defaults, true) ?? [];
}
/**
* @param array $defaults
* @return $this
*/
public function setDefaults(array $defaults): self
{
$this->defaults = json_encode($defaults);
return $this;
}
/**
* @return string|null
*/
public function getDefaultsAsString(): ?string
{
return $this->defaults;
}
/**
* @param string|null $defaults
* @return $this
*/
public function setDefaultsAsString(?string $defaults = null): self
{
$this->defaults = ($defaults) ? $defaults : '[]';
return $this;
}
/**
* @return Collection
*/
public function getWidgets(): Collection
{
return ($this->widgets)
? $this->widgets
: new ArrayCollection();
}
/**
* @param Widget $widget
* @return $this
*/
public function addWidget(Widget $widget): self
{
if (!$this->widgets->contains($widget)) {
$this->widgets[] = $widget;
$widget->setRoute($this);
$this->addCollectionLog(LogActionEnum::ADD, 'widget', $widget);
}
return $this;
}
/**
* @param Widget $widget
* @return $this
*/
public function removeWidget(Widget $widget): self
{
if ($this->widgets->contains($widget)) {
$this->widgets->removeElement($widget);
$widget->setRoute(null);
$this->addCollectionLog(LogActionEnum::REMOVE, 'widget', $widget);
}
return $this;
}
/**
* @param Route|null $route
* @return $this
*/
public function setParentRoute(?Route $route): self
{
$this->parentRoute = $route;
return $this;
}
/**
* @return Route|null
*/
public function getParentRoute(): ?Route
{
return $this->parentRoute;
}
/**
* @return Collection|null
*/
public function getChildRoutes(): ?Collection
{
return $this->childRoutes;
}
/**
* @return Collection
*/
public function getPages(): Collection
{
return ($this->pages)
? $this->pages
: new ArrayCollection();
}
/**
* @param Page $page
* @return $this
*/
public function addPage(Page $page): self
{
if (!$this->pages->contains($page)) {
$this->pages[] = $page;
$page->setRoute($this);
$this->addCollectionLog(LogActionEnum::ADD, 'page', $page);
}
return $this;
}
/**
* @param Page $page
* @return $this
*/
public function removePage(Page $page): self
{
if ($this->pages->contains($page)) {
$this->pages->removeElement($page);
$page->setRoute(null);
$this->addCollectionLog(LogActionEnum::REMOVE, 'page', $page);
}
return $this;
}
/**
* @return Collection
*/
public function getTemplates(): Collection
{
return ($this->templates)
? $this->templates
: new ArrayCollection();
}
/**
* @param Template $template
* @return $this
*/
public function addTemplate(Template $template): self
{
if (!$this->templates->contains($template)) {
$this->templates[] = $template;
$template->setRoute($this);
$this->addCollectionLog(LogActionEnum::ADD, 'template', $template);
}
return $this;
}
/**
* @param Template $template
* @return $this
*/
public function removeTemplate(Template $template): self
{
if ($this->templates->contains($template)) {
$this->templates->removeElement($template);
$template->setRoute(null);
$this->addCollectionLog(LogActionEnum::REMOVE, 'template', $template);
}
return $this;
}
/**
* @return array
*/
public function getAutoReplacedVariables(): array
{
return [
'{pageTitle}' => $this->pageTitle,
'{description}' => $this->description,
'{h1}' => $this->h1,
'{breadcrumbsTitle}' => $this->breadcrumbsTitle,
'{summaryTitle}' => $this->summaryTitle
];
}
/**
* @return string|null
*/
public function getPageTitle(): ?string
{
return $this->pageTitle;
}
/**
* @param string|null $pageTitle
* @return $this
*/
public function setPageTitle(?string $pageTitle): self
{
$this->pageTitle = $pageTitle;
return $this;
}
/**
* @return string|null
*/
public function getH1(): ?string
{
return $this->h1;
}
/**
* @param string|null $h1
* @return $this
*/
public function setH1(?string $h1): self
{
$this->h1 = $h1;
return $this;
}
/**
* @return string|null
*/
public function getBreadcrumbsTitle(): ?string
{
return $this->breadcrumbsTitle;
}
/**
* @param string|null $breadcrumbsTitle
* @return $this
*/
public function setBreadcrumbsTitle(?string $breadcrumbsTitle): self
{
$this->breadcrumbsTitle = $breadcrumbsTitle;
return $this;
}
/**
* @return string|null
*/
public function getSummaryTitle(): ?string
{
return $this->summaryTitle;
}
/**
* @param string|null $summaryTitle
* @return $this
*/
public function setSummaryTitle(?string $summaryTitle): self
{
$this->summaryTitle = $summaryTitle;
return $this;
}
/**
* @return string|null
*/
public function getCanonical(): ?string
{
return $this->canonical;
}
/**
* @param string|null $canonical
* @return $this
*/
public function setCanonical(?string $canonical = null): self
{
$this->canonical = $canonical;
return $this;
}
/**
* @return string|null
*/
public function getRobots(): ?string
{
return $this->robots;
}
/**
* @param Directory|null $directory
* @return $this
*/
public function setDirectory(?Directory $directory = null): self
{
$this->directory = $directory;
return $this;
}
/**
* @return Directory|null
*/
public function getDirectory(): ?Directory
{
return $this->directory;
}
/**
* @return Collection
*/
public function getBullet(): Collection
{
return $this->bullet ?? new ArrayCollection();
}
/**
* @param Bullet $bullet
* @return $this
*/
public function addBullet(Bullet $bullet): self
{
$this->bullet[] = $bullet;
$bullet->setRoute($this);
$this->addCollectionLog(LogActionEnum::ADD, 'bullet', $bullet);
return $this;
}
/**
* @param Bullet $bullet
* @return void
*/
public function removeBullet(Bullet $bullet): void
{
$this->bullet->removeElement($bullet);
$this->addCollectionLog(LogActionEnum::REMOVE, 'bullet', $bullet);
}
/**
* @return Route|null
*/
public function getRelatedTo(): ?Route
{
return $this->relatedTo;
}
/**
* @param Route|null $relatedTo
* @return $this
*/
public function setRelatedTo(?Route $relatedTo): self
{
$this->relatedTo = $relatedTo;
return $this;
}
/**
* @return Collection|Route[]
*/
public function getRelates(): Collection
{
return $this->relates;
}
/**
* @param Route $relate
* @return $this
*/
public function addRelates(Route $relate): self
{
if (!$this->relates->contains($relate)) {
$this->relates[] = $relate;
$relate->setRelatedTo($this);
$this->addCollectionLog(LogActionEnum::ADD, 'relates', $relate);
}
return $this;
}
/**
* @param Route $relate
* @return $this
*/
public function removeRelates(Route $relate): self
{
if ($this->relates->contains($relate)) {
$this->relates->removeElement($relate);
// set the owning side to null (unless already changed)
if ($relate->getRelatedTo() === $this) {
$relate->setRelatedTo(null);
}
$this->addCollectionLog(LogActionEnum::REMOVE, 'relates', $relate);
}
return $this;
}
/**
* @param $action
* @param $property
* @param object $value
* @return $this
*/
public function addCollectionLog($action, $property, object $value): self
{
if (LogActionEnum::exists($action)) {
$log = json_decode($this->log ?? '[]', true);
if ($value instanceof Bullet) {
$log[$property][$action][] = $value->__toArray();
} else {
$log[$property][$action][] = $value->getId();
}
$this->log = json_encode($log);
}
return $this;
}
/**
* @return Author|null
*/
public function getAuthor(): ?Author
{
return $this->author;
}
/**
* @param Author|null $author
* @return $this
*/
public function setAuthor(?Author $author): self
{
$this->author = $author;
return $this;
}
/**
* @return Author|null
*/
public function getReviewer(): ?Author
{
return $this->reviewer;
}
/**
* @param Author|null $reviewer
* @return self
*/
public function setReviewer(?Author $reviewer): self
{
$this->reviewer = $reviewer;
return $this;
}
}