src/Entity/LandingPage.php line 77

  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\CloneableInterface,
  31.     App\Entity\Interface\BlockInterface,
  32.     App\Entity\Interface\BlockableInterface,
  33.     App\Repository\LandingPageRepository;
  34. use App\StateProcessor\CloneProcessor,
  35.     App\Security\Voter\CloneVoter,
  36.     App\DTO\CloneDTO,
  37.     App\Lib\Actions,
  38.     App\Filter\IriFilter,
  39.     App\Util\ClassUtils;
  40. #[ApiResource(
  41.     description'Landing pages',
  42.     normalizationContext: ['groups' => [
  43.         'read',
  44.         'read:' self::class,
  45.         'read:' self::class . 'Translation'
  46.     ]],
  47.     denormalizationContext: ['groups' => ['write']],
  48.     security'is_granted("' Landing::class . '")',
  49.     order: ['ord' => 'desc'],
  50.     operations: [
  51.         new GetCollection(),
  52.         new Post(
  53.             uriTemplate'/landing_pages/clone',
  54.             inputCloneDTO::class,
  55.             processorCloneProcessor::class,
  56.             security'is_granted("' Actions::CLONE .'")',
  57.             securityMessageCloneVoter::MESSAGE
  58.         ),
  59.         new Post(denormalizationContext: ['groups' => ['write',  'post']]),
  60.         new Get(),
  61.         new Put(),
  62.         new Delete(),
  63.     ],
  64.     extraProperties: ['standard_put' => false],
  65. )]
  66. #[ApiFilter(SearchFilter::class, properties: ['idName' => 'exact'])]
  67. #[ApiFilter(IriFilter::class, properties: ['parent'])]
  68. #[ApiFilter(OrderFilter::class, properties: ['ord'])]
  69. #[UniqueEntity(fields: ['workingTitle''parent'], message'This working title already exists.')]
  70. #[ORM\Entity(repositoryClassLandingPageRepository::class)]
  71. class LandingPage implements
  72.     TranslatableInterface,
  73.     OrdStatableInterface,
  74.     LanguageableChildInterface,
  75.     BlockableInterface,
  76.     CloneableInterface
  77. {
  78.     use IdTrait,
  79.         UuidTrait,
  80.         OrdStatTrait,
  81.         TimestampableTrait,
  82.         TranslatableTrait,
  83.         LanguageableChildTrait,
  84.         ImportIdTrait;
  85.     #[ApiProperty(description'Parent'readableLinkfalsewritableLinkfalse)]
  86.     #[Groups(['read''post'])]
  87.     #[Assert\NotNull]
  88.     #[ORM\ManyToOne(targetEntityLanding::class, inversedBy'pages')]
  89.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  90.     private Landing $parent;
  91.     #[ApiProperty(description'Working title')]
  92.     #[Groups(['read''write'])]
  93.     #[Assert\NotBlank]
  94.     #[ORM\Column(typeTypes::STRINGlength511)]
  95.     private string $workingTitle;
  96.     #[ApiProperty(description'Identifier name')]
  97.     #[Groups(['read:' self::class, 'openform'])]
  98.     #[ORM\Column(typeTypes::STRINGlength255nullabletrue)]
  99.     private ?string $idName null;
  100.     #[ApiProperty(description'Blocks'writableLinkfalse)]
  101.     #[ORM\OneToMany(
  102.         targetEntityLandingPageBlock::class,
  103.         mappedBy'parent',
  104.         cascade: ['persist''remove']
  105.     )]
  106.     #[ORM\OrderBy(['ord' => 'desc'])]
  107.     private Collection $blocks;
  108.     public function __construct(
  109.         Landing $parent,
  110.         string $workingTitle,
  111.         ?string $idName null,
  112.         ?int $ord null,
  113.         ?bool $stat null
  114.     ) {
  115.         $this->setUuid();
  116.         $this->parent $parent;
  117.         $this->workingTitle $workingTitle;
  118.         $this->idName $idName;
  119.         $ord !== null && $this->ord $ord;
  120.         $stat !== null && $this->stat $stat;
  121.         $this->translations = new ArrayCollection();
  122.         $this->blocks = new ArrayCollection();
  123.         foreach ($this->parent->getLanguages() as $lang) {
  124.             $trans = new LandingPageTranslation($this$lang);
  125.             if ($idName) {
  126.                 $trans->setTitle($workingTitle);
  127.             }
  128.         }
  129.         $this->createdAt = new \DateTimeImmutable();
  130.         $this->updatedAt = new \DateTimeImmutable();
  131.         $parent->addPage($this);
  132.     }
  133.     public function getParent(): Landing
  134.     {
  135.         return $this->parent;
  136.     }
  137.     public function getWorkingTitle(): string
  138.     {
  139.         return $this->workingTitle;
  140.     }
  141.     public function setWorkingTitle(string $workingTitle): self
  142.     {
  143.         $this->workingTitle $workingTitle;
  144.         return $this;
  145.     }
  146.     public function getIdName(): ?string
  147.     {
  148.         return $this->idName;
  149.     }
  150.     public function setIdName(?string $idName): self
  151.     {
  152.         $this->idName $idName;
  153.         return $this;
  154.     }
  155.     /**
  156.      * @return Collection<int, LandingPageBlock>
  157.      */
  158.     public function getBlocks(): Collection
  159.     {
  160.         return $this->blocks;
  161.     }
  162.     public function addBlock(BlockInterface $block): self
  163.     {
  164.         if ($this->blocks->contains($block)) {
  165.             return $this;
  166.         }
  167.         $this->blocks[] = $block;
  168.         return $this;
  169.     }
  170.     public function removeBlock(BlockInterface $block): self
  171.     {
  172.         $this->blocks->removeElement($block);
  173.         return $this;
  174.     }
  175.     public function resetBlocks(): self
  176.     {
  177.         $this->blocks = new ArrayCollection();
  178.         return $this;
  179.     }
  180.     public function clone(): self
  181.     {
  182.         $clone = clone $this;
  183.         $clone->id null;
  184.         $clone->setUuid();
  185.         $clone->workingTitle .= ' [KOPIA]';
  186.         $clone->idName null;
  187.         $clone->ord 0;
  188.         $clone->stat false;
  189.         $clone->importId null;
  190.         ClassUtils::cloneCollection($this$clone'translations');
  191.         $clone->synchronizeTranslations();
  192.         ClassUtils::cloneCollection($this$clone'blocks');
  193.         $clone->createdAt = new \DateTimeImmutable();
  194.         $clone->updatedAt = new \DateTimeImmutable();
  195.         return $clone;
  196.     }
  197. }