Commit 6eb16e55 authored by Francesco Iannuzzelli's avatar Francesco Iannuzzelli

#26 - Events REST API: plumbing & refactoring

parent bc3c32a7
......@@ -26,6 +26,7 @@ pub/index.html
pub/test
# published stuff
pub/.htaccess
pub/404.html
pub/banners
pub/feeds
......
......@@ -182,74 +182,12 @@ class Cache
$this->fm->Download($id,$filename);
break;
case "events":
$events = array();
include_once(SERVER_ROOT."/../classes/events.php");
$ee = new Events();
$parts = explode('-', $id);
$start_ts = $parts[0];
$end_ts = $parts[1];
include_once(SERVER_ROOT."/../classes/events.php");
include_once(SERVER_ROOT."/../classes/texthelper.php");
include_once(SERVER_ROOT."/../classes/datetime.php");
include_once(SERVER_ROOT."/../classes/irl.php");
$irl = new IRL();
$ee = new Events();
$th = new TextHelper();
$ini = new Ini();
$id_language = $ini->Get('id_language');
$tr = new Translator($id_language,0,false,0);
$dt = new DateTimeHelper($tr->Translate("month"),$tr->Translate("weekdays"),$tr->Translate("hours"));
$rows = $ee->EventsRange($start_ts, $end_ts);
foreach($rows as $row) {
$e = new stdClass();
$e->id = $row['id_event'];
$e->title = $row['title'];
$e->type = $row['type'];
$e->start = $row['start'];
$e->start_date = $dt->FormatDate($row['start_date_ts']);
$e->start_time = $dt->FormatTime($row['start_date_ts']);
$e->place = $row['place'];
$e->prov = $row['geo_name'];
$e->description = $row['is_html']? $row['description'] : $th->Htmlise($row['description'],false);
$e->place_details = $th->Htmlise($row['place_details'],false);
$e->address = $row['address'];
$e->contact_name = $row['contact_name'];
$e->phone = $row['phone'];
$e->email = $row['email'];
$e->link = $row['link'];
$e->export = $irl->pub_web . '/' . $ini->Get("events_path") . "/export.php?id={$row['id_event']}";
$e->export_escaped = urlencode($e->export);
$e->export_webcal = preg_replace('#^http(s)?://#','webcal://',$e->export);
$e->gmaps = "https://www.google.com/maps/search/?api=1&hl={$tr->lang_code}&query=" . urlencode("{$row['place']}, {$row['place_details']}");
if($row['latitude']!='' && $row['longitude']!='') {
$e->gmaps .= "&ll={$row['latitude']},{$row['longitude']}";
}
if($row['length']>0) {
$row['end'] = date("Y-m-d\TH:i",$row['start_date_ts'] + ($row['length']*3600));
}
$e->url = $irl->PublicUrlGlobal('events', array('id'=>$row['id_event'],'subtype'=>'event','id_topic'=>0));
$e->end = $row['end'];
if($row['allday']) {
$e->allDay = true;
}
if($row['id_article']>0) {
include_once(SERVER_ROOT."/../classes/topic.php");
include_once(SERVER_ROOT."/../classes/article.php");
$a = new Article($row['id_article']);
$a->ArticleLoad();
$irl->topic = new Topic($a->id_topic);
$article_options = array('id'=>$row['id_article'],'id_topic'=>($a->id_topic));
if($a->jump!="") {
$article_options['jump'] = $th->String2Url($a->jump);
}
$e->article_topic = $irl->topic->name;
$e->article_title = $a->headline;
$e->article_url = $irl->PublicUrlTopic('article', $article_options, $irl->topic);
}
if($row['has_image']) {
$e->thumb = $irl->PublicUrlGlobal("event_image",array('id'=>$row['id_event'],'size'=>1,'format'=>'jpg'),TRUE );
$e->image = $irl->PublicUrlGlobal("event_image",array('id'=>$row['id_event'],'size'=>3,'format'=>'jpg'),TRUE );
}
$events[] = $e;
}
$events = $ee->EventsRangeCal($start_ts, $end_ts);
$this->fm->WritePage($filename, json_encode($events));
break;
case "aggregated_feed":
......
......@@ -224,6 +224,73 @@ class Events
return $rows;
}
public function EventsRangeCal($start_ts,$end_ts) {
$events = array();
include_once(SERVER_ROOT."/../classes/texthelper.php");
include_once(SERVER_ROOT."/../classes/datetime.php");
include_once(SERVER_ROOT."/../classes/irl.php");
$irl = new IRL();
$th = new TextHelper();
$ini = new Ini();
$id_language = $ini->Get('id_language');
$tr = new Translator($id_language,0,false,0);
$dt = new DateTimeHelper($tr->Translate("month"),$tr->Translate("weekdays"),$tr->Translate("hours"));
$rows = $this->EventsRange($start_ts, $end_ts);
foreach($rows as $row) {
$e = new stdClass();
$e->id = $row['id_event'];
$e->title = $row['title'];
$e->type = $row['type'];
$e->start = $row['start'];
$e->start_date = $dt->FormatDate($row['start_date_ts']);
$e->start_time = $dt->FormatTime($row['start_date_ts']);
$e->place = $row['place'];
$e->prov = $row['geo_name'];
$e->description = $row['is_html']? $row['description'] : $th->Htmlise($row['description'],false);
$e->place_details = $th->Htmlise($row['place_details'],false);
$e->address = $row['address'];
$e->contact_name = $row['contact_name'];
$e->phone = $row['phone'];
$e->email = $row['email'];
$e->link = $row['link'];
$e->export = $irl->pub_web . '/' . $ini->Get("events_path") . "/export.php?id={$row['id_event']}";
$e->export_escaped = urlencode($e->export);
$e->export_webcal = preg_replace('#^http(s)?://#','webcal://',$e->export);
$e->gmaps = "https://www.google.com/maps/search/?api=1&hl={$tr->lang_code}&query=" . urlencode("{$row['place']}, {$row['place_details']}");
if($row['latitude']!='' && $row['longitude']!='') {
$e->gmaps .= "&ll={$row['latitude']},{$row['longitude']}";
}
if($row['length']>0) {
$row['end'] = date("Y-m-d\TH:i",$row['start_date_ts'] + ($row['length']*3600));
}
$e->url = $irl->PublicUrlGlobal('events', array('id'=>$row['id_event'],'subtype'=>'event','id_topic'=>0));
$e->end = $row['end'];
if($row['allday']) {
$e->allDay = true;
}
if($row['id_article']>0) {
include_once(SERVER_ROOT."/../classes/topic.php");
include_once(SERVER_ROOT."/../classes/article.php");
$a = new Article($row['id_article']);
$a->ArticleLoad();
$irl->topic = new Topic($a->id_topic);
$article_options = array('id'=>$row['id_article'],'id_topic'=>($a->id_topic));
if($a->jump!="") {
$article_options['jump'] = $th->String2Url($a->jump);
}
$e->article_topic = $irl->topic->name;
$e->article_title = $a->headline;
$e->article_url = $irl->PublicUrlTopic('article', $article_options, $irl->topic);
}
if($row['has_image']) {
$e->thumb = $irl->PublicUrlGlobal("event_image",array('id'=>$row['id_event'],'size'=>1,'format'=>'jpg'),TRUE );
$e->image = $irl->PublicUrlGlobal("event_image",array('id'=>$row['id_event'],'size'=>3,'format'=>'jpg'),TRUE );
}
$events[] = $e;
}
return $events;
}
/**
* Retrieve all events associated to a specific keyword
*
......@@ -525,4 +592,26 @@ class Events
}
}
/**
* REST services for calendar
*
* @package PhPeace
* @author Francesco Iannuzzelli <francesco@phpeace.org>
*/
class EventsREST
{
public function Next($where='') {
if($where!='') {
if(strlen($where=2)) {
// check province short match
} else {
// check province match
// check region map
}
}
return array('param1'=>$param1,'param2'=>$param2);
}
}
?>
......@@ -34,18 +34,19 @@ class RestService
{
private $module;
private $operation;
private $parts;
function __construct()
{
$location = $this->GetUrlLocation();
$parts = explode("/",$location);
if($parts[1]=="rest")
$location = $this->GetUrlLocation();
$this->parts = explode("/",$location);
if($this->parts[1]=="rest")
{
$module = $parts[2];
$module = $this->parts[2];
include_once(SERVER_ROOT."/../classes/modules.php");
if(Modules::IsActiveByPath($module))
{
$operation = $parts[3];
$operation = $this->parts[3];
$this->module = $module;
$this->operation = $operation;
}
......@@ -77,6 +78,7 @@ class RestService
public function Process()
{
$return = null;
switch($this->module)
{
case "admin":
......@@ -86,24 +88,30 @@ class RestService
{
$arguments = $this->HandleRequest();
$return = call_user_func_array(array($class, $this->operation), $arguments);
if(is_array($return))
{
$this->JsonEncode($return);
}
}
break;
case "events":
include_once(SERVER_ROOT."/../classes/events.php");
$class = new EventsREST();
if(method_exists($class, $this->operation))
{
$return = call_user_func_array(array($class, $this->operation), array_slice($this->parts,4));
}
break;
}
if(is_array($return))
{
$this->JsonEncode($return);
}
}
private function GetUrlLocation()
{
$protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
$location = $_SERVER['REQUEST_URI'];
if ($_SERVER['QUERY_STRING'])
{
$location = substr($location, 0, strrpos($location, $_SERVER['QUERY_STRING']) - 1);
}
$url = $protocol.'://'.$_SERVER['HTTP_HOST'] . $location;
return $location;
}
......
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