src/Entity/JournalArticleMetadataTranslation.php line 31

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