src/Entity/User.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Doctrine\Type\ClientType;
  4. use App\Repository\UserRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use JsonSerializable;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. /**
  12.  * @ORM\Entity(repositoryClass=UserRepository::class)
  13.  * @ORM\Table(name="`users`")
  14.  * @UniqueEntity(fields={"username"}, message="There is already an account with this username")
  15.  * @method string getUserIdentifier()
  16.  */
  17. class User implements UserInterfaceJsonSerializable
  18. {
  19.     /**
  20.      * @ORM\Id
  21.      * @ORM\GeneratedValue
  22.      * @ORM\Column(type="integer")
  23.      */
  24.     private $id;
  25.     /**
  26.      * @ORM\Column(type="string", length=180)
  27.      */
  28.     private $username;
  29.     /**
  30.      * @ORM\Column(type="json")
  31.      */
  32.     private $roles = [];
  33.     /**
  34.      * @var string The hashed password
  35.      * @ORM\Column(type="string", nullable=true)
  36.      */
  37.     private $password;
  38.     /**
  39.      * @ORM\Column(type="string", length=255, nullable=true)
  40.      */
  41.     private $civility;
  42.     /**
  43.      * @ORM\Column(type="string", length=255, nullable=true)
  44.      */
  45.     private $firstName;
  46.     /**
  47.      * @ORM\Column(type="string", length=255, nullable=true)
  48.      */
  49.     private $email;
  50.     /**
  51.      * @ORM\Column(type="text", nullable=true)
  52.      */
  53.     private $description;
  54.     /**
  55.      * @ORM\Column(type="string", length=255, nullable=true)
  56.      */
  57.     private $phone;
  58.     /**
  59.      * @ORM\Column(type="string", length=255, nullable=true)
  60.      */
  61.     private $second_phone;
  62.     /**
  63.      * @ORM\Column(type="text", nullable=true)
  64.      */
  65.     private $adress;
  66.     /**
  67.      * @ORM\Column(type="string", length=255, nullable=true)
  68.      */
  69.     private $country;
  70.     /**
  71.      * @ORM\Column(type="string", length=100, nullable=true)
  72.      */
  73.     private $region;
  74.     /**
  75.      * @ORM\Column(type="string", length=255, nullable=true)
  76.      */
  77.     private $city;
  78.     /**
  79.      * @ORM\Column(type="string", length=255, nullable=true)
  80.      */
  81.     private $zip;
  82.     /**
  83.      * @ORM\Column(type="string", length=255, nullable=true)
  84.      */
  85.     private $type;
  86.     /**
  87.      * @ORM\Column(type="boolean")
  88.      */
  89.     private $isVerified false;
  90.     /**
  91.      * @ORM\OneToMany(targetEntity=Link::class, mappedBy="user",cascade={"persist"})
  92.      */
  93.     private $links;
  94.     /**
  95.      * @ORM\Column(type="datetime")
  96.      */
  97.     private $createdAt;
  98.     /**
  99.      * @ORM\Column(type="boolean", options={"default": false})
  100.      */
  101.     private $isBlocked false;
  102.     
  103.     /**
  104.      * @ORM\Column(type="datetime", nullable=true)
  105.      */
  106.     private $blockedAt;
  107.     /**
  108.      * @ORM\Column(type="datetime", nullable=true)
  109.      */
  110.     private $unblockedAt;
  111.     /**
  112.      * @ORM\Column(type="string", length=255, nullable=true)
  113.      */
  114.     private $blockReason;
  115.     /**
  116.      * @ORM\Column(type="json", nullable=true)
  117.      *
  118.      * Exemple de structure :
  119.      * {
  120.      *   "monday": [{"start": "09:00", "end": "12:00"}, {"start": "14:00", "end": "16:00"}],
  121.      *   "tuesday": [],
  122.      *   "wednesday": [{"start": "10:00", "end": "15:00"}],
  123.      *   ...
  124.      * }
  125.      */
  126.     private $blockedHours = [];
  127.    
  128.     /**
  129.      * @ORM\OneToMany(targetEntity=Document::class, mappedBy="client")
  130.      */
  131.     private $documents;
  132.     /**
  133.      * @ORM\OneToMany(targetEntity=Document::class, mappedBy="user")
  134.      */
  135.     private $userDocuments;
  136.     /**
  137.      * @ORM\OneToMany(targetEntity=Address::class, mappedBy="user")
  138.      */
  139.     private $multiAddress;
  140.     /**
  141.      * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="user")
  142.      */
  143.     private $comments;
  144.     /**
  145.      * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="client")
  146.      */
  147.     private $notes;
  148.     /**
  149.      * @ORM\ManyToOne(targetEntity=Supplier::class, inversedBy="contacts")
  150.      */
  151.     private $supplier;
  152.     /**
  153.      * @ORM\ManyToOne(targetEntity=Promotion::class, inversedBy="clients")
  154.      */
  155.     private $promotion;
  156.     /**
  157.      * @ORM\OneToMany(targetEntity=Activity::class, mappedBy="currentUser")
  158.      * @ORM\OrderBy({"createdAt" = "DESC"})
  159.      */
  160.     private $activities;
  161.     /**
  162.      * @ORM\ManyToMany(targetEntity=Produit::class, mappedBy="users")
  163.      */
  164.     private $produits;
  165.     /**
  166.      * @ORM\ManyToOne(targetEntity=GroupUser::class, inversedBy="users")
  167.      * @ORM\JoinColumn(nullable=true)
  168.      */
  169.     private $groupUser;
  170.     /**
  171.      * @ORM\ManyToOne(targetEntity=UserPost::class)
  172.      * @ORM\JoinColumn(name="user_post_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
  173.      */
  174.     private $userPost;
  175.     /**
  176.     * @ORM\Column(name="client_type", type="string", length=255, options={"default":"Nouveau client"})
  177.     */
  178.     private $clientType;
  179.     public function __construct()
  180.     {
  181.         $this->links = new ArrayCollection();
  182.         $this->documents = new ArrayCollection();
  183.         $this->userDocuments = new ArrayCollection();
  184.         $this->multiAddress = new ArrayCollection();
  185.         $this->comments = new ArrayCollection();
  186.         $this->notes = new ArrayCollection();
  187.         $this->activities = new ArrayCollection();
  188.         $this->produits = new ArrayCollection();
  189.         $this->zip="";
  190.     }
  191.     public function getId(): ?int
  192.     {
  193.         return $this->id;
  194.     }
  195.     public function getUsername(): ?string
  196.     {
  197.         return $this->username;
  198.     }
  199.     public function setUsername(string $username): self
  200.     {
  201.         $this->username $username;
  202.         return $this;
  203.     }
  204.     /**
  205.      * @see UserInterface
  206.      */
  207.     public function getRoles(): array
  208.     {
  209.         $roles $this->roles;
  210.         // guarantee every user at least has ROLE_USER
  211.         $roles[] = 'ROLE_USER';
  212.         return array_unique($roles);
  213.     }
  214.     public function setRoles(array $roles): self
  215.     {
  216.         $this->roles $roles;
  217.         return $this;
  218.     }
  219.     /**
  220.      * @see UserInterface
  221.      */
  222.     public function getPassword(): string
  223.     {
  224.         return (string) $this->password;
  225.     }
  226.     public function setPassword(string $password): self
  227.     {
  228.         $this->password $password;
  229.         return $this;
  230.     }
  231.     /**
  232.      * Returning a salt is only needed, if you are not using a modern
  233.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  234.      *
  235.      * @see UserInterface
  236.      */
  237.     public function getSalt(): ?string
  238.     {
  239.         return null;
  240.     }
  241.     /**
  242.      * @see UserInterface
  243.      */
  244.     public function eraseCredentials()
  245.     {
  246.         // If you store any temporary, sensitive data on the user, clear it here
  247.         // $this->plainPassword = null;
  248.     }
  249.     public function getCivility(): ?string
  250.     {
  251.         return $this->civility;
  252.     }
  253.     public function setCivility(string $civility): self
  254.     {
  255.         $this->civility $civility;
  256.         return $this;
  257.     }
  258.     public function getFirstName(): ?string
  259.     {
  260.         return $this->firstName;
  261.     }
  262.    public function setFirstName(?string $firstName): self
  263.     {
  264.         $this->firstName $firstName;
  265.         return $this;
  266.     }
  267.     public function getFullName(): ?string
  268.     {
  269.         return $this->firstName;
  270.     }
  271.     public function getEmail(): ?string
  272.     {
  273.         return $this->email;
  274.     }
  275.     public function setEmail(string $email): self
  276.     {
  277.         $this->email $email;
  278.         return $this;
  279.     }
  280.     public function getDescription(): ?string
  281.     {
  282.         return $this->description;
  283.     }
  284.     public function setDescription(string $description): self
  285.     {
  286.         $this->description $description;
  287.         return $this;
  288.     }
  289.     public function getPhone(): ?string
  290.     {
  291.         return $this->phone;
  292.     }
  293.     public function setPhone(string $phone): self
  294.     {
  295.         $this->phone $phone;
  296.         return $this;
  297.     }
  298.     public function getSecondPhone(): ?string
  299.     {
  300.         return $this->second_phone;
  301.     }
  302.     public function setSecondPhone(?string $second_phone): self
  303.     {
  304.         $this->second_phone $second_phone;
  305.         return $this;
  306.     }
  307.     public function getAdress(): ?string
  308.     {
  309.         return $this->adress;
  310.     }
  311.      public function setAdress(?string $adress): self
  312.     {
  313.         $this->adress $adress ?? '';
  314.         return $this;
  315.     }
  316.     
  317.     public function getCountry(): ?string
  318.     {
  319.         return $this->country;
  320.     }
  321.     public function setCountry(string $country): self
  322.     {
  323.         $this->country $country;
  324.         return $this;
  325.     }
  326.     public function getRegion(): ?string
  327.     {
  328.         return $this->region;
  329.     }
  330.     public function setRegion(?string $region): self
  331.     {
  332.         $this->region $region ?? '';
  333.         return $this;
  334.     }
  335.     public function getCity(): ?string
  336.     {
  337.         return $this->city;
  338.     }
  339.     public function setCity(string $city): self
  340.     {
  341.         $this->city $city;
  342.         return $this;
  343.     }
  344.     public function getZip(): ?string
  345.     {
  346.         return $this->zip;
  347.     }
  348.     public function setZip(string $zip): self
  349.     {
  350.         $this->zip $zip;
  351.         return $this;
  352.     }
  353.     public function getType(): ?string
  354.     {
  355.         return $this->type;
  356.     }
  357.     public function setType(string $type): self
  358.     {
  359.         $this->type $type;
  360.         return $this;
  361.     }
  362.     public function isVerified(): bool
  363.     {
  364.         return $this->isVerified;
  365.     }
  366.     public function setIsVerified(bool $isVerified): self
  367.     {
  368.         $this->isVerified $isVerified;
  369.         return $this;
  370.     }
  371.     /**
  372.      * @return Collection|Link[]
  373.      */
  374.     public function getLinks(): Collection
  375.     {
  376.         return $this->links;
  377.     }
  378.     public function addLink(Link $link): self
  379.     {
  380.         if( !$this->links->contains($link)) {
  381.             $this->links[] = $link;
  382.             $link->setUser($this);
  383.         }
  384.         return $this;
  385.     }
  386.     public function removeLink(Link $link): self
  387.     {
  388.         if( $this->links->removeElement($link)) {
  389.             // set the owning side to null (unless already changed)
  390.             if( $link->getUser() === $this) {
  391.                 $link->setUser(null);
  392.             }
  393.         }
  394.         return $this;
  395.     }
  396.     public function getCreatedAt(): ?\DateTimeInterface
  397.     {
  398.         return $this->createdAt;
  399.     }
  400.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  401.     {
  402.         $this->createdAt $createdAt;
  403.         return $this;
  404.     }
  405.     public function getBlockedHours(): ?array
  406.     {
  407.         return $this->blockedHours;
  408.     }
  409.     public function setBlockedHours(?array $blockedHours): self
  410.     {
  411.         $this->blockedHours $blockedHours;
  412.         return $this;
  413.     }
  414.     public function isBlocked(): bool
  415.     {
  416.        
  417.         return (bool) $this->isBlocked || $this->isBlockedAt(); 
  418.     }
  419.     // Nouvelle méthode avec tes paramètres (nom différent)
  420.     public function isBlockedAt( ?\DateTimeInterface $now null,?\DateTimeZone $tz null): bool {
  421.         $tz  $tz ?: new \DateTimeZone('Africa/Tunis');
  422.         $now $now ? (new \DateTimeImmutable($now->format('c')))->setTimezone($tz)
  423.                     : new \DateTimeImmutable('now'$tz);
  424.         // 1) Blocage permanent
  425.         if (!empty($this->isBlocked)) {
  426.             return true;
  427.         }
  428.         // 2) Blocage entre deux dates
  429.         $ba $this->blockedAt  instanceof \DateTimeInterface ? (new \DateTimeImmutable($this->blockedAt->format('c')))->setTimezone($tz) : null;
  430.         $ua $this->unblockedAt instanceof \DateTimeInterface ? (new \DateTimeImmutable($this->unblockedAt->format('c')))->setTimezone($tz) : null;
  431.         if ($ba) {
  432.             if ($ua) {
  433.                 if ($now >= $ba && $now $ua) return true;
  434.             } else {
  435.                 if ($now >= $ba) return true;
  436.             }
  437.         }
  438.         // 3) Plages horaires quotidiennes
  439.         if (empty($this->blockedHours) || !is_array($this->blockedHours)) {
  440.             return false;
  441.         }
  442.         $map = [
  443.             'monday'=>'mon','mon'=>'mon',
  444.             'tuesday'=>'tue','tue'=>'tue',
  445.             'wednesday'=>'wed','wed'=>'wed',
  446.             'thursday'=>'thu','thu'=>'thu',
  447.             'friday'=>'fri','fri'=>'fri',
  448.             'saturday'=>'sat','sat'=>'sat',
  449.             'sunday'=>'sun','sun'=>'sun',
  450.             '1'=>'mon','2'=>'tue','3'=>'wed','4'=>'thu','5'=>'fri','6'=>'sat','7'=>'sun','0'=>'sun',
  451.         ];
  452.         $key strtolower($now->format('D')); // mon..sun
  453.         $key $map[$key] ?? $key;
  454.         $ranges $this->blockedHours[$key] ?? [];
  455.         if (empty($ranges) || !is_array($ranges)) return false;
  456.         $toMin = static function (?string $hm): ?int {
  457.             if (!$hm || !preg_match('/^(\d{1,2}):(\d{2})$/'$hm$m)) return null;
  458.             return ((int)$m[1]) * 60 + (int)$m[2];
  459.         };
  460.         $nowMin = (int)$now->format('H') * 60 + (int)$now->format('i');
  461.         foreach ($ranges as $r) {
  462.             $start $toMin($r['start'] ?? null);
  463.             $end   $toMin($r['end']   ?? null);
  464.             if ($start === null && $end === null) continue;
  465.             if ($start !== null && $end !== null) {
  466.                 if ($start $end) {
  467.                     if ($nowMin >= $start && $nowMin $end) return true;
  468.                 } elseif ($start $end) {
  469.                     if ($nowMin >= $start || $nowMin $end) return true;
  470.                 } else {
  471.                     continue;
  472.                 }
  473.             }
  474.         }
  475.         return false;
  476.     }
  477.     public function getIsBlocked(): bool
  478.     {
  479.         return $this->isBlocked;
  480.     }
  481.     public function setIsBlocked(bool $blocked): self
  482.     {
  483.         $this->isBlocked $blocked;
  484.         return $this;
  485.     }
  486.     public function getBlockedAt(): ?\DateTimeInterface
  487.     {
  488.         return $this->blockedAt;
  489.     }
  490.     public function setBlockedAt(?\DateTimeInterface $date): self
  491.     {
  492.         $this->blockedAt $date;
  493.         return $this;
  494.     }
  495.     public function getUnblockedAt(): ?\DateTimeInterface
  496.     {
  497.         return $this->unblockedAt;
  498.     }
  499.     public function setUnblockedAt(?\DateTimeInterface $date): self
  500.     {
  501.         $this->unblockedAt $date;
  502.         return $this;
  503.     }
  504.     public function getBlockReason(): ?string
  505.     {
  506.         return $this->blockReason;
  507.     }
  508.     public function setBlockReason(?string $reason): self
  509.     {
  510.         $this->blockReason $reason;
  511.         return $this;
  512.     }
  513.    
  514.     /**
  515.      * @return Collection|Document[]
  516.      */
  517.     public function getDocuments(): Collection
  518.     {
  519.         return $this->documents;
  520.     }
  521.     public function addDocument(Document $document): self
  522.     {
  523.         if( !$this->documents->contains($document)) {
  524.             $this->documents[] = $document;
  525.             $document->setClient($this);
  526.         }
  527.         return $this;
  528.     }
  529.     public function removeDocument(Document $document): self
  530.     {
  531.         if( $this->documents->removeElement($document)) {
  532.             // set the owning side to null (unless already changed)
  533.             if( $document->getClient() === $this) {
  534.                 $document->setClient(null);
  535.             }
  536.         }
  537.         return $this;
  538.     }
  539.     /**
  540.      * @return Collection|Document[]
  541.      */
  542.     public function getUserDocuments(): Collection
  543.     {
  544.         return $this->userDocuments;
  545.     }
  546.     public function addUserDocument(Document $userDocument): self
  547.     {
  548.         if( !$this->userDocuments->contains($userDocument)) {
  549.             $this->userDocuments[] = $userDocument;
  550.             $userDocument->setUser($this);
  551.         }
  552.         return $this;
  553.     }
  554.     public function removeUserDocument(Document $userDocument): self
  555.     {
  556.         if( $this->userDocuments->removeElement($userDocument)) {
  557.             // set the owning side to null (unless already changed)
  558.             if( $userDocument->getUser() === $this) {
  559.                 $userDocument->setUser(null);
  560.             }
  561.         }
  562.         return $this;
  563.     }
  564.     /**
  565.      * @return Collection|Address[]
  566.      */
  567.     public function getMultiAddress(): Collection
  568.     {
  569.         return $this->multiAddress;
  570.     }
  571.     public function addMultiAddress(Address $multiAddress): self
  572.     {
  573.         if( !$this->multiAddress->contains($multiAddress)) {
  574.             $this->multiAddress[] = $multiAddress;
  575.             $multiAddress->setUser($this);
  576.         }
  577.         return $this;
  578.     }
  579.     public function removeMultiAddress(Address $multiAddress): self
  580.     {
  581.         if( $this->multiAddress->removeElement($multiAddress)) {
  582.             // set the owning side to null (unless already changed)
  583.             if( $multiAddress->getUser() === $this) {
  584.                 $multiAddress->setUser(null);
  585.             }
  586.         }
  587.         return $this;
  588.     }
  589.     /**
  590.      * @return Collection|Comment[]
  591.      */
  592.     public function getComments(): Collection
  593.     {
  594.         return $this->comments;
  595.     }
  596.     public function addComment(Comment $comment): self
  597.     {
  598.         if( !$this->comments->contains($comment)) {
  599.             $this->comments[] = $comment;
  600.             $comment->setUser($this);
  601.         }
  602.         return $this;
  603.     }
  604.     public function removeComment(Comment $comment): self
  605.     {
  606.         if( $this->comments->removeElement($comment)) {
  607.             // set the owning side to null (unless already changed)
  608.             if( $comment->getUser() === $this) {
  609.                 $comment->setUser(null);
  610.             }
  611.         }
  612.         return $this;
  613.     }
  614.     /**
  615.      * @return Collection|Comment[]
  616.      */
  617.     public function getNotes(): Collection
  618.     {
  619.         return $this->notes;
  620.     }
  621.     public function addNote(Comment $note): self
  622.     {
  623.         if( !$this->notes->contains($note)) {
  624.             $this->notes[] = $note;
  625.             $note->setClient($this);
  626.         }
  627.         return $this;
  628.     }
  629.     public function removeNote(Comment $note): self
  630.     {
  631.         if( $this->notes->removeElement($note)) {
  632.             // set the owning side to null (unless already changed)
  633.             if( $note->getUser() === $this) {
  634.                 $note->setUser(null);
  635.             }
  636.         }
  637.         return $this;
  638.     }
  639.     public function getSupplier(): ?Supplier
  640.     {
  641.         return $this->supplier;
  642.     }
  643.     public function setSupplier(?Supplier $supplier): self
  644.     {
  645.         $this->supplier $supplier;
  646.         return $this;
  647.     }
  648.     public function getPromotion(): ?Promotion
  649.     {
  650.         return $this->promotion;
  651.     }
  652.     public function setPromotion(?Promotion $promotion): self
  653.     {
  654.         $this->promotion $promotion;
  655.         return $this;
  656.     }
  657.     /**
  658.      * @return Collection|Activity[]
  659.      */
  660.     public function getActivities(): Collection
  661.     {
  662.         return $this->activities;
  663.     }
  664.     public function addActivity(Activity $activity): self
  665.     {
  666.         if( !$this->activities->contains($activity)) {
  667.             $this->activities[] = $activity;
  668.             $activity->setCurrentUser($this);
  669.         }
  670.         return $this;
  671.     }
  672.     public function removeActivity(Activity $activity): self
  673.     {
  674.         if( $this->activities->removeElement($activity)) {
  675.             // set the owning side to null (unless already changed)
  676.             if( $activity->getCurrentUser() === $this) {
  677.                 $activity->setCurrentUser(null);
  678.             }
  679.         }
  680.         return $this;
  681.     }
  682.     public function getGroupUser(): ?GroupUser
  683.     {
  684.         return $this->groupUser;
  685.     }
  686.     public function setGroupUser(?GroupUser $groupUser): self
  687.     {
  688.         $this->groupUser $groupUser;
  689.         return $this;
  690.     }
  691.     public function getUserPost(): ?UserPost
  692.     {
  693.         return $this->userPost;
  694.     }
  695.     public function setUserPost(?UserPost $post): self
  696.     {
  697.         $this->userPost $post;
  698.         return $this;
  699.     }
  700.     public function __call($name$arguments)
  701.     {
  702.         // TODO: Implement @method string getUserIdentifier()
  703.     }
  704.     public function jsonSerialize()
  705.     {
  706.         return array(
  707.             'adress' => $this->getAdress(),
  708.             'firstName' => $this->getFirstName(),
  709.             'username' => $this->getUsername(),
  710.             'email' => $this->getEmail(),
  711.             'password' => '',
  712.             'newPassword' => '',
  713.             'confirmPassword' => ''
  714.         );
  715.     }
  716.     /**
  717.      * @return Collection|Produit[]
  718.      */
  719.     public function getProduits(): Collection
  720.     {
  721.         return $this->produits;
  722.     }
  723.     public function addProduit(Produit $produit): self
  724.     {
  725.         if( !$this->produits->contains($produit)) {
  726.             $this->produits[] = $produit;
  727.             $produit->addUser($this);
  728.         }
  729.         return $this;
  730.     }
  731.     public function removeProduit(Produit $produit): self
  732.     {
  733.         if( $this->produits->contains($produit)) {
  734.             $this->produits->removeElement($produit);
  735.             $produit->removeUser($this);
  736.         }
  737.         return $this;
  738.     }
  739.     public function getClientType(): ?string
  740.     {
  741.         return $this->clientType;
  742.     }
  743.     public function setClientType(string $clientType): self
  744.     {
  745.         // Validate if the provided value is one of the valid ENUM values
  746.         if (!in_array($clientTypeClientType::getValidValues(), true)) {
  747.             throw new \InvalidArgumentException("Invalid client type: $clientType");
  748.         }
  749.         $this->clientType $clientType;
  750.         return $this;
  751.     }
  752. }