<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\Api\Ad\GetAdsFromClubController;
use App\Controller\Api\Club\GetClubHomepageController;
use App\Controller\Api\Club\GetClubLocalizeController;
use App\Controller\Api\Club\GetClubWidgetController;
use App\Controller\Api\Company\GetCompaniesFromClubController;
use App\Controller\Api\Post\GetPostsFromClubController;
use App\Controller\Api\User\GetUsersFromClubController;
use App\Repository\ClubRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: ClubRepository::class)]
#[ApiResource(
itemOperations: [
'get',
'put',
'delete',
'get_companies' => [
'normalization_context' => [
'groups' => 'read:club:collection:getCompanies',
],
'controller' => GetCompaniesFromClubController::class,
'method' => 'POST',
'path' => '/public/clubs/{id}/companies',
],
'get_club_localize' => [
'normalization_context' => [
'groups' => 'read:club:collection:getClubLocalize',
],
'controller' => GetClubLocalizeController::class,
'method' => 'POST',
'path' => '/public/localize',
'read' => false,
],
'get_posts' => [
'normalization_context' => [
'groups' => 'read:club:collection:getPosts',
],
'controller' => GetPostsFromClubController::class,
'method' => 'POST',
'path' => '/public/clubs/{id}/posts',
],
'get_widgets' => [
'normalization_context' => [
'groups' => 'read:club:collection:getWidgets',
],
'controller' => GetClubWidgetController::class,
'method' => 'POST',
'path' => '/public/clubs/{id}/widgets',
],
'get_homepage' => [
'controller' => GetClubHomepageController::class,
'method' => 'POST',
'path' => '/public/clubs/{id}/homepage',
'read' => false,
],
'get_ads' => [
'normalization_context' => [
'groups' => 'read:club:collection:getAds',
],
'controller' => GetAdsFromClubController::class,
'method' => 'POST',
'path' => '/public/clubs/{id}/ads',
],
'post_alert' => [
'normalization_context' => [
'groups' => 'read:club:collection:getUsers',
],
'controller' => GetUsersFromClubController::class,
'method' => 'POST',
'path' => '/clubs/{id}/users',
],
'get_users' => [
'normalization_context' => [
'groups' => 'read:club:collection:getUsers',
],
'controller' => GetUsersFromClubController::class,
'method' => 'POST',
'path' => '/clubs/{id}/users',
],
],
normalizationContext: [
'groups' => 'read:club:collection',
]
)]
class Club
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups([
'read:user:item:me',
'read:club:collection:getCompanies',
'read:club:collection',
'read:club:collection:getClubLocalize',
'read:company:collection:get',
])]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Groups([
'read:user:item:me',
'read:club:collection:getCompanies',
'read:club:collection:getClubLocalize',
'read:company:collection:get',
])]
#[Assert\Length(
max: 255,
maxMessage: "Le lien doit faire maximum 255 caractères.",
)]
private ?string $name = null;
#[ORM\OneToMany(mappedBy: 'club', targetEntity: Post::class, cascade: ["persist"])]
private Collection $posts;
#[ORM\OneToMany(mappedBy: 'club', targetEntity: Membership::class, cascade: ["persist"])]
private Collection $memberships;
#[ORM\OneToMany(mappedBy: 'club', targetEntity: Photo::class, cascade: ["persist"])]
private Collection $photos;
#[ORM\OneToMany(mappedBy: 'club', targetEntity: Ad::class)]
private Collection $ads;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $theme = null;
#[ORM\ManyToOne(inversedBy: 'clubs', cascade: ["remove"])]
#[ORM\JoinColumn(nullable: true, onDelete: "SET NULL")]
#[Groups([
'read:user:item:me',
'read:club:collection:getClubLocalize',
])]
private ?Address $address = null;
#[Groups([
'read:user:item:me',
'read:club:collection:getClubLocalize',
])]
private ?float $distance = null;
#[ORM\OneToMany(mappedBy: 'club', targetEntity: UserClub::class)]
private Collection $userClubs;
#[ORM\OneToMany(mappedBy: 'club', targetEntity: Widget::class)]
#[Groups([
'read:club:collection:getWidgets',
])]
private Collection $widgets;
#[Groups([
'read:user:item:me',
'read:club:collection:getClubLocalize',
])]
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $homeMessage = null;
#[ORM\OneToMany(mappedBy: 'club', targetEntity: Deal::class)]
private Collection $deals;
#[ORM\OneToMany(mappedBy: 'club', targetEntity: ClubManager::class, cascade: ["persist"])]
private Collection $clubManagers;
#[ORM\OneToMany(mappedBy: 'club', targetEntity: Alert::class, orphanRemoval: true)]
private Collection $alerts;
#[ORM\OneToMany(mappedBy: 'club', targetEntity: ClubPresentation::class, orphanRemoval: true)]
private Collection $clubPresentations;
#[ORM\Column(type: Types::TEXT, nullable: true)]
#[Groups([
'read:user:item:me',
'read:club:collection:getClubLocalize',
])]
#[Assert\Url(
message: "Ce lien {{ value }} n'est pas un lien valide.",
)]
private ?string $website = null;
#[ORM\Column(length: 255, nullable: true)]
#[Groups([
'read:user:item:me',
'read:club:collection:getClubLocalize',
])]
#[Assert\Regex(
pattern: '/^[0-9]{10}$/',
message: 'Doit être composé de 10 chiffres sans espace ni point'
)]
private ?string $phone = null;
#[ORM\Column(length: 255, nullable: true)]
#[Groups([
'read:user:item:me',
'read:club:collection:getClubLocalize',
])]
#[Assert\Email(
message: "Cet Email {{ value }} n'est pas un email valide.",
)]
private ?string $email = null;
public function __construct()
{
$this->posts = new ArrayCollection();
$this->memberships = new ArrayCollection();
$this->photos = new ArrayCollection();
$this->ads = new ArrayCollection();
$this->userClubs = new ArrayCollection();
$this->widgets = new ArrayCollection();
$this->deals = new ArrayCollection();
$this->clubManagers = new ArrayCollection();
$this->alerts = new ArrayCollection();
$this->clubPresentations = new ArrayCollection();
}
public function __toString(): string
{
return $this->name;
}
public function getDistance(): ?float
{
return $this->distance;
}
public function setDistance(?float $distance): self
{
$this->distance = $distance;
return $this;
}
#[Groups([
'read:user:item:me',
'read:club:collection:getCompanies',
'read:club:collection:getClubLocalize',
])]
public function getLogoUrl(): 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:user:item:me',
'read:club:collection:getClubLocalize',
'read:company:collection:get',
])]
public function getSmallLogoUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageMedium()->getUrl();
}
return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
}
#[Groups([
'read:user:item:me',
])]
public function getMediumLogoUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageMedium()->getUrl();
}
return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
}
#[Groups([
'read:user:item:me',
'read:club:collection:getClubLocalize',
])]
public function getJsonTheme(): mixed
{
return json_decode($this->theme, true);
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection<int, Post>
*/
public function getPosts(): Collection
{
return $this->posts;
}
public function addPost(Post $post): self
{
if (!$this->posts->contains($post)) {
$this->posts->add($post);
$post->setClub($this);
}
return $this;
}
public function removePost(Post $post): self
{
if ($this->posts->removeElement($post)) {
// set the owning side to null (unless already changed)
if ($post->getClub() === $this) {
$post->setClub(null);
}
}
return $this;
}
/**
* @return Collection<int, Membership>
*/
public function getMemberships(): Collection
{
return $this->memberships;
}
public function addMembership(Membership $membership): self
{
if (!$this->memberships->contains($membership)) {
$this->memberships->add($membership);
$membership->setClub($this);
}
return $this;
}
public function removeMembership(Membership $membership): self
{
if ($this->memberships->removeElement($membership)) {
// set the owning side to null (unless already changed)
if ($membership->getClub() === $this) {
$membership->setClub(null);
}
}
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->setClub($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->getClub() === $this) {
$photo->setClub(null);
}
}
return $this;
}
/**
* @return Collection<int, Ad>
*/
public function getAds(): Collection
{
return $this->ads;
}
public function addAd(Ad $ad): self
{
if (!$this->ads->contains($ad)) {
$this->ads->add($ad);
$ad->setClub($this);
}
return $this;
}
public function removeAd(Ad $ad): self
{
if ($this->ads->removeElement($ad)) {
// set the owning side to null (unless already changed)
if ($ad->getClub() === $this) {
$ad->setClub(null);
}
}
return $this;
}
public function getTheme(): mixed
{
return $this->theme;
}
public function setTheme(?string $theme): self
{
$this->theme = $theme;
return $this;
}
public function getAddress(): ?Address
{
return $this->address;
}
public function setAddress(?Address $address): self
{
$this->address = $address;
return $this;
}
/**
* @return Collection<int, UserClub>
*/
public function getUserClubs(): Collection
{
return $this->userClubs;
}
public function addUserClub(UserClub $userClub): self
{
if (!$this->userClubs->contains($userClub)) {
$this->userClubs->add($userClub);
$userClub->setClub($this);
}
return $this;
}
public function removeUserClub(UserClub $userClub): self
{
if ($this->userClubs->removeElement($userClub)) {
// set the owning side to null (unless already changed)
if ($userClub->getClub() === $this) {
$userClub->setClub(null);
}
}
return $this;
}
/**
* @return Collection<int, Widget>
*/
public function getWidgets(): Collection
{
return $this->widgets;
}
public function addWidget(Widget $widget): self
{
if (!$this->widgets->contains($widget)) {
$this->widgets->add($widget);
$widget->setClub($this);
}
return $this;
}
public function removeWidget(Widget $widget): self
{
if ($this->widgets->removeElement($widget)) {
// set the owning side to null (unless already changed)
if ($widget->getClub() === $this) {
$widget->setClub(null);
}
}
return $this;
}
public function getHomeMessage(): ?string
{
return $this->homeMessage;
}
public function setHomeMessage(?string $homeMessage): self
{
$this->homeMessage = $homeMessage;
return $this;
}
/**
* @return Collection<int, Deal>
*/
public function getDeals(): Collection
{
return $this->deals;
}
public function addDeal(Deal $deal): self
{
if (!$this->deals->contains($deal)) {
$this->deals->add($deal);
$deal->setClub($this);
}
return $this;
}
public function removeDeal(Deal $deal): self
{
if ($this->deals->removeElement($deal)) {
// set the owning side to null (unless already changed)
if ($deal->getClub() === $this) {
$deal->setClub(null);
}
}
return $this;
}
/**
* @return Collection<int, ClubManager>
*/
public function getClubManagers(): Collection
{
return $this->clubManagers;
}
public function addClubManager(ClubManager $clubManager): static
{
if (!$this->clubManagers->contains($clubManager)) {
$this->clubManagers->add($clubManager);
$clubManager->setClub($this);
}
return $this;
}
public function removeClubManager(ClubManager $clubManager): static
{
if ($this->clubManagers->removeElement($clubManager)) {
// set the owning side to null (unless already changed)
if ($clubManager->getClub() === $this) {
$clubManager->setClub(null);
}
}
return $this;
}
/**
* @return Collection<int, Alert>
*/
public function getAlerts(): Collection
{
return $this->alerts;
}
public function addAlert(Alert $alert): static
{
if (!$this->alerts->contains($alert)) {
$this->alerts->add($alert);
$alert->setClub($this);
}
return $this;
}
public function removeAlert(Alert $alert): static
{
if ($this->alerts->removeElement($alert)) {
// set the owning side to null (unless already changed)
if ($alert->getClub() === $this) {
$alert->setClub(null);
}
}
return $this;
}
/**
* @return Collection<int, ClubPresentation>
*/
public function getClubPresentations(): Collection
{
return $this->clubPresentations;
}
public function addClubPresentation(ClubPresentation $clubPresentation): static
{
if (!$this->clubPresentations->contains($clubPresentation)) {
$this->clubPresentations->add($clubPresentation);
$clubPresentation->setClub($this);
}
return $this;
}
public function removeClubPresentation(ClubPresentation $clubPresentation): static
{
if ($this->clubPresentations->removeElement($clubPresentation)) {
// set the owning side to null (unless already changed)
if ($clubPresentation->getClub() === $this) {
$clubPresentation->setClub(null);
}
}
return $this;
}
public function getWebsite(): ?string
{
return $this->website;
}
public function setWebsite(?string $website): static
{
$this->website = $website;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): static
{
$this->phone = $phone;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): static
{
$this->email = $email;
return $this;
}
}