Updates the .ics according to correction on the fly

This commit is contained in:
xdrm-brackets 2017-09-15 00:12:12 +02:00
parent bd0ed3df23
commit 4bac020e92
2 changed files with 55 additions and 4 deletions

View File

@ -99,7 +99,6 @@
foreach($_POST['name'] as $id=>$correct){ foreach($_POST['name'] as $id=>$correct){
if( !empty(trim($correct)) ){ if( !empty(trim($correct)) ){
$sqlr = DatabaseDriver::getPDO()->prepare("UPDATE event SET name = :name WHERE id_event = :ide"); $sqlr = DatabaseDriver::getPDO()->prepare("UPDATE event SET name = :name WHERE id_event = :ide");
$sqlr->execute([ ':name' => $correct, ':ide' => $id ]); $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 /* POST-CALL

View File

@ -391,6 +391,7 @@
$sqlr->execute([ ':bn' => $data[1], ':idd' => $this->d_uid, ':col' => $data[3] ]); $sqlr->execute([ ':bn' => $data[1], ':idd' => $this->d_uid, ':col' => $data[3] ]);
/* (2) If there's a traduction */ /* (2) If there's a traduction */
$basename=$data[1];
if( ($fetched=$sqlr->fetch()) ) if( ($fetched=$sqlr->fetch()) )
$data[1] = $fetched['name']; $data[1] = $fetched['name'];
@ -400,11 +401,10 @@
$RAW .= "DTSTART:${start_t}\n"; $RAW .= "DTSTART:${start_t}\n";
$RAW .= "DTEND:${data[0]}\n"; $RAW .= "DTEND:${data[0]}\n";
$RAW .= "UID:$start_t\n"; // required $RAW .= "UID:$start_t\n"; // required
$RAW .= "DESCRIPTION:$basename${data[3]}\n";
$RAW .= "SUMMARY:${data[1]}\n"; $RAW .= "SUMMARY:${data[1]}\n";
if( !is_null($data[2]) ){ if( !is_null($data[2]) )
$RAW .= "LOCATION:${data[2]}\n"; $RAW .= "LOCATION:${data[2]}\n";
$RAW .= "DESCRIPTION:${data[2]}\n";
}
$RAW .= "CATEGORIES: UPPA Calendar\n"; $RAW .= "CATEGORIES: UPPA Calendar\n";
$RAW .= "END:VEVENT\n"; $RAW .= "END:VEVENT\n";
} }