src/Entity/Author.php line 92

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Symfony\Component\Serializer\Annotation\Groups;
  7. use ApiPlatform\Metadata\ApiProperty;
  8. use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
  9. use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
  10. use ApiPlatform\Metadata\ApiFilter;
  11. use ApiPlatform\Metadata\ApiResource,
  12.     ApiPlatform\Metadata\Get,
  13.     ApiPlatform\Metadata\GetCollection,
  14.     ApiPlatform\Metadata\Post,
  15.     ApiPlatform\Metadata\Put,
  16.     ApiPlatform\Metadata\Delete;
  17. use App\Filter\OrderByUnmapped;
  18. use App\Repository\AuthorRepository,
  19.     App\Security\Voter\ArchivableVoter,
  20.     App\Lib\Actions;
  21. use App\StateProvider\StatsAuthorProvider;
  22. use App\Controller\Stats\StatsAuthorXlsxGenerator;
  23. use App\Filter\RangeUnmapped;
  24. #[ApiResource(
  25.     description'Authors',
  26.     normalizationContext: ['groups' => [
  27.         'read',
  28.         'read:' self::class,
  29.         'read:' self::class . 'Translation',
  30.         'read:' AbstractAuthor::class,
  31.         'read:' AbstractAuthor::class . 'Translation'
  32.     ]],
  33.     denormalizationContext: ['groups' => ['write']],
  34.     security'is_granted("' self::class . '")',
  35.     order: ['ord' => 'desc'],
  36.     operations: [
  37.         new GetCollection(),
  38.         new GetCollection(
  39.             name'get_author_stats',
  40.             uriTemplate'/authors/stats',
  41.             providerStatsAuthorProvider::class,
  42.             security'is_granted("ROLE_MODULE_STATS")',
  43.             normalizationContext: ['groups' => ['read:stats']],
  44.             paginationMaximumItemsPerPage500
  45.         ),
  46.         new GetCollection(
  47.             name'get_author_stats_xlsx',
  48.             uriTemplate'/authors/stats/xlsx',
  49.             providerStatsAuthorProvider::class,
  50.             controllerStatsAuthorXlsxGenerator::class,
  51.             security'is_granted("ROLE_MODULE_STATS")',
  52.         ),
  53.         new Post(),
  54.         new Get(),
  55.         new Put(),
  56.         new Delete(
  57.             securityPostDenormalize'is_granted("' Actions::DELETE '", object)',
  58.             securityPostDenormalizeMessageArchivableVoter::MESSAGE
  59.         ),
  60.     ],
  61.     extraProperties: ['standard_put' => false],
  62. )]
  63. #[ApiFilter(SearchFilter::class, properties: [
  64.     'name' => 'ipartial',
  65.     'surname' => 'ipartial',
  66.     'transcriptionName' => 'ipartial',
  67.     'transcriptionSurname' => 'ipartial',
  68.     'affiliations.country' => 'ipartial',
  69.     'affiliations.nativeTitle' => 'ipartial',
  70.     'uuid' => 'exact',
  71. ])]
  72. #[ApiFilter(OrderFilter::class, properties: [
  73.     'name',
  74.     'surname',
  75.     'transcriptionName',
  76.     'transcriptionSurname',
  77.     'affiliations.country',
  78.     'affiliations.nativeTitle',
  79.     'nameSortKey',
  80.     'surnameSortKey',
  81. ])]
  82. #[ApiFilter(OrderByUnmapped::class, properties: ['statsAmountOfViews''statsAmountOfDownloads'])]
  83. #[ApiFilter(RangeUnmapped::class, properties: ['statsAmountOfViews''statsAmountOfDownloads'])]
  84. #[ORM\Entity(repositoryClassAuthorRepository::class)]
  85. class Author extends AbstractAuthor
  86. {
  87.     #[ApiProperty(description'Statistics: journals')]
  88.     #[Groups(['read:stats'])]
  89.     private ?string $statsJournals null;
  90.     #[ApiProperty(description'Statistics: amount of views')]
  91.     #[Groups(['read:stats'])]
  92.     private int $statsAmountOfViews 0;
  93.     #[ApiProperty(description'Statistics: amount of downloads')]
  94.     #[Groups(['read:stats'])]
  95.     private int $statsAmountOfDownloads 0;
  96.     #[ApiProperty(description'Statistics: affiliations title')]
  97.     #[Groups(['read:stats'])]
  98.     private ?string $statsAffiliationsTitle null;
  99.     #[ApiProperty(description'Article relations')]
  100.     #[ORM\OneToMany(targetEntityJournalArticleAuthor::class, mappedBy'author')]
  101.     private Collection $articleRelations;
  102.     public function __construct(?string $namestring $surname, ?array $languages)
  103.     {
  104.         parent::__construct($name ?? '_no_name_'$surname$languages);
  105.         if ($this->getName() === '_no_name_') {
  106.             $this->setName(null);
  107.         }
  108.         $this->articleRelations = new ArrayCollection();
  109.     }
  110.     public function getType(): string
  111.     {
  112.         return 'author';
  113.     }
  114.     public function getStatsJournals(): ?string
  115.     {
  116.         return $this->statsJournals;
  117.     }
  118.     public function setStatsJournals(?string $statsJournals): self
  119.     {
  120.         $this->statsJournals $statsJournals;
  121.         return $this;
  122.     }
  123.     public function getStatsAmountOfViews(): int
  124.     {
  125.         return $this->statsAmountOfViews;
  126.     }
  127.     public function setStatsAmountOfViews(int $statsAmountOfViews): self
  128.     {
  129.         $this->statsAmountOfViews $statsAmountOfViews;
  130.         return $this;
  131.     }
  132.     public function getStatsAmountOfDownloads(): int
  133.     {
  134.         return $this->statsAmountOfDownloads;
  135.     }
  136.     public function setStatsAmountOfDownloads(int $statsAmountOfDownloads): self
  137.     {
  138.         $this->statsAmountOfDownloads $statsAmountOfDownloads;
  139.         return $this;
  140.     }
  141.     public function getArticleRelations(): Collection
  142.     {
  143.         return $this->articleRelations;
  144.     }
  145.     #[Groups(['read:stats'])]
  146.     public function getStatsCountry(): ?string
  147.     {
  148.         return false !== ($first $this->affiliations->first())
  149.             ? $first->getCountry()?->value
  150.             null;
  151.     }
  152.     public function getStatsAffiliationsTitle(): ?string
  153.     {
  154.         return $this->statsAffiliationsTitle;
  155.     }
  156.     public function setStatsAffiliationsTitle(string $statsAffiliationsTitle null): self
  157.     {
  158.         $this->statsAffiliationsTitle $statsAffiliationsTitle;
  159.         return $this;
  160.     }
  161.     public function getStatsAffiliations(): ?string
  162.     {
  163.         $affiliations = [];
  164.         /** @var Affiliation */
  165.         foreach ($this->affiliations as $entry) {
  166.             if (isset($affiliations[$entry->getUuid()->toString()])) {
  167.                 continue;
  168.             }
  169.             $affiliations[$entry->getUuid()->toString()] =
  170.                 $entry->readNativeLanguageTranslation('title');
  171.         }
  172.         sort($affiliations);
  173.         return implode(', ',  $affiliations);
  174.     }
  175. }