AbstractInformation.php 6.4 KB

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