You are here

bat_booking_example.module in Booking and Availability Management Tools for Drupal 7

Same filename and directory in other branches
  1. 8 modules/bat_booking/bat_booking_example/bat_booking_example.module

File

modules/bat_booking/bat_booking_example/bat_booking_example.module
View source
<?php

/**
 * @file
 */
use Roomify\Bat\Calendar\Calendar;
use Roomify\Bat\Event\Event;
use Roomify\Bat\Store\DrupalDBStore;
use Roomify\Bat\Unit\Unit;

/**
 * Implements hook_menu().
 */
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;
}

/**
 * Implements hook_theme().
 */
function bat_booking_example_theme() {
  return array(
    'booking_confirmation_page' => array(
      'template' => 'booking_confirmation_page',
      'variables' => array(
        'header' => NULL,
        'form' => NULL,
      ),
    ),
  );
}

/**
 * Implements hook_views_api().
 */
function bat_booking_example_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'bat_booking_example') . '/views',
  );
}

/**
 * Booking confirmation page.
 */
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,
  );
}

/**
 * Booking confirmation 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;
}

/**
 * Submit function for the bat_booking_confirmation_form 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)) {

      // Create a new Event.
      $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();

      // Create a new Booking.
      $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');
    }
  }
}

/**
 * Loads a DateTime object from a string.
 *
 * @param string $start_date
 *   Expects to see a date in the form Y-m-d.
 *
 * @return DateTime
 *   Object or null if invalid
 */
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;
}

/**
 * Loads a DateTime object from a string.
 *
 * @param string $end_date
 *   Expects to see a date in the form Y-m-d.
 *
 * @return DateTime
 *   Object or null if invalid
 */
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;
}

Functions

Namesort descending Description
bat_booking_confirmation_form Booking confirmation form.
bat_booking_confirmation_form_submit Submit function for the bat_booking_confirmation_form form.
bat_booking_confirmation_page Booking confirmation page.
bat_booking_example_menu Implements hook_menu().
bat_booking_example_theme Implements hook_theme().
bat_booking_example_views_api Implements hook_views_api().
end_date_load Loads a DateTime object from a string.
start_date_load Loads a DateTime object from a string.