<?php
namespace App\Entity;
use App\Entity\Embedded\DateAndTime;
use App\Entity\File\Biblios;
use App\Features\Core\Itranslatable;
use App\Form\RecruitmentCampaignType;
use App\Repository\RecruitmentCampaignRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use OSAdmin\Annotation\EntityConfig;
use OSAdmin\Annotation\EntityFormConfig;
use OSAdmin\Annotation\EntityToRoute;
use OSAdmin\Util\EntityExportableInterface;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Valid;
#[ORM\Entity(repositoryClass: RecruitmentCampaignRepository::class)]
#[EntityToRoute(
config: new EntityConfig(
title: "Gestion des campagnes de recrutement",
editSubtitle: "Edition d'une campagne",
listSubtitle: "Liste des campagnes",
),
formConfig: new EntityFormConfig(
formClass: RecruitmentCampaignType::class
)
)]
class RecruitmentCampaign implements EntityExportableInterface, Itranslatable
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id;
#[ORM\OneToMany(mappedBy: 'campaign', targetEntity: RecruitmentCampaignService::class, cascade: ["persist", "remove"], fetch: 'EAGER', orphanRemoval: true)]
private Collection $services;
#[ORM\OneToMany(mappedBy: 'campaign', targetEntity: RecruitmentCampaignAttachmentType::class, cascade: ["persist", "remove"], fetch: 'EAGER', orphanRemoval: true)]
private Collection $attachmentType;
#[ORM\OneToMany(mappedBy: 'campaign', targetEntity: ProviderRequest::class, fetch: 'EAGER')]
private Collection $requests;
#[ORM\Column(type: 'datetime_immutable')]
private ?\DateTimeImmutable $createdAt;
#[ORM\Column(type: 'boolean')]
private ?bool $isEnabled;
#[ORM\Embedded(class: DateAndTime::class)]
#[Valid]
private ?DateAndTime $start;
#[ORM\Embedded(class: DateAndTime::class)]
#[Valid]
private ?DateAndTime $end;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private string $uuid;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[NotNull]
private ?string $name= null;
#[ORM\Column(type: "json", nullable: true)]
protected array|null $nameTrans= [];
#[ORM\Column(type: 'text', nullable: true)]
private ?string $description= null;
#[ORM\Column(type: 'json', nullable: true)]
protected array|null $descriptionTrans= [];
#[ORM\OneToOne(targetEntity: Biblios::class, cascade: ['persist', 'remove'])]
private ?Biblios $biblio;
#[ORM\Column(type: 'text', nullable: true)]
private ?string $details;
#[ORM\Column(type: 'json', nullable: true)]
protected array|null $detailsTrans= [];
public function __construct()
{
$this->services = new ArrayCollection();
$this->createdAt= new \DateTimeImmutable();
$this->isEnabled= false;
$this->uuid= uniqid('', true);
$this->start= new DateAndTime(new \DateTime('now'), new \DateTime('now'));
$this->end= new DateAndTime((new \DateTime('now'))->add(new \DateInterval('P30D')), new \DateTime('now'));
$this->attachmentType = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return Collection<int, RecruitmentCampaignService>
*/
public function getServices(): Collection
{
return $this->services;
}
public function addService(RecruitmentCampaignService $service): self
{
if (!$this->services->contains($service)) {
$this->services[] = $service;
$service->setCampaign($this);
}
return $this;
}
public function removeService(RecruitmentCampaignService $service): self
{
if ($this->services->removeElement($service)) {
// set the owning side to null (unless already changed)
if ($service->getCampaign() === $this) {
$service->setCampaign(null);
}
}
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getIsEnabled(): ?bool
{
return $this->isEnabled;
}
public function setIsEnabled(bool $isEnabled): self
{
$this->isEnabled = $isEnabled;
return $this;
}
/**
* @return DateAndTime|null
*/
public function getStart(): ?DateAndTime
{
return $this->start;
}
/**
* @param DateAndTime|null $start
*/
public function setStart(?DateAndTime $start): void
{
$this->start = $start;
}
/**
* @return DateAndTime|null
*/
public function getEnd(): ?DateAndTime
{
return $this->end;
}
/**
* @param DateAndTime|null $end
*/
public function setEnd(?DateAndTime $end): void
{
$this->end = $end;
}
#[IsTrue(message: "La date de début doit être antérieure à la date de fin")]
public function isDateValid(): bool
{
return $this->end->asDate() > $this->start->asDate();
}
public function getUuid(): ?string
{
return $this->uuid;
}
public function setUuid(?string $uuid): self
{
$this->uuid = $uuid;
return $this;
}
public function date(): string
{
return $this->start->asDate()?->format('d/m/Y') . ' - ' . $this->end->asDate()?->format('d/m/Y');
}
public function __toString(): string
{
return $this->date();
}
public function isActif(): bool
{
return $this->end->asDate() > new \DateTimeImmutable();
}
public function getName(): ?string
{
if($this->nameTrans && $this->currentLocal && isset($this->nameTrans[$this->currentLocal])) {
return $this->nameTrans[$this->currentLocal];
}
return $this->nameTrans["fr"] ?? $this->name;
}
public function getDescription(): ?string
{
if($this->descriptionTrans && $this->currentLocal && isset($this->descriptionTrans[$this->currentLocal])) {
return $this->descriptionTrans[$this->currentLocal];
}
return $this->descriptionTrans["fr"] ?? $this->description;
}
public function getBiblio(): ?Biblios
{
return $this->biblio;
}
public function setBiblio(?Biblios $biblio): self
{
$this->biblio = $biblio;
return $this;
}
public function getDetails(): ?string
{
if($this->detailsTrans && $this->currentLocal && isset($this->detailsTrans[$this->currentLocal])) {
return $this->detailsTrans[$this->currentLocal];
}
return $this->detailsTrans["fr"] ?? $this->details;
}
/**
* @return Collection<int, RecruitmentCampaignAttachmentType>
*/
public function getAttachmentType(): Collection
{
return $this->attachmentType;
}
public function addAttachmentType(RecruitmentCampaignAttachmentType $attachmentType): self
{
if (!$this->attachmentType->contains($attachmentType)) {
$this->attachmentType[] = $attachmentType;
$attachmentType->setCampaign($this);
}
return $this;
}
public function removeAttachmentType(RecruitmentCampaignAttachmentType $attachmentType): self
{
if ($this->attachmentType->removeElement($attachmentType)) {
// set the owning side to null (unless already changed)
if ($attachmentType->getCampaign() === $this) {
$attachmentType->setCampaign(null);
}
}
return $this;
}
public function getNbAcceptedRequest(): int
{
if($this->requests->isEmpty()) {
return 0;
}
return $this->requests->filter(function (ProviderRequest $request) {
return $request->getStatus() === "approved";
})->count();
}
public function getNbRejectedRequest(): int
{
if($this->requests->isEmpty()) {
return 0;
}
return $this->requests->filter(function (ProviderRequest $request) {
return !$request->getStatus() || $request->getStatus() === "rejected" ;
})->count();
}
public function getNbRequest(): ?int
{
return $this->requests->count();
}
public function getExportData(): array
{
return [
'ID' => function (RecruitmentCampaign $item) {
return $item->getId();
},
'Campagne' => function (RecruitmentCampaign $item) {
return $item->getName();
},
'Date' => function (RecruitmentCampaign $item) {
return $item->date();
},
'Services' => function (RecruitmentCampaign $item) {
return implode(', ', $item->getServices()->map(function (RecruitmentCampaignService $service) {
return $service->getService()?->getName();
})->getValues());
},
'Nombre de demande' => function (RecruitmentCampaign $item) {
return $item->getNbRequest();
},
'Demande acceptée' => function (RecruitmentCampaign $item) {
return $item->getNbAcceptedRequest();
},
];
}
/**
* @return array|null
*/
public function getDescriptionTrans(): ?array
{
if(!$this->descriptionTrans && $this->description){
$this->descriptionTrans= [
"fr" => $this->description,
];
}
return $this->descriptionTrans;
}
/**
* @return array|null
*/
public function getNameTrans(): ?array
{
if(!$this->nameTrans && $this->name){
$this->nameTrans= [
"fr" => $this->name,
];
}
return $this->nameTrans;
}
/**
* @return array|null
*/
public function getDetailsTrans(): ?array
{
if(!$this->detailsTrans && $this->details){
$this->detailsTrans= [
"fr" => $this->details,
];
}
return $this->detailsTrans;
}
/**
* @param array|null $nameTrans
*/
public function setNameTrans(?array $nameTrans): void
{
$this->nameTrans = $nameTrans;
}
/**
* @param array|null $descriptionTrans
*/
public function setDescriptionTrans(?array $descriptionTrans): void
{
$this->descriptionTrans = $descriptionTrans;
}
/**
* @param array|null $detailsTrans
*/
public function setDetailsTrans(?array $detailsTrans): void
{
$this->detailsTrans = $detailsTrans;
}
private string $currentLocal = "fr";
public function setCurrentLocal(string $locale): Itranslatable
{
$this->currentLocal = $locale;
return $this;
}
public function getCurrentLocal(): string
{
return $this->currentLocal;
}
}