diff --git a/assets/scripts/php2ics.php b/assets/scripts/php2ics.php new file mode 100644 index 0000000..fba23a1 --- /dev/null +++ b/assets/scripts/php2ics.php @@ -0,0 +1,152 @@ + https://github.com/baptistereb/php2ics + +class php2ics +{ + private static $ics; + + public function __construct(string $organisation, string $product) + { + $organisation = htmlspecialchars($organisation); + $product = htmlspecialchars($product); + + $this->Init($organisation,$product); + } + + //fonction d'initialisation de l'ICS + private function Init(string $organisation, ?string $product): void + { + $organisation = htmlspecialchars($organisation); + if(isset($product)) { + $product = htmlspecialchars($product); + } else { + $product = "none"; + } + + $this::$ics .= "BEGIN:VCALENDAR\n"; + $this::$ics .= "VERSION:2.0\n"; + $this::$ics .= "PRODID:-//".$organisation."//NONSGML ".$product."\n"; + $this::$ics .= "CALSCALE:GREGORIAN\n"; + $this::$ics .= "METHOD:PUBLISH\n"; + } + + //fonction de création d'évenement + public function AddEvent( + string $title, + ?string $description, + int $begin_date, + int $end_date, + ?string $location, + ?string $url + ): void + { + $title = htmlspecialchars($title); + $begin_date = htmlspecialchars($begin_date); + $end_date = htmlspecialchars($end_date); + + $this::$ics .= "BEGIN:VEVENT\n"; + $this::$ics .= "SUMMARY:".$title."\n"; + $this::$ics .= "DTSTART:".date('Ymd',$begin_date)."T".date('His',$begin_date)."\n"; + $this::$ics .= "DTEND:".date('Ymd',$end_date)."T".date('His',$end_date)."\n"; + if(isset($location)) { + $location = htmlspecialchars($location); + $this::$ics .= "LOCATION:".$location."\n"; + } + if(isset($description)) { + $description = htmlspecialchars($description); + $this::$ics .= "DESCRIPTION:".$description."\n"; + } + if(isset($url)) { + $url = htmlspecialchars($url); + $this::$ics .= "URL:".$url."\n"; + } + $this::$ics .= "END:VEVENT\n"; + } + + //fonction de fin d'ICS + public function End(): void + { + $this::$ics .= "END:VCALENDAR"; + } + + //fonction qui permet de récupérer le code ICS + public function GetICS(): string + { + $getICS = $this::$ics; + return $getICS; + } + + //fonction qui permet de télécharger le fichier ICS + public function DownloadICS(?string $fichier): void + { + if(!isset($fichier)) { + $fichier = 'calend.ics'; + } else { + $fichier = htmlspecialchars($fichier); + } + + + $fichier .= ".ics"; + + $dwn = str_replace("
","\n", $this::$ics); + $f = fopen($fichier, 'w+'); + fputs($f, $this::$ics); + header('Location:'.$fichier); + exit; + } +} + +if(isset($_POST['download_planning'])) { + $cal = new php2ics("accueil_insa", "voila les loustiks"); + + for($day = 1; $day <= 7; $day++) { //on tourne sur les 7 jours + $hours_beginning = (int) 7; //commence à 7h tout les matin + $day_beginning = (int) 5; //commence le 5 septembre (premier jour = 5 septembre) + + $hours = $hours_beginning; + $minute = 0; + + $reqcal = $db->prepare('SELECT id,title, description, length FROM planning_insa WHERE day = ? AND num_planning = ? ORDER BY order_start ASC'); + $reqcal->execute(array($day, $planning)); + while($c = $reqcal->fetch()) { + //le planning commence à $hours_beginning le $day_beginning et sous forme : date('Y-m-d H:i:s') + if($c['title'] == " ") { //si le titre est vide c'est qu'il n'y a rien + $time = (float) $c['length']/4.1; + $hours += floor($time); + $minute += 60*($time-floor($time)); + } else { + $begin_hours = $hours; + $begin_min = $minute; + $time = (float) $c['length']/4.1; + $hours += floor($time); + $minute += round(60*($time-floor($time)),3); + while($minute >= 60) { + $minute -= 60; + $hours++; + } + while($hours >= 24) { + $hours --; //bon la on tronque entre les jours sinon c'est hyper relou à coder pour rien + } + + //pour tester décommenter la ligne juste en dessous + //echo $c['title'].": ".$begin_hours.'h'.$begin_min.'m to :'.$hours.'h'.$minute.'m
'; + + //mktime => hours, minute, second, month, day, year + $btime = mktime($begin_hours, $begin_min, 0, 9, $day_beginning+$day-1, 2022); + $etime = mktime($hours, $minute, 0, 9, $day_beginning+$day-1, 2022); + $cal->AddEvent($c['title'], $c['description'], $btime, $etime, "INSA Toulouse", "https://etud.insa-toulouse.fr/~accueil_insa/planning.php"); + + } + } + } + + $cal->End(); + + $req = $db->prepare('UPDATE planning_tmp_dl set planning = ?'); + $req->execute(array($cal->GetICS())); + + header('Location: assets/script/planning_tmp.php'); +} + +?> \ No newline at end of file diff --git a/assets/scripts/planning_tmp.php b/assets/scripts/planning_tmp.php new file mode 100644 index 0000000..f44a5a7 --- /dev/null +++ b/assets/scripts/planning_tmp.php @@ -0,0 +1,17 @@ +query('SELECT planning FROM planning_tmp_dl'); +$r = $req -> fetchAll(PDO::FETCH_ASSOC); + +header('Content-Type: text/ics'); +header('Content-Transfer-Encoding: Binary'); +header('Content-Disposition:attachment; filename=planning_semaine_accueil.ics'); + +echo $r[0]['planning']; + +exit(); + +header('Location: ../../index.php'); + +?> \ No newline at end of file diff --git a/planning.php b/planning.php index 1bb4c30..cdc97cc 100755 --- a/planning.php +++ b/planning.php @@ -229,7 +229,7 @@ if(isset($_GET['planning']) AND !empty(['planning']))