src/Entity/JournalBanner.php line 49
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
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;
use App\Entity\Trait\Languages\LanguageableChildTrait,
App\Entity\Interface\LanguageableChildInterface,
App\Filter\IriFilter,
App\Repository\JournalBannerRepository;
#[ApiResource(
description: 'Journal banners',
normalizationContext: ['groups' => [
'read',
'read:' . self::class,
'read:' . self::class . 'Translation',
'read:' . AbstractBanner::class,
'read:' . AbstractBannerTranslation::class
]],
denormalizationContext: ['groups' => ['write']],
security: 'is_granted("' . Journal::class . '")',
order: ['ord' => 'desc'],
operations: [
new GetCollection(),
new Post(denormalizationContext: ['groups' => ['write', 'post']]),
new Get(),
new Put(),
new Delete(),
],
extraProperties: ['standard_put' => false],
)]
#[ApiFilter(IriFilter::class, properties: ['parent'])]
#[UniqueEntity(fields: ['workingTitle', 'parent'], message: 'This working title already exists.')]
#[ORM\UniqueConstraint(fields: ['parent', 'workingTitle'])]
#[ORM\Entity(repositoryClass: JournalBannerRepository::class)]
class JournalBanner extends AbstractBanner implements LanguageableChildInterface
{
use LanguageableChildTrait;
#[ApiProperty(description: 'Parent', readableLink: false, writableLink: false)]
#[Groups(['read', 'post'])]
#[Assert\NotNull]
#[ORM\ManyToOne(targetEntity: Journal::class, inversedBy: 'banners')]
#[ORM\JoinColumn(onDelete: 'cascade', nullable: false)]
private Journal $parent;
#[ApiProperty(description: 'Template', writableLink: false)]
#[Groups(['post'])]
#[ORM\ManyToOne(targetEntity: Banner::class)]
#[ORM\JoinColumn(onDelete: 'set null')]
private ?Banner $template = null;
public function __construct(Journal $parent, string $workingTitle, ?Banner $template)
{
parent::__construct($workingTitle);
$this->parent = $parent;
$this->template = $template;
foreach ($this->parent->getLanguages() as $lang) {
new JournalBannerTranslation($this, $lang);
}
if ($template) {
$this->setFromTemplate($template);
}
$parent->addBanner($this);
}
public function getParent(): Journal
{
return $this->parent;
}
public function getTemplate(): ?Banner
{
return $this->template;
}
private function setFromTemplate(Banner $template): void
{
$this->isTargetBlank = $template->getIsTargetBlank();
foreach ($this->translations as $translation) {
/** @var BannerTranslation */
$translationTemplate = $template->getTranslation($translation->getLang());
if (! $translationTemplate) {
continue;
}
$translation
->setTitle($translationTemplate->getTitle())
->setIsTitleVisible($translationTemplate->getIsTitleVisible())
->setLink($translationTemplate->getLink())
->setAlt($translationTemplate->getAlt())
->setDescription($translationTemplate->getDescription());
if (! $translationTemplate->getMedia()) {
continue;
}
$translation->setMedia($translationTemplate->getMedia()->clone());
}
return;
}
}