You are here

function date_repeat_build_dates in Date 7.3

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. 6.2 date/date_repeat.inc \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.

Parameters

string|null $rrule: An RRULE string.

array|null $rrule_values: An array of rrule values.

array $field: A field definition from Field API.

array $item: The field value to process.

3 calls to date_repeat_build_dates()
DateMigrateFieldHandler::prepare in ./date.migrate.inc
Converts incoming data into the proper field arrays for Date fields.
date_repeat_field_date_field_insert in date_repeat_field/date_repeat_field.devel_generate.inc
Implements hook_date_field_insert().
date_repeat_field_widget_validate in date_repeat_field/date_repeat_field.module
Validation for date repeat form element.
1 string reference to 'date_repeat_build_dates'
DateMigrateFieldHandler::prepare in ./date.migrate.inc
Converts incoming data into the proper field arrays for Date fields.

File

date_repeat_field/date_repeat_field.module, line 451
Creates the option of Repeating Date fields and manages Date Repeat fields.

Code

function date_repeat_build_dates($rrule = NULL, $rrule_values = NULL, array $field = array(), array $item = array()) {
  include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'date_api') . '/date_api_ical.inc';
  $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['settings']['tz_handling'], $item['timezone']);
  $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
  $start = new DateObject($item['value'], $timezone_db, date_type_format($field['type']));
  $start
    ->limitGranularity($field['settings']['granularity']);
  if ($timezone != $timezone_db) {
    date_timezone_set($start, timezone_open($timezone));
  }
  if (!empty($item['value2']) && $item['value2'] != $item['value']) {
    $end = new DateObject($item['value2'], date_get_timezone_db($field['settings']['tz_handling']), date_type_format($field['type']));
    $end
      ->limitGranularity($field['settings']['granularity']);
    date_timezone_set($end, timezone_open($timezone));
  }
  else {
    $end = $start;
  }
  $duration = $start
    ->difference($end);
  $start_datetime = date_format($start, DATE_FORMAT_DATETIME);
  if (!empty($rrule_values['UNTIL']['datetime'])) {
    $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) {
    $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) {
    $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 = new DateObject($date, $timezone, DATE_FORMAT_DATETIME);
    $date_start
      ->limitGranularity($field['settings']['granularity']);
    date_timezone_set($date_start, timezone_open($timezone_db));
    $date_end = 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;
}