<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\LanguageRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraints as Assert;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
/**
* @ORM\Entity(repositoryClass="App\Repository\ImageRepository")
*/
class Image
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="text")
*/
private $description='';
/**
* @ORM\ManyToMany (targetEntity="App\Entity\Product", inversedBy="images")
* @ORM\JoinColumn(nullable=true)
*/
private $products;
/**
* @ORM\Column(type="integer")
*/
private $displayOrder=0;
/**
* @ORM\Column(type="string")
*
* @Assert\NotBlank(message="Please, upload a Image file.")
* @Assert\File(mimeTypes={ "image/*" })
*/
private $image;
/**
* @ORM\Column(type="string")
*/
private $category='';
/**
* @ORM\Column(type="string")
*/
private $filename='';
/**
* @ORM\Column(type="string")
*/
private $mime='';
/**
* @ORM\Column(type="integer")
*/
private $weight=0;
/**
* @ORM\Column(type="integer")
*/
private $height=0;
/**
* @ORM\Column(type="integer")
*/
private $width=0;
/**
* @ORM\Column(type="string")
*/
private $folder='';
/**
* @ORM\Column(type="date")
*/
private $creationDate;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $lastUpdated=null;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="images", cascade={"persist"})
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private $user;
/*************************
* All the properties set
*************************/
public function __construct(){
$this->creationDate = new \DateTime();
$this->products = new ArrayCollection();
$this->order=0;
if ($this->lastUpdated == null) {
$this->setLastUpdated(new \DateTime());
}
}
public function setHeight($height)
{
if(is_numeric($height))
$this->height = $height;
}
public function getHeight()
{
return $this->height;
}
public function setWidth($width)
{
$this->width = $width;
}
public function getWidth()
{
return $this->width;
}
public function setProducts($product){
$this->products = $product;
}
public function getProducts(){
return $this->products;
}
public function getProduct(){
if(!empty($this->getProducts()))
return $this->getProducts()->last();
}
public function addProduct(Product $product):self
{
if (!$this->products->contains($product)) {
$this->products[] = $product;
$product->addImage($this);
}
return $this;
}
public function setDisplayOrder($order)
{
$this->displayOrder = $order;
}
public function getDisplayOrder()
{
return $this->displayOrder;
}
public function getId(){
return $this->id;
}
public function setId($id){
$this->id = $id;
}
public function getUser(){
return $this->user;
}
public function setUser($user){
$this->user = $user;
}
//@TODO one day
public function setUserById($id){
}
public function getImage(){
return $this->image;
}
public function setImage($name){
$rawFile = explode('/', $name);
$this->filename = end($rawFile);
$this->weight = filesize($name);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$this->mime = finfo_file($finfo, $name);
finfo_close($finfo);
$this->image= $name;
list($width, $height, $type, $attr)= getimagesize($name);
$this->setHeight($height);
$this->setWidth($width);
}
public function getMime(){
return $this->mime;
}
public function getFileName(){
return $this->filename;
}
public function getWeight(){
return $this->weight;
}
public function getDescription(){
return $this->description;
}
public function setDescription($id){
$this->description= $id;
}
public function getCategory(){
return $this->category;
}
public function setCategory($cat){
$this->category= $cat;
}
public function getFolder(){
return $this->folder;
}
public function setFolder($folder){
$this->folder= $folder;
}
public function setCreationDate($creationDate)
{
$this->creationDate = $creationDate;
}
public function getCreationDate()
{
return $this->creationDate;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function updateLastUpdated()
{
$this->setLastUpdated(new \DateTime());
}
public function getLastUpdated(): ?\DateTimeInterface
{
return $this->lastUpdated;
}
public function setLastUpdated(?\DateTimeInterface $lastUpdated): self
{
$this->lastUpdated = $lastUpdated;
return $this;
}
// public function getUser2(): ?User
// {
// return $this->user2;
// }
//
// public function setUser2(?User $user2): self
// {
// $this->user2 = $user2;
//
// return $this;
// }
}