<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity()
* @ORM\HasLifecycleCallbacks
* @Vich\Uploadable()
*/
class Gallery
{
/**
* @var int
*
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column
*/
private ?int $id = null;
/**
* @var string
*
* @ORM\Column(length=8)
*/
private ?string $slug = null;
/**
* @var string
*
* @ORM\Column(length=255, nullable=true)
*/
private ?string $filename = null;
/**
* @var string
*
* @ORM\Column(nullable=true)
*/
private ?int $filesize = null;
/**
* @var \DateTime
*
* @ORM\Column(type=Types::DATETIME_IMMUTABLE, nullable=true)
*/
private ?\DateTimeImmutable $updated = null;
/**
* Unmapped property to handle file uploads
*
* @Vich\UploadableField(mapping="gallery_photo", fileNameProperty="filename", size="filesize")
*/
private ?File $file = null;
/**
* @var bool
*
* @ORM\Column(type=Types::BOOLEAN)
*/
private $active = true;
/**
* @var \DateTime
*
* @ORM\Column(type=Types::DATETIME_MUTABLE, nullable=true)
*/
private ?\DateTime $expiration = null;
/**
* @var GalleryView[]|Collection
*
* @ORM\OneToMany(targetEntity=GalleryView::class, mappedBy="gallery", cascade={"remove"})
* @ORM\JoinColumn(referencedColumnName="uid")
*/
private $views;
/**
* Allowed views = 0: infinite, untill active
*
* @var int
*
* @ORM\Column(type=Types::INTEGER)
*/
private $allowedViews = 0;
/**
* @var GalleryTag[]
*
* @ORM\OneToMany(targetEntity=GalleryGalleryTag::class, mappedBy="gallery", fetch="EXTRA_LAZY")
*/
private $tags;
public function __toString()
{
return $this->getSlug();
}
public function __construct()
{
$this->views = new ArrayCollection();
$this->tags = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getUpdated(): ?\DateTimeInterface
{
return $this->updated;
}
public function setUpdated(\DateTimeInterface $updated): self
{
$this->updated = $updated;
return $this;
}
/**
* @return string|null
*/
public function getFilename(): ?string
{
return $this->filename;
}
/**
* @param string|null $filename
* @return Photo
*/
public function setFilename(?string $filename): self
{
$this->filename = $filename;
return $this;
}
public function setFile(?File $file = null): void
{
$this->file = $file;
if (null !== $file) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updated = new \DateTimeImmutable();
}
}
public function getFile(): ?File
{
return $this->file;
}
/**
* @return int|null
*/
public function getFilesize(): ?int
{
return $this->filesize;
}
/**
* @param int|null $filesize
* @return Photo
*/
public function setFilesize(?int $filesize): self
{
$this->filesize = $filesize;
return $this;
}
function getRandomString(int $length = 8) {
return substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, $length);
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= $characters[mt_rand(0, strlen($characters) - 1)];
}
return $string;
}
public function isActive(): bool
{
return $this->active;
}
public function setActive(bool $active): Gallery
{
$this->active = $active;
return $this;
}
public function getExpiration(): ?\DateTime
{
return $this->expiration;
}
public function setExpiration(?\DateTime $expiration): Gallery
{
$this->expiration = $expiration;
return $this;
}
public function getViews(): Collection
{
return $this->views;
}
public function setViews(Collection $views): Gallery
{
$this->views = $views;
return $this;
}
public function addView(GalleryView $view): Gallery
{
if (!$this->views->contains($view)) {
$this->views->add($view);
}
return $this;
}
public function removeView(GalleryView $view): Gallery
{
if ($this->views->contains($view)) {
$this->views->removeElement($view);
}
return $this;
}
public function getViewsCount(): int
{
return $this->views->count();
}
public function getAllowedViews(): int
{
return $this->allowedViews;
}
public function setAllowedViews(int $allowedViews): Gallery
{
$this->allowedViews = $allowedViews;
return $this;
}
public function getTags(): Collection
{
return $this->tags;
}
public function setTags(Collection $tags): Gallery
{
$this->tags = $tags;
return $this;
}
}