src/Entity/JournalNews.php line 78
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM,
Doctrine\DBAL\Types\Types,
Doctrine\Common\Collections\Collection,
Doctrine\Common\Collections\ArrayCollection;
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,
ApiPlatform\Doctrine\Orm\Filter\OrderFilter,
ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use App\Entity\Trait\IdTrait,
App\Entity\Trait\UuidTrait,
App\Entity\Trait\OrdStatTrait,
App\Entity\Trait\TimestampableTrait,
App\Entity\Trait\TranslatableTrait,
App\Entity\Trait\Languages\LanguageableChildTrait,
App\Entity\Interface\TranslatableInterface,
App\Entity\Interface\OrdStatableInterface,
App\Entity\Interface\LanguageableChildInterface,
App\Entity\Interface\JournalBlockableInterface,
App\Entity\Interface\CloneableInterface,
App\Entity\Interface\BlockInterface,
App\Repository\JournalNewsRepository;
use App\StateProcessor\CloneProcessor,
App\Security\Voter\CloneVoter,
App\DTO\CloneDTO,
App\Lib\Actions,
App\Filter\IriFilter,
App\Util\ClassUtils;
#[ApiResource(
description: 'Journal news',
normalizationContext: ['groups' => [
'read',
'read:' . self::class,
'read:' . self::class . 'Translation',
'read:' . AbstractThumb::class
]],
denormalizationContext: ['groups' => ['write']],
security: 'is_granted("' . Journal::class . '")',
order: ['ord' => 'desc'],
operations: [
new GetCollection(),
new Post(
uriTemplate: '/journal_news/clone',
input: CloneDTO::class,
processor: CloneProcessor::class,
security: 'is_granted("' . Actions::CLONE .'")',
securityMessage: CloneVoter::MESSAGE
),
new Post(denormalizationContext: ['groups' => ['write', 'post']]),
new Get(),
new Put(),
new Delete(),
],
extraProperties: ['standard_put' => false],
)]
#[ApiFilter(SearchFilter::class, properties: ['idName' => 'exact'])]
#[ApiFilter(IriFilter::class, properties: ['parent'])]
#[ApiFilter(OrderFilter::class, properties: ['ord'])]
#[UniqueEntity(fields: ['workingTitle', 'parent'], message: 'This working title already exists.')]
#[ORM\UniqueConstraint(fields: ['parent', 'workingTitle'])]
#[ORM\Entity(repositoryClass: JournalNewsRepository::class)]
class JournalNews implements
TranslatableInterface,
OrdStatableInterface,
LanguageableChildInterface,
JournalBlockableInterface,
CloneableInterface
{
use IdTrait,
UuidTrait,
OrdStatTrait,
TimestampableTrait,
TranslatableTrait,
LanguageableChildTrait;
#[ApiProperty(description: 'Parent', readableLink: false, writableLink: false)]
#[Groups(['read', 'post'])]
#[Assert\NotNull]
#[ORM\ManyToOne(targetEntity: Journal::class, inversedBy: 'news')]
#[ORM\JoinColumn(onDelete: 'cascade', nullable: false)]
private Journal $parent;
#[ApiProperty(description: 'Working title')]
#[Groups(['read', 'write'])]
#[Assert\NotBlank]
#[ORM\Column(type: Types::STRING, length: 511)]
private string $workingTitle;
#[ApiProperty(description: 'Is UJ')]
#[Groups(['read:' . self::class, 'write'])]
#[ORM\Column(type: Types::BOOLEAN, options: ['default' => false])]
private bool $isUj = false;
#[ApiProperty(description: 'Published at')]
#[Groups(['read:' . self::class, 'write'])]
#[ORM\Column(type: Types::DATE_IMMUTABLE, options: ['default' => '1970-01-01'])]
private \DateTimeImmutable $publishedAt;
#[ApiProperty(description: 'Blocks', writableLink: false)]
#[ORM\OneToMany(
targetEntity: JournalNewsBlock::class,
mappedBy: 'parent',
cascade: ['persist', 'remove']
)]
#[ORM\OrderBy(['ord' => 'desc'])]
private Collection $blocks;
public function __construct(Journal $parent, string $workingTitle)
{
$this->setUuid();
$this->parent = $parent;
$this->workingTitle = $workingTitle;
$this->translations = new ArrayCollection();
$this->blocks = new ArrayCollection();
foreach ($this->parent->getLanguages() as $lang) {
new JournalNewsTranslation($this, $lang);
}
$this->publishedAt = new \DateTimeImmutable();
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = new \DateTimeImmutable();
$parent->addNews($this);
}
public function getParent(): Journal
{
return $this->parent;
}
public function getWorkingTitle(): string
{
return $this->workingTitle;
}
public function setWorkingTitle(string $workingTitle): self
{
$this->workingTitle = $workingTitle;
return $this;
}
public function getIsUj(): bool
{
return $this->isUj;
}
public function setIsUj(bool $isUj): self
{
$this->isUj = $isUj;
return $this;
}
public function getPublishedAt(): \DateTimeImmutable
{
return $this->publishedAt;
}
public function setPublishedAt(\DateTimeImmutable $publishedAt): self
{
$this->publishedAt = $publishedAt;
return $this;
}
/**
* @return Collection|JournalNewsBlock[]
*/
public function getBlocks(): Collection
{
return $this->blocks;
}
public function addBlock(BlockInterface $block): self
{
if ($this->blocks->contains($block)) {
return $this;
}
$this->blocks[] = $block;
return $this;
}
public function removeBlock(BlockInterface $block): self
{
$this->blocks->removeElement($block);
return $this;
}
public function resetBlocks(): self
{
$this->blocks = new ArrayCollection();
return $this;
}
public function clone(): self
{
$clone = clone $this;
$clone->id = null;
$clone->setUuid();
$clone->workingTitle .= ' [KOPIA]';
$clone->ord = 0;
$clone->stat = false;
ClassUtils::cloneCollection($this, $clone, 'translations');
$clone->synchronizeTranslations();
ClassUtils::cloneCollection($this, $clone, 'blocks');
$clone->createdAt = new \DateTimeImmutable();
$clone->updatedAt = new \DateTimeImmutable();
return $clone;
}
}