'01', 'février' => '02', 'mars' => '03', 'avril' => '04', 'mai' => '05', 'juin' => '06', 'juillet' => '07', 'août' => '08', 'septembre' => '09', 'octobre' => '10', 'novembre' => '11', 'décembre' => '12' ]; } /* (1) Constructs a filled config object * * @d_list List of the diplomes ( ID => name ) * @p_list List of the periods ( ID => first day's date ) * * @return this New instance * ---------------------------------------------------------*/ private function __construct($d_list=null, $p_list=null){ /* [1] Check arguments (type: array) =========================================================*/ if( !is_array($d_list) || !is_array($p_list) ) throw new \Exception("Config.__construct(, , <".gettype($d_list).">) received"); /* [2] Basic setter =========================================================*/ /* (1) Set the diplomes' list */ $this->diplomes = $d_list; /* (2) Set the periods' list */ $this->periods = $p_list; } /* (2) Loads the configuration from URL * * @d_script URL of the script containing the diplomes list * @p_script URL of the script containing the periods list * * @return instance The created instance * NULL on error * ---------------------------------------------------------*/ public static function load($d_script=null, $p_script=null){ /* [1] Check arguments (type + validity) =========================================================*/ { /* (1) Check arguments' types (string, string) */ if( !is_string($d_script) || !is_string($p_script) ) throw new \Exception("Config::load(, , <".gettype($d_script).">) received"); /* (2) Check @d_script URL validity */ if( !($d_h=@get_headers($d_script)) ) throw new \Exception("Config::load(<1>, <2>) received but cannot reach <1>"); /* (3) Check @p_script URL validity */ if( !($p_h=@get_headers($p_script)) ) throw new \Exception("Config::load(<1>, <2>) received but cannot reach <1>"); /* (4) Check image exists @dscript */ if( !preg_match('@HTTP.+200@', $d_h[0]) ) throw new \Exception("Config::load(<1>, <2>) received but cannot reach <1>"); /* (5) Check image exists @dscript */ if( !preg_match('@HTTP.+200@', $p_h[0]) ) throw new \Exception("Config::load(<1>, <2>) received but cannot reach <1>"); } /* [2] Get file cursors =========================================================*/ { /* (1) Try to get file cursors */ try{ $d_cur = new \SplFileObject($d_script, "r"); $p_cur = new \SplFileObject($p_script, "r"); /* (2) Raise on failure */ }catch(\Exception $e){ throw new \Exception("Config::load(<1>, <2>) cannot get a cursor on a one of both files"); } } /* [3] Filter and register content =========================================================*/{ /* (1) Manage diplomes ---------------------------------------------------------*/ { /* (1) Initialize diplomes' list */ $d_list = []; /* (2) Fetch each line and extract data */ while( !$d_cur->eof() ){ // {1} If does not match -> go to next line // if( !preg_match('@"grDiplome","(?:<)?(.+)(?:>)?","T(.+)"@i', $d_cur->fgets(), $m) ) continue; // {2} Register into diplome list // $d_list[ "T${m[2]}" ] = html_entity_decode($m[1]); } /* (2) Close file cursor */ $d_cur = null; } /* (2) Manage periods ---------------------------------------------------------*/{ /* (1) Initialize periods' list */ $p_list = []; /* (2) Fetch each line and extract data */ while( !$p_cur->eof() ){ // {1} If does not match -> go to next line // if( !preg_match('@"T\d+","du \d+ au (\d+) ([a-z]+) (\d+)","T\d+S(.+)"@i', $p_cur->fgets(), $m) ) continue; // {2} Get numeric month number // $month = self::monthAsso()[ $m[2] ]; // {3} Get monday date // $mon = date( 'Y-m-d', strtotime("${m[3]}-$month-${m[1]} - 5 days") ); // {4} Register into the list // $p_list[ "S${m[4]}" ] = $mon; } /* (2) Close file cursor */ $p_cur = null; } } /* [4] Cache into light-database =========================================================*/ { /* (1) Get diplome ids */ $sqlr = DatabaseDriver::getPDO()->query("SELECT id_diplome from diplome"); $db_ids_raw = $sqlr->fetchAll(); /* (2) Format data */ $db_ids = []; foreach($db_ids_raw as $data) $db_ids[$data['id_diplome']] = null; /* (3) For each diplome */ foreach($d_list as $id=>$name){ // {1} Add if not in db // if( !isset($db_ids[$id]) ){ $sqlr = DatabaseDriver::getPDO()->prepare("INSERT INTO `diplome`(`id_diplome`, `name`, `updated_at`) VALUES(:id, :name, DEFAULT)"); $sqlr->execute([ ':id' => $id, ':name' => $name ]); // {2} If update // }else{ $sqlr = DatabaseDriver::getPDO()->prepare("UPDATE diplome SET name = :name, updated_at = :upd WHERE id_diplome = :id"); $sqlr->execute([ ':id' => $id, ':name' => $name, 'upd' => gmmktime() ]); } } /* (4) Get period ids */ $sqlr = DatabaseDriver::getPDO()->query("SELECT id_period from period"); $db_ids_raw = $sqlr->fetchAll(); /* (5) Format data */ $db_ids = []; foreach($db_ids_raw as $data) $db_ids[$data['id_period']] = null; /* (6) For each period */ foreach($p_list as $id=>$monday){ // {1} Add if not in db // if( !isset($db_ids[$id]) ){ $sqlr = DatabaseDriver::getPDO()->prepare("INSERT INTO `period`(`id_period`, `monday`) VALUES(:id, :monday)"); $sqlr->execute([ ':id' => $id, ':monday' => $monday ]); // {2} If update // }else{ $sqlr = DatabaseDriver::getPDO()->prepare("UPDATE period SET monday = :monday WHERE id_period = :id"); $sqlr->execute([ ':id' => $id, ':monday' => $monday ]); } } } /* [x] Return the instance */ return new Config($d_list, $p_list); } /* (3) Loads the configuration from URL * * @return instance The created instance * NULL on error * ---------------------------------------------------------*/ public static function loadCached(){ /* [1] Load cache =========================================================*/ { /* (1) Get diplome list */ $d_list_raw = DatabaseDriver::getPDO()->query("SELECT * FROM diplome ORDER BY name ASC")->fetchAll(); /* (2) Format data */ $d_list = []; foreach($d_list_raw as $data) $d_list[$data['id_diplome']] = [ $data['name'], $data['updated_at'] ]; /* (1) Get diplome list */ $p_list_raw = DatabaseDriver::getPDO()->query("SELECT * FROM period")->fetchAll(); /* (2) Format data */ $p_list = []; foreach($p_list_raw as $data) $p_list[$data['id_period']] = $data['monday']; } /* [2] Return the instance =========================================================*/ return new Config($d_list, $p_list); } /* (4) Getter diplomes * * @return diplomes Diplome list * ---------------------------------------------------------*/ public function getDiplomes(){ return $this->diplomes; } /* (5) Getter periods * * @return periods Period list * ---------------------------------------------------------*/ public function getPeriods(){ return $this->periods; } }