Notification.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Core;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use App\Entity\Access\Access;
  6. use App\Repository\Core\NotificationRepository;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use JetBrains\PhpStorm\Pure;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. /**
  13. * @todo : A la suite de la migration, il faut supprimer le nom de la table pour avoir une table Notification, et supprimer l'attribut discr.
  14. *
  15. * Classe Notification. qui permet de gérer les notifications aux utilisateurs.
  16. */
  17. #[ApiResource(
  18. collectionOperations:[
  19. 'get' => [
  20. 'pagination_client_items_per_page' => true,
  21. 'pagination_maximum_items_per_page' => 20,
  22. 'order' => [
  23. 'id' => 'DESC'
  24. ]
  25. ]
  26. ],
  27. itemOperations: [
  28. 'get'
  29. ]
  30. )]
  31. #[ORM\Entity(repositoryClass:NotificationRepository::class)]
  32. #[ORM\Table(name: 'Information')]
  33. class Notification
  34. {
  35. #[ORM\Id]
  36. #[ORM\Column]
  37. #[ORM\GeneratedValue]
  38. private ?int $id = null;
  39. #[ORM\Column(length: 255, nullable: false)]
  40. private string $discr = 'notification';
  41. #[ORM\ManyToOne(inversedBy: 'notifications')]
  42. #[ORM\JoinColumn(nullable: false)]
  43. private ?Access $recipientAccess;
  44. #[ORM\Column(length: 40, nullable: true)]
  45. private ?string $name = null;
  46. #[ORM\Column(type: 'json', length: 4294967295, nullable: true)]
  47. private mixed $message = [];
  48. #[ORM\Column(nullable: true)]
  49. #[Assert\Choice(callback: ['\App\Enum\Core\NotificationTypeEnum', 'toArray'], message: 'invalid-type')]
  50. private ?string $type = null;
  51. #[ORM\Column(length: 255, nullable: true)]
  52. private ?string $link = null;
  53. #[ORM\Column(type: 'date', nullable: true)]
  54. private ?\DateTimeInterface $availabilityDate = null;
  55. #[ORM\OneToMany(mappedBy: 'notification', targetEntity: NotificationUser::class, cascade: ['persist'], orphanRemoval: true)]
  56. private Collection $notificationUsers;
  57. #[Pure] public function __construct()
  58. {
  59. $this->notificationUsers = new ArrayCollection();
  60. }
  61. public function getId(): ?int
  62. {
  63. return $this->id;
  64. }
  65. public function setName(?string $name): self
  66. {
  67. $this->name = $name;
  68. return $this;
  69. }
  70. public function getName(): ?string
  71. {
  72. return $this->name;
  73. }
  74. public function setRecipientAccess(?Access $recipientAccess): self
  75. {
  76. $this->recipientAccess = $recipientAccess;
  77. return $this;
  78. }
  79. public function getRecipientAccess(): ?Access
  80. {
  81. return $this->recipientAccess;
  82. }
  83. public function setMessage(mixed $message): self
  84. {
  85. $this->message = $message;
  86. return $this;
  87. }
  88. public function getMessage(): mixed
  89. {
  90. if(!is_array( $this->message)){
  91. return ['about' => $this->message];
  92. }
  93. return $this->message;
  94. }
  95. public function setType(?string $type): self
  96. {
  97. $this->type = $type;
  98. return $this;
  99. }
  100. public function getType(): ?string
  101. {
  102. return $this->type;
  103. }
  104. public function setLink(?string $link): self
  105. {
  106. $this->link = $link;
  107. return $this;
  108. }
  109. public function getLink(): ?string
  110. {
  111. return $this->link;
  112. }
  113. public function getAvailabilityDate(): ?string {
  114. return $this->availabilityDate?->format('Y-m-d');
  115. }
  116. public function setAvailabilityDate(?\DateTime $availabilityDate = null): self {
  117. if($availabilityDate == null) $availabilityDate = new \DateTime();
  118. $this->availabilityDate = $availabilityDate;
  119. return $this;
  120. }
  121. public function getNotificationUsers(): Collection
  122. {
  123. return $this->notificationUsers;
  124. }
  125. public function addNotificationUser(NotificationUser $notificationUsers): self
  126. {
  127. if (!$this->notificationUsers->contains($notificationUsers)) {
  128. $this->notificationUsers[] = $notificationUsers;
  129. $notificationUsers->setNotification($this);
  130. }
  131. return $this;
  132. }
  133. public function removeNotificationUser(NotificationUser $notificationUsers): self
  134. {
  135. if ($this->notificationUsers->removeElement($notificationUsers)) {
  136. // set the owning side to null (unless already changed)
  137. if ($notificationUsers->getNotification() === $this) {
  138. $notificationUsers->setNotification(null);
  139. }
  140. }
  141. return $this;
  142. }
  143. }