<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\AdRepository;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: AdRepository::class)]
#[ApiResource(
normalizationContext: [
'groups' => 'read:ad',
]
)]
class Ad
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups([
'read:club:collection:getAds',
'read:ad',
])]
private ?int $id = null;
use TimestampableEntity;
#[ORM\Column(length: 255)]
#[Groups([
'read:club:collection:getAds',
'read:ad',
])]
private ?string $title = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
#[Groups([
'read:club:collection:getAds',
'read:ad',
])]
private ?string $content = null;
#[ORM\Column(length: 255)]
#[Groups([
'read:club:collection:getAds',
'read:ad',
])]
private ?string $type = null;
#[ORM\OneToMany(mappedBy: 'ad', targetEntity: Photo::class, cascade: ["persist"])]
#[Groups([
'read:ad',
])]
private Collection $photos;
#[ORM\ManyToOne(inversedBy: 'ads')]
#[ORM\JoinColumn(nullable: false, onDelete: "CASCADE")]
#[Groups([
'read:ad',
])]
private ?Club $club = null;
#[ORM\ManyToOne(inversedBy: 'ads')]
#[Groups([
'read:club:collection:getAds',
'read:ad',
])]
private ?User $author = null;
#[ORM\ManyToOne(inversedBy: 'ads')]
#[ORM\JoinColumn(nullable: false, onDelete: "CASCADE")]
#[Groups([
'read:club:collection:getAds',
'read:ad',
])]
private ?AdCategory $category = null;
public function __construct()
{
$this->photos = new ArrayCollection();
$this->ads = new ArrayCollection();
}
#[Groups([
'read:club:collection:getAds',
'read:ad',
])]
public function getCreatedAt(): ?DateTime
{
return $this->createdAt;
}
#[Groups([
'read:club:collection:getAds',
'read:ad',
])]
public function getImageUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageLarge()->getUrl();
}
return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
}
public function getMainPhoto()
{
foreach ($this->photos as $p) {
if ($p->isMain()) {
return $p;
}
}
if (sizeof($this->photos) > 0) {
return $this->photos[0];
}
return false;
}
#[Groups([
'read:club:collection:getAds',
'read:ad',
])]
public function getSmallImageUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageMedium()->getUrl();
}
return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
}
#[Groups([
'read:club:collection:getAds',
'read:ad',
])]
public function getMediumImageUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageMedium()->getUrl();
}
return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(?string $content): self
{
$this->content = $content;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
/**
* @return Collection<int, Photo>
*/
public function getPhotos(): Collection
{
return $this->photos;
}
public function addPhoto(Photo $photo): self
{
if (!$this->photos->contains($photo)) {
$this->photos->add($photo);
$photo->setAd($this);
}
return $this;
}
public function removePhoto(Photo $photo): self
{
if ($this->photos->removeElement($photo)) {
// set the owning side to null (unless already changed)
if ($photo->getAd() === $this) {
$photo->setAd(null);
}
}
return $this;
}
public function getClub(): ?Club
{
return $this->club;
}
public function setClub(?Club $club): self
{
$this->club = $club;
return $this;
}
public function getAuthor(): ?User
{
return $this->author;
}
public function setAuthor(?User $author): self
{
$this->author = $author;
return $this;
}
public function getCategory(): ?AdCategory
{
return $this->category;
}
public function setCategory(?AdCategory $category): self
{
$this->category = $category;
return $this;
}
}