<?php
namespace App\Entity\Otpusk;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @ORM\Table(name="chains")
* @ORM\Entity(repositoryClass="App\Repository\Otpusk\ChainRepository")
*
*/
class Chain
{
const SERVER_PATH_TO_IMAGE_FOLDER = 'upload/media/chains/';
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $cName;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default"=0})
*/
private $showFilters;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default"=0})
*/
private $showLanding;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Otpusk\Hotel", mappedBy="chainId")
*/
private $hotels;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $file;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $url;
public function __construct()
{
$this->hotels = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getCName(): ?string
{
return $this->cName;
}
public function setCName(string $cName): self
{
$this->cName = $cName;
return $this;
}
public function getShowFilters(): ?bool
{
return $this->showFilters;
}
public function setShowFilters(bool $showFilters): self
{
$this->showFilters = $showFilters;
return $this;
}
public function getShowLanding(): ?bool
{
return $this->showLanding;
}
public function setShowLanding(bool $showLanding): self
{
$this->showLanding = $showLanding;
return $this;
}
/**
* @return Collection|Hotel[]
*/
public function getHotels(): Collection
{
return $this->hotels;
}
public function addHotel(Hotel $hotel): self
{
if (!$this->hotels->contains($hotel)) {
$this->hotels[] = $hotel;
$hotel->setChainId($this);
}
return $this;
}
public function removeHotel(Hotel $hotel): self
{
if ($this->hotels->contains($hotel)) {
$this->hotels->removeElement($hotel);
// set the owning side to null (unless already changed)
if ($hotel->getChainId() === $this) {
$hotel->setChainId(null);
}
}
return $this;
}
public function getHotelsCount(): int
{
return $this->hotels->count();
}
public function __toString()
{
return $this->getCName();
}
public function setFile($file)
{
$this->file = $file;
}
/**
* @return UploadedFile|Null
*/
public function getFile()
{
return $this->file ?? NULL;
}
public function getFilePath()
{
return $this->getFile() ? 'http://' . $_SERVER['SERVER_NAME'] . '/' . $this->getFile() : false;
}
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->getFile()) {
return;
}
$extension = strripos($this->getFile()->getClientOriginalName(), '.');
$extension = substr($this->getFile()->getClientOriginalName(), $extension, strlen($this->getFile()->getClientOriginalName()) - 1);
$fileName = $this->getId() . $extension;
$path = self::SERVER_PATH_TO_IMAGE_FOLDER . sprintf("%02d/%02d/", ($this->getId() / 1000) % 100, ($this->getId() / 10) % 100);
// we use the original file name here but you should
// sanitize it at least to avoid any security issues
// move takes the target directory and target filename as params
$this->getFile()->move(
$path,
$fileName
);
// set the path property to the filename where you've saved the file
$this->setFile($path . $fileName);
}
public function getUrl(): ?string
{
return $this->url;
}
public function setUrl(?string $url): self
{
$this->url = $url;
return $this;
}
}