Notification.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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\Entity\Organization\Organization;
  7. use App\Repository\Core\NotificationRepository;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use JetBrains\PhpStorm\Pure;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. use App\Enum\Core\NotificationTypeEnum;
  14. /**
  15. * @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.
  16. *
  17. * Classe Notification. qui permet de gérer les notifications aux utilisateurs.
  18. */
  19. #[ApiResource(
  20. collectionOperations:[
  21. 'get' => [
  22. 'pagination_client_items_per_page' => true,
  23. 'pagination_maximum_items_per_page' => 20,
  24. 'order' => [
  25. 'id' => 'DESC'
  26. ]
  27. ]
  28. ],
  29. itemOperations: [
  30. 'get'
  31. ]
  32. )]
  33. #[ORM\Entity(repositoryClass:NotificationRepository::class)]
  34. #[ORM\Table(name: 'Information')]
  35. class Notification
  36. {
  37. #[ORM\Id]
  38. #[ORM\Column]
  39. #[ORM\GeneratedValue]
  40. private ?int $id = null;
  41. #[ORM\Column(length: 255, nullable: false)]
  42. private string $discr = 'notification';
  43. #[ORM\ManyToOne(inversedBy: 'notifications')]
  44. #[ORM\JoinColumn(nullable: false)]
  45. private ?Access $recipientAccess;
  46. #[ORM\ManyToOne(inversedBy: 'notifications')]
  47. #[ORM\JoinColumn(nullable: false)]
  48. private ?Organization $recipientOrganization;
  49. #[ORM\Column(length: 40, nullable: true)]
  50. private ?string $name = null;
  51. #[ORM\Column(type: 'datetime', nullable: true)]
  52. private ?\DateTimeInterface $createDate;
  53. #[ORM\Column(type: 'datetime', nullable: true)]
  54. private ?\DateTimeInterface $updateDate;
  55. #[ORM\Column(type: 'json', length: 4294967295, nullable: true)]
  56. private mixed $message = [];
  57. #[ORM\Column(nullable: true)]
  58. #[Assert\Choice(callback: [NotificationTypeEnum::class, 'toArray'], message: 'invalid-type')]
  59. private ?string $type = null;
  60. #[ORM\Column(length: 255, nullable: true)]
  61. private ?string $link = null;
  62. #[ORM\Column(type: 'date', nullable: true)]
  63. private ?\DateTimeInterface $availabilityDate = null;
  64. #[ORM\OneToMany(mappedBy: 'notification', targetEntity: NotificationUser::class, cascade: ['persist'], orphanRemoval: true)]
  65. private Collection $notificationUsers;
  66. #[Pure] public function __construct()
  67. {
  68. $this->notificationUsers = new ArrayCollection();
  69. }
  70. public function getId(): ?int
  71. {
  72. return $this->id;
  73. }
  74. public function setName(?string $name): self
  75. {
  76. $this->name = $name;
  77. return $this;
  78. }
  79. public function getName(): ?string
  80. {
  81. return $this->name;
  82. }
  83. public function setRecipientAccess(?Access $recipientAccess): self
  84. {
  85. $this->recipientAccess = $recipientAccess;
  86. return $this;
  87. }
  88. public function getRecipientAccess(): ?Access
  89. {
  90. return $this->recipientAccess;
  91. }
  92. /**
  93. * @return Organization|null
  94. */
  95. public function getRecipientOrganization(): ?Organization
  96. {
  97. return $this->recipientOrganization;
  98. }
  99. /**
  100. * @param Organization|null $recipientOrganization
  101. * @return Notification
  102. */
  103. public function setRecipientOrganization(?Organization $recipientOrganization): self
  104. {
  105. $this->recipientOrganization = $recipientOrganization;
  106. return $this;
  107. }
  108. /**
  109. * @return \DateTimeInterface|null
  110. */
  111. public function getCreateDate(): ?\DateTimeInterface
  112. {
  113. return $this->createDate;
  114. }
  115. /**
  116. * @param \DateTimeInterface|null $createDate
  117. */
  118. public function setCreateDate(?\DateTimeInterface $createDate): self
  119. {
  120. $this->createDate = $createDate;
  121. return $this;
  122. }
  123. /**
  124. * @return \DateTimeInterface|null
  125. */
  126. public function getUpdateDate(): ?\DateTimeInterface
  127. {
  128. return $this->updateDate;
  129. }
  130. /**
  131. * @param \DateTimeInterface|null $updateDate
  132. */
  133. public function setUpdateDate(?\DateTimeInterface $updateDate): self
  134. {
  135. $this->updateDate = $updateDate;
  136. return $this;
  137. }
  138. public function setMessage(mixed $message): self
  139. {
  140. $this->message = $message;
  141. return $this;
  142. }
  143. public function getMessage(): mixed
  144. {
  145. if(!is_array( $this->message)){
  146. return ['about' => $this->message];
  147. }
  148. return $this->message;
  149. }
  150. public function setType(?string $type): self
  151. {
  152. $this->type = $type;
  153. return $this;
  154. }
  155. public function getType(): ?string
  156. {
  157. return $this->type;
  158. }
  159. public function setLink(?string $link): self
  160. {
  161. $this->link = $link;
  162. return $this;
  163. }
  164. public function getLink(): ?string
  165. {
  166. return $this->link;
  167. }
  168. public function getAvailabilityDate(): ?string {
  169. return $this->availabilityDate?->format('Y-m-d');
  170. }
  171. public function setAvailabilityDate(?\DateTime $availabilityDate = null): self {
  172. if($availabilityDate == null) $availabilityDate = new \DateTime();
  173. $this->availabilityDate = $availabilityDate;
  174. return $this;
  175. }
  176. public function getNotificationUsers(): Collection
  177. {
  178. return $this->notificationUsers;
  179. }
  180. public function addNotificationUser(NotificationUser $notificationUsers): self
  181. {
  182. if (!$this->notificationUsers->contains($notificationUsers)) {
  183. $this->notificationUsers[] = $notificationUsers;
  184. $notificationUsers->setNotification($this);
  185. }
  186. return $this;
  187. }
  188. public function removeNotificationUser(NotificationUser $notificationUsers): self
  189. {
  190. if ($this->notificationUsers->removeElement($notificationUsers)) {
  191. // set the owning side to null (unless already changed)
  192. if ($notificationUsers->getNotification() === $this) {
  193. $notificationUsers->setNotification(null);
  194. }
  195. }
  196. return $this;
  197. }
  198. public function getDiscr(): ?string
  199. {
  200. return $this->discr;
  201. }
  202. public function setDiscr(string $discr): self
  203. {
  204. $this->discr = $discr;
  205. return $this;
  206. }
  207. }