Commit 781a9872 authored by Francesco Iannuzzelli's avatar Francesco Iannuzzelli

#26 Events API

parent c938a4b7
<?php
use ICal\ICal;
use Kigkonsult\Icalcreator\TimezoneHandler;
/********************************************************************
PhPeace - Portal Management System
......@@ -50,10 +47,6 @@ class Event
public $errors;
/**
* @var Kigkonsult\Icalcreator\Vcalendar */
private $vcalendar;
/**
* Initialize local properties
*
......@@ -315,94 +308,6 @@ class Event
}
}
public function ExportIcsEvent($id_event) {
$base_url = $this->IcsCalendarInit($id_event);
$event = $this->EventGet($id_event);
$this->IcsEvent($event, $base_url);
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();
}
}
private function IcsCalendarInit($id_event=0,$prov='') {
require(SERVER_ROOT."/../others/vendor/autoload.php");
include_once(SERVER_ROOT."/../classes/ini.php");
include_once(SERVER_ROOT."/../classes/texthelper.php");
$this->th = new TextHelper();
$ini = new Ini();
$tz = "Europe/Rome";
$config = [
'UNIQUE_ID' => "peacelink.it"
];
$this->vcalendar = new Kigkonsult\Icalcreator\Vcalendar($config);
$this->vcalendar->setProperty( 'method', "PUBLISH" );
$calname = "Calendario PeaceLink";
$caldesc = "Gli appuntamenti del mondo pacifista italiano";
$uuid = "pck-events";
if($id_event>0) {
$uuid .= "-{$id_event}";
$calname .= " - {$id_event}";
} elseif ($prov!='') {
$uuid .= "-{$prov}";
$calname .= " - {$prov}";
$caldesc = "Appuntamenti della provincia di $prov";
}
$this->vcalendar->setProperty( "x-wr-calname", $calname);
$this->vcalendar->setProperty( "X-WR-CALDESC", $caldesc );
$this->vcalendar->setProperty( "X-WR-TIMEZONE", $tz );
$url = $ini->Get("pub_web") . '/' . $ini->Get("events_path");
$this->vcalendar->setProperty( "X-FROM-URL", $url);
$this->vcalendar->setProperty( "X-WR-RELCALID", $uuid );
return $url;
}
private function IcsEvent($event,$base_url) {
$vevent = $this->vcalendar->newVevent();
$link = "$base_url/event.php?id={$event['id_event']}";
if($event['allday']) {
$vevent->setProperty(
'DTSTART',
date("Ymd",$event['start_date_ts']),
[ "VALUE" => "DATE" ]
);
} else {
$vevent->setProperty(
'DTSTART',
new DateTime( date("Y-m-d H:i:s",$event['start_date_ts']))
);
if($event['length']>0) {
$vevent->setProperty(
'DTSTART',
new DateTime( date("Y-m-d H:i:s",$event['start_date_ts'] + ($event['length']*3600)))
);
}
}
$vevent->setProperty('LOCATION', "{$event['address']} {$event['place']}" );
$vevent->setProperty('SUMMARY', $event['title'] );
$html = $this->th->Htmlise($event['description'],false) . "<p><a href='$link'>$link</a></p>";
$vevent->setProperty( "X-ALT-DESC", $html);
if($event['is_html']) {
$this->th->Html2Text($event['description']);
}
$description = str_replace("\t",'',trim($event['description']));
$vevent->setProperty('DESCRIPTION', $description . "\n\n$link");
$vevent->setProperty( "URL", $link);
}
public function FacebookImport($link) {
include_once(SERVER_ROOT."/../classes/file.php");
$conf = new Configuration();
......
<?php
use ICal\ICal;
use Kigkonsult\Icalcreator\TimezoneHandler;
/********************************************************************
PhPeace - Portal Management System
......@@ -50,6 +53,10 @@ class Events
*/
public $id_topic;
/**
* @var Kigkonsult\Icalcreator\Vcalendar */
private $vcalendar;
/**
* Topic group ID
*
......@@ -229,7 +236,7 @@ class Events
return $this->EventsObj($rows);
}
public function EventsObj($rows) {
public function EventsObj($rows,$json=true) {
include_once(SERVER_ROOT."/../classes/translator.php");
include_once(SERVER_ROOT."/../classes/texthelper.php");
include_once(SERVER_ROOT."/../classes/datetime.php");
......@@ -242,36 +249,26 @@ class Events
$dt = new DateTimeHelper($tr->Translate("month"),$tr->Translate("weekdays"),$tr->Translate("hours"));
$events = array();
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']}");
$e = $row;
$e['id'] = $row['id_event'];
$e['start_date'] = $dt->FormatDate($row['start_date_ts']);
$e['start_time'] = $dt->FormatTime($row['start_date_ts']);
$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['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']}";
$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['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'];
$e['url'] = $irl->PublicUrlGlobal('events', array('id'=>$row['id_event'],'subtype'=>'event','id_topic'=>0));
if($row['allday']) {
$e->allDay = true;
$e['allDay'] = true;
}
if($row['id_article']>0) {
include_once(SERVER_ROOT."/../classes/topic.php");
......@@ -283,19 +280,130 @@ class Events
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);
$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 );
$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 );
}
if($json) {
$events[] = $e;
} else {
$e['xname'] = 'event';
$events['e'.$row['id_event']] = $e;
}
$events[] = $e;
}
return $events;
}
public function ExportIcsEvent($id_event) {
include_once(SERVER_ROOT."/../classes/event.php");
$e = new Event();
$event = $e->EventGet($id_event);
$base_url = $this->IcsCalendarInit($id_event,$event['title']);
$this->IcsEvent($event, $base_url);
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']}";
}
$base_url = $this->IcsCalendarInit($id,$name,$desc);
foreach($events as $event) {
$this->IcsEvent($event, $base_url);
}
return $this->vcalendar->returnCalendar();
}
private function IcsEvent($event,$base_url) {
$vevent = $this->vcalendar->newVevent();
$link = "$base_url/event.php?id={$event['id_event']}";
if($event['allday']) {
$vevent->setProperty(
'DTSTART',
date("Ymd",$event['start_date_ts']),
[ "VALUE" => "DATE" ]
);
} else {
$vevent->setProperty(
'DTSTART',
new DateTime( date("Y-m-d H:i:s",$event['start_date_ts']))
);
if($event['length']>0) {
$vevent->setProperty(
'DTSTART',
new DateTime( date("Y-m-d H:i:s",$event['start_date_ts'] + ($event['length']*3600)))
);
}
}
$vevent->setProperty('LOCATION', "{$event['address']} {$event['place']}" );
$vevent->setProperty('SUMMARY', $event['title'] );
$html = $this->th->Htmlise($event['description'],false) . "<p><a href='$link'>$link</a></p>";
$vevent->setProperty( "X-ALT-DESC", $html);
if($event['is_html']) {
$this->th->Html2Text($event['description']);
}
$description = str_replace("\t",'',trim($event['description']));
$vevent->setProperty('DESCRIPTION', $description . "\n\n$link");
$vevent->setProperty( "URL", $link);
}
private function IcsCalendarInit($id='',$name='',$desc='') {
require(SERVER_ROOT."/../others/vendor/autoload.php");
include_once(SERVER_ROOT."/../classes/ini.php");
include_once(SERVER_ROOT."/../classes/texthelper.php");
$this->th = new TextHelper();
$ini = new Ini();
$tz = "Europe/Rome";
$config = [
'UNIQUE_ID' => "peacelink.it"
];
$this->vcalendar = new Kigkonsult\Icalcreator\Vcalendar($config);
$this->vcalendar->setProperty( 'method', "PUBLISH" );
$calname = "Calendario PeaceLink";
$caldesc = $desc!=''? $desc : "Gli appuntamenti del mondo pacifista italiano";
$uuid = "pck-events";
if($id!='') {
$uuid .= "-{$id}";
$calname .= " - {$name}";
}
$this->vcalendar->setProperty( "x-wr-calname", $calname);
$this->vcalendar->setProperty( "X-WR-CALDESC", $caldesc );
$this->vcalendar->setProperty( "X-WR-TIMEZONE", $tz );
$url = $ini->Get("pub_web") . '/' . $ini->Get("events_path");
$this->vcalendar->setProperty( "X-FROM-URL", $url);
$this->vcalendar->setProperty( "X-WR-RELCALID", $uuid );
return $url;
}
/**
* Retrieve all events associated to a specific keyword
*
......@@ -672,7 +780,24 @@ class EventsREST
$info['params'] = $params;
$info['qs'] = $this->arguments;
$info['count'] = count($events);
return array('info'=>$info,'events'=>$ee->EventsObj($events));
$return = array('info'=>$info);
$return['events'] = $ee->EventsObj($events,$params['format']=='json');
switch($params['format']) {
case 'json':
header('Content-type: application/json');
echo json_encode($return);
break;
case 'xml':
header('Content-type: text/xml');
include_once(SERVER_ROOT."/../classes/xmlhelper.php");
$xh = new XmlHelper();
echo $xh->Array2Xml($return);
break;
case 'ical':
$ee = new Events();
echo $ee->IcsEvents($events,$params);
break;
}
}
}
......
......@@ -89,7 +89,11 @@ class RestService
$arguments = $this->HandleRequest();
$return = call_user_func_array(array($class, $this->operation), $arguments);
}
break;
if(is_array($return))
{
$this->JsonEncode($return);
}
break;
case "events":
include_once(SERVER_ROOT."/../classes/events.php");
$class = new EventsREST();
......@@ -101,10 +105,6 @@ class RestService
}
break;
}
if(is_array($return))
{
$this->JsonEncode($return);
}
}
private function GetUrlLocation()
......
......@@ -29,18 +29,26 @@
if (!defined('SERVER_ROOT'))
define('SERVER_ROOT',$_SERVER['DOCUMENT_ROOT']);
include_once(SERVER_ROOT."/../classes/event.php");
include_once(SERVER_ROOT."/../classes/events.php");
include_once(SERVER_ROOT."/../classes/formhelper.php");
$fh = new FormHelper(false,0,false);
$get = $fh->HttpGet();
$id = (int)$get['id'];
$e = new Event();
if($id>0) {
echo $e->ExportIcsEvent($id);
} elseif (isset($get['reg'])) {
echo $e->ExportIcsRegion($get['reg']);
} elseif (isset($get['prov'])) {
echo $e->ExportIcsProvince($get['prov']);
$ee = new Events();
echo $ee->ExportIcsEvent($id);
} else {
$er = new EventsREST();
$params = array();
$params['format'] = 'ical';
if(isset($get['prov']) && $get['prov']!='') {
$params['prov'] = $get['prov'];
}
if(isset($get['reg']) && $get['reg']!='') {
$params['reg'] = $get['reg'];
}
$er->arguments = $params;
echo $er->Next();
}
?>
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