You are here

function google_calendar_block_block_view in Google Calendar Block 7

Same name and namespace in other branches
  1. 7.2 google_calendar_block.module \google_calendar_block_block_view()

Implements hook_block_view().

File

./google_calendar_block.module, line 398
A module to provide simple Google Calendar blocks using the Google Data APIs.

Code

function google_calendar_block_block_view($delta) {

  // Load the configuration.
  $config = google_calendar_block_block_get($delta);

  // Unserialize the calendar properties.
  $data = unserialize($config['data']);

  // Prepare the basic block data.
  $block = array();
  $block['subject'] = check_plain($config['info']);

  // Statically cache the calendar data to improve performance.
  $calendars =& drupal_static(__FUNCTION__);

  // Calendar data is cached per block.
  $cid = 'google_calendar_block:' . $delta;

  // Load the calendar data if it's not found in the static cache.
  if (!isset($calendars[$cid])) {
    if (($library = libraries_detect('zf1')) && !empty($library['installed'])) {

      // Add the ZF1 library directory to the list of locations where PHP
      // looks for files in order to allow its loader to function properly.
      set_include_path(get_include_path() . PATH_SEPARATOR . realpath($library['library path'] . '/library'));

      // Load the ZF1 loader and use it to load Zend_Gdata classes.
      if (($library = libraries_load('zf1')) && !empty($library['loaded'])) {

        // Load the required Zend_Gdata classes.
        // The Zend_Gdata classes must be loaded before attempting to retrieve
        // the calendar data from the cache in order for PHP to understand how
        // to unserialize the serialized data back into an object.
        Zend_Loader::loadClass('Zend_Gdata');
        Zend_Loader::loadClass('Zend_Gdata_Calendar');

        // Load the calendar data if it's not found in the persistent cache.
        if ($cache = cache_get($cid, 'cache_google_calendar_block')) {
          $calendars[$cid] = $cache->data;
        }
        else {
          $service = new Zend_Gdata_Calendar();
          $query = $service
            ->newEventQuery();
          $query
            ->setUser($config['calendar_user']);
          $query
            ->setVisibility($config['calendar_visibility']);
          $query
            ->setProjection('full');
          $query
            ->setSingleEvents(TRUE);
          if (!empty($data['calendar_order_by'])) {
            $query
              ->setOrderby($data['calendar_order_by']);
          }
          if (!empty($data['calendar_sort_order'])) {
            $query
              ->setSortOrder($data['calendar_sort_order']);
          }
          if (!empty($data['calendar_future_events'])) {
            $query
              ->setFutureevents((bool) $data['calendar_future_events']);
          }
          if (!empty($data['calendar_max_results'])) {
            $query
              ->setMaxResults($data['calendar_max_results']);
          }
          if (!empty($data['calendar_limit_date_range'])) {
            $min = $data['calendar_start_min'];
            $date = "{$min['year']}-{$min['month']}-{$min['day']}";
            $query
              ->setStartMin($date);
            $max = $data['calendar_start_max'];
            $date = "{$max['year']}-{$max['month']}-{$max['day']}";
            $query
              ->setStartMax($date);
          }

          // Retrieve the calendar event feed.
          try {
            $calendars[$cid] = $service
              ->getCalendarEventFeed($query);
            cache_set($cid, $calendars[$cid], 'cache_google_calendar_block', CACHE_TEMPORARY);
          } catch (Zend_Gdata_App_Exception $e) {
            watchdog('google_calendar_block', $e
              ->getMessage(), array(), WATCHDOG_ERROR);
          }
        }
      }
    }
  }
  $items = array();
  if (!empty($calendars[$cid])) {
    foreach ($calendars[$cid] as $event_feed) {
      $variables = array(
        'title' => $event_feed->title
          ->getText(),
        'author' => $event_feed->author[0]->name
          ->getText(),
        'summary' => $event_feed->summary,
        'content' => $event_feed->content
          ->getText(),
        'start' => _google_calendar_block_google_date_to_timestamp($event_feed->when[0]->startTime),
        'end' => _google_calendar_block_google_date_to_timestamp($event_feed->when[0]->endTime),
        'where' => $event_feed->where[0]
          ->getValueString(),
      );
      $items[] = theme('google_calendar_block_event', $variables);
    }
  }
  $block['content'] = array(
    '#theme' => 'item_list',
    '#items' => $items,
    '#type' => 'ul',
  );
  return $block;
}