You are here

function event_calendar_data in Event 5.2

Same name and namespace in other branches
  1. 5 event.module \event_calendar_data()

Returns an array of nodes that occur on a given date. Handles content type and taxonomy filters.

Parameters

$date A date array.:

$view Who's calling this :

$types Limit to nodes of these types:

$terms Limit to events with these taxonomy terms:

$type 'prepopulate' or 'lookup', default 'lookup', whether: to prepopulate the data array or return nodes.

$end_date optional end date for list and table views:

$rewrite_parameter optional array that will be passed on to queries: in event_database.*.inc files and merged into the fourth argument of db_rewrite_sql.

Return value

An array containing all of the events taking place on the specified date, or an empty array if none exist or $type is 'prepopulate'.

Related topics

7 calls to event_calendar_data()
event_calendar_day in ./event.module
Displays a daily event calendar.
event_calendar_list in ./event.module
Creates a themed list of events.
event_calendar_month in ./event.module
Displays a monthly event calendar.
event_calendar_table in ./event.module
Creates a themed table of events.
event_calendar_week in ./event.module
Displays a weekly event calendar.

... See full list

File

./event.module, line 878

Code

function event_calendar_data($date, $view = NULL, $types = NULL, $terms = NULL, $type = 'lookup', $end_date = array(), $rewrite_parameter = array()) {
  static $data;

  // This key is used so modules can implement private calendars.
  $rewrite_key = serialize($rewrite_parameter);
  if ($type == 'prepopulate') {

    // keep dates as strings
    $year = $date['year'];
    $month = $date['month'];
    $day = $date['day'];
    $node_load = TRUE;
    if (variable_get('event_save_memory', FALSE)) {
      $node_load = FALSE;
    }
    switch ($view) {
      case 'block':
        $node_load = FALSE;

      // fall through
      case 'month':
        $first = "{$year}-{$month}-01 00:00:00";
        $last_day = date('t', mktime(0, 0, 0, $month, 1, $year));
        $last = "{$year}-{$month}-{$last_day} 23:59:59";
        break;
      case 'week':
        $dow = _event_day_of_week($date);
        $first = date('Y-m-d 00:00:00', mktime(0, 0, 0, $month, $day - $dow, $year));
        $last = date('Y-m-d 23:59:59', mktime(12, 0, 0, $month, $day + (7 - $dow), $year));
        break;
      case 'day':
        $first = "{$year}-{$month}-{$day} 00:00:00";
        $last = "{$year}-{$month}-{$day} 23:59:59";
        break;
      case 'list':
      case 'table':
        $first = "{$year}-{$month}-01 00:00:00";
        $last = $end_date['year'] . '-' . $end_date['month'] . '-' . $end_date['day'] . ' 23:59:59';
        break;
      default:
        $first = "{$year}-{$month}-01 00:00:00";
        $last_day = date('t', mktime(0, 0, 0, $month, 1, $year));
        $last = "{$year}-{$month}-{$last_day} 23:59:59";
        break;
    }
    $first_exploded = event_explode_date($first);
    $last_exploded = event_explode_date($last);

    //call the event_load function in all modules
    module_invoke_all('event_load', $first_exploded, $last_exploded, $view, $types, $terms);
    $data[$rewrite_key][$first_exploded['year']] = array();
    $data[$rewrite_key][$last_exploded['year']] = array();
    $data[$rewrite_key][$first_exploded['year']][$first_exploded['month']] = array();
    $data[$rewrite_key][$last_exploded['year']][$last_exploded['month']] = array();
    $result = event_get_events($first, $last, 'ASC', $rewrite_parameter);
    while ($nid = db_fetch_object($result)) {
      if ($node_load) {

        // we don't want to cache the nodes, we do our own caching
        $node = node_load($nid->nid, NULL, TRUE);
        node_invoke_nodeapi($node, 'view', FALSE, FALSE);
      }
      else {
        $event = array(
          'start' => $nid->event_start,
          'end' => $nid->event_end,
          'start_orig' => $nid->event_start_orig,
          'end_orig' => $nid->event_end_orig,
          'timezone' => $nid->timezone,
          'offset' => $nid->offset,
          'offset_dst' => $nid->offset_dst,
          'has_time' => $nid->has_time,
          'has_end_date' => $nid->has_end_date,
          'dst_region' => $nid->dst_region,
          'start_utc' => $nid->event_start_utc,
          'start_user' => $nid->event_start_user,
          'start_site' => $nid->event_start_site,
          'end_utc' => $nid->event_end_utc,
          'end_user' => $nid->event_end_user,
          'end_site' => $nid->event_end_site,
        );
        $node = new stdClass();
        $node->nid = $nid->nid;
        $node->title = $nid->title;
        $node->type = $nid->type;
        $node->event = $event;
        $node->event['start_exploded'] = event_explode_date($node->event['start']);
        $node->event['end_exploded'] = event_explode_date($node->event['end']);
        $node->event['start_format'] = event_format_date($node->event['start_exploded'], 'small');
        $node->event['start_time_format'] = event_format_date($node->event['start_exploded'], 'custom', variable_get('event_ampm', '0') ? 'g:i a' : 'H:i');
        $node->event['end_format'] = event_format_date($node->event['end_exploded'], 'small');
        $node->event['end_time_format'] = event_format_date($node->event['end_exploded'], 'custom', variable_get('event_ampm', '0') ? 'g:i a' : 'H:i');
      }

      // this array contains the loaded nodes, so we
      // dont have them stored for every day they occur
      $data[$rewrite_key]['nodes'][$nid->nid] = $node;

      // we have to load these here since there is no way to pass the
      // $view parameter to nodeapi through node_load :/
      $node->event['links'] = module_invoke_all('link', 'event_node_' . $view, $node, TRUE);
      if (event_is_later($node->event['start_exploded'], $first_exploded)) {
        $node_start = $node->event['start_exploded'];
      }
      else {
        $node_start = $first_exploded;
      }
      if (event_is_later($node->event['end_exploded'], $last_exploded)) {
        $node_end = $last_exploded;
      }
      else {
        $node_end = $node->event['end_exploded'];
      }
      if (event_same_day($node->event['start_exploded'], $node->event['end_exploded'])) {
        if (event_all_day($node)) {
          $nid->state = 'allday';
        }
        else {
          $nid->state = 'singleday';
        }
        $data[$rewrite_key][$node->event['start_exploded']['year']][(int) $node->event['start_exploded']['month']][(int) $node->event['start_exploded']['day']][] = drupal_clone($nid);
      }
      else {

        // roll through each day the event occurs and set an entry for each
        $cur_date = $node_start;

        // We set the time to the start of the day so we don't miss
        // events ending on an earlier hour.
        $cur_date['hour'] = '00';
        $cur_date['minute'] = '00';
        while (event_is_later($node_end, $cur_date)) {
          if (event_same_day($cur_date, $node->event['end_exploded'])) {
            if ($node->event['end_exploded']['hour'] = '23' && ($node->event['end_exploded']['minute'] = '59')) {
              $nid->state = 'allday';
            }
            else {
              $nid->state = 'end';
            }
            $data[$rewrite_key][$node->event['end_exploded']['year']][(int) $node->event['end_exploded']['month']][(int) $node->event['end_exploded']['day']][] = drupal_clone($nid);
            break;
          }
          elseif (event_same_day($cur_date, $node->event['start_exploded'])) {
            if ($node->event['start_exploded']['hour'] = '00' && ($node->event['start_exploded']['minute'] = '00')) {
              $nid->state = 'allday';
            }
            else {
              $nid->state = 'start';
            }
            $data[$rewrite_key][$node->event['start_exploded']['year']][(int) $node->event['start_exploded']['month']][(int) $node->event['start_exploded']['day']][] = drupal_clone($nid);
          }
          else {
            $nid->state = 'ongoing';
            $data[$rewrite_key][$cur_date['year']][(int) $cur_date['month']][(int) $cur_date['day']][] = drupal_clone($nid);
          }
          $cur_date = event_date_later($cur_date, 1);
        }
      }
    }
    return array();
  }
  $day_start = $date;
  $day_start['hour'] = $day_start['minute'] = $day_start['second'] = '00';

  // dates as ints.
  $year = (int) $date['year'];
  $month = (int) $date['month'];
  $day = (int) $date['day'];
  $nodes = array();
  $event_types = event_get_types();
  if (isset($data[$rewrite_key][$year][$month][$day])) {
    if (isset($types)) {

      // content type filters set
      if (isset($terms)) {

        // taxonomy and content type filters set
        foreach ($data[$rewrite_key][$year][$month][$day] as $nid) {
          $node = $data[$rewrite_key]['nodes'][$nid->nid];
          if (in_array($node->type, $types) && event_taxonomy_filter($node, $terms)) {

            // this node is the content type and taxonomy term requested
            if (count($types) == 1 && in_array($node->type, $event_types['solo'])) {

              // only display solo types if there is only one event type requested
              $node->event['current_date'] = $day_start;
              $node->event['state'] = $nid->state;
              $nodes[] = $node;
            }
            elseif (in_array($node->type, $event_types['all'])) {
              $node->event['current_date'] = $day_start;
              $node->event['state'] = $nid->state;
              $nodes[] = $node;
            }
          }
        }
      }
      else {

        // only content type filters
        foreach ($data[$rewrite_key][$year][$month][$day] as $nid) {
          $node = $data[$rewrite_key]['nodes'][$nid->nid];
          if (in_array($node->type, $types)) {
            if (count($types) == 1 && in_array($node->type, $event_types['solo'])) {

              // only display solo types if there is only one event type requested
              $node->event['current_date'] = $day_start;
              $node->event['state'] = $nid->state;
              $nodes[] = $node;
            }
            elseif (in_array($node->type, $event_types['all'])) {
              $node->event['current_date'] = $day_start;
              $node->event['state'] = $nid->state;
              $nodes[] = $node;
            }
          }
        }
      }
    }
    elseif (isset($terms)) {

      // no types, only taxonomy filters
      foreach ($data[$rewrite_key][$year][$month][$day] as $nid) {
        $node = $data[$rewrite_key]['nodes'][$nid->nid];
        if (event_taxonomy_filter($node, $terms) && in_array($node->type, $event_types['all'])) {
          $node->event['current_date'] = $day_start;
          $node->event['state'] = $nid->state;
          $nodes[] = $node;
        }
      }
    }
    else {
      foreach ($data[$rewrite_key][$year][$month][$day] as $nid) {

        // no filters set, only show events with content types states of 'all'
        $node = $data[$rewrite_key]['nodes'][$nid->nid];
        if (in_array($node->type, $event_types['all'])) {
          $node->event['current_date'] = $day_start;
          $node->event['state'] = $nid->state;
          $nodes[] = $node;
        }
      }
    }
  }
  return $nodes;
}