<?php
namespace App\Entity;
use App\Doctrine\Type\ClientType;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use JsonSerializable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ORM\Table(name="`users`")
* @UniqueEntity(fields={"username"}, message="There is already an account with this username")
* @method string getUserIdentifier()
*/
class User implements UserInterface, JsonSerializable
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180)
*/
private $username;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string", nullable=true)
*/
private $password;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $civility;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $email;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $phone;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $second_phone;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $adress;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $country;
/**
* @ORM\Column(type="string", length=100, nullable=true)
*/
private $region;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $city;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $zip;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $type;
/**
* @ORM\Column(type="boolean")
*/
private $isVerified = false;
/**
* @ORM\OneToMany(targetEntity=Link::class, mappedBy="user",cascade={"persist"})
*/
private $links;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="boolean", options={"default": false})
*/
private $isBlocked = false;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $blockedAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $unblockedAt;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $blockReason;
/**
* @ORM\Column(type="json", nullable=true)
*
* Exemple de structure :
* {
* "monday": [{"start": "09:00", "end": "12:00"}, {"start": "14:00", "end": "16:00"}],
* "tuesday": [],
* "wednesday": [{"start": "10:00", "end": "15:00"}],
* ...
* }
*/
private $blockedHours = [];
/**
* @ORM\OneToMany(targetEntity=Document::class, mappedBy="client")
*/
private $documents;
/**
* @ORM\OneToMany(targetEntity=Document::class, mappedBy="user")
*/
private $userDocuments;
/**
* @ORM\OneToMany(targetEntity=Address::class, mappedBy="user")
*/
private $multiAddress;
/**
* @ORM\OneToMany(targetEntity=Comment::class, mappedBy="user")
*/
private $comments;
/**
* @ORM\OneToMany(targetEntity=Comment::class, mappedBy="client")
*/
private $notes;
/**
* @ORM\ManyToOne(targetEntity=Supplier::class, inversedBy="contacts")
*/
private $supplier;
/**
* @ORM\ManyToOne(targetEntity=Promotion::class, inversedBy="clients")
*/
private $promotion;
/**
* @ORM\OneToMany(targetEntity=Activity::class, mappedBy="currentUser")
* @ORM\OrderBy({"createdAt" = "DESC"})
*/
private $activities;
/**
* @ORM\ManyToMany(targetEntity=Produit::class, mappedBy="users")
*/
private $produits;
/**
* @ORM\ManyToOne(targetEntity=GroupUser::class, inversedBy="users")
* @ORM\JoinColumn(nullable=true)
*/
private $groupUser;
/**
* @ORM\ManyToOne(targetEntity=UserPost::class)
* @ORM\JoinColumn(name="user_post_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
private $userPost;
/**
* @ORM\Column(name="client_type", type="string", length=255, options={"default":"Nouveau client"})
*/
private $clientType;
public function __construct()
{
$this->links = new ArrayCollection();
$this->documents = new ArrayCollection();
$this->userDocuments = new ArrayCollection();
$this->multiAddress = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->notes = new ArrayCollection();
$this->activities = new ArrayCollection();
$this->produits = new ArrayCollection();
$this->zip="";
}
public function getId(): ?int
{
return $this->id;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getCivility(): ?string
{
return $this->civility;
}
public function setCivility(string $civility): self
{
$this->civility = $civility;
return $this;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(?string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getFullName(): ?string
{
return $this->firstName;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getSecondPhone(): ?string
{
return $this->second_phone;
}
public function setSecondPhone(?string $second_phone): self
{
$this->second_phone = $second_phone;
return $this;
}
public function getAdress(): ?string
{
return $this->adress;
}
public function setAdress(?string $adress): self
{
$this->adress = $adress ?? '';
return $this;
}
public function getCountry(): ?string
{
return $this->country;
}
public function setCountry(string $country): self
{
$this->country = $country;
return $this;
}
public function getRegion(): ?string
{
return $this->region;
}
public function setRegion(?string $region): self
{
$this->region = $region ?? '';
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(string $city): self
{
$this->city = $city;
return $this;
}
public function getZip(): ?string
{
return $this->zip;
}
public function setZip(string $zip): self
{
$this->zip = $zip;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
/**
* @return Collection|Link[]
*/
public function getLinks(): Collection
{
return $this->links;
}
public function addLink(Link $link): self
{
if( !$this->links->contains($link)) {
$this->links[] = $link;
$link->setUser($this);
}
return $this;
}
public function removeLink(Link $link): self
{
if( $this->links->removeElement($link)) {
// set the owning side to null (unless already changed)
if( $link->getUser() === $this) {
$link->setUser(null);
}
}
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getBlockedHours(): ?array
{
return $this->blockedHours;
}
public function setBlockedHours(?array $blockedHours): self
{
$this->blockedHours = $blockedHours;
return $this;
}
public function isBlocked(): bool
{
return (bool) $this->isBlocked || $this->isBlockedAt();
}
// Nouvelle méthode avec tes paramètres (nom différent)
public function isBlockedAt( ?\DateTimeInterface $now = null,?\DateTimeZone $tz = null): bool {
$tz = $tz ?: new \DateTimeZone('Africa/Tunis');
$now = $now ? (new \DateTimeImmutable($now->format('c')))->setTimezone($tz)
: new \DateTimeImmutable('now', $tz);
// 1) Blocage permanent
if (!empty($this->isBlocked)) {
return true;
}
// 2) Blocage entre deux dates
$ba = $this->blockedAt instanceof \DateTimeInterface ? (new \DateTimeImmutable($this->blockedAt->format('c')))->setTimezone($tz) : null;
$ua = $this->unblockedAt instanceof \DateTimeInterface ? (new \DateTimeImmutable($this->unblockedAt->format('c')))->setTimezone($tz) : null;
if ($ba) {
if ($ua) {
if ($now >= $ba && $now < $ua) return true;
} else {
if ($now >= $ba) return true;
}
}
// 3) Plages horaires quotidiennes
if (empty($this->blockedHours) || !is_array($this->blockedHours)) {
return false;
}
$map = [
'monday'=>'mon','mon'=>'mon',
'tuesday'=>'tue','tue'=>'tue',
'wednesday'=>'wed','wed'=>'wed',
'thursday'=>'thu','thu'=>'thu',
'friday'=>'fri','fri'=>'fri',
'saturday'=>'sat','sat'=>'sat',
'sunday'=>'sun','sun'=>'sun',
'1'=>'mon','2'=>'tue','3'=>'wed','4'=>'thu','5'=>'fri','6'=>'sat','7'=>'sun','0'=>'sun',
];
$key = strtolower($now->format('D')); // mon..sun
$key = $map[$key] ?? $key;
$ranges = $this->blockedHours[$key] ?? [];
if (empty($ranges) || !is_array($ranges)) return false;
$toMin = static function (?string $hm): ?int {
if (!$hm || !preg_match('/^(\d{1,2}):(\d{2})$/', $hm, $m)) return null;
return ((int)$m[1]) * 60 + (int)$m[2];
};
$nowMin = (int)$now->format('H') * 60 + (int)$now->format('i');
foreach ($ranges as $r) {
$start = $toMin($r['start'] ?? null);
$end = $toMin($r['end'] ?? null);
if ($start === null && $end === null) continue;
if ($start !== null && $end !== null) {
if ($start < $end) {
if ($nowMin >= $start && $nowMin < $end) return true;
} elseif ($start > $end) {
if ($nowMin >= $start || $nowMin < $end) return true;
} else {
continue;
}
}
}
return false;
}
public function getIsBlocked(): bool
{
return $this->isBlocked;
}
public function setIsBlocked(bool $blocked): self
{
$this->isBlocked = $blocked;
return $this;
}
public function getBlockedAt(): ?\DateTimeInterface
{
return $this->blockedAt;
}
public function setBlockedAt(?\DateTimeInterface $date): self
{
$this->blockedAt = $date;
return $this;
}
public function getUnblockedAt(): ?\DateTimeInterface
{
return $this->unblockedAt;
}
public function setUnblockedAt(?\DateTimeInterface $date): self
{
$this->unblockedAt = $date;
return $this;
}
public function getBlockReason(): ?string
{
return $this->blockReason;
}
public function setBlockReason(?string $reason): self
{
$this->blockReason = $reason;
return $this;
}
/**
* @return Collection|Document[]
*/
public function getDocuments(): Collection
{
return $this->documents;
}
public function addDocument(Document $document): self
{
if( !$this->documents->contains($document)) {
$this->documents[] = $document;
$document->setClient($this);
}
return $this;
}
public function removeDocument(Document $document): self
{
if( $this->documents->removeElement($document)) {
// set the owning side to null (unless already changed)
if( $document->getClient() === $this) {
$document->setClient(null);
}
}
return $this;
}
/**
* @return Collection|Document[]
*/
public function getUserDocuments(): Collection
{
return $this->userDocuments;
}
public function addUserDocument(Document $userDocument): self
{
if( !$this->userDocuments->contains($userDocument)) {
$this->userDocuments[] = $userDocument;
$userDocument->setUser($this);
}
return $this;
}
public function removeUserDocument(Document $userDocument): self
{
if( $this->userDocuments->removeElement($userDocument)) {
// set the owning side to null (unless already changed)
if( $userDocument->getUser() === $this) {
$userDocument->setUser(null);
}
}
return $this;
}
/**
* @return Collection|Address[]
*/
public function getMultiAddress(): Collection
{
return $this->multiAddress;
}
public function addMultiAddress(Address $multiAddress): self
{
if( !$this->multiAddress->contains($multiAddress)) {
$this->multiAddress[] = $multiAddress;
$multiAddress->setUser($this);
}
return $this;
}
public function removeMultiAddress(Address $multiAddress): self
{
if( $this->multiAddress->removeElement($multiAddress)) {
// set the owning side to null (unless already changed)
if( $multiAddress->getUser() === $this) {
$multiAddress->setUser(null);
}
}
return $this;
}
/**
* @return Collection|Comment[]
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comment $comment): self
{
if( !$this->comments->contains($comment)) {
$this->comments[] = $comment;
$comment->setUser($this);
}
return $this;
}
public function removeComment(Comment $comment): self
{
if( $this->comments->removeElement($comment)) {
// set the owning side to null (unless already changed)
if( $comment->getUser() === $this) {
$comment->setUser(null);
}
}
return $this;
}
/**
* @return Collection|Comment[]
*/
public function getNotes(): Collection
{
return $this->notes;
}
public function addNote(Comment $note): self
{
if( !$this->notes->contains($note)) {
$this->notes[] = $note;
$note->setClient($this);
}
return $this;
}
public function removeNote(Comment $note): self
{
if( $this->notes->removeElement($note)) {
// set the owning side to null (unless already changed)
if( $note->getUser() === $this) {
$note->setUser(null);
}
}
return $this;
}
public function getSupplier(): ?Supplier
{
return $this->supplier;
}
public function setSupplier(?Supplier $supplier): self
{
$this->supplier = $supplier;
return $this;
}
public function getPromotion(): ?Promotion
{
return $this->promotion;
}
public function setPromotion(?Promotion $promotion): self
{
$this->promotion = $promotion;
return $this;
}
/**
* @return Collection|Activity[]
*/
public function getActivities(): Collection
{
return $this->activities;
}
public function addActivity(Activity $activity): self
{
if( !$this->activities->contains($activity)) {
$this->activities[] = $activity;
$activity->setCurrentUser($this);
}
return $this;
}
public function removeActivity(Activity $activity): self
{
if( $this->activities->removeElement($activity)) {
// set the owning side to null (unless already changed)
if( $activity->getCurrentUser() === $this) {
$activity->setCurrentUser(null);
}
}
return $this;
}
public function getGroupUser(): ?GroupUser
{
return $this->groupUser;
}
public function setGroupUser(?GroupUser $groupUser): self
{
$this->groupUser = $groupUser;
return $this;
}
public function getUserPost(): ?UserPost
{
return $this->userPost;
}
public function setUserPost(?UserPost $post): self
{
$this->userPost = $post;
return $this;
}
public function __call($name, $arguments)
{
// TODO: Implement @method string getUserIdentifier()
}
public function jsonSerialize()
{
return array(
'adress' => $this->getAdress(),
'firstName' => $this->getFirstName(),
'username' => $this->getUsername(),
'email' => $this->getEmail(),
'password' => '',
'newPassword' => '',
'confirmPassword' => ''
);
}
/**
* @return Collection|Produit[]
*/
public function getProduits(): Collection
{
return $this->produits;
}
public function addProduit(Produit $produit): self
{
if( !$this->produits->contains($produit)) {
$this->produits[] = $produit;
$produit->addUser($this);
}
return $this;
}
public function removeProduit(Produit $produit): self
{
if( $this->produits->contains($produit)) {
$this->produits->removeElement($produit);
$produit->removeUser($this);
}
return $this;
}
public function getClientType(): ?string
{
return $this->clientType;
}
public function setClientType(string $clientType): self
{
// Validate if the provided value is one of the valid ENUM values
if (!in_array($clientType, ClientType::getValidValues(), true)) {
throw new \InvalidArgumentException("Invalid client type: $clientType");
}
$this->clientType = $clientType;
return $this;
}
}