Advertisement
Guest User

Untitled

a guest
Dec 6th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.77 KB | None | 0 0
  1. <?php
  2. // src/Acme/EntityBundle/Entity/File.php
  3. namespace Acme\EntityBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use Symfony\Component\HttpFoundation\File\UploadedFile;
  8.  
  9. /**
  10.  * @ORM\Entity
  11.  * @ORM\Table(name="files")
  12.  * @ORM\HasLifecycleCallbacks
  13.  */
  14. class File
  15. {
  16.     /**
  17.      * @ORM\Id
  18.      * @ORM\Column(type="integer")
  19.      * @ORM\GeneratedValue(strategy="AUTO")
  20.      */
  21.     public $id;
  22.  
  23.     /**
  24.      * @ORM\Column(type="string", length=255)
  25.      * @Assert\NotBlank
  26.      */
  27.     public $title;
  28.  
  29.      /**
  30.      * @ORM\ManyToMany(targetEntity="\Acme\EntityBundle\Entity\Text", mappedBy="files")
  31.         odkazuje na text, ve kterem je pouzit
  32.       *
  33.       */
  34.    
  35.     private $text;
  36.    
  37.     /**
  38.      * @ORM\Column(type="string", length=255, nullable=true)
  39.      */
  40.     public $path;
  41.    
  42.     /**
  43.      * @ORM\Column(type="text", nullable=true)
  44.      */
  45.     private $content;
  46.    
  47.     /**
  48.      * @ORM\Column(type="integer")
  49.      */
  50.     protected $author;
  51.    
  52.     /**
  53.      * @ORM\Column(type="datetime")
  54.      */
  55.     protected $date;
  56.    
  57.  
  58.     /**
  59.      * @Assert\File(maxSize="6000000")
  60.      */
  61.     private $file;
  62.      
  63.     /**
  64.      * @ORM\Column(type="text", nullable=true)
  65.      */
  66.     private $uploadDir;
  67.    
  68.     private $temp;
  69.    
  70.    
  71.     // set default upload dir
  72.     public function __construct() {
  73.         $this->setUploadDir();
  74.     }
  75.    
  76.         public function getAbsolutePath()
  77.     {
  78.         return null === $this->path
  79.             ? null
  80.             : $this->getUploadRootDir().'/'.$this->path;
  81.     }
  82.  
  83.     public function getWebPath()
  84.     {
  85.         return null === $this->path
  86.             ? null
  87.             : $this->getUploadDir().'/'.$this->path;
  88.     }
  89.  
  90.     protected function getUploadRootDir()
  91.     {
  92.         // the absolute directory path where uploaded
  93.         // documents should be saved
  94.         return __DIR__.'/../../../../web/'.$this->getUploadDir();
  95.     }
  96.  
  97.     public function setUploadDir($uploadDir = 'uploads/files')
  98.     {
  99.         $this->uploadDir = $uploadDir;
  100.         return $this;
  101.     }
  102.    
  103.     public function getUploadDir()
  104.     {
  105.         // get rid of the __DIR__ so it doesn't screw up
  106.         // when displaying uploaded doc/image in the view.
  107.         return $this->uploadDir;
  108.     }
  109.    
  110.     /**
  111.      * Sets file.
  112.      *
  113.      * @param UploadedFile $file
  114.      */
  115.     public function setFile(UploadedFile $file = null)
  116.     {
  117.         $this->file = $file;
  118.         // check if we have an old image path
  119.         if (isset($this->path)) {
  120.             // store the old name to delete after the update
  121.             $this->temp = $this->path;
  122.             $this->path = null;
  123.         } else {
  124.             $this->path = 'initial';
  125.         }
  126.     }
  127.  
  128.     /**
  129.      * Get file.
  130.      *
  131.      * @return UploadedFile
  132.      */
  133.     public function getFile()
  134.     {
  135.         return $this->file;
  136.     }
  137.    
  138.     /**
  139.      * @ORM\PrePersist()
  140.      * @ORM\PreUpdate()
  141.      */
  142.     public function preUpload()
  143.     {
  144.         if (null !== $this->getFile()) {
  145.             // do whatever you want to generate a unique name
  146.             $filename = sha1(uniqid(mt_rand(), true));
  147.             $this->path = $filename.'.'.$this->getFile()->guessExtension();
  148.         }
  149.     }
  150. /**
  151.      * @ORM\PostPersist()
  152.      * @ORM\PostUpdate()
  153.      */
  154.     public function upload()
  155.     {
  156.         if (null === $this->getFile()) {
  157.             return;
  158.         }
  159.  
  160.         // if there is an error when moving the file, an exception will
  161.         // be automatically thrown by move(). This will properly prevent
  162.         // the entity from being persisted to the database on error
  163.         $this->getFile()->move($this->getUploadRootDir(), $this->path);
  164.  
  165.         // check if we have an old image
  166.         if (isset($this->temp)) {
  167.             // delete the old image
  168.             unlink($this->getUploadRootDir().'/'.$this->temp);
  169.             // clear the temp image path
  170.             $this->temp = null;
  171.         }
  172.         $this->file = null;
  173.     }
  174.    
  175.     /**
  176.      * @ORM\PostRemove()
  177.      */
  178.     public function removeUpload()
  179.     {
  180.         if ($file = $this->getAbsolutePath()) {
  181.             unlink($file);
  182.         }
  183.     }
  184.  
  185.     /**
  186.      * Get id
  187.      *
  188.      * @return integer
  189.      */
  190.     public function getId()
  191.     {
  192.         return $this->id;
  193.     }
  194.  
  195.     /**
  196.      * Set name
  197.      *
  198.      * @param string $title
  199.      * @return File
  200.      */
  201.     public function setTitle($title)
  202.     {
  203.         $this->title = $title;
  204.    
  205.         return $this;
  206.     }
  207.  
  208.     /**
  209.      * Get title
  210.      *
  211.      * @return string
  212.      */
  213.     public function getTitle()
  214.     {
  215.         return $this->title;
  216.     }
  217.  
  218.     /**
  219.      * Set path
  220.      *
  221.      * @param string $path
  222.      * @return File
  223.      */
  224.     public function setPath($path)
  225.     {
  226.         $this->path = $path;
  227.    
  228.         return $this;
  229.     }
  230.  
  231.     /**
  232.      * Get path
  233.      *
  234.      * @return string
  235.      */
  236.     public function getPath()
  237.     {
  238.         return $this->path;
  239.     }
  240.  
  241.     /**
  242.      * Set content
  243.      *
  244.      * @param string $content
  245.      * @return File
  246.      */
  247.     public function setContent($content)
  248.     {
  249.         $this->content = $content;
  250.    
  251.         return $this;
  252.     }
  253.  
  254.     /**
  255.      * Get content
  256.      *
  257.      * @return string
  258.      */
  259.     public function getContent()
  260.     {
  261.         return $this->content;
  262.     }
  263.  
  264.     /**
  265.      * Set author
  266.      *
  267.      * @param integer $author
  268.      * @return File
  269.      */
  270.     public function setAuthor($author)
  271.     {
  272.         $this->author = $author;
  273.    
  274.         return $this;
  275.     }
  276.  
  277.     /**
  278.      * Get author
  279.      *
  280.      * @return integer
  281.      */
  282.     public function getAuthor()
  283.     {
  284.         return $this->author;
  285.     }
  286.  
  287.     /**
  288.      * Set date
  289.      *
  290.      * @param \DateTime $date
  291.      * @return File
  292.      */
  293.     public function setDate($date)
  294.     {
  295.         $this->date = $date;
  296.    
  297.         return $this;
  298.     }
  299.  
  300.     /**
  301.      * Get date
  302.      *
  303.      * @return \DateTime
  304.      */
  305.     public function getDate()
  306.     {
  307.         return $this->date;
  308.     }
  309.  
  310.    
  311.  
  312.     /**
  313.      * Add text
  314.      *
  315.      * @param \Acme\EntityBundle\Entity\Text $text
  316.      * @return File
  317.      */
  318.     public function addText(\Acme\EntityBundle\Entity\Text $text)
  319.     {
  320.         $this->text[] = $text;
  321.    
  322.         return $this;
  323.     }
  324.  
  325.     /**
  326.      * Remove text
  327.      *
  328.      * @param \Acme\EntityBundle\Entity\Text $text
  329.      */
  330.     public function removeText(\Acme\EntityBundle\Entity\Text $text)
  331.     {
  332.         $this->text->removeElement($text);
  333.     }
  334.  
  335.     /**
  336.      * Get text
  337.      *
  338.      * @return \Doctrine\Common\Collections\Collection
  339.      */
  340.     public function getText()
  341.     {
  342.         return $this->text;
  343.     }
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement