From ca7f5bbc6a6931f9ce3f9a90c4a92efbc4516d8b Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Fri, 15 Sep 2017 20:04:04 +0200 Subject: [PATCH] Now saved location (to correct) --- build/service/CalendarExtractor.php | 93 ++++++++++++++++++++++------- build/service/Tesseract.php | 6 +- 2 files changed, 73 insertions(+), 26 deletions(-) diff --git a/build/service/CalendarExtractor.php b/build/service/CalendarExtractor.php index 7a764af..e92ca4f 100644 --- a/build/service/CalendarExtractor.php +++ b/build/service/CalendarExtractor.php @@ -177,22 +177,59 @@ // {6} Exctract event's image // $ev = $this->extractEvent("$time-$uid", [$col_x, $start_y+1], [$col_ind[$day_n+1]-1, $y]); - $this->event[$uid][$time][1] = $ev[0]; - $this->event[$uid][$time][2] = $ev[1]; - $this->event[$uid][$time][3] = $color; - // {7} Check if already exists // - $sqlr = DatabaseDriver::getPDO()->prepare("SELECT basename, color, id_diplome FROM event WHERE basename = :bd AND color = :c AND id_diplome = :idd"); - $sqlr->execute([ ':bd' => $ev[0], ':c' => $color, ':idd' => $this->d_uid ]); + /* {7} Check @event if already exists */ { - // {7.1} If does not -> insert // - if( !$sqlr->fetch() ){ + $read_name = is_null($ev['name']) ? '?' : $ev['name']; - $sqlr = DatabaseDriver::getPDO()->prepare("INSERT INTO event(id_event,basename,color,id_diplome,name) VALUES(DEFAULT,:bd,:c,:idd,:n)"); - $sqlr->execute([ ':bd' => $ev[0], ':c' => $color, ':idd' => $this->d_uid, ':n' => $ev[0] ]); + $sqlr = DatabaseDriver::getPDO()->prepare("SELECT id_event FROM event WHERE basename = :bn AND color = :c AND id_diplome = :idd"); + $sqlr->execute([ ':bn' => $read_name, ':c' => $color, ':idd' => $this->d_uid ]); + $fetched = $sqlr->fetch(); + + // {7.1} If not found in db -> insert // + if( !$fetched ){ + + // {7.2} Insert new event // + $sqlr = DatabaseDriver::getPDO()->prepare("INSERT INTO event(id_event,basename,color,id_diplome,name) VALUES(DEFAULT,:bn,:c,:idd,:n)"); + $sqlr->execute([ ':c' => $color, ':idd' => $this->d_uid, ':bn' => $read_name, ':n' => $read_name ]); + + // {7.3} Store id in current event // + $event_id = DatabaseDriver::getPDO()->lastInsertId(); + + + }else + $event_id = $fetched['id_event']; } + /* (8) Check @location if already exists */ { + + $read_location = is_null($ev['location']) ? '?' : $ev['location']; + + $sqlr = DatabaseDriver::getPDO()->prepare("SELECT id_location FROM location WHERE basename = :bn AND id_event = :ide"); + $sqlr->execute([ ':bn' => $read_location, ':ide' => $event_id ]); + $fetched = $sqlr->fetch(); + + // {8.1} If not found in db -> insert // + if( !$fetched ){ + + // {8.2} Insert new location // + $sqlr = DatabaseDriver::getPDO()->prepare("INSERT INTO location(id_location,id_event,basename,name) VALUES(DEFAULT,:ide,:bn,:n)"); + $sqlr->execute([ ':ide' => $event_id, ':bn' => $read_location, ':n' => $read_location ]); + + // {8.3} Store id in current location // + $location_id = DatabaseDriver::getPDO()->lastInsertId(); + + + }else + $location_id = $fetched['id_location']; + + } + + /* (9) Store local data for ics */ + $this->event[$uid][$time][1] = $location_id; + + } @@ -246,7 +283,7 @@ /* (2) Manage copy error */ if( !$copied ) - return [ '?', null ]; + return [ 'name' => null, 'location' => null ]; /* (3) Save to jpeg */ \imagesavealpha($clip, true); @@ -272,7 +309,7 @@ /* (2) Manage error */ }catch(\Exception $e){ - $read = [ '?', null ]; + return [ 'name' => null, 'location' => null ]; } @@ -386,14 +423,24 @@ ---------------------------------------------------------*/ foreach($events as $start_t=>$data){ - /* (1) Search if there is a correction in the database */ - $sqlr = DatabaseDriver::getPDO()->prepare("SELECT name FROM event WHERE basename = :bn AND id_diplome = :idd AND color = :col"); - $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']; + /* (1) If a name -> Search if there is a correction in the database */ + $sqlr = DatabaseDriver::getPDO()->prepare("SELECT e.id_event as ide, l.id_location as idl, l.id_location as idl e.name as ename, l.name as lname FROM event as e, location as l WHERE e.id_event = l.id_event AND l.id_location= :idl"); + $sqlr->execute([ ':idl' => $data[1] ]); + + /* (2) Default values */ + $ide = -1; + $idl = -1; + $name = '?'; + $location = '?'; + + /* (2) If a match found -> set it */ + if( ($fetched=$sqlr->fetch()) ){ + $ide = $fetched['ide']; + $idl = $fetched['idl']; + $name = $fetched['ename']; + $location = $fetched['lname']; + } /* (3) Build ICS event */ $RAW .= "BEGIN:VEVENT\n"; @@ -401,10 +448,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]) ) - $RAW .= "LOCATION:${data[2]}\n"; + $RAW .= "DESCRIPTION:event@$ide\n"; // event@id_event + $RAW .= "SUMMARY:$name\n"; + $RAW .= "DESCRIPTION:location@$idl\n"; // location@id_location + $RAW .= "LOCATION:$location\n"; $RAW .= "CATEGORIES: UPPA Calendar\n"; $RAW .= "END:VEVENT\n"; } diff --git a/build/service/Tesseract.php b/build/service/Tesseract.php index 1b2c81e..be43add 100755 --- a/build/service/Tesseract.php +++ b/build/service/Tesseract.php @@ -87,12 +87,12 @@ // Amphi ... // if( preg_match('@^a[nm](?:[bp][hln])?[ir] ?(.+)$@i', $lines[$i], $m) ) // 'amphi A', 'amphi 600 droit' - return [ $title, "Amphi ${m[1]}" ]; + return [ 'name' => $title, 'location' => "Amphi ${m[1]}" ]; // S... OR 5... // if( preg_match('@^[S|5] ?(.+)@i', $lines[$i], $m) ) // 'S10', 'S22' - return [ $title, "S. ${m[1]}" ]; + return [ 'name' => $title, 'location' => "S. ${m[1]}" ]; // If not 'Cours', 'CTD', 'TD', 'TP' // if( preg_match('@^(co[hu][trn][s5]|t[dp|c[mn]|ct[dp])@i', $lines[$i]) ) @@ -102,7 +102,7 @@ } - return [ $title, $lines[2] ]; + return [ 'name' => $title, 'location' => $lines[2] ]; }