src/Entity/JournalNews.php line 78

  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\Interface\TranslatableInterface,
  27.     App\Entity\Interface\OrdStatableInterface,
  28.     App\Entity\Interface\LanguageableChildInterface,
  29.     App\Entity\Interface\JournalBlockableInterface,
  30.     App\Entity\Interface\CloneableInterface,
  31.     App\Entity\Interface\BlockInterface,
  32.     App\Repository\JournalNewsRepository;
  33. use App\StateProcessor\CloneProcessor,
  34.     App\Security\Voter\CloneVoter,
  35.     App\DTO\CloneDTO,
  36.     App\Lib\Actions,
  37.     App\Filter\IriFilter,
  38.     App\Util\ClassUtils;
  39. #[ApiResource(
  40.     description'Journal news',
  41.     normalizationContext: ['groups' => [
  42.         'read',
  43.         'read:' self::class,
  44.         'read:' self::class . 'Translation',
  45.         'read:' AbstractThumb::class
  46.     ]],
  47.     denormalizationContext: ['groups' => ['write']],
  48.     security'is_granted("' Journal::class . '")',
  49.     order: ['ord' => 'desc'],
  50.     operations: [
  51.         new GetCollection(),
  52.         new Post(
  53.             uriTemplate'/journal_news/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\UniqueConstraint(fields: ['parent''workingTitle'])]
  71. #[ORM\Entity(repositoryClassJournalNewsRepository::class)]
  72. class JournalNews implements
  73.     TranslatableInterface,
  74.     OrdStatableInterface,
  75.     LanguageableChildInterface,
  76.     JournalBlockableInterface,
  77.     CloneableInterface
  78. {
  79.     use IdTrait,
  80.         UuidTrait,
  81.         OrdStatTrait,
  82.         TimestampableTrait,
  83.         TranslatableTrait,
  84.         LanguageableChildTrait;
  85.     #[ApiProperty(description'Parent'readableLinkfalsewritableLinkfalse)]
  86.     #[Groups(['read''post'])]
  87.     #[Assert\NotNull]
  88.     #[ORM\ManyToOne(targetEntityJournal::class, inversedBy'news')]
  89.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  90.     private Journal $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'Is UJ')]
  97.     #[Groups(['read:' self::class, 'write'])]
  98.     #[ORM\Column(typeTypes::BOOLEANoptions: ['default' => false])]
  99.     private bool $isUj false;
  100.     #[ApiProperty(description'Published at')]
  101.     #[Groups(['read:' self::class, 'write'])]
  102.     #[ORM\Column(typeTypes::DATE_IMMUTABLEoptions: ['default' => '1970-01-01'])]
  103.     private \DateTimeImmutable $publishedAt;
  104.     #[ApiProperty(description'Blocks'writableLinkfalse)]
  105.     #[ORM\OneToMany(
  106.         targetEntityJournalNewsBlock::class,
  107.         mappedBy'parent',
  108.         cascade: ['persist''remove']
  109.     )]
  110.     #[ORM\OrderBy(['ord' => 'desc'])]
  111.     private Collection $blocks;
  112.     public function __construct(Journal $parentstring $workingTitle)
  113.     {
  114.         $this->setUuid();
  115.         $this->parent $parent;
  116.         $this->workingTitle $workingTitle;
  117.         $this->translations = new ArrayCollection();
  118.         $this->blocks = new ArrayCollection();
  119.         foreach ($this->parent->getLanguages() as $lang) {
  120.             new JournalNewsTranslation($this$lang);
  121.         }
  122.         $this->publishedAt = new \DateTimeImmutable();
  123.         $this->createdAt = new \DateTimeImmutable();
  124.         $this->updatedAt = new \DateTimeImmutable();
  125.         $parent->addNews($this);
  126.     }
  127.     public function getParent(): Journal
  128.     {
  129.         return $this->parent;
  130.     }
  131.     public function getWorkingTitle(): string
  132.     {
  133.         return $this->workingTitle;
  134.     }
  135.     public function setWorkingTitle(string $workingTitle): self
  136.     {
  137.         $this->workingTitle $workingTitle;
  138.         return $this;
  139.     }
  140.     public function getIsUj(): bool
  141.     {
  142.         return $this->isUj;
  143.     }
  144.     public function setIsUj(bool $isUj): self
  145.     {
  146.         $this->isUj $isUj;
  147.         return $this;
  148.     }
  149.     public function getPublishedAt(): \DateTimeImmutable
  150.     {
  151.         return $this->publishedAt;
  152.     }
  153.     public function setPublishedAt(\DateTimeImmutable $publishedAt): self
  154.     {
  155.         $this->publishedAt $publishedAt;
  156.         return $this;
  157.     }
  158.     /**
  159.      * @return Collection|JournalNewsBlock[]
  160.      */
  161.     public function getBlocks(): Collection
  162.     {
  163.         return $this->blocks;
  164.     }
  165.     public function addBlock(BlockInterface $block): self
  166.     {
  167.         if ($this->blocks->contains($block)) {
  168.             return $this;
  169.         }
  170.         $this->blocks[] = $block;
  171.         return $this;
  172.     }
  173.     public function removeBlock(BlockInterface $block): self
  174.     {
  175.         $this->blocks->removeElement($block);
  176.         return $this;
  177.     }
  178.     public function resetBlocks(): self
  179.     {
  180.         $this->blocks = new ArrayCollection();
  181.         return $this;
  182.     }
  183.     public function clone(): self
  184.     {
  185.         $clone = clone $this;
  186.         $clone->id null;
  187.         $clone->setUuid();
  188.         $clone->workingTitle .= ' [KOPIA]';
  189.         $clone->ord 0;
  190.         $clone->stat false;
  191.         ClassUtils::cloneCollection($this$clone'translations');
  192.         $clone->synchronizeTranslations();
  193.         ClassUtils::cloneCollection($this$clone'blocks');
  194.         $clone->createdAt = new \DateTimeImmutable();
  195.         $clone->updatedAt = new \DateTimeImmutable();
  196.         return $clone;
  197.     }
  198. }