diff --git a/agenda-admin.tpl.php b/agenda-admin.tpl.php index 8133029..1c126e1 100644 --- a/agenda-admin.tpl.php +++ b/agenda-admin.tpl.php @@ -10,4 +10,3 @@

You have not created any blocks yet.

-

» .

diff --git a/agenda.admin.php b/agenda.admin.php index d885e1a..2f0b88e 100644 --- a/agenda.admin.php +++ b/agenda.admin.php @@ -90,6 +90,23 @@ function agenda_admin_delete_submit($form, $form_state) { /** + * Set Google API Key + */ +function agenda_admin_googleapi($form, &$form_state){ + $form['agenda_googleapi'] = array( + '#type' => 'textfield', + '#title' => t('Google API Key'), + '#default_value' => variable_get('agenda_googleapi', ''), + '#size' => 39, + '#maxlength' => 39, + '#description' => t('Key for server applications - https://developers.google.com/console/help/new/#usingkeys'), + '#required' => TRUE, + ); + return system_settings_form($form); +} + + +/** * Manage agenda */ function agenda_admin_configure($form, $form_state, $delta) { @@ -323,6 +340,7 @@ function agenda_admin_configure_submit($form, $form_state) { * Provide a page to debug a calendar ID that is not working */ function agenda_debug($bid) { + /* $output = array(); // Date check (http://drupal.org/node/545174) @@ -425,6 +443,7 @@ function agenda_debug($bid) { 'event_table' => array('#markup' => $event_table, '#prefix' => '

Events

', '#suffix' => '
'), '#attached' => array('css' => array(drupal_get_path('module', 'agenda') . '/agenda.css')), ); + */ } diff --git a/agenda.module b/agenda.module index 18fc38e..afe7f4e 100644 --- a/agenda.module +++ b/agenda.module @@ -23,6 +23,30 @@ function agenda_menu() { 'file' => 'agenda.admin.php', ); + $items['admin/config/services/agenda/list'] = array( + 'title' => 'List', + 'type' => MENU_DEFAULT_LOCAL_TASK, + ); + + $items['admin/config/services/agenda/googleapi'] = array( + 'title' => 'Settings', + 'description' => 'Set Google API Key', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('agenda_admin_googleapi'), + 'access arguments' => array('configure agenda blocks'), + 'type' => MENU_LOCAL_TASK, + 'file' => 'agenda.admin.php', + ); + + $items['admin/config/services/agenda/0/configure'] = array( + 'title' => 'Add new block', + 'type' => MENU_LOCAL_ACTION, + 'page callback' => 'drupal_get_form', + 'page arguments' => array('agenda_admin_configure', 4), + 'access arguments' => array('configure agenda blocks'), + 'file' => 'agenda.admin.php', + ); + $items['admin/config/services/agenda/%/configure'] = array( 'title' => 'Configure agenda block', 'type' => MENU_CALLBACK, @@ -256,7 +280,8 @@ function agenda_get_events($block, $cache = TRUE) { $eventdata = array(); foreach ($calendars as $calindex => $googleid) { list ($address, $token) = _agenda_parse_googleid($googleid); - $calendar = _agenda_load_xml($address, $token, $block); + //$calendar = _agenda_load_xml($address, $token, $block); + $calendar = _agenda_load_google($address); // If we fail to load the XML, handle it if (!$calendar) { @@ -265,14 +290,17 @@ function agenda_get_events($block, $cache = TRUE) { } // Parse out the event details - foreach ($calendar->entry as $eventxml) { - $event = _agenda_parse_event($eventxml, $block); + //foreach ($calendar->entry as $eventxml) { + //$event = _agenda_parse_event($eventxml, $block); + foreach ($calendar->getItems() as $google_event) { + $event = _agenda_parse_event($google_event, $block); if (!$event) { continue; } $event['index'] = (int) $calindex; - $event['calendar'] = (string) $calendar->title; + //$event['calendar'] = (string) $calendar->title; + $event['calendar'] = (string) $calendar->summary; $eventdata[] = $event; } } @@ -302,6 +330,7 @@ function agenda_get_events($block, $cache = TRUE) { * @return array Associative array of information about the event * @access private */ +/* function _agenda_parse_event($xml, $block) { $gd = $xml->children('http://schemas.google.com/g/2005'); @@ -361,6 +390,51 @@ function _agenda_parse_event($xml, $block) { return $event; } +*/ + + +function _agenda_parse_event($google_response, $block) { + // Timezone + $tz = new DateTimeZone($block->timezone); + + // Parse the timestamps + $updated = new DateTime($google_response->updated, $tz); + $start = new DateTime((string) ($google_response->start->dateTime) ? $google_response->start->dateTime : $google_response->start->date , $tz); + $end = new DateTime((string) ($google_response->end->dateTime) ? $google_response->end->dateTime : $google_response->end->date, $tz); + + $event = array(); + $event['title'] = htmlspecialchars((string) $google_response->summary); + $event['where'] = htmlspecialchars((string) $google_response->location); + $event['description'] = _filter_autop(filter_xss((string) $google_response->description)); + + $event['timezone'] = $block->timezone; + + $event['start original'] = (string) $google_response->start->dateTime; + $event['start date'] = $start->format($block->dateformat); + $event['start time'] = $start->format($block->timeformat); + $event['start timestamp'] = strtotime($start->format('c')); // Use strtotime instead of getTimestamp for < PHP5.3 + + $event['end original'] = (string) $google_response->end->dateTime; + $event['end date'] = $end->format($block->dateformat); + $event['end time'] = $end->format($block->timeformat); + $event['end timestamp'] = strtotime($end->format('c')); + + //Published date and time are not available in v3. + $event['updated'] = $updated->format($block->dateformat); + + //$link = (array) $google_response->htmlLink; + $event['url'] = (string) $google_response->htmlLink; + $event['link'] = l($block->linktext, $event['url'].'&ctz=America/New_York'); + + // The day the event occurs on (without time) used for grouping + $event['when'] = $start->format('Y-m-d'); + + + if($google_response->start->date){ $event['start time'] = ''; } + if($google_response->end->date){ $event['end time'] = ''; } + + return $event; +} /** @@ -399,6 +473,7 @@ function _agenda_translate($field) { * @return object An object containing the status, request and result * @access private */ +/* function _agenda_load_xml($address, $key, $block) { $url = _agenda_feed_url($address, $key, $block); $xml = drupal_http_request($url); @@ -410,7 +485,41 @@ function _agenda_load_xml($address, $key, $block) { return $data; } +*/ + +function _agenda_load_google($address) { + $calendar_id = $address; + + //Including the google-api-php-client library (required). + $autoload_path = libraries_get_path('google-api-php-client') . '/autoload.php'; + if (!file_exists($autoload_path)) { + drupal_set_message('Agenda: The google-api-php-client library was not found.', 'error'); + return; + } + include_once($autoload_path); + + // https://developers.google.com/google-apps/calendar/v3/reference + $client = new Google_Client(); + $client->setApplicationName('agenda_events_feed'); + $client->setDeveloperKey(variable_get('agenda_googleapi', '')); + + $service = new Google_Service_Calendar($client); + $optParams = array( + 'orderBy' => 'startTime', + 'singleEvents' => 'true', + 'timeMin' => date('Y-m-d') . 'T00:00:00' . date('P'), + ); + + try { + $events = $service->events->listEvents($calendar_id, $optParams); + } + catch (Exception $e) { + drupal_set_message(t('Agenda: Bad call to list events. Check Google API Key'), 'error'); + return; + } + return $events; +} /** * Return the remote path to the google feed @@ -420,6 +529,7 @@ function _agenda_load_xml($address, $key, $block) { * @return object An object containing the status, request and result * @access private */ +/* function _agenda_feed_url($address, $key, $block) { $url = sprintf(AGENDA_SOURCEPATTERN, @@ -432,7 +542,7 @@ function _agenda_feed_url($address, $key, $block) { return $url; } - +*/ /** * Parse a Google ID into the email address and token components