src/Entity/JournalBanner.php line 49

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Component\Serializer\Annotation\Groups,
  5.     Symfony\Component\Validator\Constraints as Assert,
  6.     Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  7. use ApiPlatform\Metadata\ApiResource,
  8.     ApiPlatform\Metadata\ApiProperty,
  9.     ApiPlatform\Metadata\Get,
  10.     ApiPlatform\Metadata\GetCollection,
  11.     ApiPlatform\Metadata\Post,
  12.     ApiPlatform\Metadata\Put,
  13.     ApiPlatform\Metadata\Delete,
  14.     ApiPlatform\Metadata\ApiFilter;
  15. use App\Entity\Trait\Languages\LanguageableChildTrait,
  16.     App\Entity\Interface\LanguageableChildInterface,
  17.     App\Filter\IriFilter,
  18.     App\Repository\JournalBannerRepository;
  19. #[ApiResource(
  20.     description'Journal banners',
  21.     normalizationContext: ['groups' => [
  22.         'read',
  23.         'read:' self::class,
  24.         'read:' self::class . 'Translation',
  25.         'read:' AbstractBanner::class,
  26.         'read:' AbstractBannerTranslation::class
  27.     ]],
  28.     denormalizationContext: ['groups' => ['write']],
  29.     security'is_granted("' Journal::class . '")',
  30.     order: ['ord' => 'desc'],
  31.     operations: [
  32.         new GetCollection(),
  33.         new Post(denormalizationContext: ['groups' => ['write',  'post']]),
  34.         new Get(),
  35.         new Put(),
  36.         new Delete(),
  37.     ],
  38.     extraProperties: ['standard_put' => false],
  39. )]
  40. #[ApiFilter(IriFilter::class, properties: ['parent'])]
  41. #[UniqueEntity(fields: ['workingTitle''parent'], message'This working title already exists.')]
  42. #[ORM\UniqueConstraint(fields: ['parent''workingTitle'])]
  43. #[ORM\Entity(repositoryClassJournalBannerRepository::class)]
  44. class JournalBanner extends AbstractBanner implements LanguageableChildInterface
  45. {
  46.     use LanguageableChildTrait;
  47.     #[ApiProperty(description'Parent'readableLinkfalsewritableLinkfalse)]
  48.     #[Groups(['read''post'])]
  49.     #[Assert\NotNull]
  50.     #[ORM\ManyToOne(targetEntityJournal::class, inversedBy'banners')]
  51.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  52.     private Journal $parent;
  53.     #[ApiProperty(description'Template'writableLinkfalse)]
  54.     #[Groups(['post'])]
  55.     #[ORM\ManyToOne(targetEntityBanner::class)]
  56.     #[ORM\JoinColumn(onDelete'set null')]
  57.     private ?Banner $template null;
  58.     public function __construct(Journal $parentstring $workingTitle, ?Banner $template)
  59.     {
  60.         parent::__construct($workingTitle);
  61.         $this->parent $parent;
  62.         $this->template $template;
  63.         foreach ($this->parent->getLanguages() as $lang) {
  64.             new JournalBannerTranslation($this$lang);
  65.         }
  66.         if ($template) {
  67.             $this->setFromTemplate($template);
  68.         }
  69.         $parent->addBanner($this);
  70.     }
  71.     public function getParent(): Journal
  72.     {
  73.         return $this->parent;
  74.     }
  75.     public function getTemplate(): ?Banner
  76.     {
  77.         return $this->template;
  78.     }
  79.     private function setFromTemplate(Banner $template): void
  80.     {
  81.         $this->isTargetBlank $template->getIsTargetBlank();
  82.         foreach ($this->translations as $translation) {
  83.             /** @var BannerTranslation */
  84.             $translationTemplate $template->getTranslation($translation->getLang());
  85.             if (! $translationTemplate) {
  86.                 continue;
  87.             }
  88.             $translation
  89.                 ->setTitle($translationTemplate->getTitle())
  90.                 ->setIsTitleVisible($translationTemplate->getIsTitleVisible())
  91.                 ->setLink($translationTemplate->getLink())
  92.                 ->setAlt($translationTemplate->getAlt())
  93.                 ->setDescription($translationTemplate->getDescription());
  94.             if (! $translationTemplate->getMedia()) {
  95.                 continue;
  96.             }
  97.             $translation->setMedia($translationTemplate->getMedia()->clone());
  98.         }
  99.         return;
  100.     }
  101. }