From 4bac020e92d9c051b2bad281acfd4c0d6c6894d0 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Fri, 15 Sep 2017 00:12:12 +0200 Subject: [PATCH] Updates the .ics according to correction on the fly --- build/router/controller/ics.php | 53 ++++++++++++++++++++++++++++- build/service/CalendarExtractor.php | 6 ++-- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/build/router/controller/ics.php b/build/router/controller/ics.php index e62207f..450d6cb 100644 --- a/build/router/controller/ics.php +++ b/build/router/controller/ics.php @@ -99,7 +99,6 @@ foreach($_POST['name'] as $id=>$correct){ if( !empty(trim($correct)) ){ - $sqlr = DatabaseDriver::getPDO()->prepare("UPDATE event SET name = :name WHERE id_event = :ide"); $sqlr->execute([ ':name' => $correct, ':ide' => $id ]); @@ -109,6 +108,58 @@ } + + + + /* [2] Update file one the fly + =========================================================*/ + /* (1) Get corrections */ + $sqlr = DatabaseDriver::getPDO()->prepare("SELECT CONCAT(basename, color) as uid, name FROM event WHERE id_diplome = :idd"); + $sqlr->execute([ ':idd' => $this->diplome_id ]); + + /* (2) Manage error */ + if( !($fetched=$sqlr->fetchAll()) ) + die("Cannot update the .ics file now"); + + /* (3) Format data */ + $corrections = []; + + foreach($fetched as $c) + $corrections[$c['uid']] = $c['name']; + + + /* (3) Get file pointer */ + $fp = file_get_contents(__ROOT__."/tmp/".$this->diplome_id.".ics"); + + /* (4) Manage error */ + if( !$fp ) + die("Cannot update the .ics file now"); + + + /* (4) Read line by line */ + $line = explode("\n", $fp); + + for( $l = 0 ; $l < count($line)-1 ; $l++ ){ + + // {1} If got the right pointer // + if( preg_match('@^DESCRIPTION:(.+#[\da-f]{6})$@', $line[$l], $m) ){ + + // {2} If is in the list of correction // + if( !isset($corrections[$m[1]]) ) + continue; + + // {3} Rewrite name // + $line[++$l] = "SUMMARY:".$corrections[$m[1]]; + + } + + } + + + /* (5) Back in file */ + file_put_contents(__ROOT__."/tmp/".$this->diplome_id.".ics", implode("\n", $line)); + + header('Location: /'); } /* POST-CALL diff --git a/build/service/CalendarExtractor.php b/build/service/CalendarExtractor.php index 1f371ff..7a764af 100644 --- a/build/service/CalendarExtractor.php +++ b/build/service/CalendarExtractor.php @@ -391,6 +391,7 @@ $sqlr->execute([ ':bn' => $data[1], ':idd' => $this->d_uid, ':col' => $data[3] ]); /* (2) If there's a traduction */ + $basename=$data[1]; if( ($fetched=$sqlr->fetch()) ) $data[1] = $fetched['name']; @@ -400,11 +401,10 @@ $RAW .= "DTSTART:${start_t}\n"; $RAW .= "DTEND:${data[0]}\n"; $RAW .= "UID:$start_t\n"; // required + $RAW .= "DESCRIPTION:$basename${data[3]}\n"; $RAW .= "SUMMARY:${data[1]}\n"; - if( !is_null($data[2]) ){ + if( !is_null($data[2]) ) $RAW .= "LOCATION:${data[2]}\n"; - $RAW .= "DESCRIPTION:${data[2]}\n"; - } $RAW .= "CATEGORIES: UPPA Calendar\n"; $RAW .= "END:VEVENT\n"; }