<?php
namespace Boldr\Cms\CmsBundle\Entity;
use Boldr\Cms\CmsBundle\Translation\TranslationCollection;
use Boldr\Cms\CmsBundle\Permalink\PermalinkableInterface;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\{ Collection, ArrayCollection };
use DateTimeImmutable;
/**
* @ORM\Entity
* @ORM\Table(name="cms_page")
* @ORM\HasLifecycleCallbacks
*/
class Page implements AttachmentUserInterface, PermalinkableInterface
{
use Traits\AttachmentUserTrait;
/**
* @ORM\Id
* @ORM\Column(type="integer", options={"unsigned": true})
* @ORM\GeneratedValue(strategy="AUTO")
*/
private ?int $id = null;
/**
* @ORM\ManyToOne(targetEntity=self::class)
*/
private ?Page $parent = null;
/**
* @ORM\OneToMany(targetEntity=PageTranslation::class, mappedBy="page", cascade={"persist", "remove"}, orphanRemoval=true)
* @var Collection<int, PageTranslation>
*/
private Collection $translations;
/**
* @ORM\OneToMany(targetEntity=PageAttachmentUsage::class, mappedBy="page", fetch="EXTRA_LAZY", cascade={"persist", "remove"}, orphanRemoval=true)
* @var Collection<int, AttachmentUsage>
*/
private Collection $attachmentUsages;
/**
* @ORM\ManyToOne(targetEntity=Upload::class)
*/
private ?Upload $primaryImage = null;
/**
* @ORM\Column(type="string", length="50", options={"default": "default"})
*/
private string $layout = 'default';
/**
* @ORM\Column(type="text", nullable=true)
*/
private ?string $serializedLayout = null;
/**
* @ORM\Column(type="datetime_immutable", options={"default": "CURRENT_TIMESTAMP"})
*/
private DateTimeImmutable $timeLastUpdated;
/**
* @ORM\Column(type="boolean", options={"default": false})
*/
private bool $synchronizeTranslations = true;
public function __construct()
{
$this->translations = new ArrayCollection;
$this->attachmentUsages = new ArrayCollection;
$this->timeLastUpdated = new DateTimeImmutable;
}
/**
* @ORM\Column(type="string", nullable=true)
*/
private ?string $type = null;
public function getId(): ?int { return $this->id; }
public function getParent(): ?Page { return $this->parent; }
public function setParent(?Page $parent): void
{
$p = $parent;
while ($p !== null)
{
if ($p === $this)
{
throw new \Exception('Cannot set parent (to child) of self');
}
$p = $p->getParent();
}
$this->parent = $parent;
}
public function getPrimaryImage(): ?Upload { return $this->primaryImage; }
public function setPrimaryImage(?Upload $primaryImage): void { $this->primaryImage = $primaryImage; }
public function getSerializedLayout(): ?string { return $this->serializedLayout; }
public function setSerializedLayout(?string $serializedLayout): void { $this->serializedLayout = $serializedLayout; }
public function getType(): ?string { return $this->type; }
public function setType(?string $type): void { $this->type = $type; }
/**
* @return TranslationCollection<PageTranslation>
*/
public function getTranslations(): TranslationCollection
{
return new TranslationCollection($this, $this->translations, PageTranslation::class);
}
public function updateAttachmentSearchTexts(): void
{
// Set the search text to all translations of the title
$searchText = '';
foreach ($this->translations as $translation)
{
$searchText .= $translation->getTitle() . ' ';
}
foreach ($this->attachmentUsages as $attachmentUsage)
{
$attachmentUsage->setSearchText($searchText);
}
}
public function createAttachmentUsage(Upload $upload): AttachmentUsage
{
return new PageAttachmentUsage($upload, $this);
}
/**
* @ORM\PreUpdate
* @ORM\PrePersist
*/
public function removeEmptyTranslations(): void
{
$this->getTranslations()->removeEmptyTranslations();
}
/**
* @return Collection<int, AttachmentUsage>
*/
public function getAttachmentUsages(): Collection { return $this->attachmentUsages; }
public function getLayout(): string { return $this->layout; }
public function setLayout(string $layout): void { $this->layout = $layout; }
public function getTimeLastUpdated(): DateTimeImmutable { return $this->timeLastUpdated; }
public function setTimeLastUpdated(DateTimeImmutable $timeLastUpdated): void { $this->timeLastUpdated = $timeLastUpdated; }
public function getSynchronizeTranslations(): bool { return $this->synchronizeTranslations; }
public function setSynchronizeTranslations(bool $synchronizeTranslations): void { $this->synchronizeTranslations = $synchronizeTranslations; }
/**
* @deprecated
*/
public function getPublished(): bool { return true; }
/**
* @deprecated
*/
public function setPublished(bool $published): void { }
}