You are here

function date_repeat_build_dates in Date 5.2

Same name and namespace in other branches
  1. 8 date_repeat_field/date_repeat_field.module \date_repeat_build_dates()
  2. 6.2 date/date_repeat.inc \date_repeat_build_dates()
  3. 7.3 date_repeat_field/date_repeat_field.module \date_repeat_build_dates()
  4. 7 date_repeat.inc \date_repeat_build_dates()
  5. 7.2 date_repeat_field/date_repeat_field.module \date_repeat_build_dates()

Helper function to build repeating dates from a $node_field.

Pass in either the RRULE or the $form_values array for the RRULE, whichever is missing will be created when needed.

2 calls to date_repeat_build_dates()
date_generate in date/date_content_generate.inc
_date_repeat_widget_validate in date/date_repeat.inc
Validation for date repeat form element.

File

date/date_repeat.inc, line 114
Implementation of Date Repeat API calculations for the CCK Date field.

Code

function date_repeat_build_dates($rrule = NULL, $values = NULL, $field, $item) {
  include_once './' . drupal_get_path('module', 'date_api') . '/date_api_ical.inc';
  $field_name = $field['field_name'];
  if (empty($rrule)) {
    $rrule = date_api_ical_build_rrule($values);
  }
  elseif (empty($values)) {
    $values = date_ical_parse($rrule);
  }

  // By the time we get here, the start and end dates have been
  // adjusted back to UTC, but we want localtime dates to do
  // things like '+1 Tuesday', so adjust back to localtime.
  $timezone = date_get_timezone($field['tz_handling'], $item['timezone']);
  $timezone_db = date_get_timezone_db($field['tz_handling']);
  $start = date_make_date($item['value'], $timezone_db, $field['type'], $field['granularity']);
  if ($timezone != $timezone_db) {
    date_timezone_set($start, timezone_open($timezone));
  }
  if (!empty($item['value2']) && $item['value2'] != $item['value']) {
    $end = date_make_date($item['value2'], $timezone_db, $field['type'], $field['granularity']);
    if ($timezone != $timezone_db) {
      date_timezone_set($end, timezone_open($timezone));
    }
  }
  else {
    $end = $start;
  }
  $duration = date_difference($start, $end);
  $start_datetime = date_format($start, DATE_FORMAT_DATETIME);
  if (!empty($values['UNTIL']['datetime'])) {
    $end = date_ical_date($values['UNTIL'], $timezone);
  }
  $end_datetime = date_format($end, DATE_FORMAT_DATETIME);

  // Split the RRULE into RRULE and EXDATE parts.
  $parts = date_repeat_split_rrule($rrule);
  $parsed_exceptions = (array) $parts[1];
  $exceptions = array();
  foreach ($parsed_exceptions as $exception) {
    $date = date_ical_date($exception);
    $exceptions[] = date_format($date, 'Y-m-d');
  }
  $dates = array();
  $dates = date_repeat_calc($rrule, $start_datetime, $end_datetime, $exceptions, $timezone);
  $value = array();
  foreach ($dates as $delta => $date) {

    // date_repeat_calc always returns DATE_DATETIME dates, which is
    // not necessarily $field['type'] dates.
    // Convert returned dates back to UTC before storing.
    $date_start = date_make_date($date, $timezone, DATE_DATETIME, $field['granularity']);
    date_timezone_set($date_start, timezone_open($timezone_db));
    $date_end = drupal_clone($date_start);
    date_modify($date_end, '+' . $duration . ' seconds');
    $value[$delta] = array(
      'value' => date_format($date_start, date_type_format($field['type'])),
      'value2' => date_format($date_end, date_type_format($field['type'])),
      'offset' => date_offset_get($date_start),
      'offset2' => date_offset_get($date_end),
      'timezone' => $timezone,
      'rrule' => $rrule,
    );
  }
  return $value;
}