2017-09-12 15:22:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace service;
|
|
|
|
|
2017-09-14 17:56:24 +00:00
|
|
|
use \database\core\DatabaseDriver;
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-09-12 15:22:18 +00:00
|
|
|
class Updater{
|
|
|
|
|
|
|
|
/* [1] Attributes
|
|
|
|
=========================================================*/
|
|
|
|
private $config = null;
|
2017-09-15 06:38:39 +00:00
|
|
|
private $d_data = null;
|
2017-09-12 15:22:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (1) Constructs and initialise an updater
|
|
|
|
*
|
2017-09-15 06:38:39 +00:00
|
|
|
* @d_id<String> Diplome id to update (only)
|
|
|
|
*
|
2017-09-12 15:22:18 +00:00
|
|
|
* @return instance<Updater> New Updater
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2017-09-15 06:38:39 +00:00
|
|
|
public function __construct($d_id=null){
|
2017-09-12 15:22:18 +00:00
|
|
|
|
|
|
|
/* [1] Fetch config
|
|
|
|
=========================================================*/ {
|
|
|
|
|
|
|
|
/* (1) Try to load the configuration */
|
|
|
|
$this->config = Config::load("http://sciences.univ-pau.fr/edt/_ressource.js", "http://sciences.univ-pau.fr/edt/_periode.js");
|
|
|
|
|
|
|
|
/* (2) If cannot fetch config */
|
|
|
|
if( !($this->config instanceof Config) )
|
|
|
|
throw new \Exception("Cannot fetch configuration");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-15 06:38:39 +00:00
|
|
|
|
|
|
|
$d_list = $this->config->getDiplomes();
|
|
|
|
if( !is_null($d_id) && isset($d_list[$d_id]) )
|
|
|
|
$this->d_data = [ $d_id, $d_list[$d_id] ];
|
|
|
|
|
2017-09-12 15:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Update the ICS files
|
|
|
|
*
|
|
|
|
* @return error<bool> FALSE on error
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
public function update(){
|
|
|
|
|
2017-09-15 06:38:39 +00:00
|
|
|
|
|
|
|
/* [1] If only 1 diplome to update -> do it
|
2017-09-12 15:22:18 +00:00
|
|
|
=========================================================*/
|
2017-09-15 06:38:39 +00:00
|
|
|
if( is_array($this->d_data) ){
|
|
|
|
|
|
|
|
$d_id = $this->d_data[0];
|
|
|
|
$d_name = $this->d_data[1];
|
2017-09-12 15:22:18 +00:00
|
|
|
|
2017-09-12 21:28:10 +00:00
|
|
|
file_put_contents(__ROOT__."/tmp/$d_id.ics", "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//xdrm.io//NONSGML v1.0//EN\nMETHOD:PUBLISH\nX-WR-CALNAME: UPPA $d_name\n");
|
2017-09-15 06:38:39 +00:00
|
|
|
\chmod(__ROOT__."/tmp/$d_id.ics", 0775);
|
|
|
|
|
|
|
|
/* (1) Browse each date
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
foreach($this->config->getPeriods() as $p_id=>$p_date){
|
|
|
|
|
|
|
|
/* (1) Load image in the extractor */
|
|
|
|
$calext = new CalendarExtractor($d_id, "http://sciences.univ-pau.fr/edt/diplomes/${d_id}${p_id}.png", $p_date);
|
|
|
|
|
|
|
|
/* (2) Extract calendar data */
|
|
|
|
$calext->process();
|
|
|
|
|
|
|
|
/* (3) Get ICS events representation */
|
|
|
|
$EVENTS = $calext->toIcs();
|
|
|
|
|
|
|
|
/* (4) Add to file */
|
|
|
|
file_put_contents(__ROOT__."/tmp/$d_id.ics", $EVENTS, FILE_APPEND);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
file_put_contents(__ROOT__."/tmp/$d_id.ics", "END:VCALENDAR", FILE_APPEND);
|
|
|
|
|
|
|
|
/* (2) Register update
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
$sqlr = DatabaseDriver::getPDO()->prepare("UPDATE diplome SET updated_at = CURRENT_TIMESTAMP WHERE id_diplome = :idd");
|
|
|
|
$sqlr->execute([ ':idd' => $d_id ]);
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [2] Else -> Browse each diplome
|
|
|
|
=========================================================*/
|
|
|
|
foreach($this->config->getDiplomes() as $d_id=>$d_name){
|
|
|
|
|
|
|
|
file_put_contents(__ROOT__."/tmp/$d_id.ics", "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//xdrm.io//NONSGML v1.0//EN\nMETHOD:PUBLISH\nX-WR-CALNAME:UPPA $d_name\n");
|
2017-09-15 06:09:46 +00:00
|
|
|
\chmod(__ROOT__."/tmp/$d_id.ics", 775);
|
2017-09-12 16:30:53 +00:00
|
|
|
|
2017-09-12 15:22:18 +00:00
|
|
|
/* (1) Browse each date
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
foreach($this->config->getPeriods() as $p_id=>$p_date){
|
|
|
|
|
|
|
|
/* (1) Load image in the extractor */
|
2017-09-13 13:03:36 +00:00
|
|
|
$calext = new CalendarExtractor($d_id, "http://sciences.univ-pau.fr/edt/diplomes/${d_id}${p_id}.png", $p_date);
|
2017-09-12 15:22:18 +00:00
|
|
|
|
|
|
|
/* (2) Extract calendar data */
|
|
|
|
$calext->process();
|
|
|
|
|
2017-09-12 16:09:51 +00:00
|
|
|
/* (3) Get ICS events representation */
|
|
|
|
$EVENTS = $calext->toIcs();
|
|
|
|
|
|
|
|
/* (4) Add to file */
|
|
|
|
file_put_contents(__ROOT__."/tmp/$d_id.ics", $EVENTS, FILE_APPEND);
|
|
|
|
|
2017-09-12 15:22:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 16:30:53 +00:00
|
|
|
file_put_contents(__ROOT__."/tmp/$d_id.ics", "END:VCALENDAR", FILE_APPEND);
|
|
|
|
|
2017-09-14 17:56:24 +00:00
|
|
|
/* (2) Register update
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
$sqlr = DatabaseDriver::getPDO()->prepare("UPDATE diplome SET updated_at = CURRENT_TIMESTAMP WHERE id_diplome = :idd");
|
|
|
|
$sqlr->execute([ ':idd' => $d_id ]);
|
|
|
|
|
|
|
|
|
2017-09-12 15:22:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|