src/Entity/Alert.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\AlertRepository;
  4. use Doctrine\DBAL\Types\Types;
  5. use Doctrine\ORM\Mapping as ORM;
  6. #[ORM\Entity(repositoryClassAlertRepository::class)]
  7. #[ORM\Index(name"idx_alert_user"columns: ["user_id"])]
  8. #[ORM\Index(name"idx_alert_concerned_user"columns: ["concerned_user_id"])]
  9. #[ORM\Index(name"idx_alert_statut"columns: ["statut"])]
  10. #[ORM\Index(name"idx_alert_priority"columns: ["priorite"])]
  11. #[ORM\Index(name"idx_alert_created"columns: ["created_at"])]
  12. class Alert
  13. {
  14.     #[ORM\Id]
  15.     #[ORM\GeneratedValue]
  16.     #[ORM\Column]
  17.     private ?int $id null;
  18.     #[ORM\Column(length100)]
  19.     private ?string $type null// Type d'alerte
  20.     #[ORM\Column(length50)]
  21.     private ?string $categorie null// stocks, ventes, rh, caisse, comptabilite, etc.
  22.     #[ORM\Column(length20)]
  23.     private ?string $priorite 'normale'// critique, urgente, normale, info
  24.     #[ORM\Column(length255)]
  25.     private ?string $titre null;
  26.     #[ORM\Column(typeTypes::TEXT)]
  27.     private ?string $message null;
  28.     #[ORM\Column(typeTypes::JSONnullabletrue)]
  29.     private ?array $data null// Données additionnelles en JSON
  30.     #[ORM\Column(length255nullabletrue)]
  31.     private ?string $lien null// Lien vers la ressource concernée
  32.     #[ORM\Column(length50nullabletrue)]
  33.     private ?string $icone null// Icône Bootstrap Icons
  34.     #[ORM\Column(length50nullabletrue)]
  35.     private ?string $couleur null// Couleur badge/notification
  36.     #[ORM\ManyToOne(targetEntityUser::class)]
  37.     #[ORM\JoinColumn(nullabletrue)]
  38.     private ?User $user null// Si alerte pour un utilisateur spécifique
  39.     #[ORM\Column(length255nullabletrue)]
  40.     private ?string $roles null// Rôles autorisés à voir l'alerte (JSON array serialized)
  41.     #[ORM\Column(length20)]
  42.     private ?string $statut 'nouvelle'// nouvelle, vue, traitee, archivee
  43.     #[ORM\Column]
  44.     private ?\DateTimeImmutable $createdAt null;
  45.     #[ORM\Column(nullabletrue)]
  46.     private ?\DateTimeImmutable $viewedAt null;
  47.     #[ORM\Column(nullabletrue)]
  48.     private ?\DateTimeImmutable $treatedAt null;
  49.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  50.     private ?\DateTimeInterface $expireAt null// Date d'expiration de l'alerte
  51.     #[ORM\Column]
  52.     private ?bool $persistent false// Si l'alerte reste même après traitement
  53.     #[ORM\Column(nullabletrue)]
  54.     private ?int $entityId null// ID de l'entité concernée
  55.     #[ORM\Column(length100nullabletrue)]
  56.     private ?string $entityType null// Type d'entité (Conge, Produit, etc.)
  57.     #[ORM\ManyToOne(targetEntityBoutique::class)]
  58.     #[ORM\JoinColumn(nullablefalseonDelete'CASCADE')]
  59.     private ?Boutique $boutique null// Boutique concernée par l'alerte
  60.     #[ORM\Column(length100nullabletrue)]
  61.     private ?string $messageTemplate null// Template de message pour personnalisation
  62.     #[ORM\Column(typeTypes::JSONnullabletrue)]
  63.     private ?array $messageData null// Données pour remplacer les placeholders du template
  64.     #[ORM\ManyToOne(targetEntityUser::class)]
  65.     #[ORM\JoinColumn(nullabletrueonDelete'SET NULL')]
  66.     private ?User $concernedUser null// Utilisateur directement concerné par l'alerte
  67.     public function __construct()
  68.     {
  69.         $this->createdAt = new \DateTimeImmutable();
  70.         $this->statut 'nouvelle';
  71.         $this->priorite 'normale';
  72.     }
  73.     // Getters et Setters
  74.     public function getId(): ?int
  75.     {
  76.         return $this->id;
  77.     }
  78.     public function getType(): ?string
  79.     {
  80.         return $this->type;
  81.     }
  82.     public function setType(string $type): static
  83.     {
  84.         $this->type $type;
  85.         return $this;
  86.     }
  87.     public function getCategorie(): ?string
  88.     {
  89.         return $this->categorie;
  90.     }
  91.     public function setCategorie(string $categorie): static
  92.     {
  93.         $this->categorie $categorie;
  94.         return $this;
  95.     }
  96.     public function getPriorite(): ?string
  97.     {
  98.         return $this->priorite;
  99.     }
  100.     public function setPriorite(string $priorite): static
  101.     {
  102.         $this->priorite $priorite;
  103.         return $this;
  104.     }
  105.     public function getTitre(): ?string
  106.     {
  107.         return $this->titre;
  108.     }
  109.     public function setTitre(string $titre): static
  110.     {
  111.         $this->titre $titre;
  112.         return $this;
  113.     }
  114.     public function getMessage(): ?string
  115.     {
  116.         return $this->message;
  117.     }
  118.     public function setMessage(string $message): static
  119.     {
  120.         $this->message $message;
  121.         return $this;
  122.     }
  123.     public function getData(): ?array
  124.     {
  125.         return $this->data;
  126.     }
  127.     public function setData(?array $data): static
  128.     {
  129.         $this->data $data;
  130.         return $this;
  131.     }
  132.     public function getLien(): ?string
  133.     {
  134.         return $this->lien;
  135.     }
  136.     public function setLien(?string $lien): static
  137.     {
  138.         $this->lien $lien;
  139.         return $this;
  140.     }
  141.     public function getIcone(): ?string
  142.     {
  143.         return $this->icone;
  144.     }
  145.     public function setIcone(?string $icone): static
  146.     {
  147.         $this->icone $icone;
  148.         return $this;
  149.     }
  150.     public function getCouleur(): ?string
  151.     {
  152.         return $this->couleur;
  153.     }
  154.     public function setCouleur(?string $couleur): static
  155.     {
  156.         $this->couleur $couleur;
  157.         return $this;
  158.     }
  159.     public function getUser(): ?User
  160.     {
  161.         return $this->user;
  162.     }
  163.     public function setUser(?User $user): static
  164.     {
  165.         $this->user $user;
  166.         return $this;
  167.     }
  168.     public function getRoles(): ?string
  169.     {
  170.         return $this->roles;
  171.     }
  172.     public function setRoles(?string $roles): static
  173.     {
  174.         $this->roles $roles;
  175.         return $this;
  176.     }
  177.     public function getRolesArray(): array
  178.     {
  179.         return $this->roles json_decode($this->rolestrue) : [];
  180.     }
  181.     public function setRolesArray(array $roles): static
  182.     {
  183.         $this->roles json_encode($roles);
  184.         return $this;
  185.     }
  186.     public function getStatut(): ?string
  187.     {
  188.         return $this->statut;
  189.     }
  190.     public function setStatut(string $statut): static
  191.     {
  192.         $this->statut $statut;
  193.         return $this;
  194.     }
  195.     public function getCreatedAt(): ?\DateTimeImmutable
  196.     {
  197.         return $this->createdAt;
  198.     }
  199.     public function setCreatedAt(\DateTimeImmutable $createdAt): static
  200.     {
  201.         $this->createdAt $createdAt;
  202.         return $this;
  203.     }
  204.     public function getViewedAt(): ?\DateTimeImmutable
  205.     {
  206.         return $this->viewedAt;
  207.     }
  208.     public function setViewedAt(?\DateTimeImmutable $viewedAt): static
  209.     {
  210.         $this->viewedAt $viewedAt;
  211.         return $this;
  212.     }
  213.     public function getTreatedAt(): ?\DateTimeImmutable
  214.     {
  215.         return $this->treatedAt;
  216.     }
  217.     public function setTreatedAt(?\DateTimeImmutable $treatedAt): static
  218.     {
  219.         $this->treatedAt $treatedAt;
  220.         return $this;
  221.     }
  222.     public function getExpireAt(): ?\DateTimeInterface
  223.     {
  224.         return $this->expireAt;
  225.     }
  226.     public function setExpireAt(?\DateTimeInterface $expireAt): static
  227.     {
  228.         $this->expireAt $expireAt;
  229.         return $this;
  230.     }
  231.     public function isPersistent(): ?bool
  232.     {
  233.         return $this->persistent;
  234.     }
  235.     public function setPersistent(bool $persistent): static
  236.     {
  237.         $this->persistent $persistent;
  238.         return $this;
  239.     }
  240.     public function getEntityId(): ?int
  241.     {
  242.         return $this->entityId;
  243.     }
  244.     public function setEntityId(?int $entityId): static
  245.     {
  246.         $this->entityId $entityId;
  247.         return $this;
  248.     }
  249.     public function getEntityType(): ?string
  250.     {
  251.         return $this->entityType;
  252.     }
  253.     public function setEntityType(?string $entityType): static
  254.     {
  255.         $this->entityType $entityType;
  256.         return $this;
  257.     }
  258.     // Méthodes utilitaires
  259.     public function isExpired(): bool
  260.     {
  261.         if (!$this->expireAt) {
  262.             return false;
  263.         }
  264.         return $this->expireAt < new \DateTime();
  265.     }
  266.     public function isNouvelle(): bool
  267.     {
  268.         return $this->statut === 'nouvelle';
  269.     }
  270.     public function isVue(): bool
  271.     {
  272.         return in_array($this->statut, ['vue''traitee''archivee']);
  273.     }
  274.     public function isCritique(): bool
  275.     {
  276.         return $this->priorite === 'critique';
  277.     }
  278.     public function isUrgente(): bool
  279.     {
  280.         return $this->priorite === 'urgente';
  281.     }
  282.     public function isNormale(): bool
  283.     {
  284.         return $this->priorite === 'normale';
  285.     }
  286.     public function isInfo(): bool
  287.     {
  288.         return $this->priorite === 'info';
  289.     }
  290.     public function isTraitee(): bool
  291.     {
  292.         return $this->statut === 'traitee';
  293.     }
  294.     public function isArchivee(): bool
  295.     {
  296.         return $this->statut === 'archivee';
  297.     }
  298.     public function marquerCommeVue(): void
  299.     {
  300.         if ($this->statut === 'nouvelle') {
  301.             $this->statut 'vue';
  302.             $this->viewedAt = new \DateTimeImmutable();
  303.         }
  304.     }
  305.     public function marquerCommeTraitee(): void
  306.     {
  307.         $this->statut 'traitee';
  308.         $this->treatedAt = new \DateTimeImmutable();
  309.     }
  310.     public function archiver(): void
  311.     {
  312.         $this->statut 'archivee';
  313.     }
  314.     public function getMessageTemplate(): ?string
  315.     {
  316.         return $this->messageTemplate;
  317.     }
  318.     public function setMessageTemplate(?string $messageTemplate): static
  319.     {
  320.         $this->messageTemplate $messageTemplate;
  321.         return $this;
  322.     }
  323.     public function getMessageData(): ?array
  324.     {
  325.         return $this->messageData;
  326.     }
  327.     public function setMessageData(?array $messageData): static
  328.     {
  329.         $this->messageData $messageData;
  330.         return $this;
  331.     }
  332.     public function getConcernedUser(): ?User
  333.     {
  334.         return $this->concernedUser;
  335.     }
  336.     public function setConcernedUser(?User $concernedUser): static
  337.     {
  338.         $this->concernedUser $concernedUser;
  339.         return $this;
  340.     }
  341.     public function getBoutique(): ?Boutique
  342.     {
  343.         return $this->boutique;
  344.     }
  345.     public function setBoutique(?Boutique $boutique): static
  346.     {
  347.         $this->boutique $boutique;
  348.         return $this;
  349.     }
  350.     /**
  351.      * Vérifie si un utilisateur donné est directement concerné par cette alerte
  352.      * (soit il est le concernedUser, soit l'alerte lui est personnellement adressée)
  353.      */
  354.     public function isUserConcerned(User $user): bool
  355.     {
  356.         // Si l'utilisateur est le concerné direct
  357.         if ($this->concernedUser && $this->concernedUser->getId() === $user->getId()) {
  358.             return true;
  359.         }
  360.         
  361.         // Si l'alerte est personnelle pour cet utilisateur
  362.         if ($this->user && $this->user->getId() === $user->getId()) {
  363.             return true;
  364.         }
  365.         
  366.         return false;
  367.     }
  368.     public function __toString(): string
  369.     {
  370.         return $this->titre ?? 'Alerte #' $this->id;
  371.     }
  372. }