src/Entity/Client.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Annotation\ApiSubresource;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  8. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  9. use App\Repository\ClientRepository;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Doctrine\ORM\Mapping\InheritanceType;
  14. use Gedmo\Mapping\Annotation as Gedmo;
  15. use Symfony\Component\HttpFoundation\File\File;
  16. use Symfony\Component\HttpFoundation\File\UploadedFile;
  17. use Symfony\Component\Serializer\Annotation\Groups;
  18. use Symfony\Component\Validator\Constraints as Assert;
  19. /**
  20.  * @InheritanceType("SINGLE_TABLE")
  21.  * @ApiResource(
  22.  *      normalizationContext={
  23.  *          "groups"={"client:read"}
  24.  *      },
  25.  *      denormalizationContext={
  26.  *          "groups"={"client:write"}
  27.  *      },
  28.  *     itemOperations={
  29.  *          "get"={
  30.  *              "security"="is_granted('ROLE_USER')"
  31.  *          }, 
  32.  *          "patch"={
  33.  *              "security"="is_granted('ROLE_ADMIN') or user == object.user"
  34.  *          }
  35.  *      },
  36.  *      collectionOperations={
  37.  *          "get"={
  38.  *              "security"="is_granted('ROLE_USER')"
  39.  *          },
  40.  *          "post_update"={
  41.  *              "path"="/clients/{id}",
  42.  *              "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_CLIENT')",
  43.  *              "description"="Update a Client with method POST (using content-type: 'multipart')",
  44.  *              "method"="POST",
  45.  *              "controller"="App\Controller\Api\ClientController::update"
  46.  *          }
  47.  *     }
  48.  * )
  49.  * @ApiFilter(PropertyFilter::class, 
  50.  *      arguments={
  51.  *          "parameterName"="fields", 
  52.  *          "overrideDefaultProperties"=true
  53.  *     }
  54.  * )
  55.  * @ApiFilter(OrderFilter::class, properties={"id", "user.id", "user.active", "user.lastLogin", "user.status", "user.quotas"})
  56.  * @ApiFilter(SearchFilter::class, properties={"user.email": "partial", "user.status": "exact", "user.active": "partial", "company.name": "partial", "company.id": "exact"})
  57.  * @ORM\Entity(repositoryClass=ClientRepository::class)
  58.  * @ORM\HasLifecycleCallbacks()
  59.  */
  60. class Client
  61. {
  62.     /**
  63.      * @ORM\Id
  64.      * @ORM\GeneratedValue
  65.      * @ORM\Column(type="integer")
  66.      * @Groups({"client:read", "user:read", "company:read", "team_user:read", "chat_message:read", "chat:read", "message:read"})
  67.      */
  68.     private $id;
  69.     /**
  70.      * @ORM\Column(type="string", length=255, nullable=true)
  71.      * @Groups({"client:read", "client:write", "user:read", "user:write", "company:read", "team_user:read", "chat_message:read", "chat:read", "message:read"})
  72.      * @Assert\NotBlank
  73.      */
  74.     private $firstName;
  75.     /**
  76.      * @ORM\Column(type="string", length=255, nullable=true)
  77.      * @Groups({"client:read", "client:write", "user:read", "user:write", "company:read", "team_user:read", "chat_message:read", "chat:read", "message:read"})
  78.      * @Assert\NotBlank
  79.      */
  80.     private $lastName;
  81.     /**
  82.      * @ORM\Column(type="string", length=255, nullable=true)
  83.      * @Groups({"client:read", "client:write", "user:read", "user:write", "company:read"})
  84.      */
  85.     private $paymentUid;
  86.     /**
  87.      * @ORM\OneToOne(targetEntity=User::class, inversedBy="client", cascade={"persist", "refresh", "remove"})
  88.      * @ORM\JoinColumn(nullable=false)
  89.      * @Groups({"client:read", "client:write", "company:read"})
  90.      * @var User
  91.      */
  92.     private $user;
  93.     /**
  94.      * @ORM\OneToMany(targetEntity=EmailProgrammation::class, mappedBy="client", orphanRemoval=true)
  95.      */
  96.     private $emailProgrammations;
  97.     /**
  98.      * @ORM\OneToMany(targetEntity=Registration::class, mappedBy="client", orphanRemoval=true)
  99.      */
  100.     private $registrations;
  101.     /**
  102.      * @ORM\OneToMany(targetEntity=PubClient::class, mappedBy="client", orphanRemoval=true, cascade={"persist", "refresh", "remove"})
  103.      */
  104.     private $pubClients;
  105.     /**
  106.      * @ORM\ManyToOne(targetEntity=Company::class, inversedBy="clients")
  107.      * @ORM\JoinColumn(nullable=false)
  108.      * @Assert\NotBlank
  109.      * @Groups({"client:read", "client:write", "user:read", "user:write", "message:read"})
  110.      */
  111.     private $company;
  112.     /**
  113.      * @ORM\Column(type="boolean", nullable=true , options={"default": "0"})
  114.      * @Groups({"client:read", "client:write", "user:read", "user:write", "company:read"})
  115.      */
  116.     private $isCgu false;
  117.     /**
  118.      * @ORM\ManyToOne(targetEntity=Segmentation::class, inversedBy="clients")
  119.      * @Groups({"client:read", "client:write", "user:read", "user:write", "company:read", "chat_message:read", "chat:read"})
  120.      */
  121.     private $segmentation;
  122.     /**
  123.      * @ORM\ManyToOne(targetEntity=Association::class, inversedBy="clients")
  124.      * @Groups({"client:read", "client:write", "user:read", "user:write", "company:read"})
  125.      * @ApiSubresource(maxDepth=1)
  126.      */
  127.     private $association;
  128.     /**
  129.      * @ORM\ManyToOne(targetEntity=MediaObject::class, cascade={"persist", "refresh", "remove"})
  130.      * @Groups({"client:read", "team_user:read", "user:read", "chat_message:read", "chat:read"})
  131.      */
  132.     private $avatar;
  133.     /**
  134.      * @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
  135.      * @Gedmo\Timestampable(on="create")
  136.      * @Groups({"client:read", "client:read:createdAt"})
  137.      */
  138.     private $createdAt;
  139.     /**
  140.      * @ORM\Column(type="datetime", nullable=true)
  141.      * @Gedmo\Timestampable(on="update")
  142.      * @Groups({"client:read", "client:read:updatedAt"})
  143.      */
  144.     private $updatedAt;
  145.     
  146.     public function __construct()
  147.     {
  148.         $this->user = new User();
  149.         $this->emailProgrammations = new ArrayCollection();
  150.         $this->registrations = new ArrayCollection();
  151.         $this->pubClients = new ArrayCollection();
  152.     }
  153.     public function getId(): ?int
  154.     {
  155.         return $this->id;
  156.     }
  157.     public function getFirstName(): ?string
  158.     {
  159.         return $this->firstName;
  160.     }
  161.     public function setFirstName(?string $firstName): self
  162.     {
  163.         $this->firstName $firstName;
  164.         $this->user->setFirstName($this->firstName);
  165.         $this->user->setUpdatedAt(new \DateTime);
  166.         return $this;
  167.     }
  168.     public function getLastName(): ?string
  169.     {
  170.         return $this->lastName;
  171.     }
  172.     public function setLastName(?string $lastName): self
  173.     {
  174.         $this->lastName $lastName;
  175.         $this->user->setLastName($this->lastName);
  176.         $this->user->setUpdatedAt(new \DateTime);
  177.         return $this;
  178.     }
  179.     public function getPaymentUid(): ?string
  180.     {
  181.         return $this->paymentUid;
  182.     }
  183.     public function setPaymentUid(?string $paymentUid): self
  184.     {
  185.         $this->paymentUid $paymentUid;
  186.         return $this;
  187.     }
  188.     public function getUser(): ?User
  189.     {
  190.         return $this->user;
  191.     }
  192.     public function setUser(User $user): self
  193.     {
  194.         $this->user $user;
  195.         return $this;
  196.     }
  197.     /**
  198.      * @return Collection|EmailProgrammation[]
  199.      */
  200.     public function getEmailProgrammations(): Collection
  201.     {
  202.         return $this->emailProgrammations;
  203.     }
  204.     public function addEmailProgrammation(EmailProgrammation $emailProgrammation): self
  205.     {
  206.         if (!$this->emailProgrammations->contains($emailProgrammation)) {
  207.             $this->emailProgrammations[] = $emailProgrammation;
  208.             $emailProgrammation->setClient($this);
  209.         }
  210.         return $this;
  211.     }
  212.     public function removeEmailProgrammation(EmailProgrammation $emailProgrammation): self
  213.     {
  214.         if ($this->emailProgrammations->removeElement($emailProgrammation)) {
  215.             // set the owning side to null (unless already changed)
  216.             if ($emailProgrammation->getClient() === $this) {
  217.                 $emailProgrammation->setClient(null);
  218.             }
  219.         }
  220.         return $this;
  221.     }
  222.     /**
  223.      * @return Collection|Registration[]
  224.      */
  225.     public function getRegistrations(): Collection
  226.     {
  227.         return $this->registrations;
  228.     }
  229.     public function addRegistration(Registration $registration): self
  230.     {
  231.         if (!$this->registrations->contains($registration)) {
  232.             $this->registrations[] = $registration;
  233.             $registration->setClient($this);
  234.         }
  235.         return $this;
  236.     }
  237.     public function removeRegistration(Registration $registration): self
  238.     {
  239.         if ($this->registrations->removeElement($registration)) {
  240.             // set the owning side to null (unless already changed)
  241.             if ($registration->getClient() === $this) {
  242.                 $registration->setClient(null);
  243.             }
  244.         }
  245.         return $this;
  246.     }
  247.     /**
  248.      * @return Collection|PubClient[]
  249.      */
  250.     public function getPubClients(): Collection
  251.     {
  252.         return $this->pubClients;
  253.     }
  254.     public function addPubClient(PubClient $pubClient): self
  255.     {
  256.         if (!$this->pubClients->contains($pubClient)) {
  257.             $this->pubClients[] = $pubClient;
  258.             $pubClient->setClient($this);
  259.         }
  260.         return $this;
  261.     }
  262.     public function removePubClient(PubClient $pubClient): self
  263.     {
  264.         if ($this->pubClients->removeElement($pubClient)) {
  265.             // set the owning side to null (unless already changed)
  266.             if ($pubClient->getClient() === $this) {
  267.                 $pubClient->setClient(null);
  268.             }
  269.         }
  270.         return $this;
  271.     }
  272.     public function __toString()
  273.     {
  274.         return $this->firstName ' ' $this->lastName;
  275.     }
  276.     /**
  277.      * @ORM\PrePersist()
  278.      */
  279.     public function prePersist()
  280.     {
  281.         $this->user->setCategory(User::GROUP_CLIENT);
  282.         $this->user->addRole(User::ROLE_CLIENT);
  283.     }
  284.     public function getCompany(): ?Company
  285.     {
  286.         return $this->company;
  287.     }
  288.     public function setCompany(?Company $company): self
  289.     {
  290.         $this->company $company;
  291.         return $this;
  292.     }
  293.     /**
  294.      * Return in coming events
  295.      */
  296.     public function getInComingEvents()
  297.     {
  298.         $inComingEvents = [];
  299.         if(!empty($this->getRegistrations()->toArray())) {
  300.             $today = new \DateTime("now");
  301.             foreach ($this->getRegistrations()->toArray() as $key => $registration) {
  302.                 if($registration instanceof Registration && $registration->getEvent() instanceof Event) {
  303.                     if(in_array($registration->getStatus(), [Registration::STATUS_REGISTER]) 
  304.                         && in_array($registration->getEvent()->getStatus(), [Event::STATUS_VALID]) 
  305.                         && $registration->getEvent()->getStart() >= $today) {
  306.                         $inComingEvents[] = $registration->getEvent();
  307.                     }
  308.                 }
  309.             }
  310.             $inComingEvents array_unique($inComingEventsSORT_REGULAR);
  311.         }
  312.         return $inComingEvents;
  313.     }
  314.     /**
  315.      * Return past events
  316.      */
  317.     public function getPastEvents()
  318.     {
  319.         $pastEvents = [];
  320.         if(!empty($this->getRegistrations()->toArray())) {
  321.             $today = new \DateTime("now");
  322.             foreach ($this->getRegistrations()->toArray() as $key => $registration) {
  323.                 if($registration instanceof Registration && $registration->getEvent() instanceof Event) {
  324.                     if($registration->getEvent()->getEnd() <= $today) {
  325.                         $pastEvents[] = $registration->getEvent();
  326.                     }
  327.                 }
  328.             }
  329.             $pastEvents array_unique($pastEventsSORT_REGULAR);
  330.         }
  331.         return $pastEvents;
  332.     }
  333.     public function getIsCgu(): ?bool
  334.     {
  335.         return $this->isCgu;
  336.     }
  337.     public function setIsCgu(?bool $isCgu): self
  338.     {
  339.         $this->isCgu $isCgu;
  340.         return $this;
  341.     }
  342.     public function getSegmentation(): ?Segmentation
  343.     {
  344.         return $this->segmentation;
  345.     }
  346.     public function setSegmentation(?Segmentation $segmentation): self
  347.     {
  348.         $this->segmentation $segmentation;
  349.         return $this;
  350.     }
  351.     public function getAssociation(): ?Association
  352.     {
  353.         return $this->association;
  354.     }
  355.     public function setAssociation(?Association $association): self
  356.     {
  357.         $this->association $association;
  358.         return $this;
  359.     }
  360.     public function getAvatar(): ?MediaObject
  361.     {
  362.         return $this->avatar;
  363.     }
  364.     public function setAvatar(?MediaObject $avatar): self
  365.     {
  366.         $this->avatar $avatar;
  367.         return $this;
  368.     }
  369.     /**
  370.      * @Groups({"client:write"})
  371.      */
  372.     public function setAvatarFile($file null): self
  373.     {
  374.         if($file instanceof UploadedFile) {
  375.             $avatar = empty($this->avatar) ? new MediaObject $this->avatar;
  376.             $avatar->setFile($file);
  377.             $this->setAvatar($avatar);
  378.         }
  379.         return $this;
  380.     }
  381.     public function getCreatedAt(): ?\DateTime
  382.     {
  383.         return $this->createdAt;
  384.     }
  385.     public function setCreatedAt(?\DateTime $createdAt): self
  386.     {
  387.         $this->createdAt $createdAt;
  388.         return $this;
  389.     }
  390.     public function getUpdatedAt(): ?\DateTime
  391.     {
  392.         return $this->updatedAt;
  393.     }
  394.     public function setUpdatedAt(?\DateTime $updatedAt): self
  395.     {
  396.         $this->updatedAt $updatedAt;
  397.         return $this;
  398.     }
  399.     /**
  400.      * @Groups({"client:write"})
  401.      */
  402.     public function setEmail(string $email): self
  403.     {
  404.         if($this->getUser()) {
  405.             $this->getUser()->setEmail(strtolower($email));
  406.             $this->getUser()->setUpdatedAt(new \DateTime());
  407.         }
  408.         return $this;
  409.     }
  410. }