function availability_calendar_booking_formlet_form in Availability Calendars 7.4
Same name and namespace in other branches
- 7.5 booking_formlet/availability_calendar_booking_formlet.module \availability_calendar_booking_formlet_form()
- 7.3 booking_formlet/availability_calendar_booking_formlet.module \availability_calendar_booking_formlet_form()
Callback for drupal_get_form() to create the booking formlet view.
drupal_get_form() is called by hook_field_formatter_view (in our case: availability_calendar_booking_formlet_field_formatter_view).
Parameters
string $form_id:
array $form_state:
Return value
array
1 string reference to 'availability_calendar_booking_formlet_form'
File
- booking_formlet/
availability_calendar_booking_formlet.module, line 534 - Availability Calendar booking formlet module. This submdule of the Availability Calendar module defines a field that shows a small booking form that interacts with a calendar field. The form only has an arraival and departure date field and a submit…
Code
function availability_calendar_booking_formlet_form($form_id, $form_state) {
static $formlet_count = 0;
$formlet_count++;
$entity_type = $form_state['build_info']['args'][0];
$entity = $form_state['build_info']['args'][1];
$entity_id = entity_extract_ids($entity_type, $entity);
$entity_id = $entity_id[0];
$entity_label = entity_label($entity_type, $entity);
$settings = $form_state['build_info']['args'][2];
$cids = $form_state['build_info']['args'][3];
$calendar = $form_state['build_info']['args'][4];
$calendar_settings = $form_state['build_info']['args'][5];
/** @var DateTime */
$from = $form_state['build_info']['args'][6];
/** @var DateTime */
$to = $form_state['build_info']['args'][7];
/** @var Integer */
$duration = 0;
if ($from instanceof DateTime && $to instanceof DateTime) {
//PHP5.3: use getTimestamp() and/or diff().
$timestamp_from = (int) $from
->format('U');
$timestamp_to = (int) $to
->format('U');
// To is inclusive, so add 1 day to the difference.
$duration = (int) round(($timestamp_to - $timestamp_from) / (60 * 60 * 24)) + 1;
}
$form = array();
$form['#id'] = "availability-calendar-booking-formlet-form-{$formlet_count}";
$form['cid'] = array(
'#type' => 'hidden',
'#value' => $calendar !== NULL ? $calendar['cid'] : '',
);
$form['calendar_label'] = array(
'#type' => 'hidden',
'#value' => $calendar !== NULL ? $calendar['name'] : '',
);
$form['entity_type'] = array(
'#type' => 'hidden',
'#value' => $entity_type,
);
$form['entity_id'] = array(
'#type' => 'hidden',
'#value' => $entity_id,
);
$form['entity_label'] = array(
'#type' => 'hidden',
'#value' => $entity_label,
);
$form['from_iso'] = array(
'#type' => 'hidden',
'#value' => !empty($from) ? $from
->format(AC_ISODATE) : '',
);
$form['to_iso'] = array(
'#type' => 'hidden',
'#value' => !empty($to) ? $to
->format(AC_ISODATE) : '',
);
$form['duration'] = array(
'#type' => 'hidden',
'#value' => $duration,
);
if ($settings['submit_only']) {
$form['from_display'] = array(
'#type' => 'hidden',
'#value' => $from instanceof DateTime ? availability_calendar_format_display_date($from) : '',
);
$form['to_display'] = array(
'#type' => 'hidden',
'#value' => $to instanceof DateTime ? availability_calendar_format_display_date($to) : '',
);
}
else {
$form['from_display'] = array(
'#type' => 'textfield',
'#title' => t('Arrival date'),
'#default_value' => $from instanceof DateTime ? availability_calendar_format_display_date($from) : '',
'#required' => TRUE,
'#disabled' => TRUE,
'#attributes' => array(
'readonly' => 'readonly',
),
'#prefix' => '<div class="acbf-date">',
'#suffix' => '<input class="acbf-reset-from form-reset" type="reset" value="' . t('Clear selected arrival date') . '" /></div>',
);
if ($settings['single_day_only']) {
$form['to_display'] = array(
'#type' => 'hidden',
'#value' => $to instanceof DateTime ? availability_calendar_format_display_date($to) : '',
);
$form['from_display']['#title'] = t('Date');
$form['from_display']['#suffix'] = '<input class="acbf-reset-from form-reset" type="reset" value="' . t('Clear selected date') . '" /></div>';
}
else {
$form['to_display'] = array(
'#type' => 'textfield',
'#title' => t('Departure date'),
'#default_value' => $to instanceof DateTime ? availability_calendar_format_display_date($to) : '',
'#required' => TRUE,
'#disabled' => TRUE,
'#attributes' => array(
'readonly' => 'readonly',
),
'#prefix' => '<div class="acbf-date">',
'#suffix' => '<input class="acbf-reset-both form-reset" type="reset" value="' . t('Clear selected dates') . '" /></div>',
);
// Display the duration if the settings indicate so.
if (!empty($settings['display_duration'])) {
$duration_display = '';
switch ($settings['display_duration']) {
case 'days':
$duration_display = format_plural($duration, '1 day', '@count days');
break;
case 'nights':
$duration_display = format_plural($duration, '1 night', '@count nights');
break;
case 'nights_and_days':
$duration_display = format_plural($duration, '1 night', '@count nights');
$duration_display .= ' / ';
$duration_display .= format_plural($duration + 1, '1 day', '@count days');
break;
}
$form['duration_display'] = array(
'#type' => 'textfield',
'#title' => t('Duration'),
'#default_value' => $duration_display,
'#required' => TRUE,
'#disabled' => TRUE,
'#attributes' => array(
'readonly' => 'readonly',
),
'#prefix' => '<div class="acbf-date">',
'#suffix' => '</div>',
);
}
}
}
// Add form submit button.
$form['actions'] = array(
'#type' => 'actions',
'submit' => array(
'#type' => 'submit',
'#value' => t('Book this resource ...'),
'#disabled' => TRUE,
),
);
global $language;
$post_url = '';
if ($settings['define_per_language'] && isset($settings['post_url'][$language->language])) {
$post_url = $settings['post_url'][$language->language];
}
else {
if (isset($settings['post_url'][LANGUAGE_NONE])) {
$post_url = $settings['post_url'][LANGUAGE_NONE];
}
else {
if (is_array($settings['post_url'])) {
$post_url = reset($settings['post_url']);
}
}
}
if (is_array($post_url)) {
$post_url = reset($post_url);
}
$form['#action'] = url($post_url);
$form['#method'] = !empty($settings['use_get']) ? 'GET' : 'POST';
// Add js and css.
// If only the book now button is shown, the calendar might not be shown.
// Make sure there is a javascript instance that represents the selected
// calendar (and that can be queried by our javascript).
if ($calendar !== NULL) {
availability_calendar_add_calendar_js($calendar['cid'], $calendar_settings['allocation_type']);
}
availability_calendar_booking_formlet_add_js($form['#id'], $cids, $settings);
$form['#attached']['css'] = array(
drupal_get_path('module', 'availability_calendar_booking_formlet') . '/availability_calendar_booking_formlet.css',
);
return $form;
}