function bat_date_range_fields in Booking and Availability Management Tools for Drupal 7
Same name and namespace in other branches
- 8 bat.module \bat_date_range_fields()
Utility function to create two related datepickers.
We have a few forms that need a start and end date field and we need to apply the same javascript to these forms in order to enforce specific consistent behaviours and group the form elements and javascript injection in one place.
Parameters
int $year:
int $month:
Return value
array The array holding the field definitions
3 calls to bat_date_range_fields()
- bat_event_edit_form in modules/
bat_event/ bat_event.admin.inc - Form callback: create or edit a event.
- bat_event_ui_bulk_update_form in modules/
bat_event_ui/ bat_event_ui.module - Bulk update form.
- bat_event_unit_set_state_form in modules/
bat_event/ bat_event.module - Configuration form for the VBO action: Assign fixed-state event to units.
File
- ./
bat.module, line 491 - Provides basic underlying functionality and configuration options used by all BAT modules.
Code
function bat_date_range_fields($year = NULL, $month = NULL, $granularity = 'bat_hourly') {
$date_range_fields = array();
// Create unique ids and selectors for each datepicker.
$start_date_id = drupal_html_id('datepicker-start-date');
$start_date_selector = '#' . $start_date_id . ' .form-text';
$end_date_id = drupal_html_id('datepicker-end-date');
$end_date_selector = '#' . $start_date_id . ' .form-text';
// Specify the default datepicker parameters (see date_popup_element_info())
$datepicker_options = array(
'dateFormat' => date_popup_format_to_popup(variable_get('bat_daily_date_format', 'Y-m-d')),
// Limit bookings to X days in advance according to the
// chosen configuration in your BAT installation.
// Defaults to the current day.
'minDate' => '+' . variable_get('bat_event_start_date', 0) . 'd',
);
if ($year && $month) {
// Calculate min and max dates of the specified year/month.
$date = new DateTime();
$date
->setDate($year, $month, 01);
$min_date = $date
->format(variable_get('bat_daily_date_format', 'Y-m-d'));
$date
->modify('last day of this month');
$max_date = $date
->format(variable_get('bat_daily_date_format', 'Y-m-d'));
$datepicker_options += array(
'minDate' => $min_date,
'maxDate' => $max_date,
'defaultDate' => $min_date,
'numberOfMonths' => 1,
);
}
else {
$datepicker_options += array(
'minDate' => '+' . variable_get('bat_event_start_date', 0) . 'd',
);
}
$date_range_fields['bat_start_date'] = array(
'#prefix' => '<div class="form-wrapper bat-date-range"><div class="start-date" id="' . $start_date_id . '">',
'#suffix' => '</div>',
'#type' => 'date_popup',
'#title' => t('Event Start'),
'#date_type' => DATE_DATETIME,
'#date_format' => variable_get('bat_daily_date_format', 'Y-m-d'),
'#date_increment' => 1,
'#date_year_range' => '-1:+3',
// Default parameters defined above, with an additional parameter
// linking to the jQuery selector for the end datepicker.
'#datepicker_options' => array_merge($datepicker_options, array(
'endDateSelector' => $end_date_selector,
)),
'#required' => TRUE,
);
$date_range_fields['bat_end_date'] = array(
'#prefix' => '<div class="end-date" id="' . $end_date_id . '">',
'#suffix' => '</div></div>',
'#type' => 'date_popup',
'#title' => t('Event End'),
'#date_type' => DATE_DATETIME,
'#date_format' => variable_get('bat_daily_date_format', 'Y-m-d'),
'#date_increment' => 1,
'#date_year_range' => '-1:+3',
// Default parameters defined above, with an additional parameter
// parameter linking to the jQuery selector for the start datepicker.
'#datepicker_options' => array_merge($datepicker_options, array(
'startDateSelector' => $start_date_selector,
)),
'#required' => TRUE,
'#attached' => array(
'css' => array(
drupal_get_path('module', 'bat') . '/css/bat_date_range_fields.css',
),
'js' => array(
drupal_get_path('module', 'bat') . '/js/bat_date_popup.js',
array(
'data' => array(
'bat' => array(
'batStartYear' => $year,
'batStartMonth' => $month,
'batBookingStartDay' => variable_get('bat_event_start_date', 0),
'batDateFormat' => 'dd/mm/yy',
'batDateGranularity' => $granularity,
// Here we create a listing of all datepickers registered on the
// current page. This is available for use in your own custom
// jQuery scripts as Drupal.settings.bat.datepickers.
'datepickers' => array(
$start_date_selector => array(
'endDateSelector' => $end_date_selector,
),
),
),
),
'type' => 'setting',
),
),
),
);
return $date_range_fields;
}