<?php
namespace AdminBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\VarDumper\VarDumper;
/**
* Images
*/
#[ORM\Table(name: 'images')]
#[ORM\Entity(repositoryClass: \AdminBundle\Repository\ImagesRepository::class)]
#[ORM\HasLifecycleCallbacks]
class Images
{
const IMAGE_UPLOAD_FOLDER = 'upload/tours-objectives/';
/**
* @var int
*/
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private $id;
/**
* @var string
*/
#[ORM\Column(name: 'title', type: 'string', length: 255, nullable: true)]
private $title;
/**
* @var string
*/
#[ORM\Column(name: 'path', type: 'string', length: 255)]
private $path;
/**
* @var string
*/
#[ORM\Column(name: 'url', type: 'string', length: 255, nullable: true)]
private $url;
/**
* @var \DateTime
*/
#[ORM\Column(name: 'updated', type: 'datetime', nullable: true)]
private $updated;
#[ORM\JoinColumn(name: 'objective', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: \Objectives::class, inversedBy: 'images', cascade: ['persist', 'remove'])]
private $objectives;
/**
* @var Tours
*/
#[ORM\OneToOne(targetEntity: \AdminBundle\Entity\Tours::class, mappedBy: 'image', cascade: ['persist', 'remove'])]
protected $tour;
#[Assert\File(mimeTypes: ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'])]
protected $pictureFile;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
*
* @return Images
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set path
*
* @param string $path
*
* @return Images
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* @param UploadedFile $picture
*/
public function setPictureFile(UploadedFile $pictureFile = null)
{
$this->pictureFile = $pictureFile;
}
public function removePictureFile()
{
if ($pictureFile = $this->getPath()) {
@unlink($pictureFile);
}
}
/**
* @return UploadedFile
*/
public function getPictureFile()
{
return $this->pictureFile;
}
protected function getUploadFilePath()
{
return self::IMAGE_UPLOAD_FOLDER;
}
public function getPicturePath()
{
if (!$this->title) {
return ;
}
return $this->getUploadFilePath() . $this->title;
}
public function uploadPicture()
{
if (null === $this->getPictureFile()) {
return;
}
$this->removePictureFile();
$filename = sha1(uniqid(mt_rand(), true));
$this->title = $filename.'.'.$this->getPictureFile()->guessExtension();
$this->getPictureFile()->move(
$this->getUploadFilePath(),
$this->title
);
$this->setPath($this->getPicturePath());
$this->setPictureFile(null);
}
#[ORM\PreUpdate]
#[ORM\PrePersist]
public function lifecycleFileUpload()
{
$this->uploadPicture();
}
/**
* Set url
*
* @param string $url
*
* @return Images
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* @return Tours
*/
public function getTour()
{
return $this->tour;
}
/**
* @param Tours $tour
*/
public function setTour($tour)
{
$this->tour = $tour;
}
/**
* @return mixed
*/
public function getUpdated()
{
return $this->updated;
}
/**
* @param mixed $updated
*/
public function setUpdated($updated)
{
$this->updated = $updated;
}
public function refreshUpdated()
{
$this->setUpdated(new \DateTime());
}
/**
* @return mixed
*/
public function getObjectives()
{
return $this->objectives;
}
/**
* @param mixed $objectives
*/
public function setObjectives($objectives)
{
$this->objectives = $objectives;
}
}