Guest User

Untitled

a guest
Feb 20th, 2013
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.27 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. // DON'T forget this use statement!!!
  9. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  10.  
  11.  
  12. /**
  13.  * RepoFile
  14.  * @ORM\Entity(repositoryClass="Repo\RepoBundle\Entity\Repository\RepoFileRepository")
  15.  * @ORM\Table(name="repo_files")
  16.  * @UniqueEntity("slug")
  17.  * @UniqueEntity("sha1")
  18.  * @ORM\HasLifecycleCallbacks()
  19.  
  20.  */
  21. class RepoFile {
  22.  
  23.  
  24.     // a property used temporarily while deleting
  25.     private $filenameForRemove;
  26.  
  27.     /**
  28.      * @Assert\File(maxSize="6000000", mimeTypes={"application/xml", "text/xml"})
  29.      */
  30.     public $file;
  31.  
  32.     /**
  33.      * @var integer
  34.      *
  35.      * @ORM\Column(name="id", type="integer", nullable=false)
  36.      * @ORM\Id
  37.      * @ORM\GeneratedValue(strategy="IDENTITY")
  38.      */
  39.     private $id;
  40.  
  41.     /**
  42.      * @var string
  43.      *
  44.      * @ORM\Column(name="title", type="string", length=255, nullable=false)
  45.      */
  46.     private $title;
  47.  
  48.     /**
  49.      * @var string
  50.      *
  51.      * @ORM\Column(name="slug", type="string", length=255, nullable=false, unique = true)
  52.      */
  53.     private $slug;
  54.  
  55.     /**
  56.      * @ORM\ManyToOne(targetEntity="User", inversedBy="files")
  57.      * @ORM\JoinColumn(name="author", referencedColumnName="id")
  58.      */
  59.     private $user;
  60.  
  61.     /**
  62.      * @var string
  63.      *
  64.      * @ORM\Column(name="sha1", type="string", length=50, nullable=false, unique = true)
  65.      */
  66.     private $sha1;
  67.  
  68.     /**
  69.      * @var integer
  70.      *
  71.      * @ORM\Column(name="author", type="integer", nullable=false)
  72.      */
  73.     private $author;
  74.  
  75.     /**
  76.      * @var integer
  77.      *
  78.      * @ORM\Column(name="downloads", type="integer", nullable=false, options={"default" = 0})
  79.      */
  80.     private $downloads;
  81.  
  82.     /**
  83.      * @var integer
  84.      *
  85.      * @ORM\Column(name="views", type="integer", nullable=false, options={"default" = 0})
  86.      */
  87.     private $views;
  88.  
  89.     /**
  90.      * @var integer
  91.      *
  92.      * @ORM\Column(name="created", type="integer", nullable=false, options={"default" = 0})
  93.      */
  94.     private $created;
  95.  
  96.     /**
  97.      * @var integer
  98.      *
  99.      * @ORM\Column(name="updated", type="integer", nullable=false)
  100.      */
  101.     private $updated;
  102.  
  103.     /**
  104.      * @var integer
  105.      *
  106.      * @ORM\Column(name="votes", type="integer", nullable=false, options={"default" = 0})
  107.      */
  108.     private $votes;
  109.  
  110.     /**
  111.      * Get id
  112.      *
  113.      * @return integer
  114.      */
  115.     public function getId() {
  116.         return $this->id;
  117.     }
  118.  
  119.     /**
  120.      * Set author
  121.      *
  122.      * @param integer $author
  123.      * @return RepoFile
  124.      */
  125.     public function setAuthor($author) {
  126.         $this->author = $author;
  127.  
  128.         return $this;
  129.     }
  130.  
  131.     /**
  132.      * Get author
  133.      *
  134.      * @return integer
  135.      */
  136.     public function getAuthor() {
  137.         return $this->author;
  138.     }
  139.  
  140.     /**
  141.      * Set downloads
  142.      *
  143.      * @param integer $downloads
  144.      * @return RepoFile
  145.      */
  146.     public function setDownloads($downloads) {
  147.         $this->downloads = $downloads;
  148.  
  149.         return $this;
  150.     }
  151.  
  152.     /**
  153.      * Get downloads
  154.      *
  155.      * @return integer
  156.      */
  157.     public function getDownloads() {
  158.         return $this->downloads;
  159.     }
  160.  
  161.     /**
  162.      * Set views
  163.      *
  164.      * @param integer $views
  165.      * @return RepoFile
  166.      */
  167.     public function setViews($views) {
  168.         $this->views = $views;
  169.  
  170.         return $this;
  171.     }
  172.  
  173.     /**
  174.      * Get views
  175.      *
  176.      * @return integer
  177.      */
  178.     public function getViews() {
  179.         return $this->views;
  180.     }
  181.  
  182.     /**
  183.      * Set created
  184.      *
  185.      * @param integer $created
  186.      * @return RepoFile
  187.      */
  188.     public function setCreated($created) {
  189.         $this->created = $created;
  190.  
  191.         return $this;
  192.     }
  193.  
  194.     /**
  195.      * Get created
  196.      *
  197.      * @return integer
  198.      */
  199.     public function getCreated() {
  200.         return $this->created;
  201.     }
  202.  
  203.     /**
  204.      * Set updated
  205.      *
  206.      * @param integer $updated
  207.      * @return RepoFile
  208.      */
  209.     public function setUpdated($updated) {
  210.         $this->updated = $updated;
  211.  
  212.         return $this;
  213.     }
  214.  
  215.     /**
  216.      * Get updated
  217.      *
  218.      * @return integer
  219.      */
  220.     public function getUpdated() {
  221.         return $this->updated;
  222.     }
  223.  
  224.     /**
  225.      * Set votes
  226.      *
  227.      * @param integer $votes
  228.      * @return RepoFile
  229.      */
  230.     public function setVotes($votes) {
  231.         $this->votes = $votes;
  232.  
  233.         return $this;
  234.     }
  235.  
  236.     /**
  237.      * Get votes
  238.      *
  239.      * @return integer
  240.      */
  241.     public function getVotes() {
  242.         return $this->votes;
  243.     }
  244.  
  245.     /**
  246.      * Set user
  247.      *
  248.      * @param \Repo\RepoBundle\Entity\User $user
  249.      * @return RepoFile
  250.      */
  251.     public function setUser(\Repo\RepoBundle\Entity\User $user = null) {
  252.         $this->user = $user;
  253.  
  254.         return $this;
  255.     }
  256.  
  257.     /**
  258.      * Get user
  259.      *
  260.      * @return \Repo\RepoBundle\Entity\User
  261.      */
  262.     public function getUser() {
  263.         return $this->user;
  264.     }
  265.  
  266.     ////////////////////////////
  267.  
  268.     protected function getUploadRootDir() {
  269.         // the absolute directory path where uploaded
  270.         // documents should be saved
  271.         return __DIR__ . '/../../../../web/' . $this->getUploadDir();
  272.     }
  273.  
  274.     protected function getUploadDir() {
  275.         // get rid of the __DIR__ so it doesn't screw up
  276.         // when displaying uploaded doc/image in the view.
  277.         return $this->upload_dir;
  278.     }
  279.  
  280.     public function setUploadDir($uploadDir) {
  281.         $this->upload_dir = $uploadDir;
  282.         return $this;
  283.     }
  284.  
  285. //    public function __set($name, $value) {
  286. //
  287. //        echo 'setting ' . $name . ' to ' . $value;
  288. //        $this->$name = $value;
  289. //        return;
  290. //    }
  291. //    public function __unset($name) {
  292. //        echo 'unsetting ' . $name ;
  293. //        unset($this->$name);
  294. //        return;
  295. //    }
  296.  
  297.     /**
  298.      * @ORM\PrePersist()
  299.      * @ORM\PreUpdate()
  300.      */
  301.     public function preUpload() {
  302.         if (null !== $this->file) {
  303.             $this->path = $this->file->guessExtension();
  304.             $this->sha1 = sha1($this->path);
  305.         }
  306.         $this->updated = time();
  307.         return;
  308.     }
  309.  
  310.     /**
  311.      * @ORM\PrePersist()
  312.      */
  313.     public function fixValues() {
  314.         $this->created = time();
  315.         $this->downloads = 0;
  316.         $this->views = 0;
  317.         $this->votes = 0;
  318.         $this->setTitle('Some idipo2958932856(*#(*$**@(@%333- -123=4-2title');
  319.         return;
  320.     }
  321.  
  322.     /**
  323.      * @ORM\PostPersist()
  324.      * @ORM\PostUpdate()
  325.      */
  326.     public function upload() {
  327.         if (null === $this->file) {
  328.             return;
  329.         }
  330.  
  331.         // you must throw an exception here if the file cannot be moved
  332.         // so that the entity is not persisted to the database
  333.         // which the UploadedFile move() method does
  334.         $filename = $this->id . '.' . $this->file->guessExtension();
  335.         $this->file->move(
  336.                 $this->getUploadRootDir(), $filename
  337.         );
  338.         unset($this->file);
  339.         return;
  340.     }
  341.  
  342.     /**
  343.      * @ORM\PreRemove()
  344.      */
  345.     public function storeFilenameForRemove() {
  346.         $this->filenameForRemove = $this->getAbsolutePath();
  347.     }
  348.  
  349.     /**
  350.      * @ORM\PostRemove()
  351.      */
  352.     public function removeUpload() {
  353.         if ($this->filenameForRemove) {
  354.             unlink($this->filenameForRemove);
  355.         }
  356.     }
  357.  
  358.     public function getAbsolutePath() {
  359.         return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->id . '.' . $this->path;
  360.     }
  361.  
  362.     public function slugify($text) {
  363.         // replace non letter or digits by -
  364.         $text = preg_replace('#[^\\pL\d]+#u', '-', $text);
  365.  
  366.         // trim
  367.         $text = trim($text, '-');
  368.  
  369.         // transliterate
  370.         if (function_exists('iconv')) {
  371.             $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  372.         }
  373.  
  374.         // lowercase
  375.         $text = strtolower($text);
  376.  
  377.         // remove unwanted characters
  378.         $text = preg_replace('#[^-\w]+#', '', $text);
  379.  
  380.         if (empty($text)) {
  381.             return 'n-a';
  382.         }
  383.  
  384.         return $text;
  385.     }
  386.  
  387.     /**
  388.      * Set title
  389.      *
  390.      * @param string $title
  391.      * @return RepoFile
  392.      */
  393.     public function setTitle($title) {
  394.         $this->title = $title;
  395.         $this->slug = $this->slugify($title);
  396.         return $this;
  397.     }
  398.  
  399.     /**
  400.      * Get title
  401.      *
  402.      * @return string
  403.      */
  404.     public function getTitle() {
  405.         return $this->title;
  406.     }
  407.  
  408.     /**
  409.      * Set slug
  410.      *
  411.      * @param string $slug
  412.      * @return RepoFile
  413.      */
  414.     public function setSlug($slug) {
  415.         $this->slug = $slug;
  416.  
  417.         return $this;
  418.     }
  419.  
  420.     /**
  421.      * Get slug
  422.      *
  423.      * @return string
  424.      */
  425.     public function getSlug() {
  426.         return $this->slug;
  427.     }
  428.  
  429.     /**
  430.      * Set sha1
  431.      *
  432.      * @param string $sha1
  433.      * @return RepoFile
  434.      */
  435.     public function setSha1($sha1) {
  436.         $this->sha1 = $sha1;
  437.  
  438.         return $this;
  439.     }
  440.  
  441.     /**
  442.      * Get sha1
  443.      *
  444.      * @return string
  445.      */
  446.     public function getSha1() {
  447.         return $this->sha1;
  448.     }
  449.  
  450. }
Advertisement
Add Comment
Please, Sign In to add comment