iBet7o

Ejemplo de archivos subidos con Doctrine

Feb 19th, 2013
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.24 KB | None | 0 0
  1. <?php
  2.  
  3. namespace MundoIdeas\FrontendBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use Symfony\Component\HttpFoundation\File\UploadedFile;
  9.  
  10. use MundoIdeas\FrontendBundle\Util\Util;
  11.  
  12. /**
  13.  * Proyecto
  14.  *
  15.  * @ORM\Table(name="proyecto")
  16.  * @ORM\Entity(repositoryClass="MundoIdeas\FrontendBundle\Entity\ProyectoRepository")
  17.  * @ORM\HasLifecycleCallbacks()
  18.  */
  19. class Proyecto
  20. {
  21.     /**
  22.      * @var integer
  23.      *
  24.      * @ORM\Column(name="id", type="integer")
  25.      * @ORM\Id
  26.      * @ORM\GeneratedValue(strategy="AUTO")
  27.      */
  28.     private $id;
  29.  
  30.     /**
  31.      * @var integer
  32.      *
  33.      * @ORM\ManyToOne(targetEntity="MundoIdeas\FrontendBundle\Entity\Usuario")
  34.      */
  35.     private $usuario;
  36.  
  37.     /**
  38.      * @var integer
  39.      *
  40.      * @ORM\ManyToOne(targetEntity="MundoIdeas\FrontendBundle\Entity\Categoria")
  41.      */
  42.     private $categoria;
  43.  
  44.     /**
  45.      * @var string
  46.      *
  47.      * @ORM\Column(name="nombre", type="string", length=100, unique=true)
  48.      */
  49.     private $nombre;
  50.  
  51.     /**
  52.      * @var string
  53.      *
  54.      * @ORM\Column(name="slug", type="string", length=255)
  55.      */
  56.     private $slug;
  57.  
  58.     /**
  59.      * @var string
  60.      *
  61.      * @ORM\Column(name="descripcion_corta", type="string", length=255)
  62.      */
  63.     private $descripcion_corta;
  64.  
  65.     /**
  66.      * @var string
  67.      *
  68.      * @ORM\Column(name="descripcion", type="text")
  69.      */
  70.     private $descripcion;
  71.  
  72.     /**
  73.      * @var string
  74.      *
  75.      * @ORM\Column(name="imagen", type="string", length=100)
  76.      */
  77.     private $imagen;
  78.  
  79.     /**
  80.      * @ORM\OneToMany(targetEntity="MundoIdeas\FrontendBundle\Entity\ProyectoImagen", mappedBy="proyecto", cascade={"persist"})
  81.      */
  82.     private $imagenes;
  83.  
  84.     /**
  85.      * @var integer
  86.      *
  87.      * @ORM\Column(name="estado", type="smallint")
  88.      */
  89.     private $estado;
  90.  
  91.     /**
  92.      * @var \DateTime
  93.      *
  94.      * @ORM\Column(name="fecha_creacion", type="datetime")
  95.      */
  96.     private $fecha_creacion;
  97.  
  98.     /**
  99.      * @var \DateTime
  100.      *
  101.      * @ORM\Column(name="fecha_autorizacion", type="datetime", nullable=true)
  102.      */
  103.     private $fecha_autorizacion;
  104.  
  105.     /**
  106.      * @var \DateTime
  107.      *
  108.      * @ORM\Column(name="fecha_modificacion", type="datetime", nullable=true)
  109.      */
  110.     private $fecha_modificacion;
  111.  
  112.     /**
  113.      * @var string
  114.      *
  115.      * @Assert\Image(maxSize="500k")
  116.      */
  117.     private $_file_image;
  118.  
  119.     /**
  120.      * Si se actualiza la imagen almacena la ruta absoluta de la imagen
  121.      * original para eliminarla.
  122.      *
  123.      * @var string
  124.      */
  125.     private $_pre_imagen;
  126.  
  127.     /**
  128.      * Constructor
  129.      */
  130.     public function __construct()
  131.     {
  132.         $this->imagenes = new ArrayCollection();
  133.     }
  134.  
  135.     /**
  136.      * @ORM\PrePersist
  137.      */
  138.     public function setCreatedValue()
  139.     {
  140.         $this->setEstado(3); // pendiente de autorización.
  141.         $this->setFechaCreacion(new \DateTime());
  142.     }
  143.  
  144.     /**
  145.      * @ORM\PrePersist()
  146.      * @ORM\PreUpdate()
  147.      */
  148.     public function preUpload()
  149.     {
  150.         if (NULL !== $this->_file_image)
  151.         {
  152.             $this->_pre_imagen = $this->getAbsolutePath();
  153.             $this->imagen = $this->getSlug().'.'.$this->_file_image->guessExtension();
  154.         }
  155.     }
  156.  
  157.     /**
  158.      * @ORM\PostPersist()
  159.      * @ORM\PostUpdate()
  160.      */
  161.     public function upload()
  162.     {
  163.         if (NULL === $this->_file_image)
  164.         {
  165.             return;
  166.         }
  167.  
  168.         // si hay un error al mover el archivo, move() automáticamente
  169.         // envía una excepción. Esta impedirá que la entidad se persista
  170.         // en la base de datos en caso de error
  171.         $this->_file_image->move($this->getUploadRootDir(), $this->imagen);
  172.  
  173.         if (is_file($this->_pre_imagen))
  174.         {
  175.             unlink($this->_pre_imagen);
  176.         }
  177.  
  178.         unset($this->_file_image, $this->_pre_imagen);
  179.     }
  180.  
  181.     /**
  182.      * @ORM\PostRemove()
  183.      */
  184.     public function removeUpload()
  185.     {
  186.         if ($file = $this->getAbsolutePath())
  187.         {
  188.             unlink($file);
  189.         }
  190.     }
  191.  
  192.     /**
  193.      * Get id
  194.      *
  195.      * @return integer
  196.      */
  197.     public function getId()
  198.     {
  199.         return $this->id;
  200.     }
  201.  
  202.     /**
  203.      * Set usuario
  204.      *
  205.      * @param integer $usuario
  206.      * @return Proyecto
  207.      */
  208.     public function setUsuario(\MundoIdeas\FrontendBundle\Entity\Usuario $usuario)
  209.     {
  210.         $this->usuario = $usuario;
  211.    
  212.         return $this;
  213.     }
  214.  
  215.     /**
  216.      * Get usuario
  217.      *
  218.      * @return integer
  219.      */
  220.     public function getUsuario()
  221.     {
  222.         return $this->usuario;
  223.     }
  224.  
  225.     /**
  226.      * Set categoria
  227.      *
  228.      * @param integer $categoria
  229.      * @return Proyecto
  230.      */
  231.     public function setCategoria(\MundoIdeas\FrontendBundle\Entity\Categoria $categoria)
  232.     {
  233.         $this->categoria = $categoria;
  234.    
  235.         return $this;
  236.     }
  237.  
  238.     /**
  239.      * Get categoria
  240.      *
  241.      * @return integer
  242.      */
  243.     public function getCategoria()
  244.     {
  245.         return $this->categoria;
  246.     }
  247.  
  248.     /**
  249.      * Set nombre
  250.      *
  251.      * @param string $nombre
  252.      * @return Proyecto
  253.      */
  254.     public function setNombre($nombre)
  255.     {
  256.         $this->nombre = $nombre;
  257.         $this->slug = Util::getSlug($nombre);
  258.  
  259.         return $this;
  260.     }
  261.  
  262.     /**
  263.      * Get nombre
  264.      *
  265.      * @return string
  266.      */
  267.     public function getNombre()
  268.     {
  269.         return $this->nombre;
  270.     }
  271.  
  272.     /**
  273.      * Set slug
  274.      *
  275.      * @param string $slug
  276.      * @return Proyecto
  277.      */
  278.     public function setSlug($slug)
  279.     {
  280.         $this->slug = $slug;
  281.    
  282.         return $this;
  283.     }
  284.  
  285.     /**
  286.      * Get slug
  287.      *
  288.      * @return string
  289.      */
  290.     public function getSlug()
  291.     {
  292.         return $this->slug;
  293.     }
  294.  
  295.     /**
  296.      * Set descripcion_corta
  297.      *
  298.      * @param string $descripcionCorta
  299.      * @return Proyecto
  300.      */
  301.     public function setDescripcionCorta($descripcionCorta)
  302.     {
  303.         $this->descripcion_corta = $descripcionCorta;
  304.    
  305.         return $this;
  306.     }
  307.  
  308.     /**
  309.      * Get descripcion_corta
  310.      *
  311.      * @return string
  312.      */
  313.     public function getDescripcionCorta()
  314.     {
  315.         return $this->descripcion_corta;
  316.     }
  317.  
  318.     /**
  319.      * Set descripcion
  320.      *
  321.      * @param string $descripcion
  322.      * @return Proyecto
  323.      */
  324.     public function setDescripcion($descripcion)
  325.     {
  326.         $this->descripcion = $descripcion;
  327.    
  328.         return $this;
  329.     }
  330.  
  331.     /**
  332.      * Get descripcion
  333.      *
  334.      * @return string
  335.      */
  336.     public function getDescripcion()
  337.     {
  338.         return $this->descripcion;
  339.     }
  340.  
  341.     /**
  342.      * Set imagen
  343.      *
  344.      * @param string $imagen
  345.      * @return Proyecto
  346.      */
  347.     public function setImagen($imagen)
  348.     {
  349.         $this->imagen = $imagen;
  350.    
  351.         return $this;
  352.     }
  353.  
  354.     /**
  355.      * Get imagen
  356.      *
  357.      * @return string
  358.      */
  359.     public function getImagen()
  360.     {
  361.         return $this->imagen;
  362.     }
  363.  
  364.     /**
  365.      * Set estado
  366.      *
  367.      * @param integer $estado
  368.      * @return Proyecto
  369.      */
  370.     public function setEstado($estado)
  371.     {
  372.         $this->estado = $estado;
  373.    
  374.         return $this;
  375.     }
  376.  
  377.     /**
  378.      * Get estado
  379.      *
  380.      * @return integer
  381.      */
  382.     public function getEstado()
  383.     {
  384.         return $this->estado;
  385.     }
  386.  
  387.     /**
  388.      * Set fecha_autorizacion
  389.      *
  390.      * @param \DateTime $fechaAutorizacion
  391.      * @return Proyecto
  392.      */
  393.     public function setFechaAutorizacion($fechaAutorizacion)
  394.     {
  395.         $this->fecha_autorizacion = $fechaAutorizacion;
  396.    
  397.         return $this;
  398.     }
  399.  
  400.     /**
  401.      * Get fecha_autorizacion
  402.      *
  403.      * @return \DateTime
  404.      */
  405.     public function getFechaAutorizacion()
  406.     {
  407.         return $this->fecha_autorizacion;
  408.     }
  409.  
  410.     /**
  411.      * Set fecha_creacion
  412.      *
  413.      * @param \DateTime $$fechaCreacion
  414.      * @return Proyecto
  415.      */
  416.     public function setFechaCreacion($fechaCreacion)
  417.     {
  418.         $this->fecha_creacion = $fechaCreacion;
  419.    
  420.         return $this;
  421.     }
  422.  
  423.     /**
  424.      * Get fecha_creacion
  425.      *
  426.      * @return \DateTime
  427.      */
  428.     public function getFechaCreacion()
  429.     {
  430.         return $this->fecha_creacion;
  431.     }
  432.  
  433.     /**
  434.      * Set fecha_modificacion
  435.      *
  436.      * @param \DateTime $fechaModificacion
  437.      * @return Proyecto
  438.      */
  439.     public function setFechaModificacion($fechaModificacion)
  440.     {
  441.         $this->fecha_modificacion = $fechaModificacion;
  442.    
  443.         return $this;
  444.     }
  445.  
  446.     /**
  447.      * Get fecha_modificacion
  448.      *
  449.      * @return \DateTime
  450.      */
  451.     public function getFechaModificacion()
  452.     {
  453.         return $this->fecha_modificacion;
  454.     }
  455.  
  456.     /**
  457.      * Set _file_imagen
  458.      *
  459.      * @param string $_file_image
  460.      * @return Proyecto
  461.      */
  462.     public function set_FileImage($fileImage)
  463.     {
  464.         $this->_file_image = $fileImage;
  465.  
  466.         return $this;
  467.     }
  468.  
  469.     /**
  470.      * Get imagen
  471.      *
  472.      * @return string
  473.      */
  474.     public function get_FileImage()
  475.     {
  476.         return $this->_file_image;
  477.     }
  478.  
  479.     /**
  480.      * Set _file_imagen
  481.      *
  482.      * @param string $_file_image
  483.      * @return Proyecto
  484.      */
  485.     public function set_PreImagen($preImagen)
  486.     {
  487.         $this->_pre_imagen = $preImagen;
  488.  
  489.         return $this;
  490.     }
  491.  
  492.     /**
  493.      * Get imagen
  494.      *
  495.      * @return string
  496.      */
  497.     public function get_PreImagen()
  498.     {
  499.         return $this->_pre_imagen;
  500.     }
  501.  
  502.     public function __toString()
  503.     {
  504.         return $this->getNombre();
  505.     }
  506.  
  507.     /**
  508.      * Add imagenes
  509.      *
  510.      * @param \MundoIdeas\FrontendBundle\Entity\ProyectoImagen $imagenes
  511.      * @return Proyecto
  512.      */
  513.     public function addImagen(\MundoIdeas\FrontendBundle\Entity\ProyectoImagen $imagenes)
  514.     {
  515.         $imagenes->setProyecto($this);
  516.  
  517.         $this->imagenes[] = $imagenes;
  518.  
  519.         return $this;
  520.     }
  521.  
  522.     /**
  523.      * Remove imagenes
  524.      *
  525.      * @param \MundoIdeas\FrontendBundle\Entity\ProyectoImagen $imagenes
  526.      */
  527.     public function removeImagen(\MundoIdeas\FrontendBundle\Entity\ProyectoImagen $imagenes)
  528.     {
  529.         $this->imagenes->removeElement($imagenes);
  530.     }
  531.  
  532.     /**
  533.      * Get imagenes
  534.      *
  535.      * @return \Doctrine\Common\Collections\Collection
  536.      */
  537.     public function getImagenes()
  538.     {
  539.         return $this->imagenes;
  540.     }
  541.  
  542.     /**
  543.      * Obtiene la ruta absoluta de la imagen.
  544.      *
  545.      * @return string
  546.      */
  547.     public function getAbsolutePath()
  548.     {
  549.         return NULL === $this->imagen
  550.             ? NULL
  551.             : $this->getUploadRootDir() . '/' . $this->imagen;
  552.     }
  553.  
  554.     /**
  555.      * Obtiene la ruta absoluta del directorio donde se
  556.      * deben guardar los archivos cargados.
  557.      *
  558.      * @return string
  559.      */
  560.     protected function getUploadRootDir()
  561.     {
  562.         return __DIR__.'/../../../../web/'.$this->getUploadDir();
  563.     }
  564.  
  565.     /**
  566.      * Ruta dentro de la carpeta web donde se guardan los
  567.      * archivos cargados.
  568.      *
  569.      * @return string
  570.      */
  571.     protected function getUploadDir()
  572.     {
  573.         return 'uploads/images/projects';
  574.     }
  575. }
Advertisement
Add Comment
Please, Sign In to add comment