Advertisement
Guest User

Untitled

a guest
Feb 20th, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.80 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Repo\RepoBundle\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.  * RepoFile
  11.  * @ORM\Entity(repositoryClass="Repo\RepoBundle\Entity\Repository\RepoFileRepository")
  12.  * @ORM\Table(name="repo_files")
  13.  * @ORM\HasLifecycleCallbacks()
  14.  */
  15. class RepoFile {
  16.  
  17.     // a property used temporarily while deleting
  18.     private $filenameForRemove;
  19.  
  20.     /**
  21.      * @Assert\File(maxSize="6000000")
  22.      */
  23.     public $file;
  24.  
  25.     /**
  26.      * @var integer
  27.      *
  28.      * @ORM\Column(name="id", type="integer", nullable=false)
  29.      * @ORM\Id
  30.      * @ORM\GeneratedValue(strategy="IDENTITY")
  31.      */
  32.     private $id;
  33.  
  34.     /**
  35.      * @var string
  36.      *
  37.      * @ORM\Column(name="filename", type="string", length=255, nullable=false)
  38.      */
  39.     private $filename;
  40.  
  41.     /**
  42.      * @ORM\ManyToOne(targetEntity="User", inversedBy="files")
  43.      * @ORM\JoinColumn(name="author", referencedColumnName="id")
  44.      */
  45.     private $user;
  46.  
  47.     /**
  48.      * @var string
  49.      *
  50.      * @ORM\Column(name="sha1", type="string", length=50, nullable=false)
  51.      */
  52.     private $sha1;
  53.  
  54.     /**
  55.      * @var integer
  56.      *
  57.      * @ORM\Column(name="author", type="integer", nullable=false)
  58.      */
  59.     public $author;
  60.  
  61.     /**
  62.      * @var integer
  63.      *
  64.      * @ORM\Column(name="downloads", type="integer", nullable=false)
  65.      */
  66.     private $downloads;
  67.  
  68.     /**
  69.      * @var integer
  70.      *
  71.      * @ORM\Column(name="views", type="integer", nullable=false, options={"default" = 0})
  72.      */
  73.     private $views;
  74.  
  75.     /**
  76.      * @var integer
  77.      *
  78.      * @ORM\Column(name="created", type="integer", nullable=false, options={"default" = 0})
  79.      */
  80.     private $created;
  81.  
  82.     /**
  83.      * @var integer
  84.      *
  85.      * @ORM\Column(name="updated", type="integer", nullable=false)
  86.      */
  87.     private $updated;
  88.  
  89.     /**
  90.      * @var integer
  91.      *
  92.      * @ORM\Column(name="votes", type="integer", nullable=false, options={"default" = 0})
  93.      */
  94.     private $votes;
  95.  
  96.     /**
  97.      * Get id
  98.      *
  99.      * @return integer
  100.      */
  101.     public function getId() {
  102.         return $this->id;
  103.     }
  104.  
  105.     /**
  106.      * Set filename
  107.      *
  108.      * @param string $filename
  109.      * @return RepoFile
  110.      */
  111.     public function setFilename($filename) {
  112.         $this->filename = $filename;
  113.  
  114.         return $this;
  115.     }
  116.  
  117.     /**
  118.      * Get filename
  119.      *
  120.      * @return string
  121.      */
  122.     public function getFilename() {
  123.         return $this->filename;
  124.     }
  125.  
  126.     /**
  127.      * Set sha1
  128.      *
  129.      * @param string $sha1
  130.      * @return RepoFile
  131.      */
  132.     public function setSha1($sha1) {
  133.         $this->sha1 = $sha1;
  134.  
  135.         return $this;
  136.     }
  137.  
  138.     /**
  139.      * Get sha1
  140.      *
  141.      * @return string
  142.      */
  143.     public function getSha1() {
  144.         return $this->sha1;
  145.     }
  146.  
  147.     /**
  148.      * Set author
  149.      *
  150.      * @param integer $author
  151.      * @return RepoFile
  152.      */
  153.     public function setAuthor($author) {
  154.         $this->author = $author;
  155.  
  156.         return $this;
  157.     }
  158.  
  159.     /**
  160.      * Get author
  161.      *
  162.      * @return integer
  163.      */
  164.     public function getAuthor() {
  165.         return $this->author;
  166.     }
  167.  
  168.     /**
  169.      * Set downloads
  170.      *
  171.      * @param integer $downloads
  172.      * @return RepoFile
  173.      */
  174.     public function setDownloads($downloads) {
  175.         $this->downloads = $downloads;
  176.  
  177.         return $this;
  178.     }
  179.  
  180.     /**
  181.      * Get downloads
  182.      *
  183.      * @return integer
  184.      */
  185.     public function getDownloads() {
  186.         return $this->downloads;
  187.     }
  188.  
  189.     /**
  190.      * Set views
  191.      *
  192.      * @param integer $views
  193.      * @return RepoFile
  194.      */
  195.     public function setViews($views) {
  196.         $this->views = $views;
  197.  
  198.         return $this;
  199.     }
  200.  
  201.     /**
  202.      * Get views
  203.      *
  204.      * @return integer
  205.      */
  206.     public function getViews() {
  207.         return $this->views;
  208.     }
  209.  
  210.     /**
  211.      * Set created
  212.      *
  213.      * @param integer $created
  214.      * @return RepoFile
  215.      */
  216.     public function setCreated($created) {
  217.         $this->created = $created;
  218.  
  219.         return $this;
  220.     }
  221.  
  222.     /**
  223.      * Get created
  224.      *
  225.      * @return integer
  226.      */
  227.     public function getCreated() {
  228.         return $this->created;
  229.     }
  230.  
  231.     /**
  232.      * Set updated
  233.      *
  234.      * @param integer $updated
  235.      * @return RepoFile
  236.      */
  237.     public function setUpdated($updated) {
  238.         $this->updated = $updated;
  239.  
  240.         return $this;
  241.     }
  242.  
  243.     /**
  244.      * Get updated
  245.      *
  246.      * @return integer
  247.      */
  248.     public function getUpdated() {
  249.         return $this->updated;
  250.     }
  251.  
  252.     /**
  253.      * Set votes
  254.      *
  255.      * @param integer $votes
  256.      * @return RepoFile
  257.      */
  258.     public function setVotes($votes) {
  259.         $this->votes = $votes;
  260.  
  261.         return $this;
  262.     }
  263.  
  264.     /**
  265.      * Get votes
  266.      *
  267.      * @return integer
  268.      */
  269.     public function getVotes() {
  270.         return $this->votes;
  271.     }
  272.  
  273.     /**
  274.      * Set user
  275.      *
  276.      * @param \Repo\RepoBundle\Entity\User $user
  277.      * @return RepoFile
  278.      */
  279.     public function setUser(\Repo\RepoBundle\Entity\User $user = null) {
  280.         $this->user = $user;
  281.  
  282.         return $this;
  283.     }
  284.  
  285.     /**
  286.      * Get user
  287.      *
  288.      * @return \Repo\RepoBundle\Entity\User
  289.      */
  290.     public function getUser() {
  291.         return $this->user;
  292.     }
  293.  
  294.     ////////////////////////////
  295.  
  296.     protected function getUploadRootDir() {
  297.         // the absolute directory path where uploaded
  298.         // documents should be saved
  299.         return __DIR__ . '/../../../../web/' . $this->getUploadDir();
  300.     }
  301.  
  302.     protected function getUploadDir() {
  303.         // get rid of the __DIR__ so it doesn't screw up
  304.         // when displaying uploaded doc/image in the view.
  305.         return 'uploads/documents';
  306.     }
  307.  
  308. //    public function __set($name, $value) {
  309. //
  310. //        echo 'setting ' . $name . ' to ' . $value;
  311. //        $this->$name = $value;
  312. //        return;
  313. //    }
  314. //    public function __unset($name) {
  315. //        echo 'unsetting ' . $name ;
  316. //        unset($this->$name);
  317. //        return;
  318. //    }
  319.  
  320.     /**
  321.      * @ORM\PrePersist()
  322.      * @ORM\PreUpdate()
  323.      */
  324.     public function preUpload() {
  325.         if (null !== $this->file) {
  326.             $this->path = $this->file->guessExtension();
  327.  
  328.             $filename = sha1(uniqid(mt_rand(), true));
  329.             $this->filename = $filename . '.' . $this->file->guessExtension();
  330.             $this->sha1 = sha1($this->path);
  331.         }
  332.         $this->updated = date('Y-m-d H:m:s');
  333.         return;
  334.     }
  335.  
  336.     /**
  337.      * @ORM\PrePersist()
  338.      */
  339.     public function fixValues() {
  340.         $this->created = date('Y-m-d H:m:s');
  341.         return;
  342.     }
  343.  
  344.     /**
  345.      * @ORM\PostPersist()
  346.      * @ORM\PostUpdate()
  347.      */
  348.     public function upload() {
  349.         if (null === $this->file) {
  350.             return;
  351.         }
  352.  
  353.         // you must throw an exception here if the file cannot be moved
  354.         // so that the entity is not persisted to the database
  355.         // which the UploadedFile move() method does
  356.         $this->file->move(
  357.                 $this->getUploadRootDir(), $this->id . '.' . $this->file->guessExtension()
  358.         );
  359.  
  360.  
  361.         unset($this->file);
  362.         return;
  363.     }
  364.  
  365.     /**
  366.      * @ORM\PreRemove()
  367.      */
  368.     public function storeFilenameForRemove() {
  369.         $this->filenameForRemove = $this->getAbsolutePath();
  370.     }
  371.  
  372.     /**
  373.      * @ORM\PostRemove()
  374.      */
  375.     public function removeUpload() {
  376.         if ($this->filenameForRemove) {
  377.             unlink($this->filenameForRemove);
  378.         }
  379.     }
  380.  
  381.     public function getAbsolutePath() {
  382.         return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->id . '.' . $this->path;
  383.     }
  384.  
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement