<?php
// Presentation: Upcoming.org Event Hacks
// Slides: http://getluky.net/projects/openhackday06/
// Author: Gordon Luk
// Date: 2006-09-29
// Example from presentation at Yahoo! Open Hack Day, September 2006.
// Use this code at your own risk! It's here for educational purposes only.
// 

// Requires Services_Upcoming_0.2.0. See http://community.upcoming.org/w/index.php/Services_Upcoming_API_Wrapper
// You'll need to edit and put in your API KEY.

require("Services/Upcoming.php");

$API_KEY '';
$DEBUG false;

$GROUP_ID 2285;

// Let's just do the query side for now.
$options = array(
        
'DEBUG' => $DEBUG
        
'api_key' => $API_KEY
        
'endpoint' => 'http://upcoming.org/services/rest/',
        
'auth_endpoint' => 'http://upcoming.org/services/auth/'
        
);

$errorArray = array();
$upc =& new Services_Upcoming($options$errorArray);

//--------------------------------------------------
// Query Chain example.
//-------------------------------------------------- 
$xml $upc->callMethod('group.getInfo', array('group_id' => $GROUP_ID));

$group = array(
    
'id' => $xml->group['id'],
    
'name' => $xml->group['name'],
    
'description' => $xml->group['description']
    );

//--------------------------------------------------
// Call 1: Get Events
//-------------------------------------------------- 
$xml $upc->callMethod('group.getEvents', array('group_id' => $GROUP_ID'order' => 'event_time''show_past' => 0));

$event_array = array();
$venue_id_array = array();
foreach (
$xml->event as $event) {
    
$event_array[] = array(
        
'id' => intval($event['id']),
        
'name' => (string)$event['name'],
        
'description' => (string)$event['description'],
        
'start_date' => (string)$event['start_date'],
        
'end_date' => (string)$event['end_date'],
        
'start_time' => (string)$event['start_time'],
        
'personal' => intval($event['personal']),
        
'selfpromotion' => intval($event['selfpromotion']),
        
'metro_id' => intval($event['metro_id']),
        
'venue_id' => intval($event['venue_id']),
        
'user_id' => intval($event['user_id']),
        
'category_id' => intval($event['category_id'])
        );
    
$venue_id_array[] = intval($event['venue_id']);
}

//--------------------------------------------------
// Call 2: Get Venue Information
//   Each getInfo method allows a comma-separated list of id's.
//   That's why we collected them in the past step, to only
//   Make a single venue.getInfo call. Then we store them in
//   a hash of venues.
//-------------------------------------------------- 
$xml $upc->callMethod('venue.getInfo', array('venue_id' => join(','$venue_id_array)));
$venues = array();
$metro_id_array = array();
foreach (
$xml->venue as $venue) {
    
$venues[intval($venue['id'])] = array(
        
'id' => intval($venue['id']),
        
'metro_id' => intval($venue['metro_id']),
        
'name' => (string)$venue['name'],
        
'address' => (string)$venue['address'],
        
'city' => (string)$venue['city'],
        
'phone' => (string)$venue['phone'],
        
'url' => (string)$venue['url'],
        
'description' => (string)$venue['description'],
        
'private' => intval($venue['private']),
        
'latitude' => (string)($venue['latitude']),
        
'longitude' => (string)($venue['longitude']),
        
'geocoding_precision' => intval($venue['geocoding_precision']),
        
'geocoding_ambiguous' => intval($venue['geocoding_ambiguous']),
        
'user_id' => intval($venue['user_id'])
        );
        
// Collect metro ids for the next stage.
        
$metro_id_array[] = intval($venue['metro_id']);
}

//--------------------------------------------------
// Call 3: Get Metro info.
//-------------------------------------------------- 
$xml $upc->callMethod('metro.getInfo', array('metro_id' => join(','$metro_id_array)));
$metros = array();
$state_id_array = array();
foreach (
$xml->metro as $metro) {
    
$metros[intval($metro['id'])] = array(
        
'id' => intval($metro['id']),
        
'state_id' => intval($metro['state_id']),
        
'name' => (string)$metro['name'],
        
'code' => (string)$metro['code'],
        );
    
// Collect state ids for the next stage.
    
$state_id_array[] = intval($metro['state_id']);
}

//--------------------------------------------------
// Call 4: Get State/Province/Region info.
//-------------------------------------------------- 
$xml $upc->callMethod('state.getInfo', array('state_id' => join(','$state_id_array)));
$states = array();
$country_id_array = array();
foreach (
$xml->state as $state) {
    
$states[intval($state['id'])] = array(
        
'id' => intval($state['id']),
        
'country_id' => intval($state['country_id']),
        
'name' => (string)$state['name'],
        
'code' => (string)$state['code']
        );
    
// Collect state ids for the next stage.
    
$country_id_array[] = intval($state['country_id']);
}

//--------------------------------------------------
// Call 5: Get Country info.
//-------------------------------------------------- 
$xml $upc->callMethod('country.getInfo', array('country_id' => join(','$country_id_array)));
$countries = array();
foreach (
$xml->country as $country) {
    
$countries[intval($country['id'])] = array(
        
'id' => intval($country['id']),
        
'name' => (string)$country['name'],
        
'code' => (string)$country['code']
        );
}

//--------------------------------------------------
// Query Chain example end
//-------------------------------------------------- 

// Now, by starting with the event records, we can rebuild all event and venue information here.

?>
<html>
<head><title>Group Events Example</title>
<style>
body, div, p, li {
    font-family: Verdana, Sans-Serif;
}
div.vevent {
    margin-bottom: 15px;
}
</style>
</head>
<body>
<h1><?php print htmlspecialchars($group['name']) ?></h1>
<h3>Upcoming Event Listing: (<a href="http://upcoming.org/calendar/v2/group/2285/65a0ce75d7/">ical</a> | <a href="http://upcoming.org/syndicate/v2/group/2285/65a0ce75d7/">rss</a>)</h3>
<div id="content" class="vcalendar">
<?php foreach ($event_array as $event) { 
    
// Here, we can extract the proper other objects from our hashes.
    
$venue $venues[$event['venue_id']];
    
$metro $metros[$venue['metro_id']];
    
$state $states[$metro['state_id']];
    
$country $countries[$state['country_id']];
?>
    <div class="vevent">
        <div class="summary"><a href="http://upcoming.org/event/<?php print $event['id'?>/"><?php print htmlspecialchars($event['name']) ?></a></div>
        <div class="date">
            <abbr class="dtstart" title="<?php print $event['start_date']; ?>"><?php print strftime('%A, %b %e %Y'strtotime($event['start_date'])); ?></abbr>
            <?php if ($event['end_date'] != '' && $event['end_date'] != '0000-00-00') { ?> - <abbr class="dtend" title="<?php print $event['end_date']; ?>"><?php print strftime('%A, %b %e'strtotime($event['end_date'])); ?></abbr><?php ?>
        </div>
        <div class="location vcard">
            <span class="fn org"><a href="http://upcoming.org/venue/<?php print $venue['id'?>/"><?php print htmlspecialchars($venue['name']) ?></a></span>
            <div class="adr">
                <span class="street-address"><?php print htmlspecialchars($venue['address']) ?></span><br />
                <span class="locality"><?php print htmlspecialchars($venue['city']) ?></span>, <span class="region"><?php print htmlspecialchars($state['name']) ?></span>
                <?php if ($venue['latitude'] && $venue['longitude']) { ?>
                    <span class="geo" style="display: none;">
                    <span class="latitude"><?php print $venue['latitude']; ?></span>
                    <span class="longitude"><?php print $venue['longitude']; ?></span>
                    </span>
                <?php ?>
            </div>
        </div>
    </div>
<?php ?>
</div>
</body>
</html>