src/Entity/Training/Scheduleitem.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Training;
  3. use _PHPStan_a3459023a\Nette\Utils\DateTime;
  4. use ApiPlatform\Metadata\ApiResource;
  5. use App\Repository\Training\ScheduleitemRepository;
  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(repositoryClassScheduleitemRepository::class)]
  14. #[ApiResource]
  15. #[ORM\Table(name'hs_tc_training_scheduleitem')]
  16. class Scheduleitem
  17. {
  18.     use Actions;
  19.     #[ORM\ManyToOne(inversedBy'scheduleitems')]
  20.     #[ORM\JoinColumn(nullablefalse)]
  21.     private ?Schedule $schedule null;
  22.     #[ORM\ManyToOne(inversedBy'scheduleitems')]
  23.     #[ORM\JoinColumn(nullablefalse)]
  24.     private ?Assignation $assignation 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'scheduleitem'targetEntitySeance::class, orphanRemovaltrue)]
  36.     private Collection $seances;
  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->seances = new ArrayCollection();
  45.     }
  46.     public function getSchedule(): ?Schedule
  47.     {
  48.         return $this->schedule;
  49.     }
  50.     public function setSchedule(?Schedule $schedule): self
  51.     {
  52.         $this->schedule $schedule;
  53.         return $this;
  54.     }
  55.     public function getAssignation(): ?Assignation
  56.     {
  57.         return $this->assignation;
  58.     }
  59.     public function setAssignation(?Assignation $assignation): self
  60.     {
  61.         $this->assignation $assignation;
  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.         if (!preg_match('/^\d{1,2}:\d{2}$/'$this->startAt)) {
  76.             return '08:00';
  77.         }
  78.         return $this->startAt;
  79.     }
  80.     public function setStartAt(?string $startAt): void
  81.     {
  82.         $this->startAt $startAt;
  83.     }
  84.     public function getLength(): ?string
  85.     {
  86.         return $this->length;
  87.     }
  88.     public function setLength(?string $length): self
  89.     {
  90.         $this->length $length;
  91.         return $this;
  92.     }
  93.     /**
  94.      * @throws \Exception
  95.      */
  96.     function getLengthAsDateInterval()
  97.     {
  98.         // Regex pour capturer les heures et les minutes
  99.         $pattern '/^(?:(\d+)h)?(?:\s*(\d+)min)?$/';
  100.         if (preg_match($pattern$this->getLength(), $matches)) {
  101.             $hours = isset($matches[1]) ? (int)$matches[1] : 0;
  102.             $minutes = isset($matches[2]) ? (int)$matches[2] : 0;
  103.             // Création de l'objet DateInterval
  104.             $intervalSpec sprintf('PT%dH%dM'$hours$minutes);
  105.             return new DateInterval($intervalSpec);
  106.         } else {
  107.             throw new InvalidArgumentException("Invalid time string format: $this->length");
  108.         }
  109.     }
  110.     function getLengthAsSeconds() {
  111.         return ($this->getLengthAsDateInterval()->365 24 60 60) +
  112.             ($this->getLengthAsDateInterval()->30 24 60 60) +
  113.             ($this->getLengthAsDateInterval()->24 60 60) +
  114.             ($this->getLengthAsDateInterval()->60 60) +
  115.             ($this->getLengthAsDateInterval()->60) +
  116.             $this->getLengthAsDateInterval()->s;
  117.     }
  118.     /**
  119.      * @throws \Exception
  120.      */
  121.     function getLengthAsStr()
  122.     {
  123.         return $this->getLengthAsDateInterval()->format('%hh%imin');
  124.     }
  125.     function getIntervalAsStr($interval)
  126.     {
  127.         return $interval->format('%h h %i min');
  128.     }
  129.         /**
  130.      * @return Collection<int, Seance>
  131.      */
  132.     public function getSeances(): Collection
  133.     {
  134.         return $this->seances;
  135.     }
  136.     public function addSeance(Seance $seance): self
  137.     {
  138.         if (!$this->seances->contains($seance)) {
  139.             $this->seances->add($seance);
  140.             $seance->setScheduleitem($this);
  141.         }
  142.         return $this;
  143.     }
  144.     public function removeSeance(Seance $seance): self
  145.     {
  146.         if ($this->seances->removeElement($seance)) {
  147.             // set the owning side to null (unless already changed)
  148.             if ($seance->getScheduleitem() === $this) {
  149.                 $seance->setScheduleitem(null);
  150.             }
  151.         }
  152.         return $this;
  153.     }
  154.     public function getSalle(): ?string
  155.     {
  156.         return $this->salle;
  157.     }
  158.     public function setSalle(?string $salle): void
  159.     {
  160.         $this->salle $salle;
  161.     }
  162.     public function getNature(): ?string
  163.     {
  164.         return $this->nature;
  165.     }
  166.     public function setNature(?string $nature): void
  167.     {
  168.         $this->nature $nature;
  169.     }
  170.     public function getSeancesCancelled(): array
  171.     {
  172.         return $this->seancesCancelled!=null?$this->seancesCancelled:[];
  173.     }
  174.     public function setSeancesCancelled(array $seancesCancelled): void
  175.     {
  176.         $this->seancesCancelled $seancesCancelled;
  177.     }
  178.     public function addDate(\DateTimeInterface $date): self
  179.     {
  180.         if($this->seancesCancelled==null$this->seancesCancelled=[];
  181.         if (!in_array($date->format('d-m-Y'), array_map(fn($d) => $d$this->seancesCancelled))) {
  182.             $this->seancesCancelled[] = $date->format('d-m-Y');
  183.         }
  184.         return $this;
  185.     }
  186.     public function removeDate(\DateTimeInterface $date): self
  187.     {
  188.         $formattedDate $date->format('d-m-Y');
  189.         $this->seancesCancelled array_filter(
  190.             $this->seancesCancelled,
  191.             fn($d) => $d !== $formattedDate
  192.         );
  193.         $this->seancesCancelled array_values($this->seancesCancelled);
  194.         return $this;
  195.     }
  196.     public function hasDate(\DateTimeInterface $date): bool
  197.     {
  198.         if($this->seancesCancelled==null$this->seancesCancelled=[];
  199.         foreach ($this->seancesCancelled as $d) {
  200.             if ($d === $date->format('d-m-Y')) {
  201.                 return true;
  202.             }
  203.         }
  204.         return false;
  205.     }
  206.     public function getStartAt100()
  207.     {
  208.         $dayAsSeconds 11*60*60;
  209.         $time=explode(':',$this->getStartAt());
  210.         $h=(intval($time[0])-8)*60*60;
  211.         $min=intval($time[1])*60;
  212.         return ($h+$min)/$dayAsSeconds*100;
  213.         //return $date1->format('H:i');
  214.     }
  215.     public function getStartAt100In15()
  216.     {
  217.         $dayAsSeconds 15*60*60;
  218.         $time=explode(':',$this->getStartAt());
  219.         $h=(intval($time[0])-8)*60*60;
  220.         $min=intval($time[1])*60;
  221.         return ($h+$min)/$dayAsSeconds*100;
  222.         //return $date1->format('H:i');
  223.     }
  224.     public function getLength100(): float
  225.     {
  226.         $dayAsSeconds 11*60*60;
  227.         return $this->getLengthAsSeconds()/$dayAsSeconds*100;
  228.     }
  229.     public function getLength100In15(): float
  230.     {
  231.         $dayAsSeconds 15*60*60;
  232.         return $this->getLengthAsSeconds()/$dayAsSeconds*100;
  233.     }
  234.     public function getStartAtPx()
  235.     {
  236.         $infos explode(":"$this->getStartAt());
  237.         $hours intval($infos[0]);
  238.         $minutes intval($infos[1]);
  239.         // Convertir les minutes en fraction d'heure
  240.         $minutesFraction $minutes 60;
  241.         // Calculer la valeur totale en heures décimales
  242.         $totalHours = ($hours 8) + $minutesFraction;
  243.         return $totalHours;
  244.     }
  245.     public function getLengthAtPx()
  246.     {
  247.         $pattern '/^(?:(\d+)h)?(?:\s*(\d+)min)?$/';
  248.         preg_match($pattern$this->getLength(), $matches);
  249.         $hours = isset($matches[1]) ? (int)$matches[1] : 0;
  250.         $minutes = isset($matches[2]) ? (int)$matches[2] : 0;
  251.         return $hours+$minutes/60;
  252.     }
  253.     function getDayFrench() {
  254.         $joursFrancais = [
  255.             => 'lundi',
  256.             => 'mardi',
  257.             => 'mercredi',
  258.             => 'jeudi',
  259.             => 'vendredi',
  260.             => 'samedi',
  261.             => 'dimanche',
  262.         ];
  263.         return $joursFrancais[$this->day] ?? "Jour invalide. Veuillez entrer un nombre entre 1 et 7.";
  264.     }
  265.     public function getSeancesDate()
  266.     {
  267.         $begin $this->schedule->getStartAt();
  268.         $end = ($this->schedule->getEndAt())->modify('+1 day');;
  269.         $interval DateInterval::createFromDateString('1 day');
  270.         $period = new DatePeriod($begin$interval$end);
  271.         $days = [];
  272.         foreach ($period as $dt) {
  273.             if(intval($dt->format('N')==$this->day)){
  274.                 $days[] = $dt->format('d-m-Y');
  275.             }
  276.         }
  277.         $seances = [];
  278.         foreach ($this->seances as $seance) {
  279.             if(in_array($seance->getTakesplaceOn()->format('d-m-Y'),$days)){
  280.                 $seances[] = $seance->getTakesplaceOn()->format('d-m-Y');
  281.             }
  282.         }
  283.         return ['seances'=>$seances,'days'=>$days'presumed_seances'=>array_diff($days,$seances)];
  284.     }
  285.     public function getSeancesNotCreate()
  286.     {
  287.         $begin $this->schedule->getStartAt();
  288.         $end = ($this->schedule->getEndAt())->modify('+1 day');
  289.         $now = new \DateTimeImmutable('now');
  290.         // Ajuster la fin pour ne pas dépasser la date actuelle
  291.         if ($end $now) {
  292.             $end $now;
  293.         }
  294.         $interval DateInterval::createFromDateString('1 day');
  295.         $period = new DatePeriod($begin$interval$end);
  296.         $days = [];
  297.         foreach ($period as $dt) {
  298.             if(intval($dt->format('N')==$this->day)){
  299.                 $days[] = $dt->format('d-m-Y');
  300.             }
  301.         }
  302.         $seances = [];
  303.         foreach ($this->seances as $seance) {
  304.             if(in_array($seance->getTakesplaceOn()->format('d-m-Y'),$days)){
  305.                 $seances[] = $seance->getTakesplaceOn()->format('d-m-Y');
  306.             }
  307.         }
  308.         return array_diff($days,$seances,$this->getSeancesCancelled());
  309.     }
  310.     public function getEndAt()
  311.     {
  312.         $startAt=\DateTime::createFromFormat('d/m/Y H:i',(new \DateTime('now'))->format('d/m/Y')." ".$this->getStartAt());
  313.         $endAt=$startAt->add($this->getLengthAsDateInterval());
  314.         return $endAt->format('H:i');
  315.     }
  316.     public function getSeanceByDate($date)
  317.     {
  318.         foreach ($this->seances as $seance) {
  319.             if($seance->getTakesplaceOn()->format('d/m/Y')==$date){
  320.                 return $seance;
  321.             }
  322.         }
  323.         return null;
  324.     }
  325. }