Upgrading to 1.0.0-beta1 version of the API PHP implementation.

This commit is contained in:
ekes 2017-06-09 13:30:20 +01:00
parent 4424f09f06
commit 4ed51243b5
56 changed files with 1330 additions and 824 deletions

View file

@ -16,8 +16,17 @@ require('radar_client.php');
// Shared radar connect client.
$client = radar_client();
// Basic cache for output.
$cache = radar_cache();
// If you want to empty the cache completely
//$cache->flushAll();
// If you want to remove one item like the stored HTML for evets.php
$cache->delete('events.php');
// Add a prefered language for requests. If none is set 'und' is used and
// content is returned in its original language, first language posted..
$client->setLanguage('de');
// Check to see if there is a copy in the cache.
if ($cache->contains('events.php') && $page = $cache->fetch('events.php')) {
@ -25,15 +34,14 @@ if ($cache->contains('events.php') && $page = $cache->fetch('events.php')) {
print $page['html'];
// If it's more than an hour old, get a new one.
if ($page['created'] + 60 * 60 < time()) {
$events = radar_events_retrieve($client);
$html = radar_events_format($client, $events);
$html = radar_events_page_html($client);
$cache->delete('events.php');
}
}
else {
// Generate the page and output it.
$events = radar_events_retrieve($client);
$html = radar_events_format($client, $events, true);
$html = radar_events_page_html($client);
print $html;
}
if (!empty($html)) {
@ -42,6 +50,17 @@ if (!empty($html)) {
$cache->save('events.php', $page);
}
/**
* Make HTML page.
*/
function radar_events_page_html($client) {
$request = radar_prepare_events_request($client);
$response = $client->retrieveResponse($request);
$events = $client->parseResponse($response);
$metadata = $client->parseResponseMeta($response);
return radar_events_format($client, $events, $metadata);
}
/**
* Set a filter and retrieve events matching the filter.
*
@ -51,14 +70,19 @@ if (!empty($html)) {
* @return Radar\Connect\Event[]
* Array of radar connect events.
*/
function radar_events_retrieve(\Radar\Connect\Connect $client) {
function radar_prepare_events_request(\Radar\Connect\Connect $client) {
$filter = new \Radar\Connect\Filter;
$filter->addCity('Berlin');
// Alternatives:-
//$filter->addCity('Amsterdam');
//$filter->addDate(new DateTime('tomorrow'));
//$filter->addDay();
//$filter->addCategory('music');
//Some filters don't have explicit methods to set them so for tags...
//$filter->add('tag', 'Punk');
// See docs/classes/Radar.Connect.Filter.html for full list of methods.
// You can also see all the filter values and their counts in the metadata
// returned. See the examples at the top of radar_events_format().
// Get the request.
// arguments:
@ -66,6 +90,9 @@ function radar_events_retrieve(\Radar\Connect\Connect $client) {
// $fields - array of field names to collect, empty for default
// $limit - maximum number of events to return.
$request = $client->prepareEventsRequest($filter, array(), 50);
return $request;
// Execute request.
return $client->retrieve($request);
}
@ -77,17 +104,43 @@ function radar_events_retrieve(\Radar\Connect\Connect $client) {
* The connect client.
* @param \Radar\Connect\Event[] $events
* Array of Event entities, for example response to events request.
* @param bool $output
* If HTML output should also be sent to stdout.
* @param array $metadata
* Array of counts and facets.
*
* @return string
* The HTML output.
*/
function radar_events_format(\Radar\Connect\Connect $client, array $events, $output = FALSE) {
function radar_events_format(\Radar\Connect\Connect $client, array $events, array $metadata) {
ob_start();
ob_implicit_flush(TRUE);
$html = '';
// Metadata includes the result count.
print "<p>There are {$metadata['count']} results for the query</p>\n";
// Retrieve some facets. Summaries of filters you can use
// in further narrowed queries, and their result counts.
print '<h1>Forthcoming days</h1><ul>';
foreach ($metadata['facets']['date'] as $facet) {
print '<li>' . date('Y-m-d', $facet['formatted']) . " has {$facet['count']} events</li>\n";
}
print "</ul>\n";
// For other factets it's even more convenient. The 'filter' value is also the value you set to filter the query.
print "<h1>Categories</h1><ul>\n";
foreach ($metadata['facets']['category'] as $facet) {
print "<li>{$facet['formatted']} has {$facet['count']} events you could add a filter for them with the \$filter->addCategory('{$facet['filter']}');</li>\n";
}
print "<ul>\n";
// There's no direct method to set the tag filter. They can all be set using the key that is in this array - here tag.
// So in the example above instead of $filter->addCategory you could have equally $filter->add('category', $facet['filter']);
print "<h1>Tags</h1><ul>\n";
foreach ($metadata['facets']['tag'] as $facet) {
print "<li>{$facet['formatted']} has {$facet['count']} events you could add a filter for them with the \$filter->add('tag', '{$facet['filter']}');</li>\n";
}
print "<ul>\n";
foreach ($events as $event) {
// Title and date.
print '<h1>' . $event->getTitle() . '</h1>';
@ -98,20 +151,24 @@ function radar_events_format(\Radar\Connect\Connect $client, array $events, $out
// The groups are references. If we want to get details about
// them we actually load the group itself as well.
$groups = $client->retrieveEntityMultiple($event->getGroups());
$groups = $event->getGroups();
$groups = $client->retrieveEntityMultiple($groups);
foreach ($groups as $group) {
print '<p><strong>' . $group->getTitle() . '</strong></p>';
print '<p>' . var_dump($group->getLink(), true) . ' ' . var_dump($group->getLinkRaw(), true) . '</strong></p>';
}
// Just as with the groups the locations are just the references.
// So we load them here.
$locations = $client->retrieveEntityMultiple($event->getLocations());
$locations = $event->getLocations();
$locations = $client->retrieveEntityMultiple($locations);
foreach ($locations as $location) {
print '<p>' . $location->getAddress() . '</p>';
}
// Yep and the categories, and topics.
$categories = $client->retrieveEntityMultiple($event->getCategories());
$categories = $event->getCategories();
$categories = $client->retrieveEntityMultiple($categories);
$category_names = array();
foreach ($categories as $category) {
$category_names[] = $category->getTitle();
@ -120,7 +177,8 @@ function radar_events_format(\Radar\Connect\Connect $client, array $events, $out
print '<p>Categories: ' . implode(', ', $category_names);
}
$topics = $client->retrieveEntityMultiple($event->getTopics());
$topics = $event->getTopics();
$topics = $client->retrieveEntityMultiple($topics);
$topic_names = array();
foreach ($topics as $topic) {
$topic_names[] = $topic->getTitle();
@ -131,12 +189,7 @@ function radar_events_format(\Radar\Connect\Connect $client, array $events, $out
// Outputs the HTML if requested.
$html .= ob_get_contents();
if ($output) {
ob_flush();
}
else {
ob_clean();
}
ob_clean();
}
ob_end_clean();