src/Entity/JournalArticleMetadataTranslation.php line 30

  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. use ApiPlatform\Metadata\ApiProperty,
  9.     ApiPlatform\Metadata\ApiResource,
  10.     ApiPlatform\Metadata\Get;
  11. use App\Entity\Trait\IdTrait,
  12.     App\Entity\Trait\UuidTrait,
  13.     App\Entity\Trait\TimestampableTrait;
  14. use App\Enum\Language,
  15.     App\Attribute\Sanitizeable,
  16.     App\Attribute\DenormalizationPriority,
  17.     App\Repository\JournalArticleMetadataTranslationRepository;
  18. #[ApiResource(
  19.     security'is_granted("' Journal::class . '")',
  20.     operations: [ new Get() ],
  21.     extraProperties: ['standard_put' => false],
  22. )]
  23. #[ORM\Entity(repositoryClassJournalArticleMetadataTranslationRepository::class)]
  24. class JournalArticleMetadataTranslation
  25. {
  26.     use IdTrait,
  27.         UuidTrait,
  28.         TimestampableTrait;
  29.     #[ApiProperty(description'Parent')]
  30.     #[ORM\ManyToOne(targetEntityJournalArticle::class, inversedBy'metadataTranslations')]
  31.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  32.     private JournalArticle $parent;
  33.     #[ApiProperty(description'Title')]
  34.     #[Groups(['read''write'])]
  35.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  36.     private ?string $title null;
  37.     #[ApiProperty(description'Abstract')]
  38.     #[Groups(['read:' self::class, 'write'])]
  39.     #[Sanitizeable]
  40.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  41.     private ?string $abstract null;
  42.     #[ApiProperty(description'Translation language')]
  43.     #[ORM\Column(
  44.         typeTypes::STRING,
  45.         enumTypeLanguage::class,
  46.         length7,
  47.         options: ['default' => Language::EN]
  48.     )]
  49.     private Language $lang Language::EN;
  50.     #[ApiProperty(description'Is keywords import reseting previous')]
  51.     #[Groups(['read:' self::class, 'write'])]
  52.     #[DenormalizationPriority(DenormalizationPriority::TOP)]
  53.     private bool $isKeywordsImportResetingPrevious false;
  54.     #[ApiProperty(description'Keywords')]
  55.     #[ORM\OneToMany(
  56.         targetEntityJournalArticleMetadataTranslationKeyword::class,
  57.         mappedBy'parent',
  58.         cascade: ['persist''remove'],
  59.         orphanRemovaltrue
  60.     )]
  61.     #[ORM\OrderBy(['ord' => 'desc'])]
  62.     private Collection $keywords;
  63.     public function __construct(JournalArticle $parentLanguage $lang)
  64.     {
  65.         $this->setUuid();
  66.         $this->parent $parent;
  67.         $this->lang $lang;
  68.         $this->keywords = new ArrayCollection();
  69.         $this->createdAt = new \DateTimeImmutable();
  70.         $this->updatedAt = new \DateTimeImmutable();
  71.         $parent->addMetadataTranslation($this);
  72.     }
  73.     public function getParent(): JournalArticle
  74.     {
  75.         return $this->parent;
  76.     }
  77.     public function getTitle(): ?string
  78.     {
  79.         return $this->title;
  80.     }
  81.     public function getRawTitle(): ?string
  82.     {
  83.         return strip_tags($this->title);
  84.     }
  85.     public function setTitle(?string $title): self
  86.     {
  87.         $this->title $title;
  88.         /** @var JournalArticle $parent*/
  89.         $parent $this->parent;
  90.         if ($parent->getSlugTitleLanguage() !== null) {
  91.             $nativeTitleLang $parent->getSlugTitleLanguage();
  92.         } else {
  93.             $nativeTitleLang $parent->getNativeMetadataLanguage();
  94.         }
  95.         if ($this->lang === $nativeTitleLang) {
  96.             $parent
  97.                 ->setNativeTitle($title)
  98.                 ->setNativeTitleRaw(strip_tags($title));
  99.         }
  100.         return $this;
  101.     }
  102.     public function getAbstract(): ?string
  103.     {
  104.         return $this->abstract;
  105.     }
  106.     public function setAbstract(?string $abstract): self
  107.     {
  108.         $this->abstract $abstract;
  109.         return $this;
  110.     }
  111.     public function getLang(): Language
  112.     {
  113.         return $this->lang;
  114.     }
  115.     public function setIsKeywordsImportResetingPrevious(bool $isKeywordsImportResetingPrevious)
  116.     {
  117.         $this->isKeywordsImportResetingPrevious $isKeywordsImportResetingPrevious;
  118.         return $this;
  119.     }
  120.     public function getIsKeywordsImportResetingPrevious(): bool
  121.     {
  122.         return true;
  123.     }
  124.     #[Groups(['write'])]
  125.     public function setImportedKeywords(?string $importedKeywords): self
  126.     {
  127.         if (! $importedKeywords) {
  128.             return $this;
  129.         }
  130.         $importedKeywords str_replace(';'','$importedKeywords);
  131.         /** @var JournalArticleTranslation|null */
  132.         $adjacentTranslation $this->parent->getTranslation($this->lang);
  133.         if ($this->isKeywordsImportResetingPrevious) {
  134.             $adjacentTranslation?->setMetaKeywords($importedKeywords);
  135.             $this->resetKeywords();
  136.         } elseif (! empty($adjacentTranslation?->getMetaKeywords())) {
  137.             $adjacentTranslation?->setMetaKeywords("{$adjacentTranslation->getMetaKeywords()}{$importedKeywords}");
  138.         } else {
  139.             $adjacentTranslation?->setMetaKeywords($importedKeywords);
  140.         }
  141.         $keywords array_map(
  142.             fn(string $keyword) => trim($keyword),
  143.             explode(','$importedKeywords)
  144.         );
  145.         $keywords array_filter(
  146.             $keywords,
  147.             fn(string $keyword) => ! empty($keyword)
  148.         );
  149.         foreach($keywords as $keyword) {
  150.             new JournalArticleMetadataTranslationKeyword($this$keyword);
  151.         }
  152.         $ord count($this->keywords);
  153.         foreach($this->keywords as $keyword) {
  154.             $keyword->setOrd($ord--);
  155.         }
  156.         return $this;
  157.     }
  158.     /**
  159.      * @return Collection|JournalArticleMetadataTranslationKeyword[]
  160.      */
  161.     public function getKeywords(): Collection
  162.     {
  163.         return $this->keywords;
  164.     }
  165.     public function addKeyword(JournalArticleMetadataTranslationKeyword $keyword): self
  166.     {
  167.         if ($this->keywords->contains($keyword)) {
  168.             return $this;
  169.         }
  170.         $this->keywords[] = $keyword;
  171.         return $this;
  172.     }
  173.     public function removeKeyword(JournalArticleMetadataTranslationKeyword $keyword): self
  174.     {
  175.         $this->keywords->removeElement($keyword);
  176.         return $this;
  177.     }
  178.     public function resetKeywords(): self
  179.     {
  180.         $this->keywords = new ArrayCollection();
  181.         return $this;
  182.     }
  183.     public function clone(JournalArticle $parent): self
  184.     {
  185.         $clone = clone $this;
  186.         $clone->id null;
  187.         $clone->setUuid();
  188.         $clone->parent $parent;
  189.         $clone->parent->addMetadataTranslation($clone);
  190.         $clone->resetKeywords();
  191.         $clone->createdAt = new \DateTimeImmutable();
  192.         $clone->updatedAt = new \DateTimeImmutable();
  193.         return $clone;
  194.     }
  195. }