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''read:minimal''write'])]
  35.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  36.     private ?string $title null;
  37.     #[ApiProperty(description'Annotation')]
  38.     #[Groups(['read''write'])]
  39.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  40.     private ?string $annotation null;
  41.     #[ApiProperty(description'Abstract')]
  42.     #[Groups(['read:' self::class, 'write'])]
  43.     #[Sanitizeable]
  44.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  45.     private ?string $abstract null;
  46.     #[ApiProperty(description'Translation language')]
  47.     #[ORM\Column(
  48.         typeTypes::STRING,
  49.         enumTypeLanguage::class,
  50.         length7,
  51.         options: ['default' => Language::EN]
  52.     )]
  53.     private Language $lang Language::EN;
  54.     #[ApiProperty(description'Is keywords import reseting previous')]
  55.     #[Groups(['read:' self::class, 'write'])]
  56.     #[DenormalizationPriority(DenormalizationPriority::TOP)]
  57.     private bool $isKeywordsImportResetingPrevious false;
  58.     #[ApiProperty(description'Keywords')]
  59.     #[ORM\OneToMany(
  60.         targetEntityJournalArticleMetadataTranslationKeyword::class,
  61.         mappedBy'parent',
  62.         cascade: ['persist''remove'],
  63.         orphanRemovaltrue
  64.     )]
  65.     #[ORM\OrderBy(['ord' => 'desc'])]
  66.     private Collection $keywords;
  67.     public function __construct(JournalArticle $parentLanguage $lang)
  68.     {
  69.         $this->setUuid();
  70.         $this->parent $parent;
  71.         $this->lang $lang;
  72.         $this->keywords = new ArrayCollection();
  73.         $this->createdAt = new \DateTimeImmutable();
  74.         $this->updatedAt = new \DateTimeImmutable();
  75.         $parent->addMetadataTranslation($this);
  76.     }
  77.     public function getParent(): JournalArticle
  78.     {
  79.         return $this->parent;
  80.     }
  81.     public function getTitle(): ?string
  82.     {
  83.         return $this->title;
  84.     }
  85.     #[Groups(['read:minimal'])]
  86.     public function getRawTitle(): ?string
  87.     {
  88.         return strip_tags($this->title);
  89.     }
  90.     public function setTitle(?string $title): self
  91.     {
  92.         $this->title $title;
  93.         /** @var JournalArticle $parent*/
  94.         $parent $this->parent;
  95.         if ($parent->getSlugTitleLanguage() !== null) {
  96.             $nativeTitleLang $parent->getSlugTitleLanguage();
  97.         } else {
  98.             $nativeTitleLang $parent->getNativeMetadataLanguage();
  99.         }
  100.         if ($this->lang === $nativeTitleLang) {
  101.             $parent
  102.                 ->setNativeTitle($title)
  103.                 ->setNativeTitleRaw(strip_tags($title));
  104.         }
  105.         return $this;
  106.     }
  107.     #[Groups(['read'])]
  108.     public function getFullTitle(): ?string
  109.     {
  110.         /** @var JournalArticle $parent */
  111.         $parent $this->parent;
  112.         /** @var string|null $journalTitle */
  113.         $journalTitle $parent->getParent()->readTranslation($this->lang'title');
  114.         /** @var string|null $volumeTitle */
  115.         $volumeTitle $parent->getIssue()->getVolume()->readAvailableTranslation($this->lang'title');
  116.         /** @var string|null $issueTitle */
  117.         $issueTitle $parent->getIssue()->readAvailableTranslation($this->lang'title');
  118.         return sprintf(
  119.             "%s - %s - %s - %s",
  120.             $journalTitle,
  121.             $volumeTitle,
  122.             $issueTitle,
  123.             $this->getRawTitle()
  124.         );
  125.     }
  126.     public function getAnnotation(): ?string
  127.     {
  128.         return $this->annotation;
  129.     }
  130.     public function setAnnotation(?string $annotation): self
  131.     {
  132.         $this->annotation $annotation;
  133.         return $this;
  134.     }
  135.     public function getAbstract(): ?string
  136.     {
  137.         return $this->abstract;
  138.     }
  139.     public function setAbstract(?string $abstract): self
  140.     {
  141.         $this->abstract $abstract;
  142.         return $this;
  143.     }
  144.     public function getLang(): Language
  145.     {
  146.         return $this->lang;
  147.     }
  148.     public function setIsKeywordsImportResetingPrevious(bool $isKeywordsImportResetingPrevious)
  149.     {
  150.         $this->isKeywordsImportResetingPrevious $isKeywordsImportResetingPrevious;
  151.         return $this;
  152.     }
  153.     public function getIsKeywordsImportResetingPrevious(): bool
  154.     {
  155.         return true;
  156.     }
  157.     #[Groups(['write'])]
  158.     public function setImportedKeywords(?string $importedKeywords): self
  159.     {
  160.         if (! $importedKeywords) {
  161.             return $this;
  162.         }
  163.         $importedKeywords str_replace(';'','$importedKeywords);
  164.         /** @var JournalArticleTranslation|null */
  165.         $adjacentTranslation $this->parent->getTranslation($this->lang);
  166.         if ($this->isKeywordsImportResetingPrevious) {
  167.             $adjacentTranslation?->setMetaKeywords($importedKeywords);
  168.             $this->resetKeywords();
  169.         } elseif (! empty($adjacentTranslation?->getMetaKeywords())) {
  170.             $adjacentTranslation?->setMetaKeywords("{$adjacentTranslation->getMetaKeywords()}{$importedKeywords}");
  171.         } else {
  172.             $adjacentTranslation?->setMetaKeywords($importedKeywords);
  173.         }
  174.         $keywords array_map(
  175.             fn(string $keyword) => trim($keyword),
  176.             explode(','$importedKeywords)
  177.         );
  178.         $keywords array_filter(
  179.             $keywords,
  180.             fn(string $keyword) => ! empty($keyword)
  181.         );
  182.         foreach($keywords as $keyword) {
  183.             new JournalArticleMetadataTranslationKeyword($this$keyword);
  184.         }
  185.         $ord count($this->keywords);
  186.         foreach($this->keywords as $keyword) {
  187.             $keyword->setOrd($ord--);
  188.         }
  189.         return $this;
  190.     }
  191.     /**
  192.      * @return Collection|JournalArticleMetadataTranslationKeyword[]
  193.      */
  194.     public function getKeywords(): Collection
  195.     {
  196.         return $this->keywords;
  197.     }
  198.     public function addKeyword(JournalArticleMetadataTranslationKeyword $keyword): self
  199.     {
  200.         if ($this->keywords->contains($keyword)) {
  201.             return $this;
  202.         }
  203.         $this->keywords[] = $keyword;
  204.         return $this;
  205.     }
  206.     public function removeKeyword(JournalArticleMetadataTranslationKeyword $keyword): self
  207.     {
  208.         $this->keywords->removeElement($keyword);
  209.         return $this;
  210.     }
  211.     public function resetKeywords(): self
  212.     {
  213.         $this->keywords = new ArrayCollection();
  214.         return $this;
  215.     }
  216.     public function setUpdatedAt(\DateTimeImmutable $updatedAt): self
  217.     {
  218.         $this->updatedAt $updatedAt;
  219.         $this->getParent()->setUpdatedAt($updatedAt);
  220.         return $this;
  221.     }
  222.     public function clone(JournalArticle $parent): self
  223.     {
  224.         $clone = clone $this;
  225.         $clone->id null;
  226.         $clone->setUuid();
  227.         $clone->parent $parent;
  228.         $clone->parent->addMetadataTranslation($clone);
  229.         $clone->resetKeywords();
  230.         $clone->createdAt = new \DateTimeImmutable();
  231.         $clone->updatedAt = new \DateTimeImmutable();
  232.         return $clone;
  233.     }
  234. }