src/Entity/Formation/Timetableitem.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Formation;
  3. use _PHPStan_a3459023a\Nette\Utils\DateTime;
  4. use ApiPlatform\Metadata\ApiResource;
  5. use App\Repository\Formation\TimetableitemRepository;
  6. use App\Traits\Actions;
  7. use DateInterval;
  8. use DatePeriod;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use InvalidArgumentException;
  13. #[ORM\Entity(repositoryClassTimetableitemRepository::class)]
  14. #[ApiResource]
  15. #[ORM\Table(name'hs_tc_formation_timetableitem')]
  16. class Timetableitem
  17. {
  18.     use Actions;
  19.     #[ORM\ManyToOne(inversedBy'timetableitems')]
  20.     #[ORM\JoinColumn(nullablefalse)]
  21.     private ?Timetable $timetable null;
  22.     #[ORM\ManyToOne(inversedBy'timetableitems')]
  23.     #[ORM\JoinColumn(nullablefalse)]
  24.     private ?Attribution $attribution null;
  25.     #[ORM\Column(length255)]
  26.     private ?string $day null;
  27.     #[ORM\Column(length255)]
  28.     private ?string $startAt null;
  29.     #[ORM\Column(nullabletrue)]
  30.     private ?string $length null;
  31.     #[ORM\Column(nullabletrue)]
  32.     private ?string $salle null;
  33.     #[ORM\Column(nullabletrue)]
  34.     private ?string $nature null;
  35.     #[ORM\OneToMany(mappedBy'timetableitem'targetEntityAseance::class, orphanRemovaltrue)]
  36.     private Collection $aseances;
  37.     #[ORM\Column(type"json"nullabletrue)]
  38.     private ?array $seancesCancelled=[];
  39.     public function __construct()
  40.     {
  41.         // date_default_timezone_set('Africa/Casablanca');
  42.         $this->createAt=new \DateTime('now');
  43.         $this->published=true;
  44.         $this->aseances = new ArrayCollection();
  45.     }
  46.     public function getTimetable(): ?Timetable
  47.     {
  48.         return $this->timetable;
  49.     }
  50.     public function setTimetable(?Timetable $timetable): self
  51.     {
  52.         $this->timetable $timetable;
  53.         return $this;
  54.     }
  55.     public function getAttribution(): ?Attribution
  56.     {
  57.         return $this->attribution;
  58.     }
  59.     public function setAttribution(?Attribution $attribution): self
  60.     {
  61.         $this->attribution $attribution;
  62.         return $this;
  63.     }
  64.     public function getDay(): ?string
  65.     {
  66.         return $this->day;
  67.     }
  68.     public function setDay(string $day): self
  69.     {
  70.         $this->day $day;
  71.         return $this;
  72.     }
  73.     public function getStartAt(): ?string
  74.     {
  75.         return $this->startAt;
  76.     }
  77.     public function setStartAt(?string $startAt): void
  78.     {
  79.         $this->startAt $startAt;
  80.     }
  81.     public function getLength(): ?string
  82.     {
  83.         return $this->length;
  84.     }
  85.     public function setLength(?string $length): self
  86.     {
  87.         $this->length $length;
  88.         return $this;
  89.     }
  90.     /**
  91.      * @throws \Exception
  92.      */
  93.     function getLengthAsDateInterval()
  94.     {
  95.         // Regex pour capturer les heures et les minutes
  96.         $pattern '/^(?:(\d+)h)?(?:\s*(\d+)min)?$/';
  97.         if (preg_match($pattern$this->getLength(), $matches)) {
  98.             $hours = isset($matches[1]) ? (int)$matches[1] : 0;
  99.             $minutes = isset($matches[2]) ? (int)$matches[2] : 0;
  100.             // Création de l'objet DateInterval
  101.             $intervalSpec sprintf('PT%dH%dM'$hours$minutes);
  102.             return new DateInterval($intervalSpec);
  103.         } else {
  104.             throw new InvalidArgumentException("Invalid time string format: $this->length");
  105.         }
  106.     }
  107.     function getLengthAsSeconds() {
  108.         return ($this->getLengthAsDateInterval()->365 24 60 60) +
  109.             ($this->getLengthAsDateInterval()->30 24 60 60) +
  110.             ($this->getLengthAsDateInterval()->24 60 60) +
  111.             ($this->getLengthAsDateInterval()->60 60) +
  112.             ($this->getLengthAsDateInterval()->60) +
  113.             $this->getLengthAsDateInterval()->s;
  114.     }
  115.     /**
  116.      * @throws \Exception
  117.      */
  118.     function getLengthAsStr()
  119.     {
  120.         return $this->getLengthAsDateInterval()->format('%hh%imin');
  121.     }
  122.     function getIntervalAsStr($interval)
  123.     {
  124.         return $interval->format('%h h %i min');
  125.     }
  126.         /**
  127.      * @return Collection<int, Aseance>
  128.      */
  129.     public function getAseances(): Collection
  130.     {
  131.         return $this->aseances;
  132.     }
  133.     public function addAseance(Aseance $aseance): self
  134.     {
  135.         if (!$this->aseances->contains($aseance)) {
  136.             $this->aseances->add($aseance);
  137.             $aseance->setTimetableitem($this);
  138.         }
  139.         return $this;
  140.     }
  141.     public function removeAseance(Aseance $aseance): self
  142.     {
  143.         if ($this->aseances->removeElement($aseance)) {
  144.             // set the owning side to null (unless already changed)
  145.             if ($aseance->getTimetableitem() === $this) {
  146.                 $aseance->setTimetableitem(null);
  147.             }
  148.         }
  149.         return $this;
  150.     }
  151.     public function getSalle(): ?string
  152.     {
  153.         return $this->salle;
  154.     }
  155.     public function setSalle(?string $salle): void
  156.     {
  157.         $this->salle $salle;
  158.     }
  159.     public function getNature(): ?string
  160.     {
  161.         return $this->nature;
  162.     }
  163.     public function setNature(?string $nature): void
  164.     {
  165.         $this->nature $nature;
  166.     }
  167.     public function getSeancesCancelled(): array
  168.     {
  169.         return $this->seancesCancelled!=null?$this->seancesCancelled:[];
  170.     }
  171.     public function setSeancesCancelled(array $seancesCancelled): void
  172.     {
  173.         $this->seancesCancelled $seancesCancelled;
  174.     }
  175.     public function addDate(\DateTimeInterface $date): self
  176.     {
  177.         if($this->seancesCancelled==null$this->seancesCancelled=[];
  178.         if (!in_array($date->format('d-m-Y'), array_map(fn($d) => $d$this->seancesCancelled))) {
  179.             $this->seancesCancelled[] = $date->format('d-m-Y');
  180.         }
  181.         return $this;
  182.     }
  183.     public function removeDate(\DateTimeInterface $date): self
  184.     {
  185.         $formattedDate $date->format('d-m-Y');
  186.         $this->seancesCancelled array_filter(
  187.             $this->seancesCancelled,
  188.             fn($d) => $d !== $formattedDate
  189.         );
  190.         $this->seancesCancelled array_values($this->seancesCancelled);
  191.         return $this;
  192.     }
  193.     public function hasDate(\DateTimeInterface $date): bool
  194.     {
  195.         if($this->seancesCancelled==null$this->seancesCancelled=[];
  196.         foreach ($this->seancesCancelled as $d) {
  197.             if ($d === $date->format('d-m-Y')) {
  198.                 return true;
  199.             }
  200.         }
  201.         return false;
  202.     }
  203.     public function getStartAt100()
  204.     {
  205.         $dayAsSeconds 15*60*60;
  206.         //$now= new \DateTime();
  207.         //$date1=DateTime::createFromFormat('d-m-Y H:i', $now->format('d-m-Y'.' 08:00'));
  208.         //$date2=DateTime::createFromFormat('d-m-Y H:i', $now->format('d-m-Y'.' '.$this->getStartAt()));
  209.         //return ($date2->diff($date1)->h*60*60+$date2->diff($date1)->i*60)/$dayAsSeconds*100;
  210.         $time=explode(':',$this->getStartAt());
  211.         $h=(intval($time[0])-8)*60*60;
  212.         $min=intval($time[1])*60;
  213.         return ($h+$min)/$dayAsSeconds*100;
  214.         //return $date1->format('H:i');
  215.     }
  216.     public function getLength100(): float
  217.     {
  218.         $dayAsSeconds 15*60*60;
  219.         return $this->getLengthAsSeconds()/$dayAsSeconds*100;
  220.     }
  221.     public function getStartAtPx()
  222.     {
  223. //        $infos=explode(":",$this->getStartAt());
  224. //        return intval($infos[0])-8+($infos[1] == "30" ? 0.5 : ($infos[1] == "15" ? 0.25 : 0));
  225.         $infos explode(":"$this->getStartAt());
  226.         $hours intval($infos[0]);
  227.         $minutes intval($infos[1]);
  228.         // Convertir les minutes en fraction d'heure
  229.         $minutesFraction $minutes 60;
  230.         // Calculer la valeur totale en heures décimales
  231.         $totalHours = ($hours 8) + $minutesFraction;
  232.         return $totalHours;
  233.     }
  234.     public function getLengthAtPx()
  235.     {
  236.         $pattern '/^(?:(\d+)h)?(?:\s*(\d+)min)?$/';
  237.         preg_match($pattern$this->getLength(), $matches);
  238.         $hours = isset($matches[1]) ? (int)$matches[1] : 0;
  239.         $minutes = isset($matches[2]) ? (int)$matches[2] : 0;
  240.         return $hours+$minutes/60;
  241.     }
  242.     function getDayFrench() {
  243.         $joursFrancais = [
  244.             => 'lundi',
  245.             => 'mardi',
  246.             => 'mercredi',
  247.             => 'jeudi',
  248.             => 'vendredi',
  249.             => 'samedi',
  250.             => 'dimanche',
  251.         ];
  252.         return $joursFrancais[$this->day] ?? "Jour invalide. Veuillez entrer un nombre entre 1 et 7.";
  253.     }
  254.     public function getAseancesDate()
  255.     {
  256.         $begin $this->timetable->getStartAt();
  257.         $end = ($this->timetable->getEndAt())->modify('+1 day');
  258.         $interval DateInterval::createFromDateString('1 day');
  259.         $period = new DatePeriod($begin$interval$end);
  260.         $days = [];
  261.         foreach ($period as $dt) {
  262.             if(intval($dt->format('N')==$this->day)){
  263.                 $days[] = $dt->format('d-m-Y');
  264.             }
  265.         }
  266.         $aseances = [];
  267.         foreach ($this->aseances as $aseance) {
  268.             if(in_array($aseance->getTakesplaceOn()->format('d-m-Y'),$days)){
  269.                 $aseances[] = $aseance->getTakesplaceOn()->format('d-m-Y');
  270.             }
  271.         }
  272.         return ['aseances'=>$aseances,'days'=>$days'presumed_aseances'=>array_diff($days,$aseances)];
  273.     }
  274.     public function getSeancesNotCreate()
  275.     {
  276.         $begin $this->timetable->getStartAt();
  277.         $end = ($this->timetable->getEndAt())->modify('+1 day');
  278.         $now = new \DateTimeImmutable('now');
  279.         // Ajuster la fin pour ne pas dépasser la date actuelle
  280.         if ($end $now) {
  281.             $end $now;
  282.         }
  283.         $interval DateInterval::createFromDateString('1 day');
  284.         $period = new DatePeriod($begin$interval$end);
  285.         $days = [];
  286.         foreach ($period as $dt) {
  287.             if(intval($dt->format('N')==$this->day)){
  288.                 $days[] = $dt->format('d-m-Y');
  289.             }
  290.         }
  291.         $seances = [];
  292.         foreach ($this->aseances as $seance) {
  293.             if(in_array($seance->getTakesplaceOn()->format('d-m-Y'),$days)){
  294.                 $seances[] = $seance->getTakesplaceOn()->format('d-m-Y');
  295.             }
  296.         }
  297.         return array_diff($days,$seances,$this->getSeancesCancelled());
  298.     }
  299.     public function getEndAt()
  300.     {
  301.         $startAt=\DateTime::createFromFormat('d-m-Y H:i',(new \DateTime('now'))->format('d-m-Y')." ".$this->getStartAt());
  302.         $endAt=$startAt->add($this->getLengthAsDateInterval());
  303.         return $endAt->format('H:i');
  304.     }
  305.     public function getSeanceByDate($date)
  306.     {
  307.         foreach ($this->aseances as $seance) {
  308.             if($seance->getTakesplaceOn()->format('d-m-Y')==$date){
  309.                 return $seance;
  310.             }
  311.         }
  312.         return null;
  313.     }
  314. }