Commit f205ffcf authored by Francesco Iannuzzelli's avatar Francesco Iannuzzelli

#26 Events API cache

parent 781a9872
......@@ -190,6 +190,12 @@ class Cache
$events = $ee->EventsRangeCal($start_ts, $end_ts);
$this->fm->WritePage($filename, json_encode($events));
break;
case "events_api":
include_once(SERVER_ROOT."/../classes/events.php");
$ee = new Events();
$response = $ee->RequestApi(unserialize($id));
$this->fm->WritePage($filename, serialize($response));
break;
case "aggregated_feed":
$this->fm->Touch($filename);
break;
......
......@@ -307,35 +307,7 @@ class Events
return $this->vcalendar->returnCalendar();
}
public function ExportIcsProvince($prov) {
include_once(SERVER_ROOT."/../classes/geo.php");
$geo = new Geo();
$prov = $geo->ProvinceGetByProv($prov);
if(isset($prov['id_prov'])) {
$base_url = $this->IcsCalendarInit(0,$prov['prov']);
include_once(SERVER_ROOT."/../classes/events.php");
$ee = new Events();
$events = $ee->SearchProvince($prov['id_prov']);
foreach($events as $event) {
$this->IcsEvent($event, $base_url);
}
return $this->vcalendar->returnCalendar();
}
}
public function IcsEvents($events,$params) {
$id = $name = $desc = '';
if(isset($params['id_reg']) && $params['id_reg']>0) {
$id = 'r' . $params['id_reg'];
$name = $params['reg'];
$desc = "Appuntamenti in {$params['reg']}";
}
if(isset($params['id_prov']) && $params['id_prov']>0) {
$id = 'p' . $params['id_prov'];
$name = $params['prov'];
$desc = "Appuntamenti nella provincia di {$params['prov']}";
}
public function ExportIcsEvents($events,$id,$name,$desc) {
$base_url = $this->IcsCalendarInit($id,$name,$desc);
foreach($events as $event) {
$this->IcsEvent($event, $base_url);
......@@ -580,6 +552,35 @@ class Events
return $db->QueryExe($rows, $sqlstr, true);
}
public function RequestApi($arguments) {
$formats = array('xml','json','ical');
$params = array();
$params['format'] = (isset($arguments['format']) && in_array($arguments['format'],$formats))? $arguments['format'] : 'json';
$params['id_prov'] = "0";
$params['id_reg'] = "0";
if(isset($arguments['prov']) && $arguments['prov']!='') {
$geo = new Geo();
$prov = $geo->ProvinceGetByProv($arguments['prov']);
if(isset($prov['id_prov']) && $prov['id_prov']>0) {
$params = array_merge($params,$prov);
}
} elseif(isset($arguments['reg']) && $arguments['reg']!='') {
$geo = new Geo();
$reg = $geo->RegionGetByReg($arguments['reg']);
if(isset($reg['id_reg']) && $reg['id_reg']>0) {
$params = array_merge($params,$reg);
}
}
$events = $this->NextPub($params);
$info = array();
$info['params'] = $params;
$info['qs'] = $arguments;
$info['count'] = count($events);
$return = array('info'=>$info);
$return['events'] = $this->EventsObj($events,$params['format']=='json');
return $return;
}
/**
* Search events
*
......@@ -754,34 +755,14 @@ class EventsREST
{
public $arguments;
private $formats = array('xml','json','ical');
public function Next() {
$params = array();
$params['format'] = (isset($this->arguments['format']) && in_array($this->arguments['format'],$this->formats))? $this->arguments['format'] : 'json';
$params['id_prov'] = "0";
$params['id_reg'] = "0";
if(isset($this->arguments['prov']) && $this->arguments['prov']!='') {
$geo = new Geo();
$prov = $geo->ProvinceGetByProv($this->arguments['prov']);
if(isset($prov['id_prov']) && $prov['id_prov']>0) {
$params = array_merge($params,$prov);
}
} elseif(isset($this->arguments['reg']) && $this->arguments['reg']!='') {
$geo = new Geo();
$reg = $geo->RegionGetByReg($this->arguments['reg']);
if(isset($reg['id_reg']) && $reg['id_reg']>0) {
$params = array_merge($params,$reg);
}
}
$ee = new Events();
$events = $ee->NextPub($params);
$info = array();
$info['params'] = $params;
$info['qs'] = $this->arguments;
$info['count'] = count($events);
$return = array('info'=>$info);
$return['events'] = $ee->EventsObj($events,$params['format']=='json');
include_once(SERVER_ROOT."/../classes/cache.php");
$cache = new Cache;
$cache->ttl = 10;
$cache_id = serialize($this->arguments);
$return_ser = $cache->Get('events_api',$cache_id);
$return = unserialize($return_ser);
$params = $return['info']['params'];
switch($params['format']) {
case 'json':
header('Content-type: application/json');
......@@ -794,8 +775,18 @@ class EventsREST
echo $xh->Array2Xml($return);
break;
case 'ical':
$ee = new Events();
echo $ee->IcsEvents($events,$params);
$id = $name = $desc = '';
if(isset($params['id_reg']) && $params['id_reg']>0) {
$id = 'r' . $params['id_reg'];
$name = $params['reg'];
$desc = "Appuntamenti in {$params['reg']}";
}
if(isset($params['id_prov']) && $params['id_prov']>0) {
$id = 'p' . $params['id_prov'];
$name = $params['prov'];
$desc = "Appuntamenti nella provincia di {$params['prov']}";
}
echo $ee->ExportIcsEvents($return['events'],$id,$name,$desc);
break;
}
}
......
......@@ -126,6 +126,9 @@ class PhPeaceUpdates
{
switch($version_new)
{
case "304":
$fm->DirAction("cache/events_api","check");
break;
case "300":
$this->UpdateDB("269");
break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment