<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 180, unique: true)]
private ?string $email = null;
#[ORM\Column]
private array $roles = [];
/**
* @var string The hashed password
*/
#[ORM\Column]
private ?string $password = null;
#[ORM\Column(type: 'boolean')]
private $isVerified = false;
#[ORM\Column(length: 150, nullable: true)]
private ?string $firstname = null;
#[ORM\Column(length: 150, nullable: true)]
private ?string $lastname = null;
#[ORM\Column(length: 50, nullable: true)]
private ?string $phone_number = null;
#[ORM\ManyToMany(targetEntity: Conferences::class, mappedBy: 'presidents')]
private Collection $presidents;
#[ORM\ManyToMany(targetEntity: Conferences::class, mappedBy: 'conferenciers')]
private Collection $conferenciers;
#[ORM\ManyToMany(targetEntity: Seminaires::class, mappedBy: 'admins')]
private Collection $seminaires;
#[ORM\ManyToMany(targetEntity: Seminaires::class, mappedBy: 'communicants')]
private Collection $MySeminaires;
#[ORM\Column(length: 255, nullable: true)]
private ?string $plainpassword = null;
#[ORM\ManyToMany(targetEntity: Communications::class, mappedBy: 'conferenciers')]
private Collection $communications;
#[ORM\Column(length: 255, nullable: true)]
private ?string $avatar = null;
public function __construct()
{
$this->presidents = new ArrayCollection();
$this->conferenciers = new ArrayCollection();
$this->seminaires = new ArrayCollection();
$this->MySeminaires = new ArrayCollection();
$this->communications = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function setId($id): self
{
$this->id = $id;
return $this;
}
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 getUserIdentifier(): 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;
}
public function addRole($role): self {
if(!in_array($role,$this->getRoles())){
$roles = $this->getRoles();
array_push($roles,$role);
$this->setRoles($roles);
}
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
public function __toString(): string
{
return $this->email;
}
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 getIdentite(): ?string
{
return $this->firstname.' '.$this->lastname.' ('.$this->email.')';
}
public function getPhoneNumber(): ?string
{
return $this->phone_number;
}
public function setPhoneNumber(?string $phone_number): self
{
$this->phone_number = $phone_number;
return $this;
}
/**
* @return Collection<int, Conferences>
*/
public function getPresidents(): Collection
{
return $this->presidents;
}
public function addPresident(Conferences $president): self
{
if (!$this->presidents->contains($president)) {
$this->presidents->add($president);
$president->addPresident($this);
}
return $this;
}
public function removePresident(Conferences $president): self
{
if ($this->presidents->removeElement($president)) {
$president->removePresident($this);
}
return $this;
}
/**
* @return Collection<int, Conferences>
*/
public function getConferenciers(): Collection
{
return $this->conferenciers;
}
public function addConferencier(Conferences $conferencier): self
{
if (!$this->conferenciers->contains($conferencier)) {
$this->conferenciers->add($conferencier);
$conferencier->addConferencier($this);
}
return $this;
}
public function removeConferencier(Conferences $conferencier): self
{
if ($this->conferenciers->removeElement($conferencier)) {
$conferencier->removeConferencier($this);
}
return $this;
}
/**
* @return Collection<int, Seminaires>
*/
public function getSeminaires(): Collection
{
return $this->seminaires;
}
public function addSeminaire(Seminaires $seminaire): self
{
if (!$this->seminaires->contains($seminaire)) {
$this->seminaires->add($seminaire);
$seminaire->addAdmin($this);
}
return $this;
}
public function removeSeminaire(Seminaires $seminaire): self
{
if ($this->seminaires->removeElement($seminaire)) {
$seminaire->removeAdmin($this);
}
return $this;
}
/**
* @return Collection<int, Seminaires>
*/
public function getMySeminaires(): Collection
{
return $this->MySeminaires;
}
public function addMySeminaire(Seminaires $mySeminaire): self
{
if (!$this->MySeminaires->contains($mySeminaire)) {
$this->MySeminaires->add($mySeminaire);
$mySeminaire->addCommunicant($this);
}
return $this;
}
public function removeMySeminaire(Seminaires $mySeminaire): self
{
if ($this->MySeminaires->removeElement($mySeminaire)) {
$mySeminaire->removeCommunicant($this);
}
return $this;
}
public function getPlainpassword(): ?string
{
return $this->plainpassword;
}
public function setPlainpassword(?string $plainpassword): self
{
$this->plainpassword = $plainpassword;
return $this;
}
/**
* @return Collection<int, Communications>
*/
public function getCommunications(): Collection
{
return $this->communications;
}
public function addCommunication(Communications $communication): self
{
if (!$this->communications->contains($communication)) {
$this->communications->add($communication);
$communication->addConferencier($this);
}
return $this;
}
public function removeCommunication(Communications $communication): self
{
if ($this->communications->removeElement($communication)) {
$communication->removeConferencier($this);
}
return $this;
}
public function getPP(){
//on check si l'id user à une pp
$filename = dirname(__FILE__).'/../../public/uploads/avatar/av-'.$this->id.'.png';
if(file_exists($filename)) {
//si oui on envoie le link
$link = '/uploads/avatar/av-'.$this->id.'.png';
return $link;
} else {
//si non on genere une image rond gris initiales
/*$avatar = new \LasseRafn\InitialAvatarGenerator\InitialAvatar();
//$image = $avatar->name($this->fistname.' '.$this->lastname)->generate();
$image = $avatar->name($this->firstname.' '.$this->lastname)
->length(2)
->fontSize(0.5)
->size(96) // 48 * 2
->background('#8BC34A')
->color('#fff')
->fontName('Arial, Helvetica, sans-serif')
->generate()
->stream('png', 100);
return $image;*/
$link = 'https://ui-avatars.com/api/?background=d3d3d3&color=fff&name='.$this->firstname.'+'.$this->lastname;
return $link;
}
}
public function getAvatar(): ?string
{
return $this->avatar;
}
public function setAvatar(?string $avatar): self
{
$this->avatar = $avatar;
return $this;
}
}