| Event | Date | Number of available spots |  | 
|---|
"); // template for the table header, you can add or remove columns here if you wish
define ('TABLE_END', ""); // template for the table footer
define ('TABLE_ROW', "| $(resource) | $(duration) | $(available) | $(reserve_link) | 
"); // template for the table row, each event will be listed according to this template with $(resource), $(duration), $(available) and $(reserve_link) tags replaced with the actual values
define ('RESERVE_URL', "http://www.planyo.com/reserve.php?resource_id=%d&prefill=true&start_date=%s"); // URL of the reservation page for given event. This will be used in a printf function with the first parameter being resource ID (%d) and the second being the start date (%s). The default will use simple integration, you will most likely want to replace this with the URL of the planyo page on your website to use advanced integration
// define ('RESERVE_URL', "http://www.yoursite.com/page-with-planyo-module.php?resource_id=%d&prefill=true&start_date=%s&mode=reserve"); // this is a commented-out sample to be used for advanced integration instead of the simple integration version above
define ('RESERVE_TEXT', "Make a booking"); // text to be displayed in the booking link
define ('RESOURCE_ID', NULL); // leave this as NULL if you want to display event times for all resources, or specify a resource id if you only want to display events for a single resource
define ('NO_AV_TEXT', "FULL"); // text displayed when no more tickets/places are available
function call_planyo_api($method, $params) {
  $response_raw = file_get_contents("https://www.planyo.com/rest/?api_key=".API_KEY."&method=$method"."&".$params);
  $response_json = json_decode($response_raw, true);
  if ($response_json['response_code'] == 0)
    return $response_json['data'];
  return false;
}
function output_time($date) {
  return date("g:ia", $date);
}
function output_date($date) {
  return date("F j, Y", $date);
}
function sort_event_list ($a, $b) {
  return ($a['start'] < $b['start']) ? -1 : 1;
}
function find_events($resource_filter = null) {
  $resource_list = array();
  $event_list = array();
  $api_data = call_planyo_api('get_all_resources', 'detail_level=1&list_published_only=true');
  if ($api_data && $api_data['resources'] && count($api_data['resources']) > 0) {
    $resource_list = $api_data['resources'];
  }
  foreach($resource_list as $res) {
    $resource_id = $res['id'];
    if (!$resource_filter || $resource_filter == $resource_id) {
      $event_data = call_planyo_api('get_event_times', "resource_id=$resource_id&format=array&future_only=true&expand_recurring=true");
      if ($event_data && $event_data['event_times'] && count($event_data['event_times']) > 0) {
        $duration_hours = $event_data['duration'];
        foreach($event_data['event_times'] as $e) {
          $event_start = $e['timestamp'];
          $event_list[]= array('resource_id'=>$resource_id, 'resource_name'=>$res['name'], 'start'=>$event_start, 'start_text'=>$e['text'], 'end'=>$duration_hours*3600 + $event_start, 'available'=>$e['available']);
        }
      }
    }
  }
  usort($event_list, 'sort_event_list');
  return $event_list;
}
// this function optionally accepts an array of resources for which the listing should be limited (each array items should have the keys 'id' and 'name'
// if nothing is passed, api function get_all_resources will be used to fetch all resources
function list_events ($resource_id = null) {
  echo TABLE_START;
  $events = find_events($resource_id);
  if ($events && count($events) > 0) {
    foreach($events as $e) {
      $code = TABLE_ROW;
      if (output_date($e['start']) == output_date($e['end']))
        $duration = output_date($e['start']);
      else
        $duration = output_date($e['start']).' - '.output_date($e['end']);
      $code = str_replace("$(duration)", $duration, $code);
      $code = str_replace("$(resource)", $e['resource_name'], $code);
      $code = str_replace("$(available)", $e['available'], $code);
      $code = str_replace("$(reserve_link)", ((int)$e['available'] > 0) ? "".RESERVE_TEXT."" : NO_AV_TEXT, $code);
      echo $code;
    }
  }
  echo TABLE_END;
}
$server_timezone = @date_default_timezone_get();
date_default_timezone_set ('UTC');
list_events(RESOURCE_ID);
if ($server_timezone)
  date_default_timezone_set ($server_timezone);
?>