src/Entity/JournalPage.php line 84

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM,
  4.     Doctrine\DBAL\Types\Types,
  5.     Doctrine\Common\Collections\Collection,
  6.     Doctrine\Common\Collections\ArrayCollection;
  7. use Symfony\Component\Serializer\Annotation\Groups,
  8.     Symfony\Component\Validator\Constraints as Assert,
  9.     Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  10. use ApiPlatform\Metadata\ApiResource,
  11.     ApiPlatform\Metadata\ApiProperty,
  12.     ApiPlatform\Metadata\Get,
  13.     ApiPlatform\Metadata\GetCollection,
  14.     ApiPlatform\Metadata\Post,
  15.     ApiPlatform\Metadata\Put,
  16.     ApiPlatform\Metadata\Delete,
  17.     ApiPlatform\Metadata\ApiFilter,
  18.     ApiPlatform\Doctrine\Orm\Filter\OrderFilter,
  19.     ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
  20. use App\Entity\Trait\IdTrait,
  21.     App\Entity\Trait\UuidTrait,
  22.     App\Entity\Trait\OrdStatTrait,
  23.     App\Entity\Trait\TimestampableTrait,
  24.     App\Entity\Trait\TranslatableTrait,
  25.     App\Entity\Trait\Languages\LanguageableChildTrait,
  26.     App\Entity\Trait\ImportIdTrait,
  27.     App\Entity\Interface\TranslatableInterface,
  28.     App\Entity\Interface\OrdStatableInterface,
  29.     App\Entity\Interface\LanguageableChildInterface,
  30.     App\Entity\Interface\JournalBlockableInterface,
  31.     App\Entity\Interface\CloneableInterface,
  32.     App\Entity\Interface\BlockInterface,
  33.     App\Repository\JournalPageRepository;
  34. use App\StateProcessor\CloneProcessor,
  35.     App\Security\Voter\CloneVoter,
  36.     App\Security\Voter\JournalPageVoter,
  37.     App\DTO\CloneDTO,
  38.     App\Lib\Actions,
  39.     App\Filter\IriFilter,
  40.     App\Util\ClassUtils;
  41. use App\Enum\JournalBlockType;
  42. #[ApiResource(
  43.     description'Journal pages',
  44.     normalizationContext: ['groups' => [
  45.         'read',
  46.         'read:' self::class,
  47.         'read:' self::class . 'Translation'
  48.     ]],
  49.     denormalizationContext: ['groups' => ['write']],
  50.     security'is_granted("' Journal::class . '")',
  51.     order: ['ord' => 'desc'],
  52.     operations: [
  53.         new GetCollection(),
  54.         new Post(
  55.             uriTemplate'/journal_pages/clone',
  56.             inputCloneDTO::class,
  57.             processorCloneProcessor::class,
  58.             security'is_granted("' Actions::CLONE .'")',
  59.             securityMessageCloneVoter::MESSAGE
  60.         ),
  61.         new Post(denormalizationContext: ['groups' => ['write',  'post']]),
  62.         new Get(),
  63.         new Put(),
  64.         new Delete(
  65.             securityPostDenormalize'is_granted("' Actions::DELETE '", object)',
  66.             securityPostDenormalizeMessageJournalPageVoter::MESSAGE
  67.         ),
  68.     ],
  69.     extraProperties: ['standard_put' => false],
  70. )]
  71. #[ApiFilter(SearchFilter::class, properties: ['idName' => 'exact'])]
  72. #[ApiFilter(IriFilter::class, properties: ['parent'])]
  73. #[ApiFilter(OrderFilter::class, properties: ['ord'])]
  74. #[UniqueEntity(fields: ['workingTitle''parent'], message'This working title already exists.')]
  75. // #[ORM\UniqueConstraint(fields: ['parent', 'workingTitle'])]
  76. #[ORM\Entity(repositoryClassJournalPageRepository::class)]
  77. class JournalPage implements
  78.     TranslatableInterface,
  79.     OrdStatableInterface,
  80.     LanguageableChildInterface,
  81.     JournalBlockableInterface,
  82.     CloneableInterface
  83. {
  84.     use IdTrait,
  85.         UuidTrait,
  86.         OrdStatTrait,
  87.         TimestampableTrait,
  88.         TranslatableTrait,
  89.         LanguageableChildTrait,
  90.         ImportIdTrait;
  91.     #[ApiProperty(description'Parent'readableLinkfalsewritableLinkfalse)]
  92.     #[Groups(['read''post'])]
  93.     #[Assert\NotNull]
  94.     #[ORM\ManyToOne(targetEntityJournal::class, inversedBy'pages')]
  95.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  96.     private Journal $parent;
  97.     #[ApiProperty(description'Working title')]
  98.     #[Groups(['read''write'])]
  99.     #[Assert\NotBlank]
  100.     #[ORM\Column(typeTypes::STRINGlength511)]
  101.     private string $workingTitle;
  102.     #[ApiProperty(description'Identifier name')]
  103.     #[Groups(['read:' self::class, 'openform'])]
  104.     #[ORM\Column(typeTypes::STRINGlength255nullabletrue)]
  105.     private ?string $idName null;
  106.     #[ApiProperty(description'Blocks'writableLinkfalse)]
  107.     #[ORM\OneToMany(
  108.         targetEntityJournalPageBlock::class,
  109.         mappedBy'parent',
  110.         cascade: ['persist''remove']
  111.     )]
  112.     #[ORM\OrderBy(['ord' => 'desc'])]
  113.     private Collection $blocks;
  114.     public function __construct(
  115.         Journal $parent,
  116.         string $workingTitle,
  117.         ?string $idName null,
  118.         ?int $ord null,
  119.         ?bool $stat null
  120.     ) {
  121.         $this->setUuid();
  122.         $this->parent $parent;
  123.         $this->workingTitle $workingTitle;
  124.         $this->idName $idName;
  125.         $ord !== null && $this->ord $ord;
  126.         $stat !== null && $this->stat $stat;
  127.         $this->translations = new ArrayCollection();
  128.         $this->blocks = new ArrayCollection();
  129.         foreach ($this->parent->getLanguages() as $lang) {
  130.             $newTrans = new JournalPageTranslation($this$lang);
  131.             if ($idName) {
  132.                 $newTrans->setTitle($workingTitle);
  133.             }
  134.         }
  135.         $this->createdAt = new \DateTimeImmutable();
  136.         $this->updatedAt = new \DateTimeImmutable();
  137.         $parent->addPage($this);
  138.     }
  139.     public function getParent(): Journal
  140.     {
  141.         return $this->parent;
  142.     }
  143.     public function getWorkingTitle(): string
  144.     {
  145.         return $this->workingTitle;
  146.     }
  147.     public function setWorkingTitle(string $workingTitle): self
  148.     {
  149.         $this->workingTitle $workingTitle;
  150.         return $this;
  151.     }
  152.     public function getIdName(): ?string
  153.     {
  154.         return $this->idName;
  155.     }
  156.     public function setIdName(?string $idName): self
  157.     {
  158.         $this->idName $idName;
  159.         return $this;
  160.     }
  161.     /**
  162.      * @return Collection<int, JournalPageBlock>
  163.      */
  164.     public function getBlocks(): Collection
  165.     {
  166.         return $this->blocks;
  167.     }
  168.     public function addBlock(BlockInterface $block): self
  169.     {
  170.         if ($this->blocks->contains($block)) {
  171.             return $this;
  172.         }
  173.         $this->blocks[] = $block;
  174.         return $this;
  175.     }
  176.     public function removeBlock(BlockInterface $block): self
  177.     {
  178.         $this->blocks->removeElement($block);
  179.         return $this;
  180.     }
  181.     public function resetBlocks(): self
  182.     {
  183.         $this->blocks = new ArrayCollection();
  184.         return $this;
  185.     }
  186.     public function getBlockFirst(string $type): ?JournalPageBlock
  187.     {
  188.         $type JournalBlockType::from($type);
  189.         foreach ($this->getBlocks() as $block) {
  190.             if ($block->getType() !== $type) {
  191.                 continue;
  192.             }
  193.             return $block;
  194.         }
  195.         return null;
  196.     }
  197.     public function clone(): self
  198.     {
  199.         $clone = clone $this;
  200.         $clone->id null;
  201.         $clone->setUuid();
  202.         $clone->workingTitle .= ' [KOPIA]';
  203.         $clone->idName null;
  204.         $clone->ord 0;
  205.         $clone->stat false;
  206.         $clone->importId null;
  207.         ClassUtils::cloneCollection($this$clone'translations');
  208.         $clone->synchronizeTranslations();
  209.         ClassUtils::cloneCollection($this$clone'blocks');
  210.         $clone->createdAt = new \DateTimeImmutable();
  211.         $clone->updatedAt = new \DateTimeImmutable();
  212.         return $clone;
  213.     }
  214. }