Location update (location() + correct_location())

This commit is contained in:
xdrm-brackets 2017-09-15 20:40:29 +02:00
parent ea2a254979
commit ea735c9630
1 changed files with 76 additions and 1 deletions

View File

@ -227,7 +227,82 @@
} }
// todo // todo
public function correct_location(){} public function correct_location(){
/* [1] Update data
=========================================================*/ {
/* (1) Check $_POST['name'] */
if( !isset($_POST['name']) || !is_array($_POST['name']) )
die("No data received");
/* (2) Store corrections */
foreach($_POST['name'] as $id=>$correct){
if( !empty(trim($correct)) ){
$sqlr = DatabaseDriver::getPDO()->prepare("UPDATE location SET name = :name WHERE id_location = :idl");
$sqlr->execute([ ':name' => $correct, ':idl' => $id ]);
}
}
}
/* [2] Update file one the fly
=========================================================*/
/* (1) Get corrections */
$sqlr = DatabaseDriver::getPDO()->prepare("SELECT l.id_location as id_location, l.name as name FROM location as l, event as e WHERE l.id_event = e.id_event AND e.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['id_location']] = $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:location\@(\d+)$@', $line[$l], $m) ){
// {2} If is in the list of correction //
if( !isset($corrections[$m[1]]) )
continue;
// {3} Rewrite name //
$line[++$l] = "LOCATION:".$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
* *