src/Entity/App/Sponsor.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Entity\App;
  3. use App\Utils\CommonFunctions;
  4. use App\Utils\Constants;
  5. use App\Utils\Traits\ActivoTrait;
  6. use App\Utils\Traits\PosicionTrait;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Gedmo\Mapping\Annotation as Gedmo;
  9. use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
  10. use Gedmo\Timestampable\Traits\TimestampableEntity;
  11. use Symfony\Component\HttpFoundation\File\File;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. /**
  14.  * Sponsor
  15.  *
  16.  * @ORM\Table(name="sponsor", indexes={@ORM\Index(name="IDX_TITULO", columns={"titulo"})})
  17.  * @ORM\Entity(repositoryClass="App\Repository\App\SponsorRepository")
  18.  * @ORM\HasLifecycleCallbacks()
  19.  * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
  20.  */
  21. class Sponsor
  22. {
  23.     /**
  24.      * @var integer|null
  25.      *
  26.      * @ORM\Column(name="id", type="integer")
  27.      * @ORM\Id
  28.      * @ORM\GeneratedValue
  29.      */
  30.     private $id;
  31.     /**
  32.      * @var File|null
  33.      *
  34.      * @Assert\Image(
  35.      *     mimeTypes={"image/svg+xml", "image/jpg", "image/jpeg", "image/png"},
  36.      *     mimeTypesMessage="El tipo de imagen tiene que ser SVG, JPG o PNG",
  37.      *     maxSize="2M"
  38.      * )
  39.      */
  40.     private $file;
  41.     /**
  42.      * @var string|null
  43.      *
  44.      * @ORM\Column(name="titulo", type="string", length=255)
  45.      * @Assert\NotBlank(message="El título es requerido")
  46.      * @Assert\Length(
  47.      *      max = 255,
  48.      *      maxMessage = "El título no puede tener más de {{ limit }} caracteres"
  49.      * )
  50.      */
  51.     private $titulo;
  52.     /**
  53.      * @var string|null
  54.      *
  55.      * @ORM\Column(name="imagen", type="string", length=255, nullable=true)
  56.      * @Assert\Length(
  57.      *      max = 255,
  58.      *      maxMessage = "La cantidad maxima es {{ limit }} caracteres"
  59.      * )
  60.      */
  61.     private $imagen;
  62.     /**
  63.      * @var string|null
  64.      *
  65.      * @ORM\Column(name="url", type="string", length=255, nullable=true)
  66.      * @Assert\Regex(
  67.      *     pattern="/^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}[\/]?/",
  68.      *     message="Ingrese una URL correcta"
  69.      * )
  70.      * @Assert\Length(
  71.      *      max = 255,
  72.      *      maxMessage = "La cantidad maxima es {{ limit }} caracteres"
  73.      * )
  74.      */
  75.     private $url;
  76.     use ActivoTrait;
  77.     use PosicionTrait;
  78.     use TimestampableEntity;
  79.     use SoftDeleteableEntity;
  80.     /**
  81.      * @return int|null
  82.      */
  83.     public function getId(): ?int
  84.     {
  85.         return $this->id;
  86.     }
  87.     /**
  88.      * @return File|null
  89.      */
  90.     public function getFile(): ?File
  91.     {
  92.         return $this->file;
  93.     }
  94.     /**
  95.      * @param File|null $file
  96.      */
  97.     public function setFile(?File $file): void
  98.     {
  99.         $this->file $file;
  100.     }
  101.     /**
  102.      * @return string|null
  103.      */
  104.     public function getTitulo(): ?string
  105.     {
  106.         return $this->titulo;
  107.     }
  108.     /**
  109.      * @param string|null $titulo
  110.      */
  111.     public function setTitulo(?string $titulo): void
  112.     {
  113.         $this->titulo $titulo;
  114.     }
  115.     /**
  116.      * @return string|null
  117.      */
  118.     public function getImagen(): ?string
  119.     {
  120.         return $this->imagen;
  121.     }
  122.     /**
  123.      * @param string|null $imagen
  124.      */
  125.     public function setImagen(?string $imagen): void
  126.     {
  127.         $this->imagen $imagen;
  128.     }
  129.     /**
  130.      * @return string|null
  131.      */
  132.     public function getUrl(): ?string
  133.     {
  134.         return $this->url;
  135.     }
  136.     /**
  137.      * @param string|null $url
  138.      */
  139.     public function setUrl(?string $url): void
  140.     {
  141.         $this->url $url;
  142.     }
  143.     private function getDirectoryFile()
  144.     {
  145.         $class CommonFunctions::getClassMethod(__CLASS__);
  146.         $class CommonFunctions::camelCaseToSnakeCase($class'-');
  147.         $dir CommonFunctions::getParameter('BASE_PATH_IMAGENES');
  148.         return CommonFunctions::getPathOrURLWithSlash($dir) . $class;
  149.     }
  150.     public function getAbsolutePath()
  151.     {
  152.         return CommonFunctions::getAbsolutePath($this->getDirectoryFile(), $this->getImagen());
  153.     }
  154.     public function guardarImagen()
  155.     {
  156.         if (!$this->getFile()) {
  157.             return;
  158.         }
  159.         $name 'imagen_sponsor_' $this->getId();
  160.         $filename CommonFunctions::saveUploadedFile($this->getFile(), $this->getDirectoryFile(), $name);
  161.         if ($filename && $filename != $this->getImagen()) {
  162.             $this->preRemoveImagen();
  163.             $this->setImagen($filename);
  164.         }
  165.     }
  166.     /**
  167.      * @ORM\PrePersist
  168.      */
  169.     public function prePersist()
  170.     {
  171.         if (!$this->getImagen()) {
  172.             $this->setImagen(Constants::NO_IMAGE_ICON);
  173.         }
  174.     }
  175.     /**
  176.      * @ORM\PreRemove
  177.      */
  178.     public function preRemove()
  179.     {
  180.         $this->preRemoveImagen();
  181.     }
  182.     public function preRemoveImagen()
  183.     {
  184.         CommonFunctions::removeFile($this->getAbsolutePath());
  185.     }
  186.     public function getImagenBase64()
  187.     {
  188.         $imagen null;
  189.         if ($this->getId()) {
  190.             $imagen CommonFunctions::getBase64OfImage($this->getAbsolutePath(), Constants::DEFAULT_IMAGE);
  191.         }
  192.         return $imagen;
  193.     }
  194.     public function __toString()
  195.     {
  196.         return $this->getTitulo() ?: 'Nuevo Sponsor';
  197.     }
  198. }