src/Entity/Contenido.php line 32

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\Collection;
  4. use Doctrine\DBAL\Types\Types;
  5. use Doctrine\ORM\Mapping as ORM;
  6. // use Knp\DoctrineBehaviors\Model as ORMBehaviors;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use A2lix\TranslationFormBundle\Util\Knp\KnpTranslatable;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. use Symfony\Component\Validator\Context\ExecutionContext;
  11. use Gedmo\Mapping\Annotation as Gedmo;
  12. use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
  13. use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
  14. use App\Entity\Ruta;
  15. /**
  16.  * Contenido
  17.  *
  18.  * @ORM\Table(name="contenido")
  19.  * @ORM\Entity
  20.  * @ORM\InheritanceType("SINGLE_TABLE")
  21.  * @ORM\DiscriminatorColumn(name="tipo", type="string")
  22.  * @ORM\DiscriminatorMap( {"pagina" = "Pagina","contenido" = "Contenido", "home" = "Home", "servicio" = "ContenidoServicio"} )
  23.  * @ORM\HasLifecycleCallbacks 
  24.  * @ORM\Entity(repositoryClass="App\Repository\ContenidoRepository") 
  25.  */
  26. class Contenido implements TranslatableInterface
  27. {
  28.     const ESTADO_BORRADOR 0;
  29.     const ESTADO_PUBLICADO 1;
  30.     const ESTADO_PROGRAMADO 2;
  31.     const TIPO_PAGINA    'Pagina';
  32.     const TIPO_SERVICIO 'Servicio';
  33.     const TIPO_HOME   'Home';
  34.     
  35.     const TIPO 'Contenido';
  36.     
  37.     const CFG_TEXTOEXTRA = [
  38.     "club" => ['central''inferior'],
  39.     ];
  40.  
  41.     public static function getTipoLista()
  42.     {
  43.         return array(
  44.                 self::TIPO_PAGINA,    
  45.                 self::TIPO_HOME,
  46.                 self::TIPO_SERVICIO
  47.                 );
  48.     } 
  49.     
  50.     
  51.     public static function getTipo()
  52.     {
  53.         $c get_called_class();
  54.         return $c::TIPO;
  55.     }    
  56.  
  57.     public static function getTipoEntity($tipo)
  58.     {
  59.         switch($tipo) {
  60.             case self::TIPO_PAGINA:
  61.                 return Pagina::class;
  62.             case self::TIPO_HOME:
  63.                 return Home::class;
  64.             case self::TIPO_SERVICIO:
  65.                 return ContenidoServicio::class;
  66.         }
  67.     }
  68.     
  69.     public static function getTextoExtra($clave)
  70.     {
  71.         if (isset(self::CFG_TEXTOEXTRA[$clave]))
  72.             return self::CFG_TEXTOEXTRA[$clave];
  73.         return [];
  74.     }
  75.     
  76.     /* Begin Translatable */
  77.     
  78.     use TranslatableTrait;
  79.     
  80.     public function __call($method$args)
  81.     {
  82.         //return $this->proxyCurrentLocaleTranslation($method, $arguments);
  83.         if (!method_exists(self::getTranslationEntityClass(), $method)) {
  84.             $method 'get'ucfirst($method);
  85.         }
  86.         return $this->proxyCurrentLocaleTranslation($method$args);        
  87.     }
  88.     
  89.     
  90.     public function getCacheFields($campo$locale=null)
  91.     {
  92.         if (null === $locale) {
  93.             $locale $this->getCurrentLocale();
  94.         }
  95.         
  96.         $result = [];
  97.         $datos $this->getCache()->getDatos();
  98.         $media $this->getCache()->getMedia();
  99.         $langs $this->getCache()->getLangs();
  100.         
  101.         foreach ($campo as $k => $v) {
  102.             switch ($k) {
  103.                 case 'datos':
  104.                     if ($datos and isset($datos[$v]))
  105.                         $result[] = $datos[$v];
  106.                     break;
  107.                 case 'media':
  108.                     if ($media and isset($media[$v]))
  109.                         $result[] = $media[$v];
  110.                     break;
  111.                 case 'langs':
  112.                     if ($langs[$locale] and isset($langs[$locale][$v]))
  113.                         $result[] = $langs[$locale][$v];
  114.                     break;
  115.             }
  116.         }
  117.         
  118.         if (count($result) == 1)
  119.             return $result[0];
  120.         if (count($result) > 1)   
  121.             return $result;
  122.             
  123.         return false;
  124.         
  125.     }
  126.     
  127.     
  128.     /*public function getBloques()
  129.     {
  130.         return $this->proxyCurrentLocaleTranslation('getBloques');        
  131.     }*/
  132.     
  133.     /* End Translatable */
  134.     
  135.      /**
  136.      * @var integer
  137.      *
  138.      * @ORM\Column(name="id", type="integer")
  139.      * @ORM\Id
  140.      * @ORM\GeneratedValue(strategy="AUTO")
  141.      */
  142.     protected $id;
  143.   
  144.     /**
  145.      * @var string
  146.      *
  147.      * @ORM\Column(name="estado", type="string", length=20)
  148.      */
  149.     protected $estado;
  150.     /**
  151.      * @var \DateTime
  152.      *
  153.      * @ORM\Column(name="fecha_alta", type="datetime")
  154.      */
  155.     protected $fechaAlta;
  156.     /**
  157.      * @var \DateTime
  158.      *
  159.      * @ORM\Column(name="fecha_modificacion", type="datetime", nullable=true)
  160.      */
  161.     protected $fechaModificacion;
  162.     
  163.     /**
  164.      * @var \DateTime
  165.      *
  166.      * @ORM\Column(name="fecha_publicacion", type="datetime", nullable=true)
  167.      */
  168.     protected $fechaPublicacion;    
  169.     
  170.    
  171.     /**
  172.      * ORM\OneToMany(targetEntity="ContenidoRelacionado", mappedBy="contenidoRelacionado", cascade={"persist"}, orphanRemoval=true)
  173.      */
  174.     protected $relacionadosConmigo;
  175.     /**
  176.      * ORM\OneToMany(targetEntity="ContenidoRelacionado", mappedBy="contenido", cascade={"persist"}, orphanRemoval=true)
  177.      * ORM\OrderBy({"position" = "ASC"})
  178.      */
  179.     protected $contenidoRelacionado;
  180.         
  181.     
  182.     /**
  183.      * @var string
  184.      *
  185.      * @ORM\ManyToOne(targetEntity="Imagen") 
  186.      */
  187.     protected $imagen;      
  188.     
  189.     /**
  190.      * @var string
  191.      *
  192.      * @ORM\ManyToOne(targetEntity="Galeria") 
  193.      */
  194.     protected $galeria;
  195.     
  196.     /**
  197.      * @var Array
  198.      *
  199.      * @ORM\Column(name="locales", type="array")
  200.      */
  201.     protected $locales;    
  202.     
  203.     protected $tipo;
  204.     
  205.     /**
  206.      * @var string
  207.      *
  208.      * @ORM\OneToOne(targetEntity="Ruta", cascade={"persist"}) 
  209.      * @Gedmo\SortableGroup
  210.      */
  211.     protected $ruta;    
  212.     
  213.      /**
  214.      * @var integer
  215.      *
  216.      * @ORM\Column(name="error", type="integer", nullable=true)
  217.      */
  218.     protected $error;
  219.     
  220.     /**
  221.      * @var string
  222.      *
  223.      * @ORM\Column(name="clave", type="string", length=50, nullable=true)
  224.      */
  225.     protected $clave;
  226.     
  227.     /**
  228.      * @Gedmo\SortablePosition
  229.      * @ORM\Column(name="position", type="integer")
  230.      */
  231.     private $position;    
  232.     
  233.     
  234.     /**
  235. * @Assert\Valid
  236. */
  237. protected $translations;
  238.     
  239.     
  240.     public function __construct() {
  241.         $this->relacionadosConmigo = new \Doctrine\Common\Collections\ArrayCollection();
  242.         $this->contenidoRelacionado = new \Doctrine\Common\Collections\ArrayCollection();
  243.         $this->destacadoEn = new \Doctrine\Common\Collections\ArrayCollection();     
  244.         $this->fechaAlta = new \DateTime();
  245.         $this->estado self::ESTADO_BORRADOR;
  246.         $this->locales = array("es","en");
  247.         $this->position 1;
  248.     }    
  249.     
  250.     
  251.     public function getRelacionados($locale){
  252.         $relacionados = new \Doctrine\Common\Collections\ArrayCollection();
  253.         
  254.         foreach($this->getContenidoRelacionado() as $cr)
  255.         {
  256.             if (in_array($locale,$cr->getContenidoRelacionado()->getLocales()) and $cr->getContenidoRelacionado()->getEstado() == self::ESTADO_PUBLICADO)
  257.             {
  258.                 $relacionados->add($cr->getContenidoRelacionado());
  259.             }
  260.         }
  261.         
  262.         return $relacionados;
  263.          
  264.     }
  265.     
  266.     public function getContenidoRelacionado() {
  267.         return $this->contenidoRelacionado;
  268.     }
  269.     
  270.     public function getRelacionadosConmigo() {
  271.         return $this->relacionadosConmigo;
  272.     }  
  273.     
  274.     public function addContenidoRelacionado(Contenido $contenidoRelacionado)
  275.     {  
  276.         if (!$this->getContenidoRelacionado()->contains($contenidoRelacionado))
  277.         {
  278.             $contenidoRelacionado->setContenido($this);  
  279.             $contenidoRelacionado->setTipo($contenidoRelacionado->getTipo());
  280.             $this->contenidoRelacionado[] = ($contenidoRelacionado);  
  281.         }    
  282.          return $this;
  283.     }  
  284.   
  285.     public function removeContenidoRelacionado(Contenido $contenidoRelacionado) {  
  286.         $this->contenidoRelacionado->removeElement($contenidoRelacionado);  
  287.     }  
  288.     
  289.     public function setContenidoRelacionado($relaciones)
  290.     {
  291.         if (gettype($relaciones) == "array") {
  292.             $relaciones = new ArrayCollection($relaciones);
  293.         }
  294.         foreach($relaciones as $p)
  295.         {
  296.             $p->setContenido($this);
  297.             
  298.         }
  299.         
  300.         foreach ($relaciones as $r) {
  301.             $this->addContenidoRelacionado($r);
  302.         }        
  303.     }
  304.    /* 
  305.     public function getDestacadoEn() {
  306.         return $this->destacadoEn;
  307.     } 
  308.     
  309.     public function addDestacadoEn(ZonaContenidoDestacado $destacadoEn)
  310.     {  
  311.         if (!$this->getDestacadoEn()->contains($destacadoEn))
  312.         {
  313.             $destacadoEn->setContenido($this);  
  314.             $destacadoEn->setTipo($this->getTipo());
  315.             $destacadoEn->setPosition(0);
  316.             $this->destacadoEn[] = ($destacadoEn);  
  317.         }    
  318.          return $this;
  319.     }    
  320.     
  321.     public function removeDestacadoEn(ZonaContenidoDestacado $destacadoEn) {  
  322.         $this->destacadoEn->removeElement($destacadoEn);  
  323.     }  */   
  324.     
  325.     
  326.     /**
  327.      * @ORM\PrePersist()
  328.      * @ORM\PreUpdate()
  329.      */    
  330.     public function preUpdate()
  331.     {
  332.         $this->fechaModificacion = new \DateTime();
  333.         if ($this->estado == self::ESTADO_BORRADOR)
  334.             $this->fechaPublicacion null;
  335.         
  336.     }       
  337.     
  338.     public function __toString()
  339.     {
  340.         if(is_null($this->getSlug())) {
  341.             return $this->getId().'---NULL---';
  342.         }
  343.         return $this->getSlug();
  344.     }
  345.     
  346.     public static function getEstadoLista()
  347.     {
  348.         return array(
  349.                  self::ESTADO_BORRADOR =>  'Borrador'
  350.                  self::ESTADO_PUBLICADO =>  'Publicado'
  351.                  self::ESTADO_PROGRAMADO =>  'Programado'
  352.                 );
  353.     }
  354.     
  355.     public function getTitulo()
  356.     {
  357.          return $this->translate()->getTitulo();
  358.     }  
  359.     
  360.     public function getIntro()
  361.     {
  362.          return $this->translate()->getIntro();
  363.     }     
  364.     
  365.     public function getTexto()
  366.     {
  367.          return $this->translate()->getTexto();
  368.     }   
  369.     
  370.     public function getTextoMovil()
  371.     {
  372.          return $this->translate()->getTextoMovil();
  373.     }       
  374.     
  375.     
  376. //     public function getTipo()
  377. //     {
  378. //     }
  379.     /**
  380.      * Get id
  381.      *
  382.      * @return integer 
  383.      */
  384.     public function getId()
  385.     {
  386.         return $this->id;
  387.     }    
  388.     /**
  389.      * Set estado
  390.      *
  391.      * @param integer $estado
  392.      * @return Contenido
  393.      */
  394.     public function setEstado($estado)
  395.     {
  396.         if ($this->estado != self::ESTADO_PUBLICADO)
  397.             $this->fechaPublicacion = new \DateTime();
  398.         
  399.         $this->estado $estado;
  400.     
  401.         return $this;
  402.     }
  403.     /**
  404.      * Get estado
  405.      *
  406.      * @return integer 
  407.      */
  408.     public function getEstado()
  409.     {
  410.         return $this->estado;
  411.     }
  412.     /**
  413.      * Set fechaAlta
  414.      *
  415.      * @param \DateTime $fechaAlta
  416.      * @return Contenido
  417.      */
  418.     public function setFechaAlta($fechaAlta)
  419.     {
  420.         $this->fechaAlta $fechaAlta;
  421.     
  422.         return $this;
  423.     }
  424.     /**
  425.      * Get fechaAlta
  426.      *
  427.      * @return \DateTime 
  428.      */
  429.     public function getFechaAlta()
  430.     {
  431.         return $this->fechaAlta;
  432.     }
  433.     /**
  434.      * Set fechaModificacion
  435.      *
  436.      * @param \DateTime $fechaModificacion
  437.      * @return Contenido
  438.      */
  439.     public function setFechaModificacion($fechaModificacion)
  440.     {
  441.         $this->fechaModificacion $fechaModificacion;
  442.     
  443.         return $this;
  444.     }
  445.     /**
  446.      * Get fechaModificacion
  447.      *
  448.      * @return \DateTime 
  449.      */
  450.     public function getFechaModificacion()
  451.     {
  452.         return $this->fechaModificacion;
  453.     }
  454.     
  455.     /**
  456.      * Set fechaPublicacion
  457.      *
  458.      * @param \DateTime $fechaPublicacion
  459.      * @return Contenido
  460.      */
  461.     public function setFechaPublicacion($fechaPublicacion)
  462.     {
  463.         $this->fechaPublicacion $fechaPublicacion;
  464.     
  465.         return $this;
  466.     }
  467.     /**
  468.      * Get fechaPublicacion
  469.      *
  470.      * @return \DateTime 
  471.      */
  472.     public function getFechaPublicacion()
  473.     {
  474.         return $this->fechaPublicacion;
  475.     }    
  476.     /**
  477.      * Set galeria
  478.      *
  479.      * @param gallery $galeria
  480.      * @return Contenido
  481.      */
  482.     public function setGaleria($galeria)
  483.     {
  484.         $this->galeria $galeria;
  485.     
  486.         return $this;
  487.     }
  488.     /**
  489.      * Get galeria
  490.      *
  491.      * @return gallery 
  492.      */
  493.     public function getGaleria()
  494.     {
  495.         return $this->galeria;
  496.     }
  497.     
  498.     
  499.     /**
  500.      * Set imagen
  501.      *
  502.      * @param media $imagen
  503.      * @return Contenido
  504.      */
  505.     public function setImagen($imagen)
  506.     {
  507.         $this->imagen $imagen;
  508.     
  509.         return $this;
  510.     }
  511.     /**
  512.      * Get imagen
  513.      *
  514.      * @return media 
  515.      */
  516.     public function getImagen()
  517.     {
  518.         return $this->imagen;
  519.     }     
  520.     
  521.     /**
  522.      * Set locales
  523.      *
  524.      * @param array $locales
  525.      * @return Contenido
  526.      */
  527.     public function setLocales($locales)
  528.     {
  529.         $this->locales $locales;
  530.     
  531.         return $this;
  532.     }
  533.     /**
  534.      * Get locales
  535.      *
  536.      * @return array 
  537.      */
  538.     public function getLocales()
  539.     {
  540.         return $this->locales;
  541.     }     
  542.     
  543.     
  544.     /**
  545.      * Set ruta
  546.      *
  547.      * @param Ruta $ruta
  548.      * @return Contenido
  549.      */
  550.     public function setRuta(Ruta $ruta)
  551.     {
  552.         $this->ruta $ruta;
  553.     
  554.         return $this;
  555.     }
  556.     /**
  557.      * Get ruta
  558.      *
  559.      * @return ruta 
  560.      */
  561.     public function getRuta()
  562.     {
  563.         return $this->ruta;
  564.     }     
  565.     
  566.     public function getSfId(): ?string
  567.     {
  568.         return $this->sfId;
  569.     }
  570.     public function setSfId(string $sfId): self
  571.     {
  572.         $this->sfId $sfId;
  573.         return $this;
  574.     }
  575.         
  576.     /**
  577.      * @Assert\Callback
  578.      */
  579.     public function fechasValidas(ExecutionContext $context)   
  580.     {
  581.         if ($this->estado == Contenido::ESTADO_PROGRAMADO and $this->fechaPublicacion == null)
  582.         {
  583.             //$context->addViolationAt('fechaPublicacion', 'Para programar correctamente la publicación del contenido debes introducir una fecha de publicación válida', array(), null);
  584.             
  585.             $context->buildViolation('Para programar correctamente la publicación del contenido debes introducir una fecha de publicación válida')
  586.             ->atPath('fechaPublicacion')
  587.             ->addViolation();
  588.             
  589.         }
  590.         /*$diferencia = $this->fechaFin->diff($this->fechaInicio);
  591.         if (($diferencia->m > 3 or  ($diferencia->m == 3 and ($diferencia->d > 0 or $diferencia->h > 0))) or $diferencia->invert == 0)
  592.         {
  593.             
  594.         }*/
  595.     }
  596.     
  597.     
  598.     
  599.     public function getUrl($locale null)
  600.     {
  601.         
  602.         if (null === $locale) {
  603.             $locale $this->getCurrentLocale();
  604.         }
  605.         
  606.         if ($this->ruta)
  607.             return $this->ruta->translate($locale)->getRuta()."/".$this->translate($locale)->getSlug()."-".$this->getId();
  608.     }
  609.     
  610.     public static function getSlugId($slug) {
  611.     
  612.         $tmp explode("-",$slug);
  613.         $id = (int)end($tmp);
  614.         
  615. //         dump($id);
  616.         
  617.         if ($id and is_integer($id))
  618.             return $id;
  619.             
  620.         return false;
  621.     }
  622.     /**
  623.      * @ORM\PostPersist
  624.      */
  625.     public function actualizarSlug() {
  626.     
  627.         //anyadir ID al slug
  628.         
  629. //         dump($this);
  630.         
  631.         foreach($this->getTranslations() as $t) {
  632. //             dump($t);
  633.             $slugToken explode("-",$t->getSlug());
  634. //             dump($slugToken);
  635.             if (end($slugToken) != $this->getId()) {
  636. //                 echo "Regenera";
  637.                 $t->setSlug($t->generateSlugValue([$t->getTitulo()]));
  638.             }
  639.                 
  640. //             dump($this);
  641. //             die();
  642.         }
  643.     
  644.     }
  645.     
  646.     /*public function tituloSlugValidos(ExecutionContext $context)   
  647.     {
  648.         $titulo = $this->translate()->getTitulo();
  649.         $slug =$this->translate()->getSlug();
  650.         if(empty($titulo) or empty($slug))
  651.         {
  652.             $context->addViolationAt('titulo', 'Introduce un título válido', array(), null);
  653.         }
  654.     }*/
  655.     
  656.     public function isPublicado(): bool
  657.     {
  658.         return $this->getEstado() == self::ESTADO_PUBLICADO;
  659.     }
  660.     
  661.     public function getError()
  662.     {
  663.         return $this->error;
  664.     }
  665.     public function setError($error): self
  666.     {
  667.         $this->error $error;
  668.         return $this;
  669.     }
  670.     public function getClave(): ?string
  671.     {
  672.         return $this->clave;
  673.     }
  674.     public function setClave(string $clave): self
  675.     {
  676.         $this->clave $clave;
  677.         return $this;
  678.     }
  679.     public function getPosition(): ?int
  680.     {
  681.         return $this->position;
  682.     }
  683.     public function setPosition(int $position): self
  684.     {
  685.         $this->position $position;
  686.         return $this;
  687.     }
  688.     
  689. }