src/Entity/Page.php line 94
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM,
Doctrine\DBAL\Types\Types,
Doctrine\Common\Collections\Collection,
Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Metadata\ApiResource,
ApiPlatform\Metadata\ApiProperty,
ApiPlatform\Metadata\Get,
ApiPlatform\Metadata\GetCollection,
ApiPlatform\Metadata\Post,
ApiPlatform\Metadata\Put,
ApiPlatform\Metadata\Delete,
ApiPlatform\Metadata\ApiFilter,
ApiPlatform\Doctrine\Orm\Filter\OrderFilter,
ApiPlatform\Doctrine\Orm\Filter\SearchFilter,
ApiPlatform\Doctrine\Orm\Filter\ExistsFilter;
use App\Entity\Trait\IdTrait,
App\Entity\Trait\UuidTrait,
App\Entity\Trait\OrdStatTrait,
App\Entity\Trait\TimestampableTrait,
App\Entity\Trait\TranslatableTrait,
App\Entity\Trait\Languages\LanguageableChildTrait,
App\Entity\Trait\ImportIdTrait,
App\Entity\Trait\Languages\SiteLanguagesTrait,
App\Entity\Trait\Media\ImageThumbableTrait,
App\Entity\Interface\TranslatableInterface,
App\Entity\Interface\LanguageableChildInterface,
App\Entity\Interface\OrdStatableInterface,
App\Entity\Interface\CloneableInterface,
App\Entity\Interface\LanguageableInterface,
App\Entity\Interface\ThumbableInterface,
App\Entity\Interface\BlockableInterface,
App\Entity\Interface\BlockInterface;
use App\StateProcessor\CloneProcessor,
App\Security\Voter\CloneVoter,
App\DTO\CloneDTO,
App\Lib\Actions,
App\Filter\IriFilter,
App\Util\ClassUtils,
App\Enum\Language,
App\Repository\PageRepository,
App\Security\Voter\PageVoter;
#[ApiResource(
description: 'Pages',
normalizationContext: ['groups' => [
'read',
'read:' . self::class,
'read:' . self::class . 'Translation'
]],
denormalizationContext: ['groups' => ['write']],
security: 'is_granted("' . Page::class . '")',
order: ['ord' => 'desc'],
operations: [
new GetCollection(),
new Post(
uriTemplate: '/pages/clone',
input: CloneDTO::class,
processor: CloneProcessor::class,
security: 'is_granted("' . Actions::CLONE .'")',
securityMessage: CloneVoter::MESSAGE,
securityPostDenormalize: 'is_granted("' . Actions::CLONE . '", object)',
securityPostDenormalizeMessage: PageVoter::EDIT_MESSAGE
),
new Post(
denormalizationContext: ['groups' => ['write', 'post']],
securityPostDenormalize: 'is_granted("' . Actions::CREATE . '", object)',
securityPostDenormalizeMessage: PageVoter::CREATE_MESSAGE
),
new Get(),
new Put(
securityPostDenormalize: 'is_granted("' . Actions::EDIT . '", object)',
securityPostDenormalizeMessage: PageVoter::EDIT_MESSAGE
),
new Delete(
securityPostDenormalize: 'is_granted("' . Actions::DELETE . '", object)',
securityPostDenormalizeMessage: PageVoter::DELETE_MESSAGE
),
],
extraProperties: ['standard_put' => false],
)]
#[ApiFilter(ExistsFilter::class, properties: ['parent'])]
#[ApiFilter(SearchFilter::class, properties: ['idName' => 'exact'])]
#[ApiFilter(IriFilter::class, properties: ['parent'])]
#[ApiFilter(OrderFilter::class, properties: ['ord'])]
#[ORM\Entity(repositoryClass: PageRepository::class)]
class Page implements
TranslatableInterface,
LanguageableInterface,
LanguageableChildInterface,
OrdStatableInterface,
CloneableInterface,
ThumbableInterface,
BlockableInterface
{
use IdTrait,
UuidTrait,
OrdStatTrait,
TimestampableTrait,
TranslatableTrait,
LanguageableChildTrait,
SiteLanguagesTrait,
ImageThumbableTrait,
ImportIdTrait {
SiteLanguagesTrait::getPrintLanguage insteadof LanguageableChildTrait;
}
#[ApiProperty(description: 'Parent', readableLink: false, writableLink: false)]
#[Groups(['read', 'write'])]
#[ORM\ManyToOne(targetEntity: Page::class, inversedBy: 'children')]
#[ORM\JoinColumn(onDelete: 'cascade')]
private ?Page $parent = null;
#[ApiProperty(description: 'Children pages')]
#[Groups(['read'])]
#[ORM\OneToMany(targetEntity: Page::class, mappedBy: 'parent')]
#[ORM\OrderBy(['ord' => 'DESC'])]
private Collection $children;
#[ApiProperty(description: 'Identifier name')]
#[Groups(['read:' . self::class, 'openform'])]
#[ORM\Column(type: Types::STRING, length: 255, unique: true, nullable: true)]
private ?string $idName = null;
#[ApiProperty(description: 'Can non-openform administrators publish/unpublish this page')]
#[Groups(['read:' . self::class, 'openform'])]
#[ORM\Column(type: 'boolean', options: ['default' => true])]
private bool $isStatable = true;
#[ApiProperty(description: 'Can non-openform administrators edit this page')]
#[Groups(['read:' . self::class, 'openform'])]
#[ORM\Column(type: 'boolean', options: ['default' => true])]
private bool $isEditable = true;
#[ApiProperty(description: 'Can non-openform administrators delete this page')]
#[Groups(['read:' . self::class, 'openform'])]
#[ORM\Column(type: 'boolean', options: ['default' => true])]
private bool $isDeletable = true;
#[ApiProperty(description: 'Can non-openform administrators create subpage of this page')]
#[Groups(['read:' . self::class, 'openform'])]
#[ORM\Column(type: 'boolean', options: ['default' => true])]
private bool $isSubPageable = true;
#[ApiProperty(description: 'Show in main menu')]
#[Groups(['read:' . self::class, 'write'])]
#[ORM\Column(type: 'boolean', options: ['default' => false])]
private bool $mainMenu = false;
#[ApiProperty(description: 'Blocks', writableLink: false)]
#[ORM\OneToMany(
targetEntity: PageBlock::class,
mappedBy: 'parent',
cascade: ['persist', 'remove']
)]
#[ORM\OrderBy(['ord' => 'desc'])]
private Collection $blocks;
public function __construct(
?string $idName,
?Page $parent,
?int $ord,
?bool $stat,
?array $languages,
) {
$this->setUuid();
$this->idName = $idName;
$this->parent = $parent;
$ord !== null && $this->ord = $ord;
$stat !== null && $this->stat = $stat;
$this->translations = new ArrayCollection();
$this->children = new ArrayCollection();
$this->blocks = new ArrayCollection();
$this->thumbs = new ArrayCollection();
$this->setLanguages($languages ?? Language::DEFAULT_SITE);
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = new \DateTimeImmutable();
}
public function getParent(): ?Page
{
return $this->parent;
}
public function setParent(?Page $page): self
{
$this->parent = $page;
return $this;
}
public function getChildren(): Collection
{
return $this->children;
}
public function addChild(Page $child): self
{
if ($child->getUuid()->toString() === $this->getUuid()->toString()) {
return $this;
}
if (!$this->children->contains($child)) {
$this->children[] = $child;
$child->setParent($this);
}
return $this;
}
public function removeChild(Page $child): self
{
if ($this->children->contains($child)) {
$this->children->removeElement($child);
// set the owning side to null (unless already changed)
if ($child->getParent() === $this) {
$child->setParent(null);
}
}
return $this;
}
public function resetChildren(): self
{
$this->children = new ArrayCollection();
return $this;
}
public function getIdName(): ?string
{
return $this->idName;
}
public function setIdName(?string $idName): self
{
$this->idName = $idName;
return $this;
}
public function getIsStatable(): bool
{
return $this->isStatable;
}
public function setIsStatable(bool $isStatable): self
{
$this->isStatable = $isStatable;
return $this;
}
public function getIsEditable(): bool
{
return $this->isEditable;
}
public function setIsEditable(bool $isEditable): self
{
$this->isEditable = $isEditable;
return $this;
}
public function getIsDeletable(): bool
{
return $this->isDeletable;
}
public function setIsDeletable(bool $isDeletable): self
{
$this->isDeletable = $isDeletable;
return $this;
}
public function getIsSubPageable(): bool
{
return $this->isSubPageable;
}
public function setIsSubPageable(bool $isSubPageable): self
{
$this->isSubPageable = $isSubPageable;
return $this;
}
public function isMainMenu(): bool
{
return $this->mainMenu;
}
public function setMainMenu(bool $mainMenu): self
{
$this->mainMenu = $mainMenu;
return $this;
}
public function getBlocks(): Collection
{
return $this->blocks;
}
public function addBlock(BlockInterface $block): self
{
if ($this->blocks->contains($block)) {
return $this;
}
$this->blocks[] = $block;
return $this;
}
public function removeBlock(BlockInterface $block): self
{
$this->blocks->removeElement($block);
return $this;
}
public function resetBlocks(): self
{
$this->blocks = new ArrayCollection();
return $this;
}
public function clone(): self
{
if ($this->idName) {
throw new \Exception('Predefined pages cannot be cloned.');
}
$clone = clone $this;
$clone->id = null;
$clone->setUuid();
$clone->ord = 0;
$clone->stat = false;
$clone->importId = null;
if ($clone->media) {
$clone->media = $this->media->clone();
$clone->media->setRelated(self::class . ':' . $clone->uuid->toString());
}
ClassUtils::cloneCollection($this, $clone, 'translations');
$clone->synchronizeTranslations();
$nativeLanguage = $clone->getLanguageableParent()->getNativeLanguage();
$nativeTranslation = $clone->getTranslation($nativeLanguage);
$nativeTranslation->setTitle($nativeTranslation->getTitle() . ' [KOPIA]');
ClassUtils::cloneCollection($this, $clone, 'blocks');
ClassUtils::cloneCollection($this, $clone, 'thumbs');
$clone->resetChildren();
$clone->createdAt = new \DateTimeImmutable();
$clone->updatedAt = new \DateTimeImmutable();
return $clone;
}
}