src/Entity/Author.php line 87

  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. #[ApiResource(
  24.     description'Authors',
  25.     normalizationContext: ['groups' => [
  26.         'read',
  27.         'read:' self::class,
  28.         'read:' self::class . 'Translation',
  29.         'read:' AbstractAuthor::class,
  30.         'read:' AbstractAuthor::class . 'Translation'
  31.     ]],
  32.     denormalizationContext: ['groups' => ['write']],
  33.     security'is_granted("' self::class . '")',
  34.     order: ['ord' => 'desc'],
  35.     operations: [
  36.         new GetCollection(),
  37.         new GetCollection(
  38.             name'get_author_stats',
  39.             uriTemplate'/authors/stats',
  40.             providerStatsAuthorProvider::class,
  41.             security'is_granted("ROLE_MODULE_STATS")',
  42.             normalizationContext: ['groups' => ['read:stats']],
  43.             paginationMaximumItemsPerPage500
  44.         ),
  45.         new GetCollection(
  46.             name'get_author_stats_xlsx',
  47.             uriTemplate'/authors/stats/xlsx',
  48.             providerStatsAuthorProvider::class,
  49.             controllerStatsAuthorXlsxGenerator::class,
  50.             security'is_granted("ROLE_MODULE_STATS")',
  51.         ),
  52.         new Post(),
  53.         new Get(),
  54.         new Put(),
  55.         new Delete(
  56.             securityPostDenormalize'is_granted("' Actions::DELETE '", object)',
  57.             securityPostDenormalizeMessageArchivableVoter::MESSAGE
  58.         ),
  59.     ],
  60.     extraProperties: ['standard_put' => false],
  61. )]
  62. #[ApiFilter(SearchFilter::class, properties: [
  63.     'name' => 'ipartial',
  64.     'surname' => 'ipartial',
  65.     'transcriptionName' => 'ipartial',
  66.     'transcriptionSurname' => 'ipartial',
  67.     'affiliations.country' => 'ipartial',
  68.     'affiliations.nativeTitle' => 'ipartial',
  69.     'uuid' => 'exact',
  70. ])]
  71. #[ApiFilter(OrderFilter::class, properties: [
  72.     'name',
  73.     'surname',
  74.     'transcriptionName',
  75.     'transcriptionSurname',
  76.     'affiliations.country',
  77.     'affiliations.nativeTitle',
  78. ])]
  79. #[ApiFilter(OrderByUnmapped::class, properties: ['statsAmountOfViews''statsAmountOfDownloads'])]
  80. #[ORM\Entity(repositoryClassAuthorRepository::class)]
  81. class Author extends AbstractAuthor
  82. {
  83.     #[ApiProperty(description'Statistics: journals')]
  84.     #[Groups(['read:stats'])]
  85.     private ?string $statsJournals null;
  86.     #[ApiProperty(description'Statistics: amount of views')]
  87.     #[Groups(['read:stats'])]
  88.     private int $statsAmountOfViews 0;
  89.     #[ApiProperty(description'Statistics: amount of downloads')]
  90.     #[Groups(['read:stats'])]
  91.     private int $statsAmountOfDownloads 0;
  92.     #[ApiProperty(description'Article relations')]
  93.     #[ORM\OneToMany(targetEntityJournalArticleAuthor::class, mappedBy'author')]
  94.     private Collection $articleRelations;
  95.     public function __construct(string $namestring $surname, ?array $languages)
  96.     {
  97.         parent::__construct($name$surname$languages);
  98.         $this->articleRelations = new ArrayCollection();
  99.     }
  100.     public function getType(): string
  101.     {
  102.         return 'author';
  103.     }
  104.     public function getStatsJournals(): ?string
  105.     {
  106.         return $this->statsJournals;
  107.     }
  108.     public function setStatsJournals(?string $statsJournals): self
  109.     {
  110.         $this->statsJournals $statsJournals;
  111.         return $this;
  112.     }
  113.     public function getStatsAmountOfViews(): int
  114.     {
  115.         return $this->statsAmountOfViews;
  116.     }
  117.     public function setStatsAmountOfViews(int $statsAmountOfViews): self
  118.     {
  119.         $this->statsAmountOfViews $statsAmountOfViews;
  120.         return $this;
  121.     }
  122.     public function getStatsAmountOfDownloads(): int
  123.     {
  124.         return $this->statsAmountOfDownloads;
  125.     }
  126.     public function setStatsAmountOfDownloads(int $statsAmountOfDownloads): self
  127.     {
  128.         $this->statsAmountOfDownloads $statsAmountOfDownloads;
  129.         return $this;
  130.     }
  131.     public function getArticleRelations(): Collection
  132.     {
  133.         return $this->articleRelations;
  134.     }
  135.     #[Groups(['read:stats'])]
  136.     public function getStatsCountry(): ?string
  137.     {
  138.         return false !== ($first $this->affiliations->first())
  139.             ? $first->getCountry()?->value
  140.             null;
  141.     }
  142.     #[Groups(['read:stats'])]
  143.     public function getStatsAffiliations(): ?string
  144.     {
  145.         $affiliations = [];
  146.         /** @var Affiliation */
  147.         foreach($this->affiliations as $entry) {
  148.             if (isset($affiliations[$entry->getUuid()->toString()])) {
  149.                 continue;
  150.             }
  151.             $affiliations[$entry->getUuid()->toString()] =
  152.                 $entry->readNativeLanguageTranslation('title');
  153.         }
  154.         sort($affiliations);
  155.         return implode(', ',  $affiliations);
  156.     }
  157. }