You are here

function oa_events_find_next_event in Open Atrium Events 7.2

Find the next upcoming event or the last event in a series.

Parameters

array $items: An array of dates attached to a field.

Return value

array An array, keyed by delta and event.

2 calls to oa_events_find_next_event()
oa_events_field_add_to_calendar_markup in ./oa_events.module
Create Addthis Event calendar markup.
oa_events_field_formatter_view in ./oa_events.module
Implements hook_field_formatter_view().

File

./oa_events.module, line 126
Code for the OA Events feature.

Code

function oa_events_find_next_event($items) {

  // Set some base params we'll need.
  $now = time();
  $event = NULL;
  $event_delta = NULL;
  $next_event = NULL;
  $last_event = NULL;
  $next_event_delta = NULL;
  $last_event_delta = NULL;
  $diff = 0;

  // Loop through all events.
  foreach ($items as $delta => $date) {

    // If event is a future date.
    if ($date['value'] >= $now) {

      // That happens before the last found future event
      if ($diff == 0 || $date['value'] - $now < $diff) {

        // Set it as the currently found next event, and update the difference.
        $diff = $date['value'] - $now;
        $next_event = $date;
        $next_event_delta = 0;
      }
    }
    else {
      if (empty($last_event) || $date['value'] > $last_event['value']) {
        $last_event = $date;
        $last_event_delta = $delta;
      }
    }
  }

  // If there is no future event we'll use the last occurrence.
  if (empty($next_event)) {
    $event = $last_event;
    $event_delta = $last_event_delta;
  }
  else {
    $event = $next_event;
    $event_delta = $next_event_delta;
  }
  return array(
    'delta' => $event_delta,
    'event' => $event,
  );
}