src/Entity/RecruitmentCampaign.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Embedded\DateAndTime;
  4. use App\Entity\File\Biblios;
  5. use App\Features\Core\Itranslatable;
  6. use App\Form\RecruitmentCampaignType;
  7. use App\Repository\RecruitmentCampaignRepository;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use OSAdmin\Annotation\EntityConfig;
  12. use OSAdmin\Annotation\EntityFormConfig;
  13. use OSAdmin\Annotation\EntityToRoute;
  14. use OSAdmin\Util\EntityExportableInterface;
  15. use Symfony\Component\Validator\Constraints\IsTrue;
  16. use Symfony\Component\Validator\Constraints\NotNull;
  17. use Symfony\Component\Validator\Constraints\Valid;
  18. #[ORM\Entity(repositoryClassRecruitmentCampaignRepository::class)]
  19. #[EntityToRoute(
  20.     config: new EntityConfig(
  21.         title"Gestion des campagnes de recrutement",
  22.         editSubtitle"Edition d'une campagne",
  23.         listSubtitle"Liste des campagnes",
  24.     ),
  25.     formConfig: new EntityFormConfig(
  26.         formClassRecruitmentCampaignType::class
  27.     )
  28. )]
  29. class RecruitmentCampaign implements EntityExportableInterfaceItranslatable
  30. {
  31.     #[ORM\Id]
  32.     #[ORM\GeneratedValue]
  33.     #[ORM\Column(type'integer')]
  34.     private ?int $id;
  35.     #[ORM\OneToMany(mappedBy'campaign'targetEntityRecruitmentCampaignService::class, cascade: ["persist""remove"], fetch'EAGER'orphanRemovaltrue)]
  36.     private Collection $services;
  37.     #[ORM\OneToMany(mappedBy'campaign'targetEntityRecruitmentCampaignAttachmentType::class, cascade: ["persist""remove"], fetch'EAGER'orphanRemovaltrue)]
  38.     private Collection $attachmentType;
  39.     #[ORM\OneToMany(mappedBy'campaign'targetEntityProviderRequest::class, fetch'EAGER')]
  40.     private Collection $requests;
  41.     #[ORM\Column(type'datetime_immutable')]
  42.     private ?\DateTimeImmutable $createdAt;
  43.     #[ORM\Column(type'boolean')]
  44.     private ?bool $isEnabled;
  45.     #[ORM\Embedded(class: DateAndTime::class)]
  46.     #[Valid]
  47.     private ?DateAndTime $start;
  48.     #[ORM\Embedded(class: DateAndTime::class)]
  49.     #[Valid]
  50.     private ?DateAndTime $end;
  51.     #[ORM\Column(type'string'length255nullabletrue)]
  52.     private string $uuid;
  53.     #[ORM\Column(type'string'length255nullabletrue)]
  54.     #[NotNull]
  55.     private ?string $namenull;
  56.     #[ORM\Column(type"json"nullabletrue)]
  57.     protected array|null $nameTrans= [];
  58.     #[ORM\Column(type'text'nullabletrue)]
  59.     private ?string $descriptionnull;
  60.     #[ORM\Column(type'json'nullabletrue)]
  61.     protected array|null $descriptionTrans= [];
  62.     #[ORM\OneToOne(targetEntityBiblios::class, cascade: ['persist''remove'])]
  63.     private ?Biblios $biblio;
  64.     #[ORM\Column(type'text'nullabletrue)]
  65.     private ?string $details;
  66.     #[ORM\Column(type'json'nullabletrue)]
  67.     protected array|null $detailsTrans= [];
  68.     public function __construct()
  69.     {
  70.         $this->services = new ArrayCollection();
  71.         $this->createdAt= new \DateTimeImmutable();
  72.         $this->isEnabledfalse;
  73.         $this->uuiduniqid(''true);
  74.         $this->start= new DateAndTime(new \DateTime('now'), new \DateTime('now'));
  75.         $this->end= new DateAndTime((new \DateTime('now'))->add(new \DateInterval('P30D')), new \DateTime('now'));
  76.         $this->attachmentType = new ArrayCollection();
  77.     }
  78.     public function getId(): ?int
  79.     {
  80.         return $this->id;
  81.     }
  82.     /**
  83.      * @return Collection<int, RecruitmentCampaignService>
  84.      */
  85.     public function getServices(): Collection
  86.     {
  87.         return $this->services;
  88.     }
  89.     public function addService(RecruitmentCampaignService $service): self
  90.     {
  91.         if (!$this->services->contains($service)) {
  92.             $this->services[] = $service;
  93.             $service->setCampaign($this);
  94.         }
  95.         return $this;
  96.     }
  97.     public function removeService(RecruitmentCampaignService $service): self
  98.     {
  99.         if ($this->services->removeElement($service)) {
  100.             // set the owning side to null (unless already changed)
  101.             if ($service->getCampaign() === $this) {
  102.                 $service->setCampaign(null);
  103.             }
  104.         }
  105.         return $this;
  106.     }
  107.     public function getCreatedAt(): ?\DateTimeImmutable
  108.     {
  109.         return $this->createdAt;
  110.     }
  111.     public function setCreatedAt(\DateTimeImmutable $createdAt): self
  112.     {
  113.         $this->createdAt $createdAt;
  114.         return $this;
  115.     }
  116.     public function getIsEnabled(): ?bool
  117.     {
  118.         return $this->isEnabled;
  119.     }
  120.     public function setIsEnabled(bool $isEnabled): self
  121.     {
  122.         $this->isEnabled $isEnabled;
  123.         return $this;
  124.     }
  125.     /**
  126.      * @return DateAndTime|null
  127.      */
  128.     public function getStart(): ?DateAndTime
  129.     {
  130.         return $this->start;
  131.     }
  132.     /**
  133.      * @param DateAndTime|null $start
  134.      */
  135.     public function setStart(?DateAndTime $start): void
  136.     {
  137.         $this->start $start;
  138.     }
  139.     /**
  140.      * @return DateAndTime|null
  141.      */
  142.     public function getEnd(): ?DateAndTime
  143.     {
  144.         return $this->end;
  145.     }
  146.     /**
  147.      * @param DateAndTime|null $end
  148.      */
  149.     public function setEnd(?DateAndTime $end): void
  150.     {
  151.         $this->end $end;
  152.     }
  153.     #[IsTrue(message"La date de début doit être antérieure à la date de fin")]
  154.     public function isDateValid(): bool
  155.     {
  156.         return $this->end->asDate() > $this->start->asDate();
  157.     }
  158.     public function getUuid(): ?string
  159.     {
  160.         return $this->uuid;
  161.     }
  162.     public function setUuid(?string $uuid): self
  163.     {
  164.         $this->uuid $uuid;
  165.         return $this;
  166.     }
  167.     public function date(): string
  168.     {
  169.         return $this->start->asDate()?->format('d/m/Y') . ' - ' $this->end->asDate()?->format('d/m/Y');
  170.     }
  171.     public function __toString(): string
  172.     {
  173.         return $this->date();
  174.     }
  175.     public function isActif(): bool
  176.     {
  177.         return $this->end->asDate() > new \DateTimeImmutable();
  178.     }
  179.     public function getName(): ?string
  180.     {
  181.         if($this->nameTrans && $this->currentLocal && isset($this->nameTrans[$this->currentLocal])) {
  182.             return $this->nameTrans[$this->currentLocal];
  183.         }
  184.         return $this->nameTrans["fr"] ?? $this->name;
  185.     }
  186.     public function getDescription(): ?string
  187.     {
  188.         if($this->descriptionTrans && $this->currentLocal && isset($this->descriptionTrans[$this->currentLocal])) {
  189.             return $this->descriptionTrans[$this->currentLocal];
  190.         }
  191.         return $this->descriptionTrans["fr"] ?? $this->description;
  192.     }
  193.     public function getBiblio(): ?Biblios
  194.     {
  195.         return $this->biblio;
  196.     }
  197.     public function setBiblio(?Biblios $biblio): self
  198.     {
  199.         $this->biblio $biblio;
  200.         return $this;
  201.     }
  202.     public function getDetails(): ?string
  203.     {
  204.         if($this->detailsTrans && $this->currentLocal && isset($this->detailsTrans[$this->currentLocal])) {
  205.             return $this->detailsTrans[$this->currentLocal];
  206.         }
  207.         return $this->detailsTrans["fr"] ?? $this->details;
  208.     }
  209.     /**
  210.      * @return Collection<int, RecruitmentCampaignAttachmentType>
  211.      */
  212.     public function getAttachmentType(): Collection
  213.     {
  214.         return $this->attachmentType;
  215.     }
  216.     public function addAttachmentType(RecruitmentCampaignAttachmentType $attachmentType): self
  217.     {
  218.         if (!$this->attachmentType->contains($attachmentType)) {
  219.             $this->attachmentType[] = $attachmentType;
  220.             $attachmentType->setCampaign($this);
  221.         }
  222.         return $this;
  223.     }
  224.     public function removeAttachmentType(RecruitmentCampaignAttachmentType $attachmentType): self
  225.     {
  226.         if ($this->attachmentType->removeElement($attachmentType)) {
  227.             // set the owning side to null (unless already changed)
  228.             if ($attachmentType->getCampaign() === $this) {
  229.                 $attachmentType->setCampaign(null);
  230.             }
  231.         }
  232.         return $this;
  233.     }
  234.     public function getNbAcceptedRequest(): int
  235.     {
  236.         if($this->requests->isEmpty()) {
  237.             return 0;
  238.         }
  239.         return $this->requests->filter(function (ProviderRequest $request) {
  240.             return $request->getStatus() === "approved";
  241.         })->count();
  242.     }
  243.     public function getNbRejectedRequest(): int
  244.     {
  245.         if($this->requests->isEmpty()) {
  246.             return 0;
  247.         }
  248.         return $this->requests->filter(function (ProviderRequest $request) {
  249.             return !$request->getStatus() || $request->getStatus() === "rejected" ;
  250.         })->count();
  251.     }
  252.     public function getNbRequest(): ?int
  253.     {
  254.         return $this->requests->count();
  255.     }
  256.     public function getExportData(): array
  257.     {
  258.         return [
  259.             'ID' => function (RecruitmentCampaign $item) {
  260.                 return $item->getId();
  261.             },
  262.             'Campagne' => function (RecruitmentCampaign $item) {
  263.                 return $item->getName();
  264.             },
  265.             'Date' => function (RecruitmentCampaign $item) {
  266.                 return $item->date();
  267.             },
  268.             'Services' => function (RecruitmentCampaign $item) {
  269.                 return implode(', '$item->getServices()->map(function (RecruitmentCampaignService $service) {
  270.                     return $service->getService()?->getName();
  271.                 })->getValues());
  272.             },
  273.             'Nombre de demande' => function (RecruitmentCampaign $item) {
  274.                 return $item->getNbRequest();
  275.             },
  276.             'Demande acceptée' => function (RecruitmentCampaign $item) {
  277.                 return $item->getNbAcceptedRequest();
  278.             },
  279.         ];
  280.     }
  281.     /**
  282.      * @return array|null
  283.      */
  284.     public function getDescriptionTrans(): ?array
  285.     {
  286.         if(!$this->descriptionTrans && $this->description){
  287.             $this->descriptionTrans= [
  288.                 "fr" => $this->description,
  289.             ];
  290.         }
  291.         return $this->descriptionTrans;
  292.     }
  293.     /**
  294.      * @return array|null
  295.      */
  296.     public function getNameTrans(): ?array
  297.     {
  298.         if(!$this->nameTrans && $this->name){
  299.             $this->nameTrans= [
  300.                 "fr" => $this->name,
  301.             ];
  302.         }
  303.         return $this->nameTrans;
  304.     }
  305.     /**
  306.      * @return array|null
  307.      */
  308.     public function getDetailsTrans(): ?array
  309.     {
  310.         if(!$this->detailsTrans && $this->details){
  311.             $this->detailsTrans= [
  312.                 "fr" => $this->details,
  313.             ];
  314.         }
  315.         return $this->detailsTrans;
  316.     }
  317.     /**
  318.      * @param array|null $nameTrans
  319.      */
  320.     public function setNameTrans(?array $nameTrans): void
  321.     {
  322.         $this->nameTrans $nameTrans;
  323.     }
  324.     /**
  325.      * @param array|null $descriptionTrans
  326.      */
  327.     public function setDescriptionTrans(?array $descriptionTrans): void
  328.     {
  329.         $this->descriptionTrans $descriptionTrans;
  330.     }
  331.     /**
  332.      * @param array|null $detailsTrans
  333.      */
  334.     public function setDetailsTrans(?array $detailsTrans): void
  335.     {
  336.         $this->detailsTrans $detailsTrans;
  337.     }
  338.     private string $currentLocal "fr";
  339.     public function setCurrentLocal(string $locale): Itranslatable
  340.     {
  341.         $this->currentLocal $locale;
  342.         return $this;
  343.     }
  344.     public function getCurrentLocal(): string
  345.     {
  346.         return $this->currentLocal;
  347.     }
  348. }