<?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\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Table(name="descubrir_destacado", indexes={@ORM\Index(name="IDX_TITULO", columns={"titulo"})})
* @ORM\Entity(repositoryClass="App\Repository\App\DescubrirDestacadoRepository")
* @ORM\HasLifecycleCallbacks()
*/
class DescubrirDestacado
{
/**
* @var integer|null
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
private $id;
/**
* @var string|null
*
* @ORM\Column(name="titulo", type="string", length=100)
* @Assert\NotBlank(message="El título es requerido")
* @Assert\Length(
* max = 100,
* maxMessage = "El título no puede tener más de {{ limit }} caracteres"
* )
*/
private $titulo;
/**
* @var string|null
*
* @ORM\Column(name="descripcion", type="text", nullable=true)
* @Assert\NotBlank(message="La descripción es requerida")
* @Assert\Length(
* max = 255,
* maxMessage = "La descripción no puede tener más de {{ limit }} caracteres"
* )
*/
private $descripcion;
/**
* @var string|null
*
* @ORM\Column(name="url", type="string", length=500, nullable=true)
* @Assert\NotBlank(message="La URL es requerida")
* @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 = "La URL no puede tener más de {{ limit }} caracteres"
* )
*/
private $url;
/**
* @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 maxima es {{ limit }} caracteres"
* )
*/
private $imagen;
use ActivoTrait;
use TimestampableEntity;
use PosicionTrait;
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return string|null
*/
public function getTitulo(): ?string
{
return $this->titulo;
}
/**
* @param string|null $titulo
*/
public function setTitulo(?string $titulo): void
{
$this->titulo = $titulo;
}
/**
* @return string|null
*/
public function getDescripcion(): ?string
{
return $this->descripcion;
}
/**
* @param string|null $descripcion
*/
public function setDescripcion(?string $descripcion): void
{
$this->descripcion = $descripcion;
}
/**
* @return string|null
*/
public function getUrl(): ?string
{
return $this->url;
}
/**
* @param string|null $url
*/
public function setUrl(?string $url): void
{
$this->url = $url;
}
/**
* @return string|null
*/
public function getColor(): ?string
{
return $this->color;
}
/**
* @param string|null $color
*/
public function setColor(?string $color): void
{
$this->color = $color;
}
/**
* @return File|null
*/
public function getFile(): ?File
{
return $this->file;
}
/**
* @param File|null $file
*/
public function setFile(?File $file): void
{
$this->file = $file;
}
/**
* @return string|null
*/
public function getImagen(): ?string
{
return $this->imagen;
}
/**
* @param string|null $imagen
*/
public function setImagen(?string $imagen): void
{
$this->imagen = $imagen;
}
private function getDirectoryFile()
{
$class = CommonFunctions::getClassMethod(__CLASS__);
$class = CommonFunctions::camelCaseToSnakeCase($class, '-');
$dir = CommonFunctions::getParameter('BASE_PATH_IMAGENES');
return CommonFunctions::getPathOrURLWithSlash($dir) . $class;
}
public function getAbsolutePath()
{
return CommonFunctions::getAbsolutePath($this->getDirectoryFile(), $this->getImagen());
}
public function guardarImagen()
{
$name = 'imagen_destacado_' . $this->getId();
$filename = CommonFunctions::saveUploadedFile($this->getFile(), $this->getDirectoryFile(), $name);
if ($filename && $filename != $this->getImagen()) {
$this->setImagen($filename);
}
}
/**
* @ORM\PrePersist
*/
public function prePersist()
{
$this->setImagen(Constants::NO_IMAGE_ICON);
}
/**
* @ORM\PreRemove
*/
public function preRemove()
{
CommonFunctions::removeFile($this->getAbsolutePath());
}
public function getImagenBase64()
{
$image = null;
if ($this->getId()) {
$image = CommonFunctions::getBase64OfImage($this->getAbsolutePath(), Constants::DEFAULT_IMAGE);
}
return $image;
}
public function __toString()
{
return $this->getTitulo() ?: 'Nuevo Destacado';
}
}