<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity("email",message="L'adresse mail existe déjà.")
* @Vich\Uploadable
*/
class User implements UserInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
* @Assert\NotBlank(message="Le mail est obligatoire")
* @Assert\Email(message="Email non valide")
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank(message="Le mot de passe ne doit pas être vide.")
* @Assert\Length(
* min = 8,
* minMessage = "Le mot de passe doit être minimun de {{ limit }} caractères",
* )
* @Assert\Regex(
* pattern="/((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/",
* message="Le mot de passe doit avoir au moins une lettre majuscule, une lettre miniscule, un caractère spécial et un chiffre"
* )
*/
private $password;
private $password_edit;
private $nomComplet;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\NotBlank(message="Le prénom est obligatoire")
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\NotBlank(message="Le nom est obligatoire")
*/
private $lastName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $tel;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\NotBlank(message="Le choix d'un rôle est obligatoire")
*/
private $role;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $avatar;
/**
* @var File
* @Vich\UploadableField(mapping="avatars", fileNameProperty="avatar")
*/
private $avatarFile;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $active;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $token;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @ORM\OneToMany(targetEntity=Client::class,orphanRemoval=true, mappedBy="user")
*/
private $clients;
/**
* @ORM\OneToMany(targetEntity=Piece::class,orphanRemoval=true, mappedBy="user")
*/
private $pieces;
/**
* @ORM\OneToMany(targetEntity=Payment::class, mappedBy="user")
*/
private $payments;
/**
* @ORM\OneToMany(targetEntity=Gift::class, mappedBy="user")
*/
private $gifts;
public function __construct()
{
$this->clients = new ArrayCollection();
$this->pieces = new ArrayCollection();
$this->payments = new ArrayCollection();
$this->gifts = new ArrayCollection();
}
/**
* @ORM\PrePersist
*/
public function createDate()
{
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
}
/**
* @ORM\PreUpdate
*/
public function updateDate()
{
$this->updatedAt = new \DateTime();
}
public function list_roles()
{
return [
'ROLE_SUPER_ADMIN' => 'Super Admin',
'ROLE_RESPONSABLE' => 'Responsable',
];
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->email;
}
/**
* @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;
}
/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(?string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(?string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getTel(): ?string
{
return $this->tel;
}
public function setTel(?string $tel): self
{
$this->tel = $tel;
return $this;
}
public function getRole(): ?string
{
return $this->role;
}
public function setRole(?string $role): self
{
$this->role = $role;
return $this;
}
public function getAvatar(): ?string
{
return $this->avatar;
}
public function setAvatar(?string $avatar): self
{
$this->avatar = $avatar;
return $this;
}
public function getActive(): ?bool
{
return $this->active;
}
public function setActive(?bool $active): self
{
$this->active = $active;
return $this;
}
public function getToken(): ?string
{
return $this->token;
}
public function setToken(?string $token): self
{
$this->token = $token;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Set avatarFile.
*
* @param string $avatarFile
*
* @return AvatarFile
*/
public function setAvatarFile(File $avatarFile = null)
{
$this->avatarFile = $avatarFile;
if ($avatarFile)
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
/**
* Get avatarFile.
*
* @return string
*/
public function getAvatarFile()
{
return $this->avatarFile;
}
public function getPasswordEdit(): ?string
{
return $this->password_edit;
}
public function setPasswordEdit(string $password_edit): void
{
$this->password_edit = $password_edit;
}
public function getNomcomplet(): ?string
{
return $this->firstName." ".$this->lastName;
}
public function generateRandom(){
$random = '';
$str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
for($i = 0; $i<50; $i++){
$random .= $str[rand(0,61)];
}
return $random;
}
/**
* @return Collection|Client[]
*/
public function getClients(): Collection
{
return $this->clients;
}
public function addClient(Client $client): self
{
if (!$this->clients->contains($client)) {
$this->clients[] = $client;
$client->setUser($this);
}
return $this;
}
public function removeClient(Client $client): self
{
if ($this->clients->contains($client)) {
$this->clients->removeElement($client);
// set the owning side to null (unless already changed)
if ($client->getUser() === $this) {
$client->setUser(null);
}
}
return $this;
}
/**
* @return Collection|Piece[]
*/
public function getPieces(): Collection
{
return $this->pieces;
}
public function addPiece(Piece $piece): self
{
if (!$this->pieces->contains($piece)) {
$this->pieces[] = $piece;
$piece->setUser($this);
}
return $this;
}
public function removePiece(Piece $piece): self
{
if ($this->pieces->contains($piece)) {
$this->pieces->removeElement($piece);
// set the owning side to null (unless already changed)
if ($piece->getUser() === $this) {
$piece->setUser(null);
}
}
return $this;
}
/**
* Check if user is Assistante
* @return BOOL
*/
public function isSuperAdmin(){
return in_array('ROLE_SUPER_ADMIN', $this->getRoles());
}
/**
* Check if user is Assistante
* @return BOOL
*/
public function isResponsable(){
return in_array('ROLE_RESPONSABLE', $this->getRoles());
}
public function isActive(): ?bool
{
return $this->active;
}
/**
* @return Collection<int, Payment>
*/
public function getPayments(): Collection
{
return $this->payments;
}
public function addPayment(Payment $payment): self
{
if (!$this->payments->contains($payment)) {
$this->payments->add($payment);
$payment->setUser($this);
}
return $this;
}
public function removePayment(Payment $payment): self
{
if ($this->payments->removeElement($payment)) {
// set the owning side to null (unless already changed)
if ($payment->getUser() === $this) {
$payment->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Gift>
*/
public function getGifts(): Collection
{
return $this->gifts;
}
public function addGift(Gift $gift): self
{
if (!$this->gifts->contains($gift)) {
$this->gifts->add($gift);
$gift->setUser($this);
}
return $this;
}
public function removeGift(Gift $gift): self
{
if ($this->gifts->removeElement($gift)) {
// set the owning side to null (unless already changed)
if ($gift->getUser() === $this) {
$gift->setUser(null);
}
}
return $this;
}
public function getGift(){
foreach($this->getGifts() as $gift){
return $gift;
}
}
public function getCollectedPoints(){
$gift = $this->getGift();
if(is_null($gift)){
return 0;
}
$points = 0;
foreach($this->getClients() as $client){
if(is_null($client->getDateRef()) or !$client->isEditInvoices()){
continue;
}
$points += $client->getCollectedPoints($client->getDateRef(),$gift->getPercent());
}
return $points;
}
public function canViewProgramm(Programm $programm){
if($this->isSuperAdmin()){
return true;
}
if(!$programm->getClient()->isEditFitness()){
return false;
}
return $programm->getClient()->getDateRef() <= $programm->getDateFin();
}
public function canViewProgramme(Programme $programme){
if($this->isSuperAdmin()){
return true;
}
if(!$programme->getClient()->isEditDiete()){
return false;
}
return $programme->getClient()->getDateRef() <= $programme->getDateFin();
}
public function canViewInvoice(Piece $piece){
if($this->isSuperAdmin()){
return true;
}
if(!$piece->getClient()->isEditInvoices()){
return false;
}
return $piece->getClient()->getDateRef() <= $piece->getDate();
}
public function canViewPayment(Payment $payment){
if($this->isSuperAdmin()){
return true;
}
if(!$payment->getClient()->isEditInvoices()){
return false;
}
return $payment->getClient()->getDateRef() <= $payment->getDate();
}
}