src/Entity/JournalPage.php line 84
<?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,
Symfony\Component\Validator\Constraints as Assert,
Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
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;
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\Interface\TranslatableInterface,
App\Entity\Interface\OrdStatableInterface,
App\Entity\Interface\LanguageableChildInterface,
App\Entity\Interface\JournalBlockableInterface,
App\Entity\Interface\CloneableInterface,
App\Entity\Interface\BlockInterface,
App\Repository\JournalPageRepository;
use App\StateProcessor\CloneProcessor,
App\Security\Voter\CloneVoter,
App\Security\Voter\JournalPageVoter,
App\DTO\CloneDTO,
App\Lib\Actions,
App\Filter\IriFilter,
App\Util\ClassUtils;
use App\Enum\JournalBlockType;
#[ApiResource(
description: 'Journal pages',
normalizationContext: ['groups' => [
'read',
'read:' . self::class,
'read:' . self::class . 'Translation'
]],
denormalizationContext: ['groups' => ['write']],
security: 'is_granted("' . Journal::class . '")',
order: ['ord' => 'desc'],
operations: [
new GetCollection(),
new Post(
uriTemplate: '/journal_pages/clone',
input: CloneDTO::class,
processor: CloneProcessor::class,
security: 'is_granted("' . Actions::CLONE .'")',
securityMessage: CloneVoter::MESSAGE
),
new Post(denormalizationContext: ['groups' => ['write', 'post']]),
new Get(),
new Put(),
new Delete(
securityPostDenormalize: 'is_granted("' . Actions::DELETE . '", object)',
securityPostDenormalizeMessage: JournalPageVoter::MESSAGE
),
],
extraProperties: ['standard_put' => false],
)]
#[ApiFilter(SearchFilter::class, properties: ['idName' => 'exact'])]
#[ApiFilter(IriFilter::class, properties: ['parent'])]
#[ApiFilter(OrderFilter::class, properties: ['ord'])]
#[UniqueEntity(fields: ['workingTitle', 'parent'], message: 'This working title already exists.')]
// #[ORM\UniqueConstraint(fields: ['parent', 'workingTitle'])]
#[ORM\Entity(repositoryClass: JournalPageRepository::class)]
class JournalPage implements
TranslatableInterface,
OrdStatableInterface,
LanguageableChildInterface,
JournalBlockableInterface,
CloneableInterface
{
use IdTrait,
UuidTrait,
OrdStatTrait,
TimestampableTrait,
TranslatableTrait,
LanguageableChildTrait,
ImportIdTrait;
#[ApiProperty(description: 'Parent', readableLink: false, writableLink: false)]
#[Groups(['read', 'post'])]
#[Assert\NotNull]
#[ORM\ManyToOne(targetEntity: Journal::class, inversedBy: 'pages')]
#[ORM\JoinColumn(onDelete: 'cascade', nullable: false)]
private Journal $parent;
#[ApiProperty(description: 'Working title')]
#[Groups(['read', 'write'])]
#[Assert\NotBlank]
#[ORM\Column(type: Types::STRING, length: 511)]
private string $workingTitle;
#[ApiProperty(description: 'Identifier name')]
#[Groups(['read:' . self::class, 'openform'])]
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $idName = null;
#[ApiProperty(description: 'Blocks', writableLink: false)]
#[ORM\OneToMany(
targetEntity: JournalPageBlock::class,
mappedBy: 'parent',
cascade: ['persist', 'remove']
)]
#[ORM\OrderBy(['ord' => 'desc'])]
private Collection $blocks;
public function __construct(
Journal $parent,
string $workingTitle,
?string $idName = null,
?int $ord = null,
?bool $stat = null
) {
$this->setUuid();
$this->parent = $parent;
$this->workingTitle = $workingTitle;
$this->idName = $idName;
$ord !== null && $this->ord = $ord;
$stat !== null && $this->stat = $stat;
$this->translations = new ArrayCollection();
$this->blocks = new ArrayCollection();
foreach ($this->parent->getLanguages() as $lang) {
$newTrans = new JournalPageTranslation($this, $lang);
if ($idName) {
$newTrans->setTitle($workingTitle);
}
}
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = new \DateTimeImmutable();
$parent->addPage($this);
}
public function getParent(): Journal
{
return $this->parent;
}
public function getWorkingTitle(): string
{
return $this->workingTitle;
}
public function setWorkingTitle(string $workingTitle): self
{
$this->workingTitle = $workingTitle;
return $this;
}
public function getIdName(): ?string
{
return $this->idName;
}
public function setIdName(?string $idName): self
{
$this->idName = $idName;
return $this;
}
/**
* @return Collection<int, JournalPageBlock>
*/
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 getBlockFirst(string $type): ?JournalPageBlock
{
$type = JournalBlockType::from($type);
foreach ($this->getBlocks() as $block) {
if ($block->getType() !== $type) {
continue;
}
return $block;
}
return null;
}
public function clone(): self
{
$clone = clone $this;
$clone->id = null;
$clone->setUuid();
$clone->workingTitle .= ' [KOPIA]';
$clone->idName = null;
$clone->ord = 0;
$clone->stat = false;
$clone->importId = null;
ClassUtils::cloneCollection($this, $clone, 'translations');
$clone->synchronizeTranslations();
ClassUtils::cloneCollection($this, $clone, 'blocks');
$clone->createdAt = new \DateTimeImmutable();
$clone->updatedAt = new \DateTimeImmutable();
return $clone;
}
}