<?php
namespace App\Entity\Training;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\Training\SeanceRepository;
use App\Traits\Actions;
use DateInterval;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use InvalidArgumentException;
#[ORM\Entity(repositoryClass: SeanceRepository::class)]
#[ORM\Table(name: 'hs_tc_training_seance')]
#[ApiResource]
class Seance
{
use Actions;
#[ORM\ManyToOne(inversedBy: 'seances')]
#[ORM\JoinColumn(nullable: false)]
private ?Scheduleitem $scheduleitem = null;
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
private ?\DateTimeInterface $takesplaceOn = null;
#[ORM\Column(length: 255)]
private ?string $startAt = null;
#[ORM\Column(nullable: true)]
private ?string $length = null;
#[ORM\Column(nullable: true)]
private ?string $salle = null;
#[ORM\OneToMany(mappedBy: 'seance', targetEntity: Absence::class, orphanRemoval: true)]
private Collection $absences;
public function __construct()
{
// date_default_timezone_set('Africa/Casablanca');
$this->createAt=new \DateTime('now');
$this->published=true;
$this->absences = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getScheduleitem(): ?Scheduleitem
{
return $this->scheduleitem;
}
public function setScheduleitem(?Scheduleitem $scheduleitem): self
{
$this->scheduleitem = $scheduleitem;
return $this;
}
public function getTakesplaceOn(): ?\DateTimeInterface
{
return $this->takesplaceOn;
}
public function setTakesplaceOn(?\DateTimeInterface $takesplaceOn): self
{
$this->takesplaceOn = $takesplaceOn;
return $this;
}
public function getStartAt(): ?string
{
return $this->startAt==null?$this->scheduleitem->getStartAt():$this->startAt;
}
public function setStartAt(?string $startAt): void
{
$this->startAt = $startAt;
}
public function getLength(): ?string
{
return $this->length==null?$this->scheduleitem->getLength():$this->length;
}
public function setLength(?string $length): void
{
$this->length = $length;
}
/**
* @return Collection<int, Absence>
*/
public function getAbsences(): Collection
{
return $this->absences;
}
public function addAbsence(Absence $absence): self
{
if (!$this->absences->contains($absence)) {
$this->absences->add($absence);
$absence->setSeance($this);
}
return $this;
}
public function removeAbsence(Absence $absence): self
{
if ($this->absences->removeElement($absence)) {
// set the owning side to null (unless already changed)
if ($absence->getSeance() === $this) {
$absence->setSeance(null);
}
}
return $this;
}
public function setCreateAt2(\DateTime $createAt): Seance
{
$this->createAt = $createAt;
return $this;
}
public function getAbsenceByTrainee(Trainee $trainee): ?Absence
{
foreach ($this->absences as $absence) {
if ($absence->getTrainee() === $trainee) {
return $absence;
}
}
return null;
}
public function getAbsencesPublished()
{
$absences=[];
foreach ($this->getAbsences() as $absence) {
if ($absence->isPublished()===true) {
$absences[]=$absence;
}
}
return $absences;
}
public function getSalle(): ?string
{
return $this->salle;
}
public function setSalle(?string $salle): void
{
$this->salle = $salle;
}
function getDayFrench() {
$joursFrancais = [
1 => 'lundi',
2 => 'mardi',
3 => 'mercredi',
4 => 'jeudi',
5 => 'vendredi',
6 => 'samedi',
7 => 'dimanche',
];
return $joursFrancais[$this->takesplaceOn->format('N')] ?? "Jour invalide. Veuillez entrer un nombre entre 1 et 7.";
}
/**
* @throws \Exception
*/
function getLengthAsDateInterval()
{
// Regex pour capturer les heures et les minutes
$pattern = '/^(?:(\d+)h)?(?:\s*(\d+)min)?$/';
if (preg_match($pattern, $this->getLength(), $matches)) {
$hours = isset($matches[1]) ? (int)$matches[1] : 0;
$minutes = isset($matches[2]) ? (int)$matches[2] : 0;
// Création de l'objet DateInterval
$intervalSpec = sprintf('PT%dH%dM', $hours, $minutes);
return new DateInterval($intervalSpec);
} else {
throw new InvalidArgumentException("Invalid time string format: $this->length");
}
}
function getLengthAsSeconds() {
return ($this->getLengthAsDateInterval()->y * 365 * 24 * 60 * 60) +
($this->getLengthAsDateInterval()->m * 30 * 24 * 60 * 60) +
($this->getLengthAsDateInterval()->d * 24 * 60 * 60) +
($this->getLengthAsDateInterval()->h * 60 * 60) +
($this->getLengthAsDateInterval()->i * 60) +
$this->getLengthAsDateInterval()->s;
}
public function getEndAt()
{
$startAt=\DateTime::createFromFormat('d/m/Y H:i',(new \DateTime('now'))->format('d/m/Y')." ".$this->getStartAt());
$endAt=$startAt->add($this->getLengthAsDateInterval());
return $endAt->format('H:i');
}
}