AbstractInformation.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Core;
  4. use ApiPlatform\Metadata\ApiResource;
  5. use App\Entity\Access\Access;
  6. use App\Entity\Organization\Organization;
  7. // use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Auditable;
  8. use App\Enum\Core\NotificationTypeEnum;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use JetBrains\PhpStorm\Pure;
  13. use Symfony\Component\Serializer\Annotation\Context;
  14. use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
  15. /**
  16. * @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.
  17. *
  18. * Classe Notification. qui permet de gérer les notifications aux utilisateurs.
  19. */
  20. #[ApiResource(operations: [])]
  21. // #[Auditable]
  22. #[ORM\Entity]
  23. #[ORM\Table(name: 'Information')]
  24. #[ORM\InheritanceType('SINGLE_TABLE')]
  25. #[ORM\DiscriminatorColumn(name: 'discr', type: 'string')]
  26. #[ORM\DiscriminatorMap([
  27. 'notification' => 'Notification',
  28. 'tips' => 'Tips',
  29. ])]
  30. class AbstractInformation
  31. {
  32. #[ORM\Id]
  33. #[ORM\Column]
  34. #[ORM\GeneratedValue]
  35. protected ?int $id = null;
  36. #[ORM\ManyToOne(inversedBy: 'notifications')]
  37. #[ORM\JoinColumn(nullable: false)]
  38. protected ?Access $recipientAccess;
  39. #[ORM\ManyToOne(inversedBy: 'notifications')]
  40. #[ORM\JoinColumn(nullable: false)]
  41. protected ?Organization $recipientOrganization;
  42. #[ORM\Column(length: 40, nullable: true)]
  43. protected ?string $name = null;
  44. #[ORM\Column(type: 'datetime', nullable: true)]
  45. protected ?\DateTimeInterface $createDate;
  46. #[ORM\Column(type: 'datetime', nullable: true)]
  47. protected ?\DateTimeInterface $updateDate;
  48. #[ORM\Column(type: 'json', length: 4294967295, nullable: true)]
  49. protected mixed $message = [];
  50. #[ORM\Column(length: 50, nullable: true, enumType: NotificationTypeEnum::class)]
  51. protected ?NotificationTypeEnum $type = null;
  52. #[ORM\Column(length: 255, nullable: true)]
  53. protected ?string $link = null;
  54. #[ORM\Column(type: 'date', nullable: true)]
  55. #[Context(normalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
  56. protected ?\DateTimeInterface $availabilityDate = null;
  57. #[ORM\OneToMany(mappedBy: 'notification', targetEntity: NotificationUser::class, cascade: ['persist'], orphanRemoval: true)]
  58. protected Collection $notificationUsers;
  59. #[Pure]
  60. public function __construct()
  61. {
  62. $this->notificationUsers = new ArrayCollection();
  63. }
  64. public function getId(): ?int
  65. {
  66. return $this->id;
  67. }
  68. public function setName(?string $name): self
  69. {
  70. $this->name = $name;
  71. return $this;
  72. }
  73. public function getName(): ?string
  74. {
  75. return $this->name;
  76. }
  77. public function setRecipientAccess(?Access $recipientAccess): self
  78. {
  79. $this->recipientAccess = $recipientAccess;
  80. return $this;
  81. }
  82. public function getRecipientAccess(): ?Access
  83. {
  84. return $this->recipientAccess;
  85. }
  86. public function getRecipientOrganization(): ?Organization
  87. {
  88. return $this->recipientOrganization;
  89. }
  90. public function setRecipientOrganization(?Organization $recipientOrganization): self
  91. {
  92. $this->recipientOrganization = $recipientOrganization;
  93. return $this;
  94. }
  95. public function getCreateDate(): ?\DateTimeInterface
  96. {
  97. return $this->createDate;
  98. }
  99. public function setCreateDate(?\DateTimeInterface $createDate): self
  100. {
  101. $this->createDate = $createDate;
  102. return $this;
  103. }
  104. public function getUpdateDate(): ?\DateTimeInterface
  105. {
  106. return $this->updateDate;
  107. }
  108. public function setUpdateDate(?\DateTimeInterface $updateDate): self
  109. {
  110. $this->updateDate = $updateDate;
  111. return $this;
  112. }
  113. public function setMessage(mixed $message): self
  114. {
  115. $this->message = $message;
  116. return $this;
  117. }
  118. public function getMessage(): mixed
  119. {
  120. if (!is_array($this->message)) {
  121. return ['about' => $this->message];
  122. }
  123. return $this->message;
  124. }
  125. public function setType(?NotificationTypeEnum $type): self
  126. {
  127. $this->type = $type;
  128. return $this;
  129. }
  130. public function getType(): ?NotificationTypeEnum
  131. {
  132. return $this->type;
  133. }
  134. public function setLink(?string $link): self
  135. {
  136. $this->link = $link;
  137. return $this;
  138. }
  139. public function getLink(): ?string
  140. {
  141. return $this->link;
  142. }
  143. public function getAvailabilityDate(): ?\DateTimeInterface
  144. {
  145. return $this->availabilityDate;
  146. }
  147. public function setAvailabilityDate(?\DateTime $availabilityDate = null): self
  148. {
  149. if ($availabilityDate == null) {
  150. $availabilityDate = new \DateTime();
  151. }
  152. $this->availabilityDate = $availabilityDate;
  153. return $this;
  154. }
  155. public function getNotificationUsers(): Collection
  156. {
  157. return $this->notificationUsers;
  158. }
  159. public function addNotificationUser(NotificationUser $notificationUsers): self
  160. {
  161. if (!$this->notificationUsers->contains($notificationUsers)) {
  162. $this->notificationUsers[] = $notificationUsers;
  163. $notificationUsers->setNotification($this);
  164. }
  165. return $this;
  166. }
  167. public function removeNotificationUser(NotificationUser $notificationUsers): self
  168. {
  169. if ($this->notificationUsers->removeElement($notificationUsers)) {
  170. // set the owning side to null (unless already changed)
  171. if ($notificationUsers->getNotification() === $this) {
  172. $notificationUsers->setNotification(null);
  173. }
  174. }
  175. return $this;
  176. }
  177. }