Advertisement
Modius22

Picture Admin

Jun 24th, 2017
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.28 KB | None | 0 0
  1. <?php
  2.  
  3. namespace AppBundle\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.  * Picture
  11.  *
  12.  * @ORM\Table(name="picture")
  13.  * @ORM\Entity(repositoryClass="AppBundle\Repository\PictureRepository")
  14.  * @ORM\HasLifecycleCallbacks
  15.  */
  16. class Picture
  17. {
  18.     /**
  19.      * @var int
  20.      *
  21.      * @ORM\Column(name="id", type="integer")
  22.      * @ORM\Id
  23.      * @ORM\GeneratedValue(strategy="AUTO")
  24.      */
  25.     private $id;
  26.  
  27.     /**
  28.      * @var string
  29.      * @Assert\File( maxSize = "1024k", mimeTypesMessage = "Please upload a valid Image")
  30.      * @ORM\Column(name="image", type="string", length=255, nullable=true, unique=true)
  31.      */
  32.     private $image;
  33.  
  34.     /**
  35.      * @var \DateTime
  36.      *
  37.      * @ORM\Column(name="stamp", type="datetime")
  38.      */
  39.     private $stamp;
  40.  
  41.     /**
  42.      * @var int
  43.      *
  44.      * @ORM\Column(name="sort", type="integer")
  45.      */
  46.     private $sort;
  47.  
  48.     /**
  49.      * @var Campground[]
  50.      * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Campground", inversedBy="picture")
  51.      */
  52.     private $campgrounds;
  53.  
  54.     /**
  55.      * Unmapped property to handle file uploads
  56.      */
  57.     private $file;
  58.  
  59.     /**
  60.      * Get id
  61.      *
  62.      * @return int
  63.      */
  64.     public function getId()
  65.     {
  66.         return $this->id;
  67.     }
  68.  
  69.     /**
  70.      * Set image
  71.      *
  72.      * @param string $image
  73.      *
  74.      * @return Picture
  75.      */
  76.     public function setImage($image)
  77.     {
  78.         $this->image = $image;
  79.  
  80.         return $this;
  81.     }
  82.  
  83.     /**
  84.      * Get image
  85.      *
  86.      * @return string
  87.      */
  88.     public function getImage()
  89.     {
  90.         return $this->image;
  91.     }
  92.  
  93.     /**
  94.      * Set stamp
  95.      *
  96.      * @param \DateTime $stamp
  97.      *
  98.      * @return Picture
  99.      */
  100.     public function setStamp($stamp)
  101.     {
  102.         $this->stamp = $stamp;
  103.  
  104.         return $this;
  105.     }
  106.  
  107.     /**
  108.      * Get stamp
  109.      *
  110.      * @return \DateTime
  111.      */
  112.     public function getStamp()
  113.     {
  114.         return $this->stamp;
  115.     }
  116.  
  117.     /**
  118.      * Set sort
  119.      *
  120.      * @param integer $sort
  121.      *
  122.      * @return Picture
  123.      */
  124.     public function setSort($sort)
  125.     {
  126.         $this->sort = $sort;
  127.  
  128.         return $this;
  129.     }
  130.  
  131.     /**
  132.      * Get sort
  133.      *
  134.      * @return int
  135.      */
  136.     public function getSort()
  137.     {
  138.         return $this->sort;
  139.     }
  140.  
  141.     /**
  142.      * Set campground
  143.      *
  144.      * @param \AppBundle\Entity\Campground $campground
  145.      *
  146.      * @return Picture
  147.      */
  148.     public function setCampground(\AppBundle\Entity\Campground $campground = null)
  149.     {
  150.         $this->campground = $campground;
  151.  
  152.         return $this;
  153.     }
  154.  
  155.     /**
  156.      * Get campground
  157.      *
  158.      * @return \AppBundle\Entity\Campground
  159.      */
  160.     public function getCampground()
  161.     {
  162.         return $this->campground;
  163.     }
  164.  
  165.     /**
  166.      * Sets file.
  167.      *
  168.      * @param UploadedFile $file
  169.      */
  170.     public function setFile(UploadedFile $file = null)
  171.     {
  172.         $this->file = $file;
  173.     }
  174.  
  175.     /**
  176.      * Get file.
  177.      *
  178.      * @return UploadedFile
  179.      */
  180.     public function getFile()
  181.     {
  182.         return $this->file;
  183.     }
  184.  
  185.     /**
  186.      * Manages the copying of the file to the relevant place on the server
  187.      */
  188.     public function upload()
  189.     {
  190.         // the file property can be empty if the field is not required
  191.         if (null === $this->getFile()) {
  192.             return;
  193.         }
  194.  
  195.         // we use the original file name here but you should
  196.         // sanitize it at least to avoid any security issues
  197.  
  198.         // move takes the target directory and target filename as params
  199.         $this->getFile()->move(
  200.             SERVER_PATH_TO_IMAGE_FOLDER,
  201.             $this->getFile()->getClientOriginalName()
  202.         );
  203.  
  204.         // set the path property to the filename where you've saved the file
  205.         $this->image = $this->getFile()->getClientOriginalName();
  206.  
  207.         // clean up the file property as you won't need it anymore
  208.         $this->setFile(null);
  209.     }
  210.  
  211.     /**
  212.      * @ORM\PrePersist
  213.      * @ORM\PreUpdate
  214.      */
  215.     public function lifecycleFileUpload() {
  216.       $this->upload();
  217.     }
  218.     public function refreshUpdated() {
  219.         $this->setStamp(new \DateTime("now"));
  220.     }
  221.  
  222.     /**
  223.      * Constructor
  224.      */
  225.     public function __construct()
  226.     {
  227.         $this->stamp = new \DateTime();
  228.         $this->sort = 0;
  229.         $this->campgrounds = new \Doctrine\Common\Collections\ArrayCollection();
  230.     }
  231.  
  232.     /**
  233.      * Add campground
  234.      *
  235.      * @param \AppBundle\Entity\Campground $campground
  236.      *
  237.      * @return Picture
  238.      */
  239.     public function addCampground(\AppBundle\Entity\Campground $campground)
  240.     {
  241.         $this->campgrounds[] = $campground;
  242.  
  243.         return $this;
  244.     }
  245.  
  246.     /**
  247.      * Remove campground
  248.      *
  249.      * @param \AppBundle\Entity\Campground $campground
  250.      */
  251.     public function removeCampground(\AppBundle\Entity\Campground $campground)
  252.     {
  253.         $this->campgrounds->removeElement($campground);
  254.     }
  255.  
  256.     /**
  257.      * Get campgrounds
  258.      *
  259.      * @return \Doctrine\Common\Collections\Collection
  260.      */
  261.     public function getCampgrounds()
  262.     {
  263.         return $this->campgrounds;
  264.     }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement