src/Entity/App/Tarjeta.php line 21

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.  * @ORM\Entity(repositoryClass="App\Repository\App\TarjetaRepository")
  15.  * @ORM\HasLifecycleCallbacks()
  16.  * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
  17.  */
  18. class Tarjeta
  19. {
  20.     const ASPECT_RATIO = array('width' => 1'height' => 1);
  21.     /**
  22.      * @var integer|null
  23.      * @ORM\Id
  24.      * @ORM\GeneratedValue(strategy="AUTO")
  25.      * @ORM\Column(type="integer")
  26.      */
  27.     private $id;
  28.     /**
  29.      * @var File|null
  30.      *
  31.      * @Assert\Image(
  32.      *     mimeTypes={"image/svg+xml", "image/jpg", "image/jpeg", "image/png"},
  33.      *     mimeTypesMessage="El tipo de imagen tiene que ser SVG, JPG o PNG",
  34.      *     maxSize="2M"
  35.      * )
  36.      */
  37.     private $file;
  38.     /**
  39.      * @var string|null
  40.      * @ORM\Column(type="string", name="titulo")
  41.      * @ORM\Column(type="string", nullable=false)
  42.      * @Assert\Length(
  43.      *  max = 5000,
  44.      *  maxMessage = "La cantidad maxima es {{ limit }} caracteres"
  45.      *  )
  46.      */
  47.     private ?string $titulo;
  48.     /**
  49.      * @var string|null
  50.      * @ORM\Column(type="string", name="descripcion")
  51.      * @ORM\Column(type="string", nullable=true)
  52.      * @Assert\Length(
  53.      *  max = 5000,
  54.      *  maxMessage = "La cantidad maxima es {{ limit }} caracteres"
  55.      *  )
  56.      */
  57.     private ?string $descripcion null;
  58.     /**
  59.      * @var Seccion|null
  60.      * @ORM\ManyToOne(targetEntity="App\Entity\App\Seccion", inversedBy="tarjetas")
  61.      * @ORM\JoinColumn(nullable=false)
  62.      */
  63.     private $seccion;
  64.     /**
  65.      * @var string|null
  66.      *
  67.      * @ORM\Column(name="link", type="string", length=255)
  68.      * @Assert\NotBlank(message="El valor es requerido")
  69.      * @Assert\Length(
  70.      *      max = 255,
  71.      *      maxMessage = "La cantidad maxima es {{ limit }} caracteres"
  72.      * )
  73.      */
  74.     private $link;
  75.     /**
  76.      * @var string|null
  77.      *
  78.      * @ORM\Column(name="imagen", type="string", length=255)
  79.      * @Assert\Length(
  80.      *      max = 255,
  81.      *      maxMessage = "La cantidad maxima es {{ limit }} caracteres"
  82.      * )
  83.      */
  84.     private $imagen;
  85.     public function getId(): ?int
  86.     {
  87.         return $this->id;
  88.     }
  89.     public function setId(?int $id): void
  90.     {
  91.         $this->id $id;
  92.     }
  93.     public function getFile(): ?File
  94.     {
  95.         return $this->file;
  96.     }
  97.     public function setFile(?File $file): void
  98.     {
  99.         $this->file $file;
  100.     }
  101.     public function getTitulo(): ?string
  102.     {
  103.         return $this->titulo;
  104.     }
  105.     public function setTitulo(?string $titulo): void
  106.     {
  107.         $this->titulo $titulo;
  108.     }
  109.     public function getDescripcion(): ?string
  110.     {
  111.         return $this->descripcion;
  112.     }
  113.     public function setDescripcion(?string $descripcion): void
  114.     {
  115.         $this->descripcion $descripcion;
  116.     }
  117.     public function getSeccion(): ?Seccion
  118.     {
  119.         return $this->seccion;
  120.     }
  121.     public function setSeccion(?Seccion $seccion): void
  122.     {
  123.         $this->seccion $seccion;
  124.     }
  125.     public function getLink(): ?string
  126.     {
  127.         return $this->link;
  128.     }
  129.     public function setLink(?string $link): void
  130.     {
  131.         $this->link $link;
  132.     }
  133.     public function getImagen(): ?string
  134.     {
  135.         return $this->imagen;
  136.     }
  137.     public function setImagen(?string $imagen): void
  138.     {
  139.         $this->imagen $imagen;
  140.     }
  141.     private function getDirectoryFile()
  142.     {
  143.         $class CommonFunctions::getClassMethod(__CLASS__);
  144.         $class CommonFunctions::camelCaseToSnakeCase($class'-');
  145.         $dir CommonFunctions::getParameter('BASE_PATH_IMAGENES');
  146.         return CommonFunctions::getPathOrURLWithSlash($dir) . $class;
  147.     }
  148.     public function guardarImagen()
  149.     {
  150.         $name 'imagen_tarjeta_' $this->getId();
  151.         $filename CommonFunctions::saveUploadedFile($this->getFile(), $this->getDirectoryFile(), $name);
  152.         if ($filename && $filename != $this->getImagen()) {
  153.             $this->setImagen($filename);
  154.         }
  155.     }
  156.     /**
  157.      * @ORM\PrePersist
  158.      */
  159.     public function prePersist()
  160.     {
  161.         $this->setImagen(Constants::NO_IMAGE_ICON);
  162.     }
  163.     /**
  164.      * @ORM\PreRemove
  165.      */
  166.     public function preRemove()
  167.     {
  168.         CommonFunctions::removeFile($this->getAbsolutePath());
  169.     }
  170.     public function getAbsolutePath()
  171.     {
  172.         return CommonFunctions::getAbsolutePath($this->getDirectoryFile(), $this->getImagen());
  173.     }
  174.     public function getImagenBase64()
  175.     {
  176.         $image null;
  177.         if ($this->getId()) {
  178.             $image CommonFunctions::getBase64OfImage($this->getAbsolutePath(), Constants::DEFAULT_IMAGE);
  179.         }
  180.         return $image;
  181.     }
  182.     use ActivoTrait;
  183.     use PosicionTrait;
  184.     use TimestampableEntity;
  185.     use SoftDeleteableEntity;
  186. }