function node_recur_node_recur_element_validate in Node recur 7.2
Make some adjustments for UI additions
1 string reference to 'node_recur_node_recur_element_validate'
- _node_recur_node_recur_form in ./
node_recur.pages.inc - Helper function to provide the basics of the form
File
- ./
node_recur.pages.inc, line 228
Code
function node_recur_node_recur_element_validate($element, &$form_state, &$form) {
$field_name = node_recur_get_date_field_name($element['#entity']->type);
$rrule_str = $form_state['values'][$field_name][LANGUAGE_NONE][0]['rrule'];
$rrule_arr = $form_state['input'][$field_name][LANGUAGE_NONE][0]['rrule'];
$start_date = $form_state['input'][$field_name][LANGUAGE_NONE][0]['value'];
$date_time_str = $start_date['date'] . (isset($start_date['time']) ? " - {$start_date['time']}" : '');
$date_create = date_create_from_format($element['rrule']['#date_format'], $date_time_str);
$rrule_weekend = array(
"BYDAY=SA,SU",
);
$end_date = isset($rrule_arr['until_child']['datetime']) ? $rrule_arr['until_child']['datetime']['date'] : NULL;
$count = (int) $rrule_arr['count_child'];
$end_date_obj = NULL;
// Catch weekend day selections if exclude weekends is checked
$found = $rrule_arr['FREQ'] == 'WEEKLY' && !empty(array_intersect($rrule_arr['weekly']['BYDAY'], array(
'SA',
'SU',
)));
if (!empty($found) && $form_state['input'][$field_name]['exclude_weekends']) {
form_set_error($field_name . '][exclude_weekends', t('You may not select a weekend day while excluding weekends.'));
return;
}
// Check for errors, but return empty to allow date fields to set the messages
if ($rrule_arr['range_of_repeat'] == 'COUNT' && !$count) {
// Error. Return to allow date fields to throw empty error
return;
}
if ($rrule_arr['range_of_repeat'] == 'UNTIL') {
if (empty($end_date)) {
// Error. Return to allow date fields to throw empty error
return;
}
// Parse the rule logic:
$end_date_obj = date_create_from_format('m/d/Y', $rrule_arr['until_child']['datetime']['date']);
// Check for invalid until date in the past
if ($end_date_obj
->format(DATE_FORMAT_ICAL) <= $date_create
->format(DATE_FORMAT_ICAL)) {
$error_field_base = implode('][', $element['#parents']);
$error_field_until = $error_field_base . '][rrule][until_child][datetime';
$message = t('Until date may not be set to a date in the past');
form_set_error($error_field_until, $message);
return;
}
}
else {
// Because date_repeat includes the start date in it's list we need to
// increment the COUNT by 1, to meet the expected behavior of requesting
// N times from the start date, without create a duplicate node
$count++;
$form_state['values'][$field_name][LANGUAGE_NONE][0]['rrule'] = preg_replace('/(COUNT=\\d+)/', "COUNT={$count}", $rrule_str);
}
/*
* Weekends cannot be dynamically excluded in the Date Repeat ICAL RRule simply by stating remove SA and SU
* We must explicitly set each individual weekend date in the RRule via the EXDATE attribute
*/
if ((bool) $form_state['input'][$field_name]['exclude_weekends']) {
$weekends_start = clone $date_create;
$weekends_end_date = isset($end_date_obj) ? clone $end_date_obj : clone $date_create;
// Find the end date which changes based on the type of request sent
// Either UNTIL an end date or given OR an explict COUNT is given
if (empty($end_date_obj)) {
$interval_count = $count;
if ($rrule_arr['FREQ'] == 'DAILY') {
$interval_count = (int) ceil($count / 5);
$interval_freq = 'WEEKS';
}
else {
$interval_freq = substr_replace($rrule_arr['FREQ'], '', -2);
}
$interval = date_interval_create_from_date_string($interval_count . ' ' . $interval_freq);
$weekends_end_date
->add($interval);
$rrule_weekend[] = "INTERVAL=1";
$rrule_weekend[] = "FREQ={$interval_freq}";
}
// Add the dates to the RRule
$ex_dates = date_repeat_calc(implode(';', $rrule_weekend), $weekends_start
->getTimestamp(), $weekends_end_date
->getTimestamp());
if (!empty($ex_dates)) {
$form_state['values'][$field_name][LANGUAGE_NONE][0]['rrule'] .= "\nEXDATE:" . implode(',', $ex_dates);
}
}
}