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.      * @ORM\OneToMany(targetEntity="App\Entity\App\TipoDestacado", mappedBy="categoria", cascade={"remove"})
  96.      */
  97.     private $tiposDestacados;
  98.     /**
  99.      * @var TipoCategoria|null
  100.      * @ORM\ManyToOne(targetEntity="App\Entity\App\TipoCategoria", inversedBy="categorias")
  101.      * @ORM\JoinColumn(name="tipo_categoria_id", referencedColumnName="id", nullable=false)
  102.      */
  103.     private $tipoCategoria;
  104.     /**
  105.      * @ORM\ManyToMany(targetEntity="App\Entity\App\Tramite", mappedBy="categorias")
  106.      */
  107.     private $tramites;
  108.     /**
  109.      * @var string|null
  110.      * @ORM\Column(name="url", type="string")
  111.      */
  112.     private $url;
  113.     /**
  114.      * @var string|null
  115.      * @ORM\Column(name="etiqueta", type="string")
  116.      */
  117.     private $etiqueta;
  118.     /**
  119.      * @var string|null
  120.      * @ORM\Column(name="color", type="string", length=32, nullable=true)
  121.      * @Assert\Length(
  122.      *      max = 32,
  123.      *      maxMessage = "El color no puede tener más de {{ limit }} caracteres"
  124.      * )
  125.      * @Assert\Regex(
  126.      *     pattern="/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/",
  127.      *     message="El color debe ser un código hexadecimal válido (ej: #FFF o #FFFFFF)"
  128.      * )
  129.      */
  130.     private $color;
  131.     use ActivoTrait;
  132.     use PosicionTrait;
  133.     use TimestampableEntity;
  134.     use SoftDeleteableEntity;
  135.     public function __construct()
  136.     {
  137.         $this->subCategorias = new ArrayCollection();
  138.         $this->grupos = new ArrayCollection();
  139.         $this->materialesRecursos = new ArrayCollection();
  140.         $this->tramites = new ArrayCollection();
  141.         $this->tiposDestacados = new ArrayCollection();
  142.     }
  143.     /**
  144.      * @return int|null
  145.      */
  146.     public function getId(): ?int
  147.     {
  148.         return $this->id;
  149.     }
  150.     /**
  151.      * @return File|null
  152.      */
  153.     public function getFile(): ?File
  154.     {
  155.         return $this->file;
  156.     }
  157.     /**
  158.      * @param File|null $file
  159.      */
  160.     public function setFile(?File $file): void
  161.     {
  162.         $this->file $file;
  163.     }
  164.     /**
  165.      * @return string|null
  166.      */
  167.     public function getNombre(): ?string
  168.     {
  169.         return $this->nombre;
  170.     }
  171.     /**
  172.      * @param string|null $nombre
  173.      */
  174.     public function setNombre(?string $nombre): void
  175.     {
  176.         $this->nombre $nombre;
  177.     }
  178.     /**
  179.      * @return string|null
  180.      */
  181.     public function getDescripcion(): ?string
  182.     {
  183.         return $this->descripcion;
  184.     }
  185.     /**
  186.      * @param string|null $descripcion
  187.      */
  188.     public function setDescripcion(?string $descripcion): void
  189.     {
  190.         $this->descripcion $descripcion;
  191.     }
  192.     /**
  193.      * @return string|null
  194.      */
  195.     public function getIcono(): ?string
  196.     {
  197.         return $this->icono;
  198.     }
  199.     /**
  200.      * @param string|null $icono
  201.      */
  202.     public function setIcono(?string $icono): void
  203.     {
  204.         $this->icono $icono;
  205.     }
  206.     /**
  207.      * @return Categoria|null
  208.      */
  209.     public function getCategoria(): ?Categoria
  210.     {
  211.         return $this->categoria;
  212.     }
  213.     /**
  214.      * @param Categoria|null $categoria
  215.      */
  216.     public function setCategoria(?Categoria $categoria): void
  217.     {
  218.         $this->categoria $categoria;
  219.     }
  220.     /**
  221.      * @return Collection
  222.      */
  223.     public function getSubCategorias(): Collection
  224.     {
  225.         return $this->subCategorias;
  226.     }
  227.     /**
  228.      * @param Collection $subCategorias
  229.      */
  230.     public function setSubCategorias(Collection $subCategorias): void
  231.     {
  232.         $this->subCategorias $subCategorias;
  233.     }
  234.     /**
  235.      * @return Collection
  236.      */
  237.     public function getGrupos(): Collection
  238.     {
  239.         return $this->grupos;
  240.     }
  241.     /**
  242.      * @param Collection $grupos
  243.      */
  244.     public function setGrupos(Collection $grupos): void
  245.     {
  246.         $this->grupos $grupos;
  247.     }
  248.     /**
  249.      * @return Collection
  250.      */
  251.     public function getMaterialesRecursos(): Collection
  252.     {
  253.         return $this->materialesRecursos;
  254.     }
  255.     /**
  256.      * @param Collection $materialesRecursos
  257.      */
  258.     public function setMaterialesRecursos(Collection $materialesRecursos): void
  259.     {
  260.         $this->materialesRecursos $materialesRecursos;
  261.     }
  262.     private function getDirectoryFile()
  263.     {
  264.         $class CommonFunctions::getClassMethod(__CLASS__);
  265.         $class CommonFunctions::camelCaseToSnakeCase($class'-');
  266.         $dir CommonFunctions::getParameter('BASE_PATH_IMAGENES');
  267.         return CommonFunctions::getPathOrURLWithSlash($dir) . $class;
  268.     }
  269.     public function getAbsolutePath()
  270.     {
  271.         return CommonFunctions::getAbsolutePath($this->getDirectoryFile(), $this->getIcono());
  272.     }
  273.     public function guardarImagen()
  274.     {
  275.         $name 'imagen_categoria_' $this->getId();
  276.         $filename CommonFunctions::saveUploadedFile($this->getFile(), $this->getDirectoryFile(), $name);
  277.         if ($filename && $filename != $this->getIcono()) {
  278.             $this->preRemove();
  279.             $this->setIcono($filename);
  280.         }
  281.     }
  282.     /**
  283.      * @ORM\PrePersist
  284.      */
  285.     public function prePersist()
  286.     {
  287.         if (!$this->getIcono()) {
  288.             $this->setIcono(Constants::NO_IMAGE_ICON);
  289.         }
  290.     }
  291.     /**
  292.      * @ORM\PreRemove
  293.      */
  294.     public function preRemove()
  295.     {
  296.         CommonFunctions::removeFile($this->getAbsolutePath());
  297.     }
  298.     public function getImagenBase64()
  299.     {
  300.         $image null;
  301.         if ($this->getId()) {
  302.             $image CommonFunctions::getBase64OfImage($this->getAbsolutePath(), Constants::DEFAULT_IMAGE);
  303.         }
  304.         return $image;
  305.     }
  306.     public function getIconoValue()
  307.     {
  308.         return $this->esSubCategoria() ? $this->getIcono() : $this->getImagenBase64();
  309.     }
  310.     public function esSubCategoria()
  311.     {
  312.         return $this->getCategoria() !== null;
  313.     }
  314.     public function getNivel($nivel 1)
  315.     {
  316.         if ($this->getCategoria()) {
  317.             $nivel $this->getCategoria()->getNivel($nivel 1);
  318.         }
  319.         return $nivel;
  320.     }
  321.     public function getBreadcrumb($categorias = array())
  322.     {
  323.         if ($this->getCategoria()) {
  324.             $categorias[] = $this->getCategoria();
  325.             $categorias array_reverse($this->getCategoria()->getBreadcrumb($categorias));
  326.         }
  327.         return array_reverse($categorias);
  328.     }
  329.     public function getMaterialesRecursosCategoria()
  330.     {
  331.         $materialesRecursos $this->getMaterialesRecursos();
  332.         $materialesRecursosCategoria = array();
  333.         foreach ($materialesRecursos as $materialRecurso) {
  334.             if (!$materialRecurso->getGrupo()) {
  335.                 $materialesRecursosCategoria[] = $materialRecurso;
  336.             }
  337.         }
  338.         return $materialesRecursosCategoria;
  339.     }
  340.     public function getAllChilds()
  341.     {
  342.         return array_merge(
  343.             $this->getChildCategorias(),
  344.             $this->getChildGrupos(),
  345.             $this->getChildTramites(),
  346.             $this->getChildMaterialesRecursos()
  347.         );
  348.     }
  349.     private function getChildCategorias($categorias = array())
  350.     {
  351.         $categorias array_merge($categorias$this->getSubCategorias()->toArray());
  352.         foreach ($this->getSubCategorias() as $categoria) {
  353.             $categorias array_merge($categorias$categoria->getChildCategorias());
  354.         }
  355.         return $categorias;
  356.     }
  357.     private function getChildGrupos()
  358.     {
  359.         $grupos $this->getGrupos()->toArray();
  360.         foreach ($this->getChildCategorias() as $categoria) {
  361.             $grupos array_merge($grupos$categoria->getGrupos()->toArray());
  362.         }
  363.         return $grupos;
  364.     }
  365.     private function getChildTramites()
  366.     {
  367.         $tramites = array();
  368.         foreach ($this->getChildGrupos() as $grupo) {
  369.             $tramites array_merge($tramites$grupo->getTramites()->toArray());
  370.         }
  371.         return $tramites;
  372.     }
  373.     private function getChildMaterialesRecursos()
  374.     {
  375.         $materialesRecursos $this->getMaterialesRecursos()->toArray();
  376.         foreach ($this->getChildCategorias() as $categoria) {
  377.             $materialesRecursos array_merge($materialesRecursos$categoria->getMaterialesRecursos()->toArray());
  378.         }
  379.         return $materialesRecursos;
  380.     }
  381.     public function __toString()
  382.     {
  383.         $nuevo $this->esSubCategoria() ? 'Nueva subcategoría' 'Nueva categoría';
  384.         return $this->getNombre() ?: $nuevo;
  385.     }
  386.     public function getTipoCategoria(): ?TipoCategoria
  387.     {
  388.         return $this->tipoCategoria;
  389.     }
  390.     public function setTipoCategoria(?TipoCategoria $tipoCategoria): void
  391.     {
  392.         $this->tipoCategoria $tipoCategoria;
  393.     }
  394.     public function getTramites(): Collection
  395.     {
  396.         return $this->tramites;
  397.     }
  398.     public function getUrl(): ?string
  399.     {
  400.         return $this->url;
  401.     }
  402.     public function setUrl(?string $url): void
  403.     {
  404.         $this->url $url;
  405.     }
  406.     public function getEtiqueta(): ?string
  407.     {
  408.         return $this->etiqueta;
  409.     }
  410.     public function setEtiqueta(?string $etiqueta): void
  411.     {
  412.         $this->etiqueta $etiqueta;
  413.     }
  414.     public function getColor(): ?string
  415.     {
  416.         return $this->color;
  417.     }
  418.     public function setColor(?string $color): void
  419.     {
  420.         $this->color $color;
  421.     }
  422.     /**
  423.      * @return Collection
  424.      */
  425.     public function getTiposDestacados(): Collection
  426.     {
  427.         return $this->tiposDestacados;
  428.     }
  429.     /**
  430.      * @param Collection $tiposDestacados
  431.      */
  432.     public function setTiposDestacados(Collection $tiposDestacados): void
  433.     {
  434.         $this->tiposDestacados $tiposDestacados;
  435.     }
  436. }