src/Entity/Image.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use App\Repository\LanguageRepository;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. use App\Entity\User;
  10. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\DependencyInjection\Container;
  13. /**
  14.  * @ORM\Entity(repositoryClass="App\Repository\ImageRepository")
  15.  */
  16. class Image
  17. {
  18.     /**
  19.      * @ORM\Id
  20.      * @ORM\GeneratedValue
  21.      * @ORM\Column(type="integer")
  22.      */
  23.     private $id;
  24.     /**
  25.      * @ORM\Column(type="text")
  26.      */
  27.     private $description='';
  28.     /**
  29.      * @ORM\ManyToMany (targetEntity="App\Entity\Product", inversedBy="images")
  30.      * @ORM\JoinColumn(nullable=true)
  31.      */
  32.     private $products;
  33.     /**
  34.      * @ORM\Column(type="integer")
  35.      */
  36.     private $displayOrder=0;
  37.     /**
  38.      * @ORM\Column(type="string")
  39.      *
  40.      * @Assert\NotBlank(message="Please, upload a Image file.")
  41.      * @Assert\File(mimeTypes={ "image/*" })
  42.      */
  43.     private $image;
  44.     /**
  45.      * @ORM\Column(type="string")
  46.      */
  47.     private $category='';
  48.     /**
  49.      * @ORM\Column(type="string")
  50.      */
  51.     private $filename='';
  52.     /**
  53.      * @ORM\Column(type="string")
  54.      */
  55.     private $mime='';
  56.     /**
  57.      * @ORM\Column(type="integer")
  58.      */
  59.     private $weight=0;
  60.     /**
  61.      * @ORM\Column(type="integer")
  62.      */
  63.     private $height=0;
  64.     /**
  65.      * @ORM\Column(type="integer")
  66.      */
  67.     private $width=0;
  68.     /**
  69.      * @ORM\Column(type="string")
  70.      */
  71.     private $folder='';
  72.     /**
  73.      * @ORM\Column(type="date")
  74.      */
  75.     private $creationDate;
  76.     /**
  77.      * @ORM\Column(type="datetime", nullable=true)
  78.      */
  79.     private $lastUpdated=null;
  80.     /**
  81.      * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="images", cascade={"persist"})
  82.      * @ORM\JoinColumn(onDelete="CASCADE")
  83.      */
  84.     private $user;
  85.     /*************************
  86.      * All the properties set
  87.      *************************/
  88.     public function __construct(){
  89.         $this->creationDate = new \DateTime();
  90.         $this->products = new ArrayCollection();
  91.         $this->order=0;
  92.         if ($this->lastUpdated == null) {
  93.             $this->setLastUpdated(new \DateTime());
  94.         }
  95.     }
  96.     public function setHeight($height)
  97.     {
  98.         if(is_numeric($height))
  99.             $this->height $height;
  100.     }
  101.     public function getHeight()
  102.     {
  103.         return $this->height;
  104.     }
  105.     public function setWidth($width)
  106.     {
  107.         $this->width $width;
  108.     }
  109.     public function getWidth()
  110.     {
  111.         return $this->width;
  112.     }
  113.     public function setProducts($product){
  114.         $this->products $product;
  115.     }
  116.     public function getProducts(){
  117.         return $this->products;
  118.     }
  119.     public function getProduct(){
  120.     if(!empty($this->getProducts()))
  121.         return $this->getProducts()->last();
  122.     }
  123.     public function addProduct(Product $product):self
  124.     {
  125.         if (!$this->products->contains($product)) {
  126.             $this->products[] = $product;
  127.             $product->addImage($this);
  128.         }
  129.         return $this;
  130.     }
  131.     public function setDisplayOrder($order)
  132.     {
  133.         $this->displayOrder $order;
  134.     }
  135.     public function getDisplayOrder()
  136.     {
  137.         return $this->displayOrder;
  138.     }
  139.     public function getId(){
  140.         return $this->id;
  141.     }
  142.     public function setId($id){
  143.         $this->id $id;
  144.     }
  145.     public function getUser(){
  146.         return $this->user;
  147.     }
  148.     public function setUser($user){
  149.             $this->user $user;
  150.     }
  151.     //@TODO one day
  152.     public function setUserById($id){
  153.     }
  154.     public function getImage(){
  155.         return $this->image;
  156.     }
  157.     public function setImage($name){
  158.         $rawFile explode('/'$name);
  159.         $this->filename end($rawFile);
  160.         $this->weight filesize($name);
  161.         $finfo finfo_open(FILEINFO_MIME_TYPE);
  162.         $this->mime finfo_file($finfo$name);
  163.         finfo_close($finfo);
  164.         $this->image$name;
  165.         list($width$height$type$attr)= getimagesize($name);
  166.         $this->setHeight($height);
  167.         $this->setWidth($width);
  168.     }
  169.     public function getMime(){
  170.         return $this->mime;
  171.     }
  172.     public function getFileName(){
  173.         return $this->filename;
  174.     }
  175.     public function getWeight(){
  176.         return $this->weight;
  177.     }
  178.     public function getDescription(){
  179.         return $this->description;
  180.     }
  181.     public function setDescription($id){
  182.         $this->description$id;
  183.     }
  184.     public function getCategory(){
  185.         return $this->category;
  186.     }
  187.     public function setCategory($cat){
  188.         $this->category$cat;
  189.     }
  190.     public function getFolder(){
  191.         return $this->folder;
  192.     }
  193.     public function setFolder($folder){
  194.         $this->folder$folder;
  195.     }
  196.     public function setCreationDate($creationDate)
  197.     {
  198.         $this->creationDate $creationDate;
  199.     }
  200.     public function getCreationDate()
  201.     {
  202.         return $this->creationDate;
  203.     }
  204.     /**
  205.      * @ORM\PrePersist()
  206.      * @ORM\PreUpdate()
  207.      */
  208.     public function updateLastUpdated()
  209.     {
  210.         $this->setLastUpdated(new \DateTime());
  211.     }
  212.     public function getLastUpdated(): ?\DateTimeInterface
  213.     {
  214.         return $this->lastUpdated;
  215.     }
  216.     public function setLastUpdated(?\DateTimeInterface $lastUpdated): self
  217.     {
  218.         $this->lastUpdated $lastUpdated;
  219.         return $this;
  220.     }
  221.     // public function getUser2(): ?User
  222.     // {
  223.     //     return $this->user2;
  224.     // }
  225.     //
  226.     // public function setUser2(?User $user2): self
  227.     // {
  228.     //     $this->user2 = $user2;
  229.     //
  230.     //     return $this;
  231.     // }
  232. }