src/Entity/App/Categoria.php line 33

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 App\Validator\UniqueCategoriaNombre;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use Gedmo\Mapping\Annotation as Gedmo;
  12. use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
  13. use Gedmo\Timestampable\Traits\TimestampableEntity;
  14. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  15. use Symfony\Component\HttpFoundation\File\File;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17. /**
  18.  * Categoria
  19.  *
  20.  * @ORM\Table(name="categoria", indexes={@ORM\Index(name="IDX_NOMBRE", columns={"nombre"})})
  21.  * @ORM\Entity(repositoryClass="App\Repository\App\CategoriaRepository")
  22.  * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
  23.  * @ORM\HasLifecycleCallbacks()
  24.  * @UniqueEntity(
  25.  *        fields={"etiqueta"},
  26.  *        message="La etiqueta ya está en uso. Por favor, elige otro."
  27.  *    )
  28.  * @UniqueCategoriaNombre()
  29.  */
  30. class Categoria
  31. {
  32.     const ASPECT_RATIO = array('width' => 1'height' => 1);
  33.     /**
  34.      * @var integer|null
  35.      *
  36.      * @ORM\Column(name="id", type="integer")
  37.      * @ORM\Id
  38.      * @ORM\GeneratedValue
  39.      */
  40.     private $id;
  41.     /**
  42.      * @var File|null
  43.      */
  44.     private $file;
  45.     /**
  46.      * @var string|null
  47.      *
  48.      * @ORM\Column(name="nombre", type="string", length=255)
  49.      * @Assert\NotBlank(message="El valor es requerido")
  50.      */
  51.     private $nombre;
  52.     /**
  53.      * @var string|null
  54.      *
  55.      * @ORM\Column(name="descripcion", type="string", length=255, nullable=true)
  56.      * @Assert\Length(
  57.      *      max = 255,
  58.      *      maxMessage = "La cantidad maxima es {{ limit }} caracteres"
  59.      * )
  60.      */
  61.     private $descripcion;
  62.     /**
  63.      * @var string|null
  64.      *
  65.      * @ORM\Column(name="icono", type="text")
  66.      * @Assert\Length(
  67.      *      max = 2000,
  68.      *      maxMessage = "La cantidad maxima es {{ limit }} caracteres"
  69.      * )
  70.      */
  71.     private $icono;
  72.     /**
  73.      * @var Categoria|null
  74.      *
  75.      * @ORM\ManyToOne(targetEntity="App\Entity\App\Categoria", inversedBy="subCategorias")
  76.      * @ORM\JoinColumn(name="categoria_id", referencedColumnName="id")
  77.      */
  78.     private $categoria;
  79.     /**
  80.      * @ORM\OneToMany(targetEntity="App\Entity\App\Categoria", mappedBy="categoria", cascade={"persist", "remove"})
  81.      * @ORM\OrderBy({"posicion"="ASC", "nombre"="ASC"})
  82.      */
  83.     private $subCategorias;
  84.     /**
  85.      * @ORM\OneToMany(targetEntity="App\Entity\App\Grupo", mappedBy="categoria", cascade={"persist", "remove"})
  86.      * @ORM\OrderBy({"posicion"="ASC", "nombre"="ASC"})
  87.      */
  88.     private $grupos;
  89.     /**
  90.      * @ORM\OneToMany(targetEntity="App\Entity\App\MaterialRecurso", mappedBy="categoria", cascade={"persist", "remove"})
  91.      * @ORM\OrderBy({"posicion"="ASC", "nombre"="ASC"})
  92.      */
  93.     private $materialesRecursos;
  94.     /**
  95.      * @var TipoCategoria|null
  96.      * @ORM\ManyToOne(targetEntity="App\Entity\App\TipoCategoria", inversedBy="categorias")
  97.      * @ORM\JoinColumn(name="tipo_categoria_id", referencedColumnName="id", nullable=false)
  98.      */
  99.     private $tipoCategoria;
  100.     /**
  101.      * @ORM\ManyToMany(targetEntity="App\Entity\App\Tramite", mappedBy="categorias")
  102.      */
  103.     private $tramites;
  104.     /**
  105.      * @var string|null
  106.      * @ORM\Column(name="url", type="string")
  107.      */
  108.     private $url;
  109.     /**
  110.      * @var string|null
  111.      * @ORM\Column(name="etiqueta", type="string")
  112.      */
  113.     private $etiqueta;
  114.     /**
  115.      * @var string|null
  116.      * @ORM\Column(name="color", type="string", length=32, nullable=true)
  117.      * @Assert\Length(
  118.      *      max = 32,
  119.      *      maxMessage = "El color no puede tener más de {{ limit }} caracteres"
  120.      * )
  121.      * @Assert\Regex(
  122.      *     pattern="/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/",
  123.      *     message="El color debe ser un código hexadecimal válido (ej: #FFF o #FFFFFF)"
  124.      * )
  125.      */
  126.     private $color;
  127.     use ActivoTrait;
  128.     use PosicionTrait;
  129.     use TimestampableEntity;
  130.     use SoftDeleteableEntity;
  131.     public function __construct()
  132.     {
  133.         $this->subCategorias = new ArrayCollection();
  134.         $this->grupos = new ArrayCollection();
  135.         $this->materialesRecursos = new ArrayCollection();
  136.         $this->tramites = new ArrayCollection();
  137.     }
  138.     /**
  139.      * @return int|null
  140.      */
  141.     public function getId(): ?int
  142.     {
  143.         return $this->id;
  144.     }
  145.     /**
  146.      * @return File|null
  147.      */
  148.     public function getFile(): ?File
  149.     {
  150.         return $this->file;
  151.     }
  152.     /**
  153.      * @param File|null $file
  154.      */
  155.     public function setFile(?File $file): void
  156.     {
  157.         $this->file $file;
  158.     }
  159.     /**
  160.      * @return string|null
  161.      */
  162.     public function getNombre(): ?string
  163.     {
  164.         return $this->nombre;
  165.     }
  166.     /**
  167.      * @param string|null $nombre
  168.      */
  169.     public function setNombre(?string $nombre): void
  170.     {
  171.         $this->nombre $nombre;
  172.     }
  173.     /**
  174.      * @return string|null
  175.      */
  176.     public function getDescripcion(): ?string
  177.     {
  178.         return $this->descripcion;
  179.     }
  180.     /**
  181.      * @param string|null $descripcion
  182.      */
  183.     public function setDescripcion(?string $descripcion): void
  184.     {
  185.         $this->descripcion $descripcion;
  186.     }
  187.     /**
  188.      * @return string|null
  189.      */
  190.     public function getIcono(): ?string
  191.     {
  192.         return $this->icono;
  193.     }
  194.     /**
  195.      * @param string|null $icono
  196.      */
  197.     public function setIcono(?string $icono): void
  198.     {
  199.         $this->icono $icono;
  200.     }
  201.     /**
  202.      * @return Categoria|null
  203.      */
  204.     public function getCategoria(): ?Categoria
  205.     {
  206.         return $this->categoria;
  207.     }
  208.     /**
  209.      * @param Categoria|null $categoria
  210.      */
  211.     public function setCategoria(?Categoria $categoria): void
  212.     {
  213.         $this->categoria $categoria;
  214.     }
  215.     /**
  216.      * @return Collection
  217.      */
  218.     public function getSubCategorias(): Collection
  219.     {
  220.         return $this->subCategorias;
  221.     }
  222.     /**
  223.      * @param Collection $subCategorias
  224.      */
  225.     public function setSubCategorias(Collection $subCategorias): void
  226.     {
  227.         $this->subCategorias $subCategorias;
  228.     }
  229.     /**
  230.      * @return Collection
  231.      */
  232.     public function getGrupos(): Collection
  233.     {
  234.         return $this->grupos;
  235.     }
  236.     /**
  237.      * @param Collection $grupos
  238.      */
  239.     public function setGrupos(Collection $grupos): void
  240.     {
  241.         $this->grupos $grupos;
  242.     }
  243.     /**
  244.      * @return Collection
  245.      */
  246.     public function getMaterialesRecursos(): Collection
  247.     {
  248.         return $this->materialesRecursos;
  249.     }
  250.     /**
  251.      * @param Collection $materialesRecursos
  252.      */
  253.     public function setMaterialesRecursos(Collection $materialesRecursos): void
  254.     {
  255.         $this->materialesRecursos $materialesRecursos;
  256.     }
  257.     private function getDirectoryFile()
  258.     {
  259.         $class CommonFunctions::getClassMethod(__CLASS__);
  260.         $class CommonFunctions::camelCaseToSnakeCase($class'-');
  261.         $dir CommonFunctions::getParameter('BASE_PATH_IMAGENES');
  262.         return CommonFunctions::getPathOrURLWithSlash($dir) . $class;
  263.     }
  264.     public function getAbsolutePath()
  265.     {
  266.         return CommonFunctions::getAbsolutePath($this->getDirectoryFile(), $this->getIcono());
  267.     }
  268.     public function guardarImagen()
  269.     {
  270.         $name 'imagen_categoria_' $this->getId();
  271.         $filename CommonFunctions::saveUploadedFile($this->getFile(), $this->getDirectoryFile(), $name);
  272.         if ($filename && $filename != $this->getIcono()) {
  273.             $this->preRemove();
  274.             $this->setIcono($filename);
  275.         }
  276.     }
  277.     /**
  278.      * @ORM\PrePersist
  279.      */
  280.     public function prePersist()
  281.     {
  282.         if (!$this->getIcono()) {
  283.             $this->setIcono(Constants::NO_IMAGE_ICON);
  284.         }
  285.     }
  286.     /**
  287.      * @ORM\PreRemove
  288.      */
  289.     public function preRemove()
  290.     {
  291.         CommonFunctions::removeFile($this->getAbsolutePath());
  292.     }
  293.     public function getImagenBase64()
  294.     {
  295.         $image null;
  296.         if ($this->getId()) {
  297.             $image CommonFunctions::getBase64OfImage($this->getAbsolutePath(), Constants::DEFAULT_IMAGE);
  298.         }
  299.         return $image;
  300.     }
  301.     public function getIconoValue()
  302.     {
  303.         return $this->esSubCategoria() ? $this->getIcono() : $this->getImagenBase64();
  304.     }
  305.     public function esSubCategoria()
  306.     {
  307.         return $this->getCategoria() !== null;
  308.     }
  309.     public function getNivel($nivel 1)
  310.     {
  311.         if ($this->getCategoria()) {
  312.             $nivel $this->getCategoria()->getNivel($nivel 1);
  313.         }
  314.         return $nivel;
  315.     }
  316.     public function getBreadcrumb($categorias = array())
  317.     {
  318.         if ($this->getCategoria()) {
  319.             $categorias[] = $this->getCategoria();
  320.             $categorias array_reverse($this->getCategoria()->getBreadcrumb($categorias));
  321.         }
  322.         return array_reverse($categorias);
  323.     }
  324.     public function getMaterialesRecursosCategoria()
  325.     {
  326.         $materialesRecursos $this->getMaterialesRecursos();
  327.         $materialesRecursosCategoria = array();
  328.         foreach ($materialesRecursos as $materialRecurso) {
  329.             if (!$materialRecurso->getGrupo()) {
  330.                 $materialesRecursosCategoria[] = $materialRecurso;
  331.             }
  332.         }
  333.         return $materialesRecursosCategoria;
  334.     }
  335.     public function getAllChilds()
  336.     {
  337.         return array_merge(
  338.             $this->getChildCategorias(),
  339.             $this->getChildGrupos(),
  340.             $this->getChildTramites(),
  341.             $this->getChildMaterialesRecursos()
  342.         );
  343.     }
  344.     private function getChildCategorias($categorias = array())
  345.     {
  346.         $categorias array_merge($categorias$this->getSubCategorias()->toArray());
  347.         foreach ($this->getSubCategorias() as $categoria) {
  348.             $categorias array_merge($categorias$categoria->getChildCategorias());
  349.         }
  350.         return $categorias;
  351.     }
  352.     private function getChildGrupos()
  353.     {
  354.         $grupos $this->getGrupos()->toArray();
  355.         foreach ($this->getChildCategorias() as $categoria) {
  356.             $grupos array_merge($grupos$categoria->getGrupos()->toArray());
  357.         }
  358.         return $grupos;
  359.     }
  360.     private function getChildTramites()
  361.     {
  362.         $tramites = array();
  363.         foreach ($this->getChildGrupos() as $grupo) {
  364.             $tramites array_merge($tramites$grupo->getTramites()->toArray());
  365.         }
  366.         return $tramites;
  367.     }
  368.     private function getChildMaterialesRecursos()
  369.     {
  370.         $materialesRecursos $this->getMaterialesRecursos()->toArray();
  371.         foreach ($this->getChildCategorias() as $categoria) {
  372.             $materialesRecursos array_merge($materialesRecursos$categoria->getMaterialesRecursos()->toArray());
  373.         }
  374.         return $materialesRecursos;
  375.     }
  376.     public function __toString()
  377.     {
  378.         $nuevo $this->esSubCategoria() ? 'Nueva subcategoría' 'Nueva categoría';
  379.         return $this->getNombre() ?: $nuevo;
  380.     }
  381.     public function getTipoCategoria(): ?TipoCategoria
  382.     {
  383.         return $this->tipoCategoria;
  384.     }
  385.     public function setTipoCategoria(?TipoCategoria $tipoCategoria): void
  386.     {
  387.         $this->tipoCategoria $tipoCategoria;
  388.     }
  389.     public function getTramites(): Collection
  390.     {
  391.         return $this->tramites;
  392.     }
  393.     public function getUrl(): ?string
  394.     {
  395.         return $this->url;
  396.     }
  397.     public function setUrl(?string $url): void
  398.     {
  399.         $this->url $url;
  400.     }
  401.     public function getEtiqueta(): ?string
  402.     {
  403.         return $this->etiqueta;
  404.     }
  405.     public function setEtiqueta(?string $etiqueta): void
  406.     {
  407.         $this->etiqueta $etiqueta;
  408.     }
  409.     public function getColor(): ?string
  410.     {
  411.         return $this->color;
  412.     }
  413.     public function setColor(?string $color): void
  414.     {
  415.         $this->color $color;
  416.     }
  417. }