You are here

bat_api.module in Booking and Availability Management API 7

Same filename and directory in other branches
  1. 8 bat_api.module
  2. 7.2 bat_api.module

File

bat_api.module
View source
<?php

/**
 * API access to booking data for Rooms
 */

/**
 * Implements hook_ctools_plugin_api().
 */
function bat_api_ctools_plugin_api($module = NULL, $api = NULL) {
  if ($module == "services" && $api == "services") {
    return array(
      "version" => "3",
    );
  }
}

/**
 * Implements hook_ctools_plugin_directory().
 */
function bat_api_ctools_plugin_directory($owner, $plugin_type) {
  if ($owner == 'bat_api' && $plugin_type == 'rooms_unit_field_handler') {
    return 'plugins/rooms_unit_field_handler';
  }
}

/**
 * Implements hook_ctools_plugin_type().
 */
function bat_api_ctools_plugin_type() {
  return array(
    'rooms_unit_field_handler' => array(
      'use hooks' => TRUE,
    ),
  );
}

/**
 * Implements hook_permission()
 */
function bat_api_permission() {
  return array(
    'access units index service' => array(
      'title' => t('Access units index service'),
      'description' => t('Access units index service.'),
    ),
    'access add unit service' => array(
      'title' => t('Access add unit service'),
      'description' => t('Access add unit service.'),
    ),
    'access availability index service' => array(
      'title' => t('Access availability index service'),
      'description' => t('Access availability index service.'),
    ),
    'access update availability service' => array(
      'title' => t('Access update availability service'),
      'description' => t('Access update availability service.'),
    ),
    'access pricing index service' => array(
      'title' => t('Access pricing index service'),
      'description' => t('Access pricing index service.'),
    ),
    'access update pricing service' => array(
      'title' => t('Access update pricing service'),
      'description' => t('Access update pricing service.'),
    ),
    'access booking index service' => array(
      'title' => t('Access booking index service'),
      'description' => t('Access booking index service.'),
    ),
    'access add booking service' => array(
      'title' => t('Access add booking service'),
      'description' => t('Access add booking service.'),
    ),
  );
}

/**
 * Implements hook_services_resources().
 */
function bat_api_services_resources() {
  $resources = array();
  $resources['units'] = array(
    'operations' => array(
      'index' => array(
        'callback' => 'bat_api_services_units_index',
        'help' => t('Returns an object containing bookable unit information.'),
        'access arguments' => array(
          'access units index service',
        ),
        'args' => array(
          array(
            'name' => 'ids',
            'type' => 'string',
            'optional' => TRUE,
            'default value' => '',
            'description' => t('IDs'),
            'source' => array(
              'param' => 'ids',
            ),
          ),
          array(
            'name' => 'offset',
            'type' => 'string',
            'optional' => TRUE,
            'default value' => 0,
            'description' => t('Result offset to start listing'),
            'source' => array(
              'param' => 'offset',
            ),
          ),
          array(
            'name' => 'limit',
            'type' => 'string',
            'optional' => TRUE,
            'default value' => 100,
            'description' => t('Number of items to return'),
            'source' => array(
              'param' => 'limit',
            ),
          ),
        ),
      ),
      'update' => array(
        'callback' => 'bat_api_services_edit_unit',
        'access arguments' => array(
          'access edit unit service',
        ),
        'help' => t('Edit an existing Bookable Unit.'),
        'args' => array(
          array(
            'name' => 'unit_id',
            'type' => 'integer',
            'description' => t('Unit ID'),
            'source' => array(
              'path' => 0,
            ),
            'optional' => FALSE,
          ),
          array(
            'name' => 'rooms_unit',
            'type' => 'array',
            'description' => t('The rooms unit data to update'),
            'source' => 'data',
            'optional' => FALSE,
          ),
        ),
      ),
    ),
    'actions' => array(
      'add' => array(
        'callback' => 'bat_api_services_add_unit',
        'access arguments' => array(
          'access add unit service',
        ),
        'help' => t('Add a new Bookable Unit.'),
        'args' => array(
          array(
            'name' => 'name',
            'type' => 'string',
            'description' => t('Name'),
            'source' => array(
              'data' => 'name',
            ),
            'optional' => FALSE,
          ),
          array(
            'name' => 'type',
            'type' => 'string',
            'description' => t('Type'),
            'source' => array(
              'data' => 'type',
            ),
            'optional' => FALSE,
          ),
          array(
            'name' => 'default_state',
            'type' => 'integer',
            'description' => t('Default availability'),
            'source' => array(
              'data' => 'default_state',
            ),
            'optional' => FALSE,
          ),
          array(
            'name' => 'bookable',
            'type' => 'integer',
            'description' => t('Bookable'),
            'source' => array(
              'data' => 'bookable',
            ),
            'optional' => FALSE,
          ),
          array(
            'name' => 'status',
            'type' => 'integer',
            'description' => t('Published'),
            'source' => array(
              'data' => 'status',
            ),
            'optional' => FALSE,
          ),
        ),
      ),
    ),
  );
  $resources['availability'] = array(
    'operations' => array(
      'index' => array(
        'callback' => 'bat_api_services_availability_index',
        'help' => t('Returns an object containing availability information.'),
        'access arguments' => array(
          'access availability index service',
        ),
        'args' => array(
          array(
            'name' => 'units',
            'type' => 'string',
            'description' => t('Unit IDs'),
            'source' => array(
              'param' => 'units',
            ),
            'optional' => FALSE,
          ),
          array(
            'name' => 'start_date',
            'type' => 'string',
            'description' => t('Start date'),
            'source' => array(
              'param' => 'start_date',
            ),
            'optional' => FALSE,
          ),
          array(
            'name' => 'duration',
            'type' => 'string',
            'description' => t('Duration'),
            'source' => array(
              'param' => 'duration',
            ),
            'optional' => FALSE,
          ),
        ),
      ),
      'update' => array(
        'callback' => 'bat_api_services_update_availability',
        'access arguments' => array(
          'access update availability service',
        ),
        'help' => t('Update the availability for a given unit.'),
        'args' => array(
          array(
            'name' => 'unit_id',
            'type' => 'integer',
            'description' => t('Unit ID'),
            'source' => array(
              'path' => 0,
            ),
            'optional' => FALSE,
          ),
          array(
            'name' => 'availability_info',
            'type' => 'array',
            'description' => t('Availability information'),
            'source' => 'data',
            'optional' => FALSE,
          ),
        ),
      ),
    ),
  );
  $resources['pricing'] = array(
    'operations' => array(
      'index' => array(
        'callback' => 'bat_api_services_pricing_index',
        'help' => t('Returns an object containing pricing information.'),
        'access arguments' => array(
          'access pricing index service',
        ),
        'args' => array(
          array(
            'name' => 'units',
            'type' => 'string',
            'description' => t('Unit IDs'),
            'source' => array(
              'param' => 'units',
            ),
            'optional' => FALSE,
          ),
          array(
            'name' => 'start_date',
            'type' => 'string',
            'description' => t('Start date'),
            'source' => array(
              'param' => 'start_date',
            ),
            'optional' => FALSE,
          ),
          array(
            'name' => 'duration',
            'type' => 'string',
            'description' => t('Duration'),
            'source' => array(
              'param' => 'duration',
            ),
            'optional' => FALSE,
          ),
        ),
      ),
      'update' => array(
        'callback' => 'bat_api_services_update_pricing',
        'access arguments' => array(
          'access update pricing service',
        ),
        'help' => t('Update the pricing for a given unit.'),
        'args' => array(
          array(
            'name' => 'unit_id',
            'type' => 'integer',
            'description' => t('Unit ID'),
            'source' => array(
              'path' => 0,
            ),
            'optional' => FALSE,
          ),
          array(
            'name' => 'pricing_info',
            'type' => 'array',
            'description' => t('Pricing information'),
            'source' => 'data',
            'optional' => FALSE,
          ),
        ),
      ),
    ),
  );
  $resources['booking'] = array(
    'operations' => array(
      'index' => array(
        'callback' => 'bat_api_services_booking_index',
        'help' => t('Returns an object containing booking information.'),
        'access arguments' => array(
          'access booking index service',
        ),
        'args' => array(
          array(
            'name' => 'units',
            'type' => 'string',
            'description' => t('Unit IDs'),
            'source' => array(
              'param' => 'units',
            ),
            'optional' => FALSE,
          ),
          array(
            'name' => 'start_date',
            'type' => 'string',
            'description' => t('Start date'),
            'source' => array(
              'param' => 'start_date',
            ),
            'optional' => FALSE,
          ),
          array(
            'name' => 'duration',
            'type' => 'string',
            'description' => t('Duration'),
            'source' => array(
              'param' => 'duration',
            ),
            'optional' => FALSE,
          ),
        ),
      ),
    ),
    'actions' => array(
      'add' => array(
        'callback' => 'bat_api_services_add_booking',
        'access arguments' => array(
          'access add booking service',
        ),
        'help' => t('Add a new Rooms Unit.'),
        'args' => array(
          array(
            'name' => 'type',
            'type' => 'string',
            'description' => t('Type'),
            'source' => array(
              'data' => 'type',
            ),
            'optional' => FALSE,
          ),
          array(
            'name' => 'start_date',
            'type' => 'string',
            'description' => t('Start date'),
            'source' => array(
              'data' => 'start_date',
            ),
            'optional' => FALSE,
          ),
          array(
            'name' => 'end_date',
            'type' => 'string',
            'description' => t('End date'),
            'source' => array(
              'data' => 'end_date',
            ),
            'optional' => FALSE,
          ),
          array(
            'name' => 'unit_id',
            'type' => 'string',
            'description' => t('Unit ID'),
            'source' => array(
              'data' => 'unit_id',
            ),
            'optional' => FALSE,
          ),
        ),
      ),
    ),
  );
  return $resources;
}

/**
 * Retrieve a list of units.
 *
 * @param $unit_ids
 */
function bat_api_services_units_index($unit_ids, $offset, $limit) {
  $return = new stdClass();
  $return->sessid = session_id();
  ctools_include('plugins');
  $field_handlers = ctools_get_plugins('bat_api', 'rooms_unit_field_handler');
  $ids = array_filter(explode(',', $unit_ids));
  $query = db_select('rooms_units', 'n')
    ->fields('n', array(
    'unit_id',
    'type',
    'name',
    'default_state',
    'bookable',
    'status',
  ));
  if (!empty($ids)) {
    $query
      ->condition('unit_id', $ids, 'IN');
  }
  $query
    ->orderBy('unit_id');
  $query
    ->range($offset, $limit);
  $rooms_units = $query
    ->execute()
    ->fetchAll();
  $units = array();
  foreach ($rooms_units as $unit) {
    $units[$unit->unit_id] = array(
      'properties' => array(
        'type' => $unit->type,
        'name' => $unit->name,
        'default_state' => $unit->default_state,
        'bookable' => $unit->bookable,
        'status' => $unit->status,
      ),
      'fields' => array(),
    );
    foreach ($field_handlers as $handler) {
      $class = ctools_plugin_get_class($handler, 'handler');
      $object_handler = new $class(rooms_unit_load($unit->unit_id));
      $field_type = $object_handler
        ->getFieldType();
      $field_info = $object_handler
        ->getFieldInfo();
      if (!empty($field_info)) {
        $units[$unit->unit_id]['fields'][$field_type] = $field_info;
      }
    }
  }
  $return->units = $units;
  return $return;
}

/**
 * Create a new unit.
 *
 * @param $name
 * @param $type
 * @param $default_state
 * @param $bookable
 * @param $status
 */
function bat_api_services_add_unit($name, $type, $default_state, $bookable, $status) {
  $return = new stdClass();
  $return->sessid = session_id();
  if (rooms_unit_type_load($type) !== FALSE && $name != '') {
    $unit = rooms_unit_create(array(
      'name' => $name,
      'type' => $type,
      'default_state' => $default_state,
      'bookable' => $bookable,
      'status' => $status,
      'uid' => 0,
      'created' => REQUEST_TIME,
      'changed' => REQUEST_TIME,
    ));
    rooms_unit_save($unit);
    $return->unit_id = $unit->unit_id;
    $return->status = 1;
  }
  else {
    $return->status = 0;
  }
  return $return;
}

/**
 * Update an existing unit.
 *
 * @param $unit_id
 * @param $rooms_unit_data
 */
function bat_api_services_edit_unit($unit_id, $rooms_unit_data) {
  $return = new stdClass();
  $return->sessid = session_id();
  $unit = rooms_unit_load($unit_id);
  if ($unit !== FALSE) {
    $unit->name = isset($rooms_unit_data['name']) ? $rooms_unit_data['name'] : $unit->name;
    $unit->default_state = isset($rooms_unit_data['default_state']) ? $rooms_unit_data['default_state'] : $unit->default_state;
    $unit->bookable = isset($rooms_unit_data['bookable']) ? $rooms_unit_data['bookable'] : $unit->bookable;
    $unit->status = isset($rooms_unit_data['status']) ? $rooms_unit_data['status'] : $unit->status;
    $unit->uid = isset($rooms_unit_data['uid']) ? $rooms_unit_data['uid'] : $unit->uid;
    $unit->changed = REQUEST_TIME;
    rooms_unit_save($unit);
    $return->status = 1;
  }
  else {
    $return->status = 0;
  }
  return $return;
}

/**
 * Retrieves data in for each unit in a order list of unit ids.
 *
 * @param $unit_ids
 * @param $start_date
 * @param $duration
 */
function bat_api_services_availability_index($unit_ids, $start_date, $duration) {
  $return = new stdClass();
  $return->sessid = session_id();
  $start_date_object = new DateTime($start_date);
  $end_date_object = clone $start_date_object;
  $end_date_object
    ->add(new DateInterval('P' . $duration));
  $today = new DateTime();
  if (!user_access('view past availability information') && $today > $start_date_object) {
    if ($today > $end_date_object) {
      $return->events = array();
      return $return;
    }
    $start_date_object = $today;
  }
  $ids = explode(',', $unit_ids);
  $events_json = array();
  foreach ($ids as $id) {
    $unit = rooms_unit_load($id);
    $event_style = rooms_availability_get_style(ROOMS_AVAILABILITY_ADMIN_STYLE, $unit);
    $rc = new UnitCalendar($unit->unit_id, $unit->default_state);
    $events = $rc
      ->getEvents($start_date_object, $end_date_object);
    foreach ($events as $event) {
      if (variable_get('rooms_calendar_events_view', '0') == '0') {
        $event->end_date
          ->add(new DateInterval('P1D'));
      }
      $events_json[$id][] = $event
        ->formatJson($event_style, $unit->name);
    }
  }
  $return->events = $events_json;
  return $return;
}

/**
 * Update the availability for a given unit.
 *
 * @param $unit_id
 * @param $availability_info
 */
function bat_api_services_update_availability($unit_id, $availability_info) {
  $return = new stdClass();
  $return->sessid = session_id();
  $event_id = $availability_info['event_id'];
  $start_date = new DateTime($availability_info['start_date']);
  $end_date = new DateTime($availability_info['end_date']);
  $be = new BookingEvent($unit_id, $event_id, $start_date, $end_date);
  $events = array(
    $be,
  );
  $rc = new UnitCalendar($unit_id);
  $response = $rc
    ->updateCalendar($events);
  if ($response[$event_id] == ROOMS_BLOCKED) {
    $return->status = 0;
  }
  elseif ($response[$event_id] == ROOMS_UPDATED) {
    $return->status = 1;
  }
  return $return;
}

/**
 * Retrieves pricing data in for each unit in an order list of unit ids.
 *
 * @param $unit_ids
 * @param $start_date
 * @param $duration
 */
function bat_api_services_pricing_index($unit_ids, $start_date, $duration) {
  $return = new stdClass();
  $return->sessid = session_id();
  $start_date_object = new DateTime($start_date);
  $end_date_object = clone $start_date_object;
  $end_date_object
    ->add(new DateInterval('P' . $duration));
  $ids = explode(',', $unit_ids);
  $events_json = array();
  foreach ($ids as $id) {
    $unit = rooms_unit_load($id);
    $rc = new UnitPricingCalendar($unit->unit_id, $unit->default_state);
    $events = $rc
      ->getEvents($start_date_object, $end_date_object);
    foreach ($events as $event) {
      $events_json[$id][] = $event
        ->formatJson();
    }
  }
  $return->events = $events_json;
  return $return;
}

/**
 * Update the pricing data for a given unit.
 *
 * @param $unit_id
 * @param $pricing_info
 */
function bat_api_services_update_pricing($unit_id, $pricing_info) {
  $return = new stdClass();
  $return->sessid = session_id();
  $amount = $pricing_info['amount'];
  $start_date = $pricing_info['start_date'];
  $end_date = $pricing_info['end_date'];
  $operation = $pricing_info['operation'];
  $days = $pricing_info['days'];
  update_calendar_event($unit_id, $amount, $start_date, $end_date, $operation, $days);
  $return->status = 1;
  return $return;
}

/**
 * Provides bookings from the start date onwards ordered by unit ids.
 *
 * @param $unit_ids
 * @param $start_date
 * @param $duration
 */
function bat_api_services_booking_index($unit_ids, $start_date, $duration) {
  $return = new stdClass();
  $return->sessid = session_id();
  $ids = explode(',', $unit_ids);
  $query = db_select('rooms_bookings', 'n')
    ->fields('n', array(
    'booking_id',
    'type',
    'name',
    'unit_id',
    'start_date',
    'end_date',
  ));
  if (!empty($ids)) {
    $query
      ->condition('unit_id', $ids, 'IN');
  }
  $query
    ->orderBy('unit_id');
  $rooms_bookings = $query
    ->execute()
    ->fetchAll();
  $bookings = array();
  foreach ($rooms_bookings as $rooms_booking) {
    $bookings[$rooms_booking->unit_id][] = array(
      'booking_id' => $rooms_booking->booking_id,
      'type' => $rooms_booking->type,
      'name' => $rooms_booking->name,
      'start_date' => $rooms_booking->start_date,
      'end_date' => $rooms_booking->end_date,
    );
  }
  $return->bookings = $bookings;
  return $return;
}

/**
 * Create a new booking.
 *
 * @param $name
 * @param $type
 * @param $start_date
 * @param $end_date
 */
function bat_api_services_add_booking($type, $start_date, $end_date, $unit_id) {
  $return = new stdClass();
  $return->sessid = session_id();
  if (rooms_booking_type_load($type) !== FALSE) {
    $booking = rooms_booking_create(array(
      'type' => $type,
      'start_date' => $start_date,
      'end_date' => $end_date,
      'created' => REQUEST_TIME,
      'changed' => REQUEST_TIME,
    ));
    $unit = rooms_unit_load($unit_id);
    if ($unit !== FALSE) {
      $booking->unit_id = $unit_id;
      $booking->unit_type = $unit->type;
    }
    rooms_booking_save($booking);
    $return->booking_id = $booking->booking_id;
    $return->status = 1;
  }
  else {
    $return->status = 0;
  }
  return $return;
}

Functions

Namesort descending Description
bat_api_ctools_plugin_api Implements hook_ctools_plugin_api().
bat_api_ctools_plugin_directory Implements hook_ctools_plugin_directory().
bat_api_ctools_plugin_type Implements hook_ctools_plugin_type().
bat_api_permission Implements hook_permission()
bat_api_services_add_booking Create a new booking.
bat_api_services_add_unit Create a new unit.
bat_api_services_availability_index Retrieves data in for each unit in a order list of unit ids.
bat_api_services_booking_index Provides bookings from the start date onwards ordered by unit ids.
bat_api_services_edit_unit Update an existing unit.
bat_api_services_pricing_index Retrieves pricing data in for each unit in an order list of unit ids.
bat_api_services_resources Implements hook_services_resources().
bat_api_services_units_index Retrieve a list of units.
bat_api_services_update_availability Update the availability for a given unit.
bat_api_services_update_pricing Update the pricing data for a given unit.