<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Repository\ClientRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\InheritanceType;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @InheritanceType("SINGLE_TABLE")
* @ApiResource(
* normalizationContext={
* "groups"={"client:read"}
* },
* denormalizationContext={
* "groups"={"client:write"}
* },
* itemOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "patch"={
* "security"="is_granted('ROLE_ADMIN') or user == object.user"
* }
* },
* collectionOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "post_update"={
* "path"="/clients/{id}",
* "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_CLIENT')",
* "description"="Update a Client with method POST (using content-type: 'multipart')",
* "method"="POST",
* "controller"="App\Controller\Api\ClientController::update"
* }
* }
* )
* @ApiFilter(PropertyFilter::class,
* arguments={
* "parameterName"="fields",
* "overrideDefaultProperties"=true
* }
* )
* @ApiFilter(OrderFilter::class, properties={"id", "user.id", "user.active", "user.lastLogin", "user.status", "user.quotas"})
* @ApiFilter(SearchFilter::class, properties={"user.email": "partial", "user.status": "exact", "user.active": "partial", "company.name": "partial", "company.id": "exact"})
* @ORM\Entity(repositoryClass=ClientRepository::class)
* @ORM\HasLifecycleCallbacks()
*/
class Client
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"client:read", "user:read", "company:read", "team_user:read", "chat_message:read", "chat:read", "message:read"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"client:read", "client:write", "user:read", "user:write", "company:read", "team_user:read", "chat_message:read", "chat:read", "message:read"})
* @Assert\NotBlank
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"client:read", "client:write", "user:read", "user:write", "company:read", "team_user:read", "chat_message:read", "chat:read", "message:read"})
* @Assert\NotBlank
*/
private $lastName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"client:read", "client:write", "user:read", "user:write", "company:read"})
*/
private $paymentUid;
/**
* @ORM\OneToOne(targetEntity=User::class, inversedBy="client", cascade={"persist", "refresh", "remove"})
* @ORM\JoinColumn(nullable=false)
* @Groups({"client:read", "client:write", "company:read"})
* @var User
*/
private $user;
/**
* @ORM\OneToMany(targetEntity=EmailProgrammation::class, mappedBy="client", orphanRemoval=true)
*/
private $emailProgrammations;
/**
* @ORM\OneToMany(targetEntity=Registration::class, mappedBy="client", orphanRemoval=true)
*/
private $registrations;
/**
* @ORM\OneToMany(targetEntity=PubClient::class, mappedBy="client", orphanRemoval=true, cascade={"persist", "refresh", "remove"})
*/
private $pubClients;
/**
* @ORM\ManyToOne(targetEntity=Company::class, inversedBy="clients")
* @ORM\JoinColumn(nullable=false)
* @Assert\NotBlank
* @Groups({"client:read", "client:write", "user:read", "user:write", "message:read"})
*/
private $company;
/**
* @ORM\Column(type="boolean", nullable=true , options={"default": "0"})
* @Groups({"client:read", "client:write", "user:read", "user:write", "company:read"})
*/
private $isCgu = false;
/**
* @ORM\ManyToOne(targetEntity=Segmentation::class, inversedBy="clients")
* @Groups({"client:read", "client:write", "user:read", "user:write", "company:read", "chat_message:read", "chat:read"})
*/
private $segmentation;
/**
* @ORM\ManyToOne(targetEntity=Association::class, inversedBy="clients")
* @Groups({"client:read", "client:write", "user:read", "user:write", "company:read"})
* @ApiSubresource(maxDepth=1)
*/
private $association;
/**
* @ORM\ManyToOne(targetEntity=MediaObject::class, cascade={"persist", "refresh", "remove"})
* @Groups({"client:read", "team_user:read", "user:read", "chat_message:read", "chat:read"})
*/
private $avatar;
/**
* @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
* @Gedmo\Timestampable(on="create")
* @Groups({"client:read", "client:read:createdAt"})
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Gedmo\Timestampable(on="update")
* @Groups({"client:read", "client:read:updatedAt"})
*/
private $updatedAt;
public function __construct()
{
$this->user = new User();
$this->emailProgrammations = new ArrayCollection();
$this->registrations = new ArrayCollection();
$this->pubClients = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(?string $firstName): self
{
$this->firstName = $firstName;
$this->user->setFirstName($this->firstName);
$this->user->setUpdatedAt(new \DateTime);
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(?string $lastName): self
{
$this->lastName = $lastName;
$this->user->setLastName($this->lastName);
$this->user->setUpdatedAt(new \DateTime);
return $this;
}
public function getPaymentUid(): ?string
{
return $this->paymentUid;
}
public function setPaymentUid(?string $paymentUid): self
{
$this->paymentUid = $paymentUid;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(User $user): self
{
$this->user = $user;
return $this;
}
/**
* @return Collection|EmailProgrammation[]
*/
public function getEmailProgrammations(): Collection
{
return $this->emailProgrammations;
}
public function addEmailProgrammation(EmailProgrammation $emailProgrammation): self
{
if (!$this->emailProgrammations->contains($emailProgrammation)) {
$this->emailProgrammations[] = $emailProgrammation;
$emailProgrammation->setClient($this);
}
return $this;
}
public function removeEmailProgrammation(EmailProgrammation $emailProgrammation): self
{
if ($this->emailProgrammations->removeElement($emailProgrammation)) {
// set the owning side to null (unless already changed)
if ($emailProgrammation->getClient() === $this) {
$emailProgrammation->setClient(null);
}
}
return $this;
}
/**
* @return Collection|Registration[]
*/
public function getRegistrations(): Collection
{
return $this->registrations;
}
public function addRegistration(Registration $registration): self
{
if (!$this->registrations->contains($registration)) {
$this->registrations[] = $registration;
$registration->setClient($this);
}
return $this;
}
public function removeRegistration(Registration $registration): self
{
if ($this->registrations->removeElement($registration)) {
// set the owning side to null (unless already changed)
if ($registration->getClient() === $this) {
$registration->setClient(null);
}
}
return $this;
}
/**
* @return Collection|PubClient[]
*/
public function getPubClients(): Collection
{
return $this->pubClients;
}
public function addPubClient(PubClient $pubClient): self
{
if (!$this->pubClients->contains($pubClient)) {
$this->pubClients[] = $pubClient;
$pubClient->setClient($this);
}
return $this;
}
public function removePubClient(PubClient $pubClient): self
{
if ($this->pubClients->removeElement($pubClient)) {
// set the owning side to null (unless already changed)
if ($pubClient->getClient() === $this) {
$pubClient->setClient(null);
}
}
return $this;
}
public function __toString()
{
return $this->firstName . ' ' . $this->lastName;
}
/**
* @ORM\PrePersist()
*/
public function prePersist()
{
$this->user->setCategory(User::GROUP_CLIENT);
$this->user->addRole(User::ROLE_CLIENT);
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
/**
* Return in coming events
*/
public function getInComingEvents()
{
$inComingEvents = [];
if(!empty($this->getRegistrations()->toArray())) {
$today = new \DateTime("now");
foreach ($this->getRegistrations()->toArray() as $key => $registration) {
if($registration instanceof Registration && $registration->getEvent() instanceof Event) {
if(in_array($registration->getStatus(), [Registration::STATUS_REGISTER])
&& in_array($registration->getEvent()->getStatus(), [Event::STATUS_VALID])
&& $registration->getEvent()->getStart() >= $today) {
$inComingEvents[] = $registration->getEvent();
}
}
}
$inComingEvents = array_unique($inComingEvents, SORT_REGULAR);
}
return $inComingEvents;
}
/**
* Return past events
*/
public function getPastEvents()
{
$pastEvents = [];
if(!empty($this->getRegistrations()->toArray())) {
$today = new \DateTime("now");
foreach ($this->getRegistrations()->toArray() as $key => $registration) {
if($registration instanceof Registration && $registration->getEvent() instanceof Event) {
if($registration->getEvent()->getEnd() <= $today) {
$pastEvents[] = $registration->getEvent();
}
}
}
$pastEvents = array_unique($pastEvents, SORT_REGULAR);
}
return $pastEvents;
}
public function getIsCgu(): ?bool
{
return $this->isCgu;
}
public function setIsCgu(?bool $isCgu): self
{
$this->isCgu = $isCgu;
return $this;
}
public function getSegmentation(): ?Segmentation
{
return $this->segmentation;
}
public function setSegmentation(?Segmentation $segmentation): self
{
$this->segmentation = $segmentation;
return $this;
}
public function getAssociation(): ?Association
{
return $this->association;
}
public function setAssociation(?Association $association): self
{
$this->association = $association;
return $this;
}
public function getAvatar(): ?MediaObject
{
return $this->avatar;
}
public function setAvatar(?MediaObject $avatar): self
{
$this->avatar = $avatar;
return $this;
}
/**
* @Groups({"client:write"})
*/
public function setAvatarFile($file = null): self
{
if($file instanceof UploadedFile) {
$avatar = empty($this->avatar) ? new MediaObject : $this->avatar;
$avatar->setFile($file);
$this->setAvatar($avatar);
}
return $this;
}
public function getCreatedAt(): ?\DateTime
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTime $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTime
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTime $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @Groups({"client:write"})
*/
public function setEmail(string $email): self
{
if($this->getUser()) {
$this->getUser()->setEmail(strtolower($email));
$this->getUser()->setUpdatedAt(new \DateTime());
}
return $this;
}
}