<?php
namespace App\Entity;
use App\Entity\TimestampableTrait;
use App\Repository\PartnerRepository;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @ORM\Entity(repositoryClass=PartnerRepository::class)
* @UniqueEntity(fields="name")
*/
class Partner
{
use TimestampableTrait;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, unique=true)
*/
private $name;
/**
* @Gedmo\Slug(fields={"name"})
* @ORM\Column(length=100, unique=true)
*/
private $slug;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $src;
/**
* @Assert\Image(maxSize="2M")
*/
private $file;
private $temp;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $link;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getSrc(): ?string
{
return $this->src;
}
public function setSrc(string $src): self
{
$this->src = $src;
return $this;
}
public function setTemp($temp)
{
$this->temp = $temp;
return $this;
}
public function getTemp()
{
return $this->temp;
}
public function setFile(UploadedFile $file)
{
$this->file = $file;
if (!is_null($this->src)) {
$this->temp = $this->src;
$this->src = null;
}
return $this;
}
public function getFile()
{
return $this->file;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (is_null($this->file)) {
return;
}
$this->src = md5(uniqid()).'.'.$this->file->guessExtension();
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (is_null($this->file)) {
return;
}
if (!is_null($this->temp)) {
$oldFile = $this->getUploadRootDir().'/'.$this->temp;
if (file_exists($oldFile)) {
@unlink($oldFile);
}
}
$this->file->move(
$this->getUploadRootDir(),$this->src
);
}
/**
* @ORM\PreRemove()
*/
public function preRemoveUpload()
{
$this->temp = $this->getUploadRootDir().'/'.$this->src;
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if (file_exists($this->temp)) {
@unlink($this->temp);
}
}
public function getUploadDir()
{
return 'uploads/files/partner';
}
protected function getUploadRootDir()
{
return __DIR__.'/../../../../public/'.$this->getUploadDir();
}
protected function removeThumb()
{
$tab = ['117x74', '117x117', '160x100', '180x92', '214x214', '300x200','720x480','900x300', '1033x276'];
foreach ($tab as $type) {
$url = __DIR__.'/../../../../public/media/cache/'.$type.'/'.$this->getUploadDir().'/'.$this->temp;
if (file_exists($url)) {
@unlink($url);
}
}
}
public function getWebPath()
{
return $this->getUploadDir().'/'.$this->getSrc();
}
public function getLink(): ?string
{
return $this->link;
}
public function setLink(?string $link): self
{
$this->link = $link;
return $this;
}
}