<?php
declare(strict_types=1);
namespace App\Entity;
use App\Repository\CommunityRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\Sluggable\SluggableTrait;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\HttpFoundation\File\File;
/**
* @ORM\Entity(repositoryClass=CommunityRepository::class)
* @Vich\Uploadable
*/
class Community implements TranslatableInterface, SluggableInterface
{
use TranslatableTrait;
use SluggableTrait;
public function __toString()
{
if( $name = $this->translate()->getName()) {
return $name;
}
// if no translation has been added, return empty string instead.
return '';
}
/**
* @ORM\OneToMany(targetEntity="App\Entity\Masjid", mappedBy="community")
* @ORM\OrderBy({"name" = "ASC"})
*/
private $masjids;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Emirate::class)
* @ORM\JoinColumn(nullable=false)
*/
private $emirate;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $mainPic;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="community_mainPic", fileNameProperty="mainPic")
*
* @var File|null
*/
private $imageFile;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTimeInterface|null
*/
private $updatedAt;
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile
*/
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
// VERY IMPORTANT:
// 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
if ($image) {
// if 'updatedAt' is not defined in your entity, use another property
$this->updatedAt = new \DateTime('now');
}
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function __construct()
{
$this->masjids = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEmirate(): ?Emirate
{
return $this->emirate;
}
public function setEmirate(?Emirate $emirate): self
{
$this->emirate = $emirate;
return $this;
}
/**
* @return Collection|Masjid[]
*/
public function getMasjids(): Collection
{
return $this->masjids;
}
public function addMasjid(Masjid $masjid): self
{
if (!$this->masjids->contains($masjid)) {
$this->masjids[] = $masjid;
$masjid->setCommunity($this);
}
return $this;
}
public function removeMasjid(Masjid $masjid): self
{
if ($this->masjids->removeElement($masjid)) {
// set the owning side to null (unless already changed)
if ($masjid->getCommunity() === $this) {
$masjid->setCommunity(null);
}
}
return $this;
}
/**
* @return string[]
*/
public function getSluggableFields(): array
{
return ["name"];
}
public function generateSlugValue($values): string
{
return implode('-', $values);
}
public function shouldRegenerateSlugOnUpdate(): bool{
return false;
}
public function getMainPic(): ?string
{
return $this->mainPic;
}
public function setMainPic(?string $mainPic): self
{
$this->mainPic = $mainPic;
return $this;
}
}