<?php
/**
* This file is part of Symfony Keen Template Project.
* (c) 2021 One AM SRL
*/
namespace App\Entity;
use App\Entity\Embeddable\File\CoverFile;
use App\Entity\Page\PageCallToAction;
use App\Repository\PageRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface;
use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
use Knp\DoctrineBehaviors\Model\Sluggable\SluggableTrait;
use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\String\Slugger\AsciiSlugger;
#[ORM\Entity(repositoryClass: PageRepository::class)]
class Page implements SluggableInterface, TimestampableInterface
{
use TimestampableTrait;
use SluggableTrait;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 255)]
#[Groups(['ajax'])]
private $name;
#[ORM\Column(type: 'text', nullable: true)]
#[Groups(['ajax'])]
private $content;
#[ORM\Column(type: 'text')]
#[Groups(['ajax'])]
private $introduction;
#[ORM\Column(type: 'boolean')]
#[Groups(['ajax'])]
private $visible;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $metaTitle;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $metaDescription;
#[ORM\Embedded(class: CoverFile::class)]
#[Groups(['ajax'])]
private $cover;
#[ORM\Embedded(class: CoverFile::class)]
#[Groups(['ajax'])]
private $background;
#[ORM\OneToMany(mappedBy: 'page', targetEntity: PageCallToAction::class, cascade: ['persist'])]
#[ORM\OrderBy(['position' => 'ASC'])]
private $pageCallToActions;
#[ORM\ManyToOne(targetEntity: Product::class, inversedBy: 'pages')]
#[ORM\JoinColumn(nullable: false)]
#[Groups(['ajax'])]
private $product;
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
private $parent;
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)]
private $children;
public function __construct()
{
$this->visible = false;
$this->cover = new CoverFile();
$this->pageCallToActions = new ArrayCollection();
$this->children = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(?string $content): self
{
$this->content = $content;
return $this;
}
public function getIntroduction(): ?string
{
return $this->introduction;
}
public function setIntroduction(?string $introduction): self
{
$this->introduction = $introduction;
return $this;
}
public function shouldGenerateUniqueSlugs(): bool
{
return true;
}
public function generateSlugValue($values): string
{
if ($this->getParent()) {
return
(new AsciiSlugger())->slug($this->getParent()->getName(), $this->getSlugDelimiter())->lower()->toString().'/'.
(new AsciiSlugger())->slug($this->getName(), $this->getSlugDelimiter())->lower()->toString()
;
}
return (new AsciiSlugger())->slug($this->getName(), $this->getSlugDelimiter())->lower()->toString();
}
public function getSluggableFields(): array
{
return ['name'];
}
public function getMetaTitle(): ?string
{
return $this->metaTitle;
}
public function setMetaTitle(?string $metaTitle): self
{
$this->metaTitle = $metaTitle;
return $this;
}
public function getMetaDescription(): ?string
{
return $this->metaDescription;
}
public function setMetaDescription(?string $metaDescription): self
{
$this->metaDescription = $metaDescription;
return $this;
}
public function getCover(): ?CoverFile
{
return $this->cover;
}
public function setCover(?CoverFile $cover): self
{
$this->cover = $cover;
return $this;
}
public function getBackground(): ?CoverFile
{
return $this->background;
}
public function setBackground(?CoverFile $background): self
{
$this->background = $background;
return $this;
}
public function getVisible(): bool
{
return $this->visible;
}
public function setVisible(bool $visible): self
{
$this->visible = $visible;
return $this;
}
/**
* @return Collection<int, PageCallToAction>
*/
public function getPageCallToActions(): Collection
{
return $this->pageCallToActions;
}
public function addPageCallToAction(PageCallToAction $pageCallToAction): self
{
if (!$this->pageCallToActions->contains($pageCallToAction)) {
$this->pageCallToActions[] = $pageCallToAction;
$pageCallToAction->setPage($this);
}
return $this;
}
public function removePageCallToAction(PageCallToAction $pageCallToAction): self
{
if ($this->pageCallToActions->removeElement($pageCallToAction)) {
// set the owning side to null (unless already changed)
if ($pageCallToAction->getPage() === $this) {
$pageCallToAction->setPage(null);
}
}
return $this;
}
public function getProduct(): ?Product
{
return $this->product;
}
public function setProduct(?Product $product): self
{
$this->product = $product;
return $this;
}
#[Groups(['ajax'])]
public function getCreatedAt(): \DateTimeInterface
{
return $this->createdAt;
}
public function __toString(): string
{
return $this->getName();
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection<int, self>
*/
public function getChildren(): Collection
{
return $this->children;
}
public function addChild(self $child): self
{
if (!$this->children->contains($child)) {
$this->children[] = $child;
$child->setParent($this);
}
return $this;
}
public function removeChild(self $child): self
{
if ($this->children->removeElement($child)) {
// set the owning side to null (unless already changed)
if ($child->getParent() === $this) {
$child->setParent(null);
}
}
return $this;
}
}