site-accueil-insa/assets/scripts/php2ics.php
2022-10-15 17:21:01 +02:00

150 lines
No EOL
4.5 KiB
PHP

<?php
//documentation ici => 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("<br>","\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<br>';
//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();
$cal->DownloadICS("planning_SA_INSA");
}
?>