From ea735c9630d45b909a014f4b9443fdb481098397 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Fri, 15 Sep 2017 20:40:29 +0200 Subject: [PATCH] Location update (location() + correct_location()) --- build/router/controller/ics.php | 77 ++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/build/router/controller/ics.php b/build/router/controller/ics.php index d47aa7c..b33a49a 100644 --- a/build/router/controller/ics.php +++ b/build/router/controller/ics.php @@ -227,7 +227,82 @@ } // 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 *