function date_repeat_build_dates in Date 8
Same name and namespace in other branches
- 5.2 date/date_repeat.inc \date_repeat_build_dates()
- 6.2 date/date_repeat.inc \date_repeat_build_dates()
- 7.3 date_repeat_field/date_repeat_field.module \date_repeat_build_dates()
- 7 date_repeat.inc \date_repeat_build_dates()
- 7.2 date_repeat_field/date_repeat_field.module \date_repeat_build_dates()
Helper function to build repeating dates from an $entity field.
Pass in either the RRULE or the $form_values array for the RRULE, whichever is missing will be created when needed.
3 calls to date_repeat_build_dates()
- DateMigrateFieldHandler::prepare in date_migrate/
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/
date.migrate.inc - Converts incoming data into the proper field arrays for Date fields.
File
- date_repeat_field/
date_repeat_field.module, line 418 - Creates the option of Repeating date fields and manages Date fields that use the Date Repeat API.
Code
function date_repeat_build_dates($rrule = NULL, $rrule_values = NULL, $field, $item) {
$field_name = $field['field_name'];
if (empty($rrule)) {
$rrule = DateiCalParse::ical_build_rrule($rrule_values);
}
if (empty($rrule_values)) {
$rrule_values = DateiCalParse::parse_rrule($rrule);
}
// Split the RRULE into RRULE, EXDATE, and RDATE parts.
// This assumes the EXDATE and RDATE are on their own lines
// below the RRULE, which is the way they will be constructed
// by this code.
// First parse the complete rule to get exceptions and additions.
$parts = DateiCalParse::split_rrule($rrule);
$parsed_exceptions = (array) $parts[1];
$parsed_rdates = (array) $parts[2];
// Next make sure only the rrule is left from a multi-line entry.
// But store the multi-line entry to be saved again.
$raw_rrule = $rrule;
$rrule = DateiCalParse::extract_rrule_string($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 DrupalDateTime($item['value'], $timezone_db, date_type_format($field['type']));
if ($timezone != $timezone_db) {
date_timezone_set($start, timezone_open($timezone));
}
if (!empty($item['value2']) && $item['value2'] != $item['value']) {
if ($end = new DrupalDateTime($item['value2'], date_get_timezone_db($field['settings']['tz_handling']), date_type_format($field['type']))) {
date_timezone_set($end, timezone_open($timezone));
}
}
if (empty($end)) {
$end = $start;
}
$interval = $start
->diff($end);
if (!empty($rrule_values['UNTIL']['datetime'])) {
$end = DateiCalParse::ical_date($rrule_values['UNTIL'], $timezone);
}
elseif (!empty($rrule_values['COUNT'])) {
$end = NULL;
}
elseif (empty($rrule_values['COUNT'])) {
// No UNTIL and no COUNT?
return array();
}
$exceptions = array();
foreach ($parsed_exceptions as $exception) {
if ($date = DateiCalParse::ical_date($exception, $timezone)) {
$exceptions[] = date_format($date, 'Y-m-d');
}
}
$additions = array();
foreach ($parsed_rdates as $rdate) {
if ($date = DateiCalParse::ical_date($rdate, $timezone)) {
$additions[] = date_format($date, 'Y-m-d');
}
}
$calc = new DateRRuleCalc($rrule, $start, $end, $exceptions, $additions);
$dates = $calc
->compute();
$value = array();
foreach ($dates as $delta => $date) {
// date_repeat_calc always returns DATE_ISO dates, which is
// not necessarily $field['type'] dates.
// Convert returned dates back to db timezone before storing.
$date_start = new DrupalDateTime($date, $timezone, DATE_FORMAT_DATETIME);
date_timezone_set($date_start, timezone_open($timezone_db));
$date_end = clone $date_start;
$date_end
->add($interval);
$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' => $raw_rrule,
);
}
return $value;
}