<?php
namespace App\Entity\App;
use App\Utils\CommonFunctions;
use App\Utils\Constants;
use App\Utils\Traits\ActivoTrait;
use App\Utils\Traits\PosicionTrait;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Table(name="linda", indexes={@ORM\Index(name="IDX_TITULO", columns={"titulo"})})
* @ORM\Entity(repositoryClass="App\Repository\App\LindaRepository")
* @ORM\HasLifecycleCallbacks()
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
*/
class Linda
{
/**
* @var int|null
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
private $id;
/**
* @var string|null
* @ORM\Column(name="titulo", type="string", length=255)
* @Assert\NotBlank(message="El título es requerido")
* @Assert\Length(
* max = 255,
* maxMessage = "El título no puede tener más de {{ limit }} caracteres"
* )
*/
private $titulo;
/**
* @var string|null
* @ORM\Column(name="titulo_boton", type="string", length=255)
* @Assert\NotBlank(message="El título del botón es requerido")
* @Assert\Length(
* max = 255,
* maxMessage = "El título del botón no puede tener más de {{ limit }} caracteres"
* )
*/
private $tituloBoton;
/**
* @var string|null
* @ORM\Column(name="link_redireccion", type="string", length=500)
* @Assert\NotBlank(message="El link de redirección es requerido")
* @Assert\Regex(
* pattern="/^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}[\/]?/",
* message="Ingrese un link correcto"
* )
* @Assert\Length(
* max = 500,
* maxMessage = "El link no puede tener más de {{ limit }} caracteres"
* )
*/
private $linkRedireccion;
/**
* @var string|null
* @ORM\Column(name="color", type="string", length=255, nullable=true)
* @Assert\NotBlank(message="El color es requerido")
* @Assert\Length(
* max = 255,
* maxMessage = "El color no puede tener más de {{ limit }} caracteres"
* )
*/
private $color;
/**
* @var File|null
* @Assert\Image(
* mimeTypes={"image/jpg", "image/jpeg", "image/png", "image/webp"},
* mimeTypesMessage="El tipo de imagen tiene que ser JPG, PNG, WEBP",
* maxSize="400K"
* )
*/
private $file;
/**
* @var string|null
* @ORM\Column(name="imagen", type="string", length=255)
* @Assert\Length(
* max = 255,
* maxMessage = "La cantidad máxima es {{ limit }} caracteres"
* )
*/
private $imagen;
/**
* @var string|null
* @ORM\Column(name="posicion_contenido", type="string", length=50, nullable=true)
* @Assert\NotBlank(message="La posición es requerida")
* @Assert\Length(
* max = 50,
* maxMessage = "La posición no puede tener más de {{ limit }} caracteres"
* )
*/
private $posicionContenido;
use ActivoTrait;
use PosicionTrait;
use TimestampableEntity;
use SoftDeleteableEntity;
public function getId(): ?int
{
return $this->id;
}
public function getTitulo(): ?string
{
return $this->titulo;
}
public function setTitulo(?string $titulo): void
{
$this->titulo = $titulo;
}
public function getTituloBoton(): ?string
{
return $this->tituloBoton;
}
public function setTituloBoton(?string $tituloBoton): void
{
$this->tituloBoton = $tituloBoton;
}
public function getLinkRedireccion(): ?string
{
return $this->linkRedireccion;
}
public function setLinkRedireccion(?string $linkRedireccion): void
{
$this->linkRedireccion = $linkRedireccion;
}
public function getColor(): ?string
{
return $this->color;
}
public function setColor(?string $color): void
{
$this->color = $color;
}
public function getFile(): ?File
{
return $this->file;
}
public function setFile(?File $file): void
{
$this->file = $file;
}
public function getImagen(): ?string
{
return $this->imagen;
}
public function setImagen(?string $imagen): void
{
$this->imagen = $imagen;
}
public function getPosicionContenido(): ?string
{
return $this->posicionContenido;
}
public function setPosicionContenido(?string $posicionContenido): void
{
$this->posicionContenido = $posicionContenido;
}
private function getDirectoryFile(): string
{
$class = CommonFunctions::getClassMethod(__CLASS__);
$class = CommonFunctions::camelCaseToSnakeCase($class, '-');
$dir = CommonFunctions::getParameter('BASE_PATH_IMAGENES');
return CommonFunctions::getPathOrURLWithSlash($dir) . $class;
}
public function getAbsolutePath(): ?string
{
return CommonFunctions::getAbsolutePath($this->getDirectoryFile(), $this->getImagen());
}
public function guardarImagen(): void
{
$name = 'imagen_linda_' . $this->getId();
$filename = CommonFunctions::saveUploadedFile($this->getFile(), $this->getDirectoryFile(), $name);
if ($filename && $filename !== $this->getImagen()) {
$this->setImagen($filename);
}
}
/**
* @ORM\PrePersist
*/
public function prePersist(): void
{
$this->setImagen(Constants::NO_IMAGE_ICON);
}
/**
* @ORM\PreRemove
*/
public function preRemove(): void
{
CommonFunctions::removeFile($this->getAbsolutePath());
}
public function getImagenBase64(): ?string
{
$image = null;
if ($this->getId()) {
$image = CommonFunctions::getBase64OfImage($this->getAbsolutePath(), Constants::DEFAULT_IMAGE);
}
return $image;
}
public function __toString()
{
return $this->getTitulo() ?: 'Nuevo Linda';
}
}