<?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\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ORM\Table(name="`user`")
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $mail;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @ORM\OneToOne(targetEntity=Carrito::class, mappedBy="usuario", cascade={"persist", "remove"})
*/
private $carrito;
/**
* @ORM\OneToMany(targetEntity=Venta::class, mappedBy="usuario")
*/
private $ventas;
/**
* @ORM\OneToOne(targetEntity=Direccion::class, cascade={"persist", "remove"})
*/
private $direccion;
/**
* @ORM\OneToMany(targetEntity=Publicacion::class, mappedBy="autor")
*/
private $publicacions;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $telefono;
/**
* @ORM\ManyToMany(targetEntity=Curso::class, inversedBy="users")
*/
private $cursos;
/**
* @ORM\Column(type="string", length=255)
*/
private $apellido;
/**
* @ORM\Column(type="string", length=255)
*/
private $nombre;
/**
* @ORM\OneToOne(targetEntity=Imagen::class, cascade={"persist", "remove"})
*/
private $avatar;
/**
* @ORM\OneToMany(targetEntity=Contacto::class, mappedBy="usuarioLogueado")
*/
private $contactos;
public function __construct()
{
$this->ventas = new ArrayCollection();
$this->publicacions = new ArrayCollection();
$this->cursos = new ArrayCollection();
$this->contactos = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function getMail(): string
{
return (string) $this->mail;
}
public function setMail(string $mail): self
{
$this->mail = $mail;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->username;
}
//Funcion que me dice si el usuario tiene rol admin
public function isAdmin()
{
//Obtengo los roles
$rolesArray = $this->getRoles();
//Defino el flag para el rol
$flagAdmin = false;
foreach ($rolesArray as $rol) {
if ($rol == "ROLE_ADMIN") {
$flagAdmin = true;
break;
}
}
return $flagAdmin;
}
/**
* @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 PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $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 __toString()
{
return strtoupper("$this->username");
}
public function getCarrito(): ?Carrito
{
return $this->carrito;
}
public function setCarrito(?Carrito $carrito): self
{
// unset the owning side of the relation if necessary
if ($carrito === null && $this->carrito !== null) {
$this->carrito->setUsuario(null);
}
// set the owning side of the relation if necessary
if ($carrito !== null && $carrito->getUsuario() !== $this) {
$carrito->setUsuario($this);
}
$this->carrito = $carrito;
return $this;
}
/**
* @return Collection|Venta[]
*/
public function getVentas(): Collection
{
return $this->ventas;
}
public function addVenta(Venta $venta): self
{
if (!$this->ventas->contains($venta)) {
$this->ventas[] = $venta;
$venta->setUsuario($this);
}
return $this;
}
public function removeVenta(Venta $venta): self
{
if ($this->ventas->removeElement($venta)) {
// set the owning side to null (unless already changed)
if ($venta->getUsuario() === $this) {
$venta->setUsuario(null);
}
}
return $this;
}
public function getDireccion(): ?Direccion
{
return $this->direccion;
}
public function setDireccion(?Direccion $direccion): self
{
$this->direccion = $direccion;
return $this;
}
/**
* @return Collection|Publicacion[]
*/
public function getPublicacions(): Collection
{
return $this->publicacions;
}
public function addPublicacion(Publicacion $publicacion): self
{
if (!$this->publicacions->contains($publicacion)) {
$this->publicacions[] = $publicacion;
$publicacion->setAutor($this);
}
return $this;
}
public function removePublicacion(Publicacion $publicacion): self
{
if ($this->publicacions->removeElement($publicacion)) {
// set the owning side to null (unless already changed)
if ($publicacion->getAutor() === $this) {
$publicacion->setAutor(null);
}
}
return $this;
}
public function getTelefono(): ?string
{
return $this->telefono;
}
public function setTelefono(?string $telefono): self
{
$this->telefono = $telefono;
return $this;
}
//Funcion interna para verificar si un usuario compro el curso
public function getImagenCurso($idCurso)
{
$cursoSeleccionado = null;
//Itero entre todos los cursos para saber si un usuario lo tiene o no
foreach ($this->getCursos() as $curso) {
if ($curso->getId() == $idCurso) {
$cursoSeleccionado = $curso;
}
}
if ($cursoSeleccionado != null) {
if ($cursoSeleccionado->getImagenes()) {
//Obtengo Portada
$imagen = $cursoSeleccionado->getImagenes()->first();
//Retorno
return $imagen->getUrl();
}
}
//Si no lo encontre entre todos los cursos retorno false
return null;
}
//Funcion que me dice si un usuario posee un curso o no
public function poseeUnCurso($curso)
{
//Obtengo los cursos actuales..
$cursos = $this->getCursos();
//Si el curso esta entre los cursos del usuario..
if ($cursos->contains($curso)) {
return true;
} else {
return false;
}
}
/**
* @return Collection<int, Curso>
*/
public function getCursos(): Collection
{
return $this->cursos;
}
public function addCurso(Curso $curso): self
{
if (!$this->cursos->contains($curso)) {
$this->cursos[] = $curso;
}
return $this;
}
public function removeCurso(Curso $curso): self
{
$this->cursos->removeElement($curso);
return $this;
}
public function getApellido(): ?string
{
return $this->apellido;
}
public function setApellido(string $apellido): self
{
$this->apellido = $apellido;
return $this;
}
public function getNombre(): ?string
{
return $this->nombre;
}
public function setNombre(string $nombre): self
{
$this->nombre = $nombre;
return $this;
}
public function getAvatar(): ?Imagen
{
return $this->avatar;
}
public function setAvatar(?Imagen $avatar): self
{
$this->avatar = $avatar;
return $this;
}
/**
* @return Collection<int, Contacto>
*/
public function getContactos(): Collection
{
return $this->contactos;
}
public function addContacto(Contacto $contacto): self
{
if (!$this->contactos->contains($contacto)) {
$this->contactos[] = $contacto;
$contacto->setUsuarioLogueado($this);
}
return $this;
}
public function removeContacto(Contacto $contacto): self
{
if ($this->contactos->removeElement($contacto)) {
// set the owning side to null (unless already changed)
if ($contacto->getUsuarioLogueado() === $this) {
$contacto->setUsuarioLogueado(null);
}
}
return $this;
}
public function getApellidoNombre(): string
{
return (string) $this->apellido . "," . $this->nombre;
}
public function getVentasEnProceso()
{
$ventasAux = new ArrayCollection();
foreach ($this->getVentas() as $venta) {
if ($venta->getEstado()->getId() == 2) {
$ventasAux->add($venta);
}
}
return $ventasAux;
}
public function getVentasFinalizadas()
{
$ventasAux = new ArrayCollection();
foreach ($this->getVentas() as $venta) {
if ($venta->getEstado()->getId() == 3) {
$ventasAux->add($venta);
}
}
return $ventasAux;
}
public function getVentasCanceladas()
{
$ventasAux = new ArrayCollection();
foreach ($this->getVentas() as $venta) {
if ($venta->getEstado()->getId() == 4) {
$ventasAux->add($venta);
}
}
return $ventasAux;
}
public function getVentasRechazadas()
{
$ventasAux = new ArrayCollection();
foreach ($this->getVentas() as $venta) {
if ($venta->getEstado() == 5) {
$ventasAux->add($venta);
}
}
return $ventasAux;
}
}