You are here

function rooms_date_range_fields in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7

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 have a specific consistent behaviour and groups the form elements and javascript injection in one place.

Parameters

int $year:

int $month:

Return value

array The array holding the field definitions

6 calls to rooms_date_range_fields()
rooms_availability_update_status_form in modules/rooms_availability/rooms_availability.module
Form to manage the availability for specific month.
rooms_booking_availability_search_form_builder in modules/rooms_booking_manager/rooms_booking_manager.availability_search.inc
Form callback for rooms_booking_availability_search_form_builder.
rooms_booking_edit_form in modules/rooms_booking/rooms_booking.admin.inc
Form callback: create or edit a booking.
rooms_pricing_update_form in modules/rooms_pricing/rooms_pricing.module
Form to manage the room units pricing.
update_availability_calendar_form in modules/rooms_availability/rooms_availability.module
A basic form that allows us to update the state of the calendar.

... See full list

File

./rooms.module, line 835
Provides basic underlying functionality and configuration options used by all Rooms modules

Code

function rooms_date_range_fields($year = NULL, $month = NULL) {
  $date_range_fields = array();
  $date_format = variable_get('rooms_date_format', 'd-m-Y');

  // Create unique ids and selectors for each picker.
  $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';
  $days_in_advance = '+' . variable_get('rooms_booking_start_date', 1) . 'd';

  // Allow users with the "make same-day bookings" permission to make
  // bookings starting on any day.
  if (user_access('make same-day bookings')) {
    $days_in_advance = '+0d';
  }

  // Specify the default datepicker parameters (see date_popup_element_info())
  $datepicker_options = array(
    'dateFormat' => date_popup_format_to_popup($date_format),
    // Limit bookings to X days in advance, depending on the
    // chosen configuration in your Rooms installation, defaults
    // to one day in advance.
    'minDate' => $days_in_advance,
  );
  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($date_format);
    $date
      ->modify('last day of this month');
    $max_date = $date
      ->format($date_format);
    $datepicker_options += array(
      'minDate' => $min_date,
      'maxDate' => $max_date,
      'defaultDate' => $min_date,
      'numberOfMonths' => 1,
    );
  }
  else {
    $datepicker_options += array(
      'minDate' => $days_in_advance,
    );
  }
  $date_range_fields['rooms_start_date'] = array(
    '#prefix' => '<div class="form-wrapper rooms-date-range"><div class="start-date" id="' . $start_date_id . '">',
    '#suffix' => '</div>',
    '#type' => 'date_popup',
    '#title' => variable_get_value('rooms_arrival_date'),
    '#date_type' => DATE_DATETIME,
    '#date_format' => $date_format,
    '#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,
  );
  $start_day = variable_get('rooms_booking_start_date', 1);

  // Allow users with the "make same-day bookings" permission to make
  // bookings starting on any day.
  if (user_access('make same-day bookings')) {
    $start_day = '0';
  }
  $date_range_fields['rooms_end_date'] = array(
    '#prefix' => '<div class="end-date" id="' . $end_date_id . '">',
    '#suffix' => '</div></div>',
    '#type' => 'date_popup',
    '#title' => variable_get_value('rooms_departure_date'),
    '#date_type' => DATE_DATETIME,
    '#date_format' => $date_format,
    '#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', 'rooms') . '/css/rooms_date_range_fields.css',
      ),
      'js' => array(
        drupal_get_path('module', 'rooms') . '/js/rooms_date_popup.js',
        array(
          'data' => array(
            'rooms' => array(
              'roomsStartYear' => $year,
              'roomsStartMonth' => $month,
              'roomsBookingStartDay' => $start_day,
              'roomsDateFormat' => date_popup_format_to_popup($date_format),
              // 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.rooms.datepickers.
              'datepickers' => array(
                $start_date_selector => array(
                  'endDateSelector' => $end_date_selector,
                ),
              ),
            ),
          ),
          'type' => 'setting',
        ),
      ),
    ),
  );
  return $date_range_fields;
}