Guest User

Untitled

a guest
Sep 11th, 2011
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.67 KB | None | 0 0
  1. <?php
  2. namespace Site\MonBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Component\Validator\Constraints as Assert;
  5. use Symfony\Component\HttpFoundation\File\UploadedFile;
  6.  
  7. /**
  8.  * @ORM\Entity
  9.  * @ORM\HasLifecycleCallbacks
  10.  */
  11. class Photo
  12. {
  13.     /**
  14.      * @ORM\GeneratedValue
  15.      * @ORM\Id
  16.      * @ORM\Column(type="integer")
  17.      */
  18.     private $id;
  19.    
  20.     /**
  21.      * @ORM\Column(type="string",length="255")
  22.      */    
  23.     private $nom;
  24.    
  25.     /**
  26.      * @Assert\File(maxSize="6000000")
  27.      * @ORM\Column(type="string", length=255, nullable=true)
  28.      */
  29.     public $file;
  30.    
  31.     /**
  32.      * @ORM\Column(type="string", length=255, nullable=true)
  33.      */
  34.     public $path;
  35.  
  36.     /**
  37.      * @ORM\Column(type="integer")
  38.      */
  39.      private $ordre;
  40.    
  41.     /**
  42.      * @ORM\Column(type="string",length="1")
  43.      * @Assert\NotBlank()
  44.      * @Assert\Choice(choices = {"B", "P", "C"})
  45.      */    
  46.      // B : Brouillon - P : PubliĆ© - C : Corbeille
  47.     private $etat;
  48.    
  49.     /**
  50.      * @ORM\ManyToOne(targetEntity="Bien")
  51.      */    
  52.     private $bien;
  53.  
  54.     /**
  55.      * Get id
  56.      *
  57.      * @return integer
  58.      */
  59.     public function getId()
  60.     {
  61.         return $this->id;
  62.     }
  63.  
  64.     /**
  65.      * Set nom
  66.      *
  67.      * @param string $nom
  68.      */
  69.     public function setNom($nom)
  70.     {
  71.         $this->nom = $nom;
  72.     }
  73.  
  74.     /**
  75.      * Get nom
  76.      *
  77.      * @return string
  78.      */
  79.     public function getNom()
  80.     {
  81.         return $this->nom;
  82.     }
  83.  
  84.  
  85.     /**
  86.      * Set ordre
  87.      *
  88.      * @param integer $ordre
  89.      */
  90.     public function setOrdre($ordre)
  91.     {
  92.         $this->ordre = $ordre;
  93.     }
  94.  
  95.     /**
  96.      * Get ordre
  97.      *
  98.      * @return integer
  99.      */
  100.     public function getOrdre()
  101.     {
  102.         return $this->ordre;
  103.     }
  104.  
  105.     /**
  106.      * Set etat
  107.      *
  108.      * @param string $etat
  109.      */
  110.     public function setEtat($etat)
  111.     {
  112.         $this->etat = $etat;
  113.     }
  114.  
  115.     /**
  116.      * Get etat
  117.      *
  118.      * @return string
  119.      */
  120.     public function getEtat()
  121.     {
  122.         return $this->etat;
  123.     }
  124.  
  125.     /**
  126.      * Set bien
  127.      *
  128.      * @param Site\DefiscBundle\Entity\Bien $bien
  129.      */
  130.     public function setBien(\Site\DefiscBundle\Entity\Bien $bien)
  131.     {
  132.         $this->bien = $bien;
  133.     }
  134.  
  135.     /**
  136.      * Get bien
  137.      *
  138.      * @return Site\DefiscBundle\Entity\Bien
  139.      */
  140.     public function getBien()
  141.     {
  142.         return $this->bien;
  143.     }
  144.    
  145.        
  146.     /**
  147.      * Set path
  148.      *
  149.      * @param string $path
  150.      */
  151.     public function setPath($path)
  152.     {
  153.         $this->path = $path;
  154.     }
  155.  
  156.     /**
  157.      * Get path
  158.      *
  159.      * @return string
  160.      */
  161.     public function getPath()
  162.     {
  163.         return $this->path;
  164.     }
  165.    
  166.     /**
  167.      * Set file
  168.      *
  169.      * @param string $file
  170.      */
  171.     public function setFile($file) {
  172.         $this->file = $file;
  173.     }
  174.  
  175.     /**
  176.      * Get file
  177.      *
  178.      * @return string
  179.      */
  180.     public function getFile() {
  181.         return $this->file;
  182.     }
  183.  
  184.  
  185.     public function getFullFilePath() {
  186.         return null === $this->file ? null : $this->getUploadRootDir(). $this->file;
  187.     }
  188.  
  189.     protected function getUploadRootDir() {
  190.         // the absolute directory path where uploaded documents should be saved
  191.         return $this->getTmpUploadRootDir().$this->getId()."/";
  192.     }
  193.  
  194.     protected function getTmpUploadRootDir() {
  195.         // the absolute directory path where uploaded documents should be saved
  196.         return __DIR__ . '/../../../../web/uploads/movies/';
  197.     }
  198.  
  199.     /**
  200.      * @ORM\PrePersist()
  201.      * @ORM\PreUpdate()
  202.      */
  203.     public function uploadFile() {
  204.         // the file property can be empty if the field is not required
  205.         if (null === $this->file) {
  206.             return;
  207.         }
  208.         if(!$this->id){
  209.             $this->file->move($this->getTmpUploadRootDir(), $this->file->getClientOriginalName());
  210.         }else{
  211.             $this->file->move($this->getUploadRootDir(), $this->file->getClientOriginalName());
  212.         }
  213.         $this->setFile($this->file->getClientOriginalName());
  214.     }
  215.    
  216.     /**
  217.      * @ORM\PostPersist()
  218.      */
  219.     public function moveFile()
  220.     {
  221.         if (null === $this->file) {
  222.             return;
  223.         }
  224.         if(!is_dir($this->getUploadRootDir())){
  225.             mkdir($this->getUploadRootDir());
  226.         }
  227.         copy($this->getTmpUploadRootDir().$this->file, $this->getFullFilePath());
  228.         unlink($this->getTmpUploadRootDir().$this->file);
  229.     }
  230.  
  231.     /**
  232.      * @ORM\PreRemove()
  233.      */
  234.     public function removeFile()
  235.     {
  236.         unlink($this->getFullFilePath());
  237.         rmdir($this->getUploadRootDir());
  238.     }
  239.  
  240. }
Advertisement
Add Comment
Please, Sign In to add comment