You are here

function event_calendar_data in Event 5

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

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

Parameters

$year The year the event is taking place.:

$month The month the event is taking place.:

$day The day the event is taking place. No leading zeroes.:

$view Who's calling this :

$types Limit to nodes of these types:

$terms Limit to events with these taxonomy terms:

Return value

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

Related topics

2 calls to event_calendar_data()
event_render_day in ./event.module
Returns an day of events for a calendar.
event_render_day_single in ./event.module
Returns a link to the event page for a single day.

File

./event.module, line 1015

Code

function event_calendar_data($year, $month, $day, $view = NULL, $types = NULL, $terms = NULL) {
  static $data;
  global $user;
  $day_start = _event_mktime(0, 0, 0, $month, $day, $year);
  if (!is_array($data[$year][$month])) {
    $data[$year][$month] = array();

    //call the event_load function in all modules
    module_invoke_all('event_load', $year, $month, $day, $view, $types, $terms);

    // get GMT values from local date values for db query
    $first = _event_mktime(0, 0, 0, $month, 1, $year);
    $last = _event_mktime(23, 59, 59, $month + 1, 0, $year);
    $result = db_query(db_rewrite_sql('SELECT n.nid, e.event_start FROM {node} n INNER JOIN {event} e ON n.nid = e.nid WHERE n.status = 1 AND ((e.event_start >= %d AND e.event_start <= %d) OR (e.event_end >= %d AND e.event_end <= %d) OR (e.event_start <= %d AND e.event_end >= %d)) ORDER BY event_start '), $first, $last, $first, $last, $first, $last);
    while ($nid = db_fetch_object($result)) {
      $node = node_load($nid->nid);

      // 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, FALSE);

      // this array contains the loaded nodes for the month, so we dont have them stored for every day they occur
      $data[$year][$month]['nodes'][$nid->nid] = $node;
      $node_start = gmmktime(0, 0, 0, _event_date('m', $node->event_start, $node->start_offset), _event_date('d', $node->event_start, $node->start_offset), _event_date('Y', $node->event_start, $node->start_offset));
      $node_end = gmmktime(0, 0, 0, _event_date('m', $node->event_end, $node->end_offset), _event_date('d', $node->event_end, $node->end_offset), _event_date('Y', $node->event_end, $node->end_offset));
      if ($node_start == $node_end && gmdate('m', $node_start) == $month) {
        $nid->event_state = 'singleday';
        $data[$year][$month][gmdate('j', $node_start)][] = $nid;
      }
      else {

        // because $first is a local timestamp we compensate for the tz offset here so we dont have to do repetitive day value comparisons below
        $first -= $first % 86400;

        // roll through each day the event occurs and set an entry for each
        for ($x = $first > $node_start ? $first : $node_start; $x < $last; $x += 86400) {
          if (gmdate('m', $x) == $month) {
            if ($x == $node_end) {
              $nid->event_state = 'end';
              $data[$year][$month][gmdate('j', $x)][] = drupal_clone($nid);
              $x = $last + 1;
            }
            elseif ($x == $node_start) {
              $nid->event_state = 'start';
              $data[$year][$month][gmdate('j', $x)][] = drupal_clone($nid);
            }
            else {
              $nid->event_state = 'ongoing';
              $data[$year][$month][gmdate('j', $x)][] = drupal_clone($nid);
            }
          }
        }
      }
    }
  }
  $nodes = array();
  $event_types = event_get_types();
  if ($data[$year][$month][$day]) {
    if (isset($types)) {

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

        // taxonomy and content type filters set
        foreach ($data[$year][$month][$day] as $nid) {
          $node = $data[$year][$month]['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->event_state;
              $nodes[] = $node;
            }
            elseif (in_array($node->type, $event_types['all'])) {
              $node->event_current_date = $day_start;
              $node->event_state = $nid->event_state;
              $nodes[] = $node;
            }
          }
        }
      }
      else {

        // only content type filters
        foreach ($data[$year][$month][$day] as $nid) {
          $node = $data[$year][$month]['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->event_state;
              $nodes[] = $node;
            }
            elseif (in_array($node->type, $event_types['all'])) {
              $node->event_current_date = $day_start;
              $node->event_state = $nid->event_state;
              $nodes[] = $node;
            }
          }
        }
      }
    }
    elseif (isset($terms)) {

      // no types, only taxonomy filters
      foreach ($data[$year][$month][$day] as $nid) {
        $node = $data[$year][$month]['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->event_state;
          $nodes[] = $node;
        }
      }
    }
    else {
      foreach ($data[$year][$month][$day] as $nid) {

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