You are here

function date_repeat_build_dates in Date 6.2

Same name and namespace in other branches
  1. 8 date_repeat_field/date_repeat_field.module \date_repeat_build_dates()
  2. 5.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_content_generate in date/date_content_generate.inc
Implementation of Devel module's hook_content_generate().
_date_repeat_widget_validate in date/date_repeat.inc
Validation for date repeat form element.

File

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

Code

function date_repeat_build_dates($rrule = NULL, $rrule_values = NULL, $field, $item) {
  module_load_include('inc', 'date_api', 'date_api_ical');
  $field_name = $field['field_name'];
  if (empty($rrule)) {
    $rrule = date_api_ical_build_rrule($rrule_values);
  }
  elseif (empty($rrule_values)) {
    $rrule_values = date_ical_parse_rrule(NULL, $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'], date_get_timezone_db($field['tz_handling']), $field['type'], $field['granularity']);
    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($rrule_values['UNTIL']['datetime'])) {
    if (strlen($rrule_values['UNTIL']['datetime']) < 11) {
      $rrule_values['UNTIL']['datetime'] .= ' 23:59:59';
    }
    $end = date_ical_date($rrule_values['UNTIL'], $timezone);
    $end_datetime = date_format($end, DATE_FORMAT_DATETIME);
  }
  elseif (!empty($rrule_values['COUNT'])) {
    $end_datetime = NULL;
  }
  else {

    // No UNTIL and no COUNT?
    return array();
  }

  // Split the RRULE into RRULE, EXDATE, and RDATE parts.
  $parts = date_repeat_split_rrule($rrule);
  $parsed_exceptions = (array) $parts[1];
  $exceptions = array();
  foreach ($parsed_exceptions as $exception) {
    if (strlen($exception['datetime']) < 11) {
      $exception['datetime'] .= ' 00:00:00';
    }
    $date = date_ical_date($exception, $timezone);
    $exceptions[] = date_format($date, 'Y-m-d');
  }
  $parsed_rdates = (array) $parts[2];
  $additions = array();
  foreach ($parsed_rdates as $rdate) {
    if (strlen($rdate['datetime']) < 11) {
      $rdate['datetime'] .= ' 00:00:00';
    }
    $date = date_ical_date($rdate, $timezone);
    $additions[] = date_format($date, 'Y-m-d');
  }
  $dates = date_repeat_calc($rrule, $start_datetime, $end_datetime, $exceptions, $timezone, $additions);
  $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 db timezone 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;
}