src/Entity/Formation/Aseance.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Formation;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use App\Repository\Formation\AseanceRepository;
  5. use App\Traits\Actions;
  6. use DateInterval;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\DBAL\Types\Types;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use InvalidArgumentException;
  12. #[ORM\Entity(repositoryClassAseanceRepository::class)]
  13. #[ORM\Table(name'hs_tc_formation_aseance')]
  14. #[ApiResource]
  15. class Aseance
  16. {
  17.     use Actions;
  18.     #[ORM\ManyToOne(inversedBy'aseances')]
  19.     #[ORM\JoinColumn(nullablefalse)]
  20.     private ?Timetableitem $timetableitem null;
  21.     #[ORM\Column(typeTypes::DATE_MUTABLEnullabletrue)]
  22.     private ?\DateTimeInterface $takesplaceOn null;
  23.     #[ORM\Column(length255)]
  24.     private ?string $startAt null;
  25.     #[ORM\Column(nullabletrue)]
  26.     private ?string $length null;
  27.     #[ORM\Column(nullabletrue)]
  28.     private ?string $salle null;
  29.     #[ORM\OneToMany(mappedBy'aseance'targetEntityAabsence::class, orphanRemovaltrue)]
  30.     private Collection $aabsences;
  31.     public function __construct()
  32.     {
  33.         // date_default_timezone_set('Africa/Casablanca');
  34.         $this->createAt=new \DateTime('now');
  35.         $this->published=true;
  36.         $this->aabsences = new ArrayCollection();
  37.     }
  38.     public function getId(): ?int
  39.     {
  40.         return $this->id;
  41.     }
  42.     public function getTimetableitem(): ?Timetableitem
  43.     {
  44.         return $this->timetableitem;
  45.     }
  46.     public function setTimetableitem(?Timetableitem $timetableitem): self
  47.     {
  48.         $this->timetableitem $timetableitem;
  49.         return $this;
  50.     }
  51.     public function getTakesplaceOn(): ?\DateTimeInterface
  52.     {
  53.         return $this->takesplaceOn;
  54.     }
  55.     public function setTakesplaceOn(?\DateTimeInterface $takesplaceOn): self
  56.     {
  57.         $this->takesplaceOn $takesplaceOn;
  58.         return $this;
  59.     }
  60.     public function getStartAt(): ?string
  61.     {
  62.         return $this->startAt==null?$this->timetableitem->getStartAt():$this->startAt;
  63.     }
  64.     public function setStartAt(?string $startAt): void
  65.     {
  66.         $this->startAt $startAt;
  67.     }
  68.     public function getLength(): ?string
  69.     {
  70.         return $this->length==null?$this->timetableitem->getLength():$this->length;
  71.     }
  72.     public function setLength(?string $length): void
  73.     {
  74.         $this->length $length;
  75.     }
  76.     /**
  77.      * @return Collection<int, Aabsence>
  78.      */
  79.     public function getAabsences(): Collection
  80.     {
  81.         return $this->aabsences;
  82.     }
  83.     public function addAabsence(Aabsence $aabsence): self
  84.     {
  85.         if (!$this->aabsences->contains($aabsence)) {
  86.             $this->aabsences->add($aabsence);
  87.             $aabsence->setAseance($this);
  88.         }
  89.         return $this;
  90.     }
  91.     public function removeAabsence(Aabsence $aabsence): self
  92.     {
  93.         if ($this->aabsences->removeElement($aabsence)) {
  94.             // set the owning side to null (unless already changed)
  95.             if ($aabsence->getAseance() === $this) {
  96.                 $aabsence->setAseance(null);
  97.             }
  98.         }
  99.         return $this;
  100.     }
  101.     public function setCreateAt2(\DateTime $createAt): Aseance
  102.     {
  103.         $this->createAt $createAt;
  104.         return $this;
  105.     }
  106.     public function getAabsenceByParticipant(Participant $participant): ?Aabsence
  107.     {
  108.         foreach ($this->aabsences as $aabsence) {
  109.             if ($aabsence->getParticipant() === $participant) {
  110.                 return $aabsence;
  111.             }
  112.         }
  113.         return null;
  114.     }
  115.     public function getAbsencesPublished()
  116.     {
  117.         $absences=[];
  118.         foreach ($this->getAabsences() as $absence) {
  119.             if ($absence->isPublished()===true) {
  120.                 $absences[]=$absence;
  121.             }
  122.         }
  123.         return $absences;
  124.     }
  125.     public function getSalle(): ?string
  126.     {
  127.         return $this->salle;
  128.     }
  129.     public function setSalle(?string $salle): void
  130.     {
  131.         $this->salle $salle;
  132.     }
  133.     function getDayFrench() {
  134.         $joursFrancais = [
  135.             => 'lundi',
  136.             => 'mardi',
  137.             => 'mercredi',
  138.             => 'jeudi',
  139.             => 'vendredi',
  140.             => 'samedi',
  141.             => 'dimanche',
  142.         ];
  143.         return $joursFrancais[$this->takesplaceOn->format('N')] ?? "Jour invalide. Veuillez entrer un nombre entre 1 et 7.";
  144.     }
  145.     /**
  146.      * @throws \Exception
  147.      */
  148.     function getLengthAsDateInterval()
  149.     {
  150.         // Regex pour capturer les heures et les minutes
  151.         $pattern '/^(?:(\d+)h)?(?:\s*(\d+)min)?$/';
  152.         if (preg_match($pattern$this->getLength(), $matches)) {
  153.             $hours = isset($matches[1]) ? (int)$matches[1] : 0;
  154.             $minutes = isset($matches[2]) ? (int)$matches[2] : 0;
  155.             // Création de l'objet DateInterval
  156.             $intervalSpec sprintf('PT%dH%dM'$hours$minutes);
  157.             return new DateInterval($intervalSpec);
  158.         } else {
  159.             throw new InvalidArgumentException("Invalid time string format: $this->length");
  160.         }
  161.     }
  162.     function getLengthAsSeconds() {
  163.         return ($this->getLengthAsDateInterval()->365 24 60 60) +
  164.             ($this->getLengthAsDateInterval()->30 24 60 60) +
  165.             ($this->getLengthAsDateInterval()->24 60 60) +
  166.             ($this->getLengthAsDateInterval()->60 60) +
  167.             ($this->getLengthAsDateInterval()->60) +
  168.             $this->getLengthAsDateInterval()->s;
  169.     }
  170.     public function getEndAt()
  171.     {
  172.         $startAt=\DateTime::createFromFormat('d-m-Y H:i',(new \DateTime('now'))->format('d-m-Y')." ".$this->getStartAt());
  173.         $endAt=$startAt->add($this->getLengthAsDateInterval());
  174.         return $endAt->format('H:i');
  175.     }
  176. }