src/Entity/Training/Scheduleevent.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\Traits\Actions;
  6. use DateInterval;
  7. use DatePeriod;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\DBAL\Types\Types;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use InvalidArgumentException;
  13. #[ORM\Entity()]
  14. #[ApiResource]
  15. #[ORM\Table(name'hs_tc_training_scheduleevent')]
  16. class Scheduleevent
  17. {
  18.     use Actions;
  19.     #[ORM\ManyToOne(inversedBy'scheduleevents')]
  20.     #[ORM\JoinColumn(nullablefalse)]
  21.     private ?Schedule $schedule null;
  22.     #[ORM\Column(typeTypes::DATE_MUTABLEnullabletrue)]
  23.     private ?\DateTimeInterface $takesplaceOn null;
  24.     #[ORM\Column(length255)]
  25.     private ?string $title null;
  26.     #[ORM\Column(length255)]
  27.     private ?string $startAt null;
  28.     #[ORM\Column(nullabletrue)]
  29.     private ?string $length null;
  30.     #[ORM\Column(nullabletrue)]
  31.     private ?string $nature null;
  32.     public function __construct()
  33.     {
  34.         // date_default_timezone_set('Africa/Casablanca');
  35.         $this->createAt=new \DateTime('now');
  36.         $this->published=true;
  37.     }
  38.     public function getTakesplaceOn(): ?\DateTimeInterface
  39.     {
  40.         return $this->takesplaceOn;
  41.     }
  42.     public function setTakesplaceOn(?\DateTimeInterface $takesplaceOn): void
  43.     {
  44.         $this->takesplaceOn $takesplaceOn;
  45.     }
  46.     public function getTitle(): ?string
  47.     {
  48.         return $this->title;
  49.     }
  50.     public function setTitle(?string $title): void
  51.     {
  52.         $this->title $title;
  53.     }
  54.     public function getSchedule(): ?Schedule
  55.     {
  56.         return $this->schedule;
  57.     }
  58.     public function setSchedule(?Schedule $schedule): self
  59.     {
  60.         $this->schedule $schedule;
  61.         return $this;
  62.     }
  63.     public function getStartAt(): ?string
  64.     {
  65.         return $this->startAt;
  66.     }
  67.     public function setStartAt(?string $startAt): void
  68.     {
  69.         $this->startAt $startAt;
  70.     }
  71.     public function getLength(): ?string
  72.     {
  73.         return $this->length;
  74.     }
  75.     public function setLength(?string $length): self
  76.     {
  77.         $this->length $length;
  78.         return $this;
  79.     }
  80.     /**
  81.      * @throws \Exception
  82.      */
  83.     function getLengthAsDateInterval()
  84.     {
  85.         // Regex pour capturer les heures et les minutes
  86.         $pattern '/^(?:(\d+)h)?(?:\s*(\d+)min)?$/';
  87.         if (preg_match($pattern$this->getLength(), $matches)) {
  88.             $hours = isset($matches[1]) ? (int)$matches[1] : 0;
  89.             $minutes = isset($matches[2]) ? (int)$matches[2] : 0;
  90.             // Création de l'objet DateInterval
  91.             $intervalSpec sprintf('PT%dH%dM'$hours$minutes);
  92.             return new DateInterval($intervalSpec);
  93.         } else {
  94.             throw new InvalidArgumentException("Invalid time string format: $this->length");
  95.         }
  96.     }
  97.     function getLengthAsSeconds() {
  98.         return ($this->getLengthAsDateInterval()->365 24 60 60) +
  99.             ($this->getLengthAsDateInterval()->30 24 60 60) +
  100.             ($this->getLengthAsDateInterval()->24 60 60) +
  101.             ($this->getLengthAsDateInterval()->60 60) +
  102.             ($this->getLengthAsDateInterval()->60) +
  103.             $this->getLengthAsDateInterval()->s;
  104.     }
  105.     /**
  106.      * @throws \Exception
  107.      */
  108.     function getLengthAsStr()
  109.     {
  110.         return $this->getLengthAsDateInterval()->format('%hh%imin');
  111.     }
  112.     function getIntervalAsStr($interval)
  113.     {
  114.         return $interval->format('%h h %i min');
  115.     }
  116.     public function getNature(): ?string
  117.     {
  118.         return $this->nature;
  119.     }
  120.     public function setNature(?string $nature): void
  121.     {
  122.         $this->nature $nature;
  123.     }
  124.     public function getStartAt100()
  125.     {
  126.         $dayAsSeconds 11*60*60;
  127. //        $now= new \DateTime();
  128. //        $date1=DateTime::createFromFormat('d/m/Y H:i', $now->format('d/m/Y'.' 08:00'));
  129. //        $date2=DateTime::createFromFormat('d/m/Y H:i', $now->format('d/m/Y'.' '.$this->getStartAt()));
  130. //        return ($date2->diff($date1)->h*60*60+$date2->diff($date1)->i*60)/$dayAsSeconds*100;
  131.         $time=explode(':',$this->getStartAt());
  132.         $h=(intval($time[0])-8)*60*60;
  133.         $min=intval($time[1])*60;
  134.         return ($h+$min)/$dayAsSeconds*100;
  135.         //return $date1->format('H:i');
  136.     }
  137.     public function getLength100(): float
  138.     {
  139.         $dayAsSeconds 11*60*60;
  140.         return $this->getLengthAsSeconds()/$dayAsSeconds*100;
  141.     }
  142.     public function getStartAtPx()
  143.     {
  144.         $infos=explode(":",$this->getStartAt());
  145.         return intval($infos[0])-8+($infos[1] == "30" 0.5 : ($infos[1] == "15" 0.25 0));
  146.     }
  147.     public function getLengthAtPx()
  148.     {
  149.         $pattern '/^(?:(\d+)h)?(?:\s*(\d+)min)?$/';
  150.         preg_match($pattern$this->getLength(), $matches);
  151.         $hours = isset($matches[1]) ? (int)$matches[1] : 0;
  152.         $minutes = isset($matches[2]) ? (int)$matches[2] : 0;
  153.         return $hours+$minutes/60;
  154.     }
  155.     public function getEndAt()
  156.     {
  157.         $startAt=\DateTime::createFromFormat('d/m/Y H:i',(new \DateTime('now'))->format('d/m/Y')." ".$this->getStartAt());
  158.         $endAt=$startAt->add($this->getLengthAsDateInterval());
  159.         return $endAt->format('H:i');
  160.     }
  161.     public function getDay()
  162.     {
  163.         return $this->takesplaceOn->format('N');
  164.     }
  165. }