View source
<?php
function commerce_coupon_date_inline_conditions_info() {
$conditions['commerce_coupon_date_evaluate_date'] = array(
'label' => t('Date range'),
'entity type' => 'commerce_coupon',
'callbacks' => array(
'configure' => 'commerce_coupon_date_evaluate_date_configure',
'build' => 'commerce_coupon_date_evaluate_date_build',
),
);
return $conditions;
}
function commerce_coupon_date_evaluate_date_build($coupon, $start = NULL, $end = NULL) {
if ($end) {
$end_timestamp = strtotime('+1 day', $end);
}
$date_format = 'D, m/d/Y';
$lower_pass = REQUEST_TIME >= $start;
$upper_pass = REQUEST_TIME <= $end_timestamp;
if ($start && $end) {
$outcome = $lower_pass && $upper_pass;
$message = t('Unfortunately, this coupon is only valid starting %start until %end.', array(
'%start' => format_date($start, 'custom', $date_format),
'%end' => format_date($end, 'custom', $date_format),
));
}
else {
if ($end) {
$outcome = $upper_pass;
$message = t('Unfortunately, this coupon is only valid until %end.', array(
'%end' => format_date($end, 'custom', $date_format),
));
}
else {
if ($start) {
$outcome = $lower_pass;
$message = t('Unfortunately, this coupon is only valid starting %start.', array(
'%start' => format_date($start, 'custom', $date_format),
));
}
}
}
if (!$outcome) {
$error =& drupal_static('commerce_coupon_error_' . strtolower($coupon->code));
$error = $message;
}
return $outcome;
}
function commerce_coupon_date_evaluate_date_configure($settings) {
if (is_string($settings)) {
$settings = unserialize($settings);
}
$form['start'] = array(
'#type' => 'date_popup',
'#title' => t('Start date'),
'#date_format' => 'm/d/Y',
'#default_value' => !empty($settings['start']) ? $settings['start'] : '',
'#element_validate' => array(
'commerce_coupon_date_range_start_validate',
),
);
$form['end'] = array(
'#type' => 'date_popup',
'#title' => t('End date'),
'#date_format' => 'm/d/Y',
'#default_value' => !empty($settings['end']) ? $settings['end'] : '',
);
return $form;
}
function commerce_coupon_date_range_start_validate($element, &$form_state, $form) {
if (empty($element['#value']['date'])) {
return;
}
$type = array_pop($element['#parents']);
$parents = $element['#parents'];
$parents[] = $type == 'start' ? 'end' : 'start';
$comp_value = drupal_array_get_nested_value($form_state['values'], $parents);
if (!empty($comp_value['date'])) {
$start_date = new DateTime($element['#value']['date'] . ' ' . $element['#value']['time']);
$start_ts = date_format($start_date, 'U');
$end_date = new DateTime($comp_value['date'] . ' ' . $comp_value['time']);
$end_ts = date_format($end_date, 'U');
if ($start_ts > $end_ts) {
form_set_error(implode('][', $parents), t('Start date must be less than end date.'));
}
}
}
function commerce_coupon_date_flush_caches() {
_commerce_coupon_install_inline_conditions_field();
}