<?php
namespace App\Entity;
use App\Repository\CountryRepository;
use Doctrine\Common\Collections\Collection;
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;
use App\Entity\{Client, Transporteur, TimestampableTrait};
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Intl\Countries;
use Symfony\Component\Intl\Currencies;
/**
* @ORM\Entity(repositoryClass=CountryRepository::class)
* @UniqueEntity(fields="code")
*/
class Country
{
use TimestampableTrait;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, unique=true)
*/
private $code;
/**
* @Gedmo\Slug(fields={"code"})
* @ORM\Column(length=100, unique=true)
*/
private $slug;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $flag;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $flagImage;
/**
* @Assert\Image(maxSize="2M", minWidth=160, minHeight=160)
*/
private $file;
private $temp;
/**
* @ORM\OneToMany(targetEntity=City::class, mappedBy="country", orphanRemoval=true)
*/
private $cities;
/**
* @ORM\OneToMany(targetEntity=Park::class, mappedBy="country")
*/
private $parks;
/**
* @ORM\OneToMany(targetEntity=Transporteur::class, mappedBy="country")
*/
private $transporteurs;
/**
* @ORM\OneToMany(targetEntity=Department::class, mappedBy="country")
*/
private $departments;
/**
* @ORM\Column(type="boolean")
*/
private $status = false;
public function __construct()
{
$this->cities = new ArrayCollection();
$this->parks = new ArrayCollection();
$this->transporteurs = new ArrayCollection();
$this->departments = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
public function getPays()
{
return Countries::getName($this->getCode());
}
// public function getMonnaie()
// {
// return Currencies::getSymbol('INR');
// }
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getFlag(): ?string
{
return $this->flag;
}
public function setFlag(?string $flag): self
{
$this->flag = $flag;
return $this;
}
public function getFlagImage(): ?string
{
return $this->flagImage;
}
public function setFlagImage(?string $flagImage): self
{
$this->flagImage = $flagImage;
return $this;
}
public function setFile(UploadedFile $file)
{
$this->file = $file;
if (!is_null($this->flagImage)) {
$this->temp = $this->flagImage;
$this->flagImage = null;
}
return $this;
}
public function getFile()
{
return $this->file;
}
public function setTemp($temp)
{
$this->temp = $temp;
return $this;
}
public function getTemp()
{
return $this->temp;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (is_null($this->file)) {
return;
}
$this->flagImage = 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->flagImage
);
}
/**
* @ORM\PreRemove()
*/
public function preRemoveUpload()
{
$this->temp = $this->flagImage;
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
$temp = $this->getUploadRootDir().'/'.$this->temp;
if (file_exists($temp)) {
@unlink($temp);
}
$this->removeThumb();
}
public function getUploadDir()
{
return 'uploads/files/country';
}
protected function getUploadRootDir()
{
return __DIR__.'/../../../../public/'.$this->getUploadDir();
}
protected function removeThumb()
{
$tab = ['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->getFlagImage();
}
public function getThumbPath()
{
return '/uploads/files/country'.'/'.$this->getFlagImage();
}
/**
* @return Collection<int, City>
*/
public function getCities(): Collection
{
return $this->cities;
}
public function addCity(City $city): self
{
if (!$this->cities->contains($city)) {
$this->cities[] = $city;
$city->setCountry($this);
}
return $this;
}
public function removeCity(City $city): self
{
if ($this->cities->removeElement($city)) {
// set the owning side to null (unless already changed)
if ($city->getCountry() === $this) {
$city->setCountry(null);
}
}
return $this;
}
/**
* @return Collection<int, Park>
*/
public function getParks(): Collection
{
return $this->parks;
}
public function addPark(Park $park): self
{
if (!$this->parks->contains($park)) {
$this->parks[] = $park;
$park->setCountry($this);
}
return $this;
}
public function removePark(Park $park): self
{
if ($this->parks->removeElement($park)) {
// set the owning side to null (unless already changed)
if ($park->getCountry() === $this) {
$park->setCountry(null);
}
}
return $this;
}
/**
* @return Collection<int, Transporteur>
*/
public function getTransporteurs(): Collection
{
return $this->transporteurs;
}
public function addTransporteur(Transporteur $transporteur): self
{
if (!$this->transporteurs->contains($transporteur)) {
$this->transporteurs[] = $transporteur;
$transporteur->setCountry($this);
}
return $this;
}
public function removeTransporteur(Transporteur $transporteur): self
{
if ($this->transporteurs->removeElement($transporteur)) {
// set the owning side to null (unless already changed)
if ($transporteur->getCountry() === $this) {
$transporteur->setCountry(null);
}
}
return $this;
}
/**
* @return Collection<int, Department>
*/
public function getDepartments(): Collection
{
return $this->departments;
}
public function addDepartment(Department $department): self
{
if (!$this->departments->contains($department)) {
$this->departments[] = $department;
$department->setCountry($this);
}
return $this;
}
public function removeDepartment(Department $department): self
{
if ($this->departments->removeElement($department)) {
// set the owning side to null (unless already changed)
if ($department->getCountry() === $this) {
$department->setCountry(null);
}
}
return $this;
}
public function isStatus(): ?bool
{
return $this->status;
}
public function setStatus(bool $status): self
{
$this->status = $status;
return $this;
}
}