src/Entity/Contenido.php line 32
- <?php
- namespace App\Entity;
- use Doctrine\Common\Collections\Collection;
- use Doctrine\DBAL\Types\Types;
- use Doctrine\ORM\Mapping as ORM;
- // use Knp\DoctrineBehaviors\Model as ORMBehaviors;
- use Doctrine\Common\Collections\ArrayCollection;
- use A2lix\TranslationFormBundle\Util\Knp\KnpTranslatable;
- use Symfony\Component\Validator\Constraints as Assert;
- use Symfony\Component\Validator\Context\ExecutionContext;
- use Gedmo\Mapping\Annotation as Gedmo;
- use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
- use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
- use App\Entity\Ruta;
- /**
- * Contenido
- *
- * @ORM\Table(name="contenido")
- * @ORM\Entity
- * @ORM\InheritanceType("SINGLE_TABLE")
- * @ORM\DiscriminatorColumn(name="tipo", type="string")
- * @ORM\DiscriminatorMap( {"pagina" = "Pagina","contenido" = "Contenido", "home" = "Home", "servicio" = "ContenidoServicio"} )
- * @ORM\HasLifecycleCallbacks
- * @ORM\Entity(repositoryClass="App\Repository\ContenidoRepository")
- */
- class Contenido implements TranslatableInterface
- {
- const ESTADO_BORRADOR = 0;
- const ESTADO_PUBLICADO = 1;
- const ESTADO_PROGRAMADO = 2;
- const TIPO_PAGINA = 'Pagina';
- const TIPO_SERVICIO = 'Servicio';
- const TIPO_HOME = 'Home';
- const TIPO = 'Contenido';
- const CFG_TEXTOEXTRA = [
- "club" => ['central', 'inferior'],
- ];
- public static function getTipoLista()
- {
- return array(
- self::TIPO_PAGINA,
- self::TIPO_HOME,
- self::TIPO_SERVICIO
- );
- }
- public static function getTipo()
- {
- $c = get_called_class();
- return $c::TIPO;
- }
- public static function getTipoEntity($tipo)
- {
- switch($tipo) {
- case self::TIPO_PAGINA:
- return Pagina::class;
- case self::TIPO_HOME:
- return Home::class;
- case self::TIPO_SERVICIO:
- return ContenidoServicio::class;
- }
- }
- public static function getTextoExtra($clave)
- {
- if (isset(self::CFG_TEXTOEXTRA[$clave]))
- return self::CFG_TEXTOEXTRA[$clave];
- return [];
- }
- /* Begin Translatable */
- use TranslatableTrait;
- public function __call($method, $args)
- {
- //return $this->proxyCurrentLocaleTranslation($method, $arguments);
- if (!method_exists(self::getTranslationEntityClass(), $method)) {
- $method = 'get'. ucfirst($method);
- }
- return $this->proxyCurrentLocaleTranslation($method, $args);
- }
- public function getCacheFields($campo, $locale=null)
- {
- if (null === $locale) {
- $locale = $this->getCurrentLocale();
- }
- $result = [];
- $datos = $this->getCache()->getDatos();
- $media = $this->getCache()->getMedia();
- $langs = $this->getCache()->getLangs();
- foreach ($campo as $k => $v) {
- switch ($k) {
- case 'datos':
- if ($datos and isset($datos[$v]))
- $result[] = $datos[$v];
- break;
- case 'media':
- if ($media and isset($media[$v]))
- $result[] = $media[$v];
- break;
- case 'langs':
- if ($langs[$locale] and isset($langs[$locale][$v]))
- $result[] = $langs[$locale][$v];
- break;
- }
- }
- if (count($result) == 1)
- return $result[0];
- if (count($result) > 1)
- return $result;
- return false;
- }
- /*public function getBloques()
- {
- return $this->proxyCurrentLocaleTranslation('getBloques');
- }*/
- /* End Translatable */
- /**
- * @var integer
- *
- * @ORM\Column(name="id", type="integer")
- * @ORM\Id
- * @ORM\GeneratedValue(strategy="AUTO")
- */
- protected $id;
- /**
- * @var string
- *
- * @ORM\Column(name="estado", type="string", length=20)
- */
- protected $estado;
- /**
- * @var \DateTime
- *
- * @ORM\Column(name="fecha_alta", type="datetime")
- */
- protected $fechaAlta;
- /**
- * @var \DateTime
- *
- * @ORM\Column(name="fecha_modificacion", type="datetime", nullable=true)
- */
- protected $fechaModificacion;
- /**
- * @var \DateTime
- *
- * @ORM\Column(name="fecha_publicacion", type="datetime", nullable=true)
- */
- protected $fechaPublicacion;
- /**
- * ORM\OneToMany(targetEntity="ContenidoRelacionado", mappedBy="contenidoRelacionado", cascade={"persist"}, orphanRemoval=true)
- */
- protected $relacionadosConmigo;
- /**
- * ORM\OneToMany(targetEntity="ContenidoRelacionado", mappedBy="contenido", cascade={"persist"}, orphanRemoval=true)
- * ORM\OrderBy({"position" = "ASC"})
- */
- protected $contenidoRelacionado;
- /**
- * @var string
- *
- * @ORM\ManyToOne(targetEntity="Imagen")
- */
- protected $imagen;
- /**
- * @var string
- *
- * @ORM\ManyToOne(targetEntity="Galeria")
- */
- protected $galeria;
- /**
- * @var Array
- *
- * @ORM\Column(name="locales", type="array")
- */
- protected $locales;
- protected $tipo;
- /**
- * @var string
- *
- * @ORM\OneToOne(targetEntity="Ruta", cascade={"persist"})
- * @Gedmo\SortableGroup
- */
- protected $ruta;
- /**
- * @var integer
- *
- * @ORM\Column(name="error", type="integer", nullable=true)
- */
- protected $error;
- /**
- * @var string
- *
- * @ORM\Column(name="clave", type="string", length=50, nullable=true)
- */
- protected $clave;
- /**
- * @Gedmo\SortablePosition
- * @ORM\Column(name="position", type="integer")
- */
- private $position;
- /**
- * @Assert\Valid
- */
- protected $translations;
- public function __construct() {
- $this->relacionadosConmigo = new \Doctrine\Common\Collections\ArrayCollection();
- $this->contenidoRelacionado = new \Doctrine\Common\Collections\ArrayCollection();
- $this->destacadoEn = new \Doctrine\Common\Collections\ArrayCollection();
- $this->fechaAlta = new \DateTime();
- $this->estado = self::ESTADO_BORRADOR;
- $this->locales = array("es","en");
- $this->position = 1;
- }
- public function getRelacionados($locale){
- $relacionados = new \Doctrine\Common\Collections\ArrayCollection();
- foreach($this->getContenidoRelacionado() as $cr)
- {
- if (in_array($locale,$cr->getContenidoRelacionado()->getLocales()) and $cr->getContenidoRelacionado()->getEstado() == self::ESTADO_PUBLICADO)
- {
- $relacionados->add($cr->getContenidoRelacionado());
- }
- }
- return $relacionados;
- }
- public function getContenidoRelacionado() {
- return $this->contenidoRelacionado;
- }
- public function getRelacionadosConmigo() {
- return $this->relacionadosConmigo;
- }
- public function addContenidoRelacionado(Contenido $contenidoRelacionado)
- {
- if (!$this->getContenidoRelacionado()->contains($contenidoRelacionado))
- {
- $contenidoRelacionado->setContenido($this);
- $contenidoRelacionado->setTipo($contenidoRelacionado->getTipo());
- $this->contenidoRelacionado[] = ($contenidoRelacionado);
- }
- return $this;
- }
- public function removeContenidoRelacionado(Contenido $contenidoRelacionado) {
- $this->contenidoRelacionado->removeElement($contenidoRelacionado);
- }
- public function setContenidoRelacionado($relaciones)
- {
- if (gettype($relaciones) == "array") {
- $relaciones = new ArrayCollection($relaciones);
- }
- foreach($relaciones as $p)
- {
- $p->setContenido($this);
- }
- foreach ($relaciones as $r) {
- $this->addContenidoRelacionado($r);
- }
- }
- /*
- public function getDestacadoEn() {
- return $this->destacadoEn;
- }
- public function addDestacadoEn(ZonaContenidoDestacado $destacadoEn)
- {
- if (!$this->getDestacadoEn()->contains($destacadoEn))
- {
- $destacadoEn->setContenido($this);
- $destacadoEn->setTipo($this->getTipo());
- $destacadoEn->setPosition(0);
- $this->destacadoEn[] = ($destacadoEn);
- }
- return $this;
- }
- public function removeDestacadoEn(ZonaContenidoDestacado $destacadoEn) {
- $this->destacadoEn->removeElement($destacadoEn);
- } */
- /**
- * @ORM\PrePersist()
- * @ORM\PreUpdate()
- */
- public function preUpdate()
- {
- $this->fechaModificacion = new \DateTime();
- if ($this->estado == self::ESTADO_BORRADOR)
- $this->fechaPublicacion = null;
- }
- public function __toString()
- {
- if(is_null($this->getSlug())) {
- return $this->getId().'---NULL---';
- }
- return $this->getSlug();
- }
- public static function getEstadoLista()
- {
- return array(
- self::ESTADO_BORRADOR => 'Borrador',
- self::ESTADO_PUBLICADO => 'Publicado',
- self::ESTADO_PROGRAMADO => 'Programado'
- );
- }
- public function getTitulo()
- {
- return $this->translate()->getTitulo();
- }
- public function getIntro()
- {
- return $this->translate()->getIntro();
- }
- public function getTexto()
- {
- return $this->translate()->getTexto();
- }
- public function getTextoMovil()
- {
- return $this->translate()->getTextoMovil();
- }
- // public function getTipo()
- // {
- // }
- /**
- * Get id
- *
- * @return integer
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * Set estado
- *
- * @param integer $estado
- * @return Contenido
- */
- public function setEstado($estado)
- {
- if ($this->estado != self::ESTADO_PUBLICADO)
- $this->fechaPublicacion = new \DateTime();
- $this->estado = $estado;
- return $this;
- }
- /**
- * Get estado
- *
- * @return integer
- */
- public function getEstado()
- {
- return $this->estado;
- }
- /**
- * Set fechaAlta
- *
- * @param \DateTime $fechaAlta
- * @return Contenido
- */
- public function setFechaAlta($fechaAlta)
- {
- $this->fechaAlta = $fechaAlta;
- return $this;
- }
- /**
- * Get fechaAlta
- *
- * @return \DateTime
- */
- public function getFechaAlta()
- {
- return $this->fechaAlta;
- }
- /**
- * Set fechaModificacion
- *
- * @param \DateTime $fechaModificacion
- * @return Contenido
- */
- public function setFechaModificacion($fechaModificacion)
- {
- $this->fechaModificacion = $fechaModificacion;
- return $this;
- }
- /**
- * Get fechaModificacion
- *
- * @return \DateTime
- */
- public function getFechaModificacion()
- {
- return $this->fechaModificacion;
- }
- /**
- * Set fechaPublicacion
- *
- * @param \DateTime $fechaPublicacion
- * @return Contenido
- */
- public function setFechaPublicacion($fechaPublicacion)
- {
- $this->fechaPublicacion = $fechaPublicacion;
- return $this;
- }
- /**
- * Get fechaPublicacion
- *
- * @return \DateTime
- */
- public function getFechaPublicacion()
- {
- return $this->fechaPublicacion;
- }
- /**
- * Set galeria
- *
- * @param gallery $galeria
- * @return Contenido
- */
- public function setGaleria($galeria)
- {
- $this->galeria = $galeria;
- return $this;
- }
- /**
- * Get galeria
- *
- * @return gallery
- */
- public function getGaleria()
- {
- return $this->galeria;
- }
- /**
- * Set imagen
- *
- * @param media $imagen
- * @return Contenido
- */
- public function setImagen($imagen)
- {
- $this->imagen = $imagen;
- return $this;
- }
- /**
- * Get imagen
- *
- * @return media
- */
- public function getImagen()
- {
- return $this->imagen;
- }
- /**
- * Set locales
- *
- * @param array $locales
- * @return Contenido
- */
- public function setLocales($locales)
- {
- $this->locales = $locales;
- return $this;
- }
- /**
- * Get locales
- *
- * @return array
- */
- public function getLocales()
- {
- return $this->locales;
- }
- /**
- * Set ruta
- *
- * @param Ruta $ruta
- * @return Contenido
- */
- public function setRuta(Ruta $ruta)
- {
- $this->ruta = $ruta;
- return $this;
- }
- /**
- * Get ruta
- *
- * @return ruta
- */
- public function getRuta()
- {
- return $this->ruta;
- }
- public function getSfId(): ?string
- {
- return $this->sfId;
- }
- public function setSfId(string $sfId): self
- {
- $this->sfId = $sfId;
- return $this;
- }
- /**
- * @Assert\Callback
- */
- public function fechasValidas(ExecutionContext $context)
- {
- if ($this->estado == Contenido::ESTADO_PROGRAMADO and $this->fechaPublicacion == null)
- {
- //$context->addViolationAt('fechaPublicacion', 'Para programar correctamente la publicación del contenido debes introducir una fecha de publicación válida', array(), null);
- $context->buildViolation('Para programar correctamente la publicación del contenido debes introducir una fecha de publicación válida')
- ->atPath('fechaPublicacion')
- ->addViolation();
- }
- /*$diferencia = $this->fechaFin->diff($this->fechaInicio);
- if (($diferencia->m > 3 or ($diferencia->m == 3 and ($diferencia->d > 0 or $diferencia->h > 0))) or $diferencia->invert == 0)
- {
- }*/
- }
- public function getUrl($locale = null)
- {
- if (null === $locale) {
- $locale = $this->getCurrentLocale();
- }
- if ($this->ruta)
- return $this->ruta->translate($locale)->getRuta()."/".$this->translate($locale)->getSlug()."-".$this->getId();
- }
- public static function getSlugId($slug) {
- $tmp = explode("-",$slug);
- $id = (int)end($tmp);
- // dump($id);
- if ($id and is_integer($id))
- return $id;
- return false;
- }
- /**
- * @ORM\PostPersist
- */
- public function actualizarSlug() {
- //anyadir ID al slug
- // dump($this);
- foreach($this->getTranslations() as $t) {
- // dump($t);
- $slugToken = explode("-",$t->getSlug());
- // dump($slugToken);
- if (end($slugToken) != $this->getId()) {
- // echo "Regenera";
- $t->setSlug($t->generateSlugValue([$t->getTitulo()]));
- }
- // dump($this);
- // die();
- }
- }
- /*public function tituloSlugValidos(ExecutionContext $context)
- {
- $titulo = $this->translate()->getTitulo();
- $slug =$this->translate()->getSlug();
- if(empty($titulo) or empty($slug))
- {
- $context->addViolationAt('titulo', 'Introduce un título válido', array(), null);
- }
- }*/
- public function isPublicado(): bool
- {
- return $this->getEstado() == self::ESTADO_PUBLICADO;
- }
- public function getError()
- {
- return $this->error;
- }
- public function setError($error): self
- {
- $this->error = $error;
- return $this;
- }
- public function getClave(): ?string
- {
- return $this->clave;
- }
- public function setClave(string $clave): self
- {
- $this->clave = $clave;
- return $this;
- }
- public function getPosition(): ?int
- {
- return $this->position;
- }
- public function setPosition(int $position): self
- {
- $this->position = $position;
- return $this;
- }
- }