Advertisement
Garethp

Entity

Jul 7th, 2014
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.65 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Garethp\PhotosBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\HttpFoundation\File\UploadedFile;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8.  
  9. /**
  10.  * @ORM\Entity
  11.  * @ORM\HasLifecycleCallbacks
  12.  */
  13. class Photo
  14. {
  15.     /**
  16.      * @var integer
  17.      */
  18.     private $id;
  19.  
  20.     /**
  21.      * @var string
  22.      */
  23.     private $title;
  24.  
  25.     /**
  26.      * @var string
  27.      */
  28.     private $filename;
  29.  
  30.     /**
  31.      * @Assert\File(maxSize="6000000")
  32.      */
  33.     private $file;
  34.  
  35.     /**
  36.      * @var \DateTime
  37.      */
  38.     private $timestamp;
  39.  
  40.     private $temp;
  41.  
  42.     public function __construct()
  43.     {
  44.         $this->setTimestamp(new \DateTime());
  45.     }
  46.  
  47.     /**
  48.      * Get id
  49.      *
  50.      * @return integer
  51.      */
  52.     public function getId()
  53.     {
  54.         return $this->id;
  55.     }
  56.  
  57.     /**
  58.      * Set title
  59.      *
  60.      * @param string $title
  61.      * @return Photo
  62.      */
  63.     public function setTitle($title)
  64.     {
  65.         $this->title = $title;
  66.  
  67.         return $this;
  68.     }
  69.  
  70.     /**
  71.      * Get title
  72.      *
  73.      * @return string
  74.      */
  75.     public function getTitle()
  76.     {
  77.         return $this->title;
  78.     }
  79.  
  80.     /**
  81.      * Set filename
  82.      *
  83.      * @param string $filename
  84.      * @return Photo
  85.      */
  86.     public function setFilename($filename)
  87.     {
  88.         $this->filename = $filename;
  89.  
  90.         return $this;
  91.     }
  92.  
  93.     /**
  94.      * Get filename
  95.      *
  96.      * @return string
  97.      */
  98.     public function getFilename()
  99.     {
  100.         return $this->filename;
  101.     }
  102.  
  103.     public function getAbsolutePath()
  104.     {
  105.         return null === $this->filename
  106.             ? null
  107.             : $this->getUploadRootDir().'/' . $this->id . " - " . $this->filename;
  108.     }
  109.  
  110.     public function getWebPath()
  111.     {
  112.         return null === $this->filename
  113.             ? null
  114.             : $this->getUploadDir().'/' . $this->id . " - " . $this->filename;
  115.     }
  116.  
  117.     protected function getUploadRootDir()
  118.     {
  119.         // the absolute directory path where uploaded
  120.         // documents should be saved
  121.         return __DIR__.'/../../../../../../web/'.$this->getUploadDir();
  122.     }
  123.  
  124.     protected function getUploadDir()
  125.     {
  126.         // get rid of the __DIR__ so it doesn't screw up
  127.         // when displaying uploaded doc/image in the view.
  128.         return 'uploads/photos';
  129.     }
  130.  
  131.     public function upload()
  132.     {
  133.         if(null === $this->getFile())
  134.             return;
  135.  
  136.         if(isset($this->temp))
  137.         {
  138.             unlink($this->temp);
  139.             $this->temp = null;
  140.         }
  141.  
  142.         $this->getFile()->move(
  143.             $this->getUploadRootDir(),
  144.             $this->id . " - " . $this->getFile()->getClientOriginalName()
  145.         );
  146.  
  147.         $this->setFile(null);
  148.     }
  149.  
  150.     public function storeFilenameForRemove()
  151.     {
  152.         $this->temp = $this->getAbsolutePath();
  153.     }
  154.  
  155.     public function removeUpload()
  156.     {
  157.         if(isset($this->temp))
  158.         {
  159.             unlink($this->temp);
  160.         }
  161.     }
  162.  
  163.     /**
  164.      * Sets file.
  165.      *
  166.      * @param UploadedFile $file
  167.      */
  168.     public function setFile(UploadedFile $file = null)
  169.     {
  170.         $this->file = $file;
  171.  
  172.         if(is_file($this->getAbsolutePath()))
  173.         {
  174.             $this->temp = $this->getAbsolutePath();
  175.         }
  176.         else
  177.         {
  178.             $this->filename = 'initial';
  179.         }
  180.     }
  181.  
  182.     /**
  183.      * Get file.
  184.      *
  185.      * @return UploadedFile
  186.      */
  187.     public function getFile()
  188.     {
  189.         return $this->file;
  190.     }
  191.  
  192.     public function preUpload()
  193.     {
  194.         if (null !== $this->getFile()) {
  195.             $this->filename = $this->getFile()->getClientOriginalName();
  196.         }
  197.     }
  198.  
  199.     /**
  200.      * Set timestamp
  201.      *
  202.      * @param \DateTime $timestamp
  203.      * @return Photo
  204.      */
  205.     public function setTimestamp($timestamp)
  206.     {
  207.         $this->timestamp = $timestamp;
  208.  
  209.         return $this;
  210.     }
  211.  
  212.     /**
  213.      * Get timestamp
  214.      *
  215.      * @return \DateTime
  216.      */
  217.     public function getTimestamp()
  218.     {
  219.         return $this->timestamp;
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement