<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\PostRepository;
use DateTime;
use DateTimeInterface;
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;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: PostRepository::class)]
#[ApiResource]
class Post
{
const TYPE_ARTICLE = 'article';
const TYPE_EVENT = 'event';
const STATUS_DRAFT = 'draft';
const STATUS_ONLINE = 'online';
const STATUS_ARCHIVED = 'archived';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups([
'read:club:collection:getPosts',
])]
private ?int $id = null;
use TimestampableEntity;
#[ORM\Column(length: 255)]
#[Assert\NotBlank]
#[Groups([
'read:club:collection:getPosts',
])]
private ?string $title = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
#[Groups([
'read:club:collection:getPosts',
])]
private ?string $content = null;
#[ORM\ManyToOne(inversedBy: 'posts')]
#[ORM\JoinColumn(nullable: true, onDelete: "SET NULL")]
private ?Club $club = null;
#[ORM\ManyToOne(inversedBy: 'posts')]
#[ORM\JoinColumn(nullable: true, onDelete: "SET NULL")]
#[Groups([
'read:club:collection:getPosts',
])]
private ?User $author = null;
#[ORM\OneToMany(mappedBy: 'post', targetEntity: Photo::class, cascade: ["persist"])]
private Collection $photos;
#[ORM\Column(length: 255)]
#[Assert\NotBlank]
#[Groups([
'read:club:collection:getPosts',
])]
private ?string $type = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?DateTimeInterface $eventDate = null;
#[ORM\Column(length: 255)]
private ?string $status = null;
public function __construct()
{
$this->photos = new ArrayCollection();
$this->type = self::TYPE_ARTICLE;
$this->status = self::STATUS_DRAFT;
}
public static function getTypeList(): array
{
return [
self::TYPE_ARTICLE => 'Article',
self::TYPE_EVENT => 'Évènement',
];
}
public function getTypeLabel(): ?string
{
$list = self::getTypeList();
return ($this->type != null && array_key_exists($this->type, $list) ? $list[$this->type] : null);
}
public static function getStatusList(): array
{
return [
self::STATUS_DRAFT => 'Brouillon',
self::STATUS_ONLINE => 'En ligne',
self::STATUS_ARCHIVED => 'Archivé',
];
}
public function getStatusLabel(): ?string
{
$list = self::getStatusList();
return ($this->status != null && array_key_exists($this->status, $list) ? $list[$this->status] : null);
}
public function getStatusClass(): ?string
{
return match ($this->status) {
self::STATUS_DRAFT => 'primary',
self::STATUS_ONLINE => 'success',
self::STATUS_ARCHIVED => 'light',
default => null,
};
}
#[Groups([
'read:club:collection:getPosts',
])]
public function getCreatedAt(): ?DateTime
{
return $this->createdAt;
}
#[Groups([
'read:club:collection:getPosts',
])]
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:getPosts',
])]
public function getSmallImageUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageSmall()->getUrl();
}
return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
}
#[Groups([
'read:club:collection:getPosts',
])]
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 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;
}
/**
* @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->setPost($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->getPost() === $this) {
$photo->setPost(null);
}
}
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(?string $type): self
{
$this->type = $type;
return $this;
}
public function getEventDate(): ?DateTimeInterface
{
return $this->eventDate;
}
public function setEventDate(?DateTimeInterface $eventDate): self
{
$this->eventDate = $eventDate;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): static
{
$this->status = $status;
return $this;
}
}