View source
<?php
use Roomify\Bat\Calendar\Calendar;
use Roomify\Bat\Event\Event;
use Roomify\Bat\Store\DrupalDBStore;
use Roomify\Bat\Unit\Unit;
function bat_booking_example_menu() {
$items = array();
$items['booking/%start_date/%end_date/%'] = array(
'title' => 'Booking',
'page callback' => 'bat_booking_confirmation_page',
'page arguments' => array(
1,
2,
3,
),
'access arguments' => array(
'book units',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function bat_booking_example_theme() {
return array(
'booking_confirmation_page' => array(
'template' => 'booking_confirmation_page',
'variables' => array(
'header' => NULL,
'form' => NULL,
),
),
);
}
function bat_booking_example_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'bat_booking_example') . '/views',
);
}
function bat_booking_confirmation_page($start_date, $end_date, $type_id) {
$header = $start_date
->format('Y-m-d') . ' - ' . $end_date
->format('Y-m-d');
$form = drupal_get_form('bat_booking_confirmation_form', $start_date, $end_date, $type_id);
return array(
'#theme' => 'booking_confirmation_page',
'#header' => $header,
'#form' => $form,
);
}
function bat_booking_confirmation_form($form, &$form_state, $start_date, $end_date, $type_id) {
$form['start_date'] = array(
'#type' => 'hidden',
'#value' => $start_date,
);
$form['end_date'] = array(
'#type' => 'hidden',
'#value' => $end_date,
);
$form['type_id'] = array(
'#type' => 'hidden',
'#value' => $type_id,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Confirm booking',
);
return $form;
}
function bat_booking_confirmation_form_submit($form, &$form_state) {
global $user;
$event_type = 'availability_example';
$start_date = $form_state['values']['start_date'];
$end_date = $form_state['values']['end_date'];
$end_date
->sub(new DateInterval('PT1M'));
$type_id = $form_state['values']['type_id'];
$state_ids = array_keys(bat_event_get_states($event_type));
$state_store = new DrupalDBStore($event_type, DrupalDBStore::BAT_STATE);
$valid_states = array_merge(array(
0,
), array_slice($state_ids, 0, 1));
$drupal_units = bat_unit_load_multiple(FALSE, array(
'type_id' => $type_id,
));
$bat_units = array();
foreach ($drupal_units as $unit_id => $unit) {
$bat_units[] = new Unit($unit_id, $unit
->getEventDefaultValue($event_type));
}
if (count($bat_units)) {
$calendar = new Calendar($bat_units, $state_store);
$response = $calendar
->getMatchingUnits($start_date, $end_date, $valid_states, array());
$valid_unit_ids = array_keys($response
->getIncluded());
if (count($valid_unit_ids)) {
$event = bat_event_create(array(
'type' => $event_type,
'start_date' => $start_date
->format('Y-m-d H:i:s'),
'end_date' => $end_date
->format('Y-m-d H:i:s'),
'uid' => $user->uid,
));
$event->event_bat_unit_reference[LANGUAGE_NONE][0]['target_id'] = reset($valid_unit_ids);
$event->event_state_reference[LANGUAGE_NONE][0]['state_id'] = end($state_ids);
$event
->save();
$booking = bat_booking_create(array(
'type' => 'standard',
'label' => 'Example Booking',
));
$booking->booking_start_date[LANGUAGE_NONE][0]['value'] = $start_date
->format('Y-m-d H:i:s');
$booking->booking_end_date[LANGUAGE_NONE][0]['value'] = $end_date
->format('Y-m-d H:i:s');
$booking->booking_event_reference[LANGUAGE_NONE][0]['target_id'] = $event->event_id;
$booking
->save();
drupal_set_message(t('Booking created'));
}
else {
drupal_set_message(t('No units'), 'error');
}
}
}
function start_date_load($start_date) {
$start_date = check_plain($start_date);
try {
$sd = new DateTime($start_date);
} catch (Exception $e) {
$sd = 0;
}
return $sd;
}
function end_date_load($end_date) {
$end_date = check_plain($end_date);
try {
$ed = new DateTime($end_date);
} catch (Exception $e) {
$ed = 0;
}
return $ed;
}