You are here

rooms_booking.install in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7

Sets up the base table for our entity and a table to store information about the entity types.

File

modules/rooms_booking/rooms_booking.install
View source
<?php

/**
 * @file
 * Sets up the base table for our entity and a table to store information about
 * the entity types.
 */

/**
 * Implements hook_install().
 */
function rooms_booking_install() {

  // Create a basic booking type.
  rooms_booking_create_standard_booking_type();
}

/**
 * Implements hook_schema().
 */
function rooms_booking_schema() {
  $schema = array();
  $schema['rooms_bookings'] = array(
    'description' => 'The base table for Bookings.',
    'fields' => array(
      'booking_id' => array(
        'description' => 'Primary Key: Identifier for a Booking.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'type' => array(
        'description' => 'The Booking type of this Booking.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'language' => array(
        'description' => 'The language of the Booking.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'name' => array(
        'description' => 'The name of the Booking - a human-readable identifier.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the Unit was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the Unit was most recently saved.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'customer_id' => array(
        'description' => 'The customer_id this booking is tied to.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'uid' => array(
        'description' => 'The {users}.uid that owns this booking.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'unit_type' => array(
        'description' => 'The type of Unit the booking refers to',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'unit_id' => array(
        'description' => 'The unit_id this booking may be tied to.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'start_date' => array(
        'type' => 'varchar',
        'length' => 20,
        'not null' => FALSE,
        'description' => 'The start date for the booking.',
      ),
      'end_date' => array(
        'type' => 'varchar',
        'length' => 20,
        'not null' => FALSE,
        'description' => 'The end date for the booking.',
      ),
      'order_id' => array(
        'description' => 'The order_id this booking may be tied to.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'price' => array(
        'description' => 'The total price for the booking.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'booking_status' => array(
        'description' => 'The booking status (1 published, 0 unpublished).',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'data' => array(
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'A serialized array of additional data.',
      ),
    ),
    'primary key' => array(
      'booking_id',
    ),
    'indexes' => array(
      'booking_id' => array(
        'booking_id',
      ),
      'type' => array(
        'type',
      ),
      'customer_id' => array(
        'customer_id',
      ),
      'uid' => array(
        'uid',
      ),
    ),
  );
  $schema['rooms_booking_type'] = array(
    'description' => 'Stores information about defined booking types.',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique booking type identifier.',
      ),
      'type' => array(
        'description' => 'The machine-readable name of this booking type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'label' => array(
        'description' => 'The human-readable name of this booking type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'The weight of this booking type in relation to others.',
      ),
      'data' => array(
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'A serialized array of additional data related to this unit type.',
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0x1,
        'size' => 'tiny',
        'description' => 'The exportable status of the entity.',
      ),
      'module' => array(
        'description' => 'The name of the providing module if the entity has been defined in code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'id',
    ),
    'unique keys' => array(
      'type' => array(
        'type',
      ),
    ),
  );

  // Stores the customer name and potentially relates it to a Commerce
  // customer profile.
  $schema['rooms_customers'] = array(
    'description' => 'Stores information about customers.',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique booking type identifier.',
      ),
      'name' => array(
        'description' => 'Customer\'s name.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'commerce_customer_id' => array(
        'type' => 'int',
        'not null' => FALSE,
        'description' => 'Commerce customer id.',
      ),
    ),
    'primary key' => array(
      'id',
    ),
    'unique keys' => array(
      'type' => array(
        'name',
      ),
    ),
  );
  return $schema;
}

/**
 * Creates the default standard booking type.
 */
function rooms_booking_create_standard_booking_type() {
  $booking = new RoomsBookingType(array(
    'label' => 'Standard Booking',
    'type' => 'standard_booking',
  ));
  rooms_booking_type_save($booking);
}

/**
 * Create booking_status field in rooms_booking table.
 */
function rooms_booking_update_7001() {
  $booking_status = array(
    'description' => 'The booking status (1 published, 0 unpublished).',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'initial' => 1,
  );
  db_add_field('rooms_bookings', 'booking_status', $booking_status);
  return t('Add field booking_status in Rooms Booking table.');
}

/**
 * Create customers table.
 */
function rooms_booking_update_7002() {
  $schema['rooms_customers'] = array(
    'description' => 'Stores information about customers.',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique customer identifier.',
      ),
      'name' => array(
        'description' => 'Customer\'s name.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'commerce_customer_id' => array(
        'type' => 'int',
        'not null' => FALSE,
        'description' => 'Commerce customer id.',
      ),
    ),
    'primary key' => array(
      'id',
    ),
    'unique keys' => array(
      'type' => array(
        'name',
      ),
    ),
  );
  db_create_table('rooms_customers', $schema['rooms_customers']);
}

/**
 * Create price field in rooms_booking table.
 */
function rooms_booking_update_7003() {
  $price = array(
    'description' => 'The total price for the booking.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'initial' => 0,
  );
  db_add_field('rooms_bookings', 'price', $price);
  return t('Add field price in Rooms Booking table.');
}

/**
 * Create owner field in rooms_booking table.
 */
function rooms_booking_update_7004() {
  db_add_field('rooms_bookings', 'uid', array(
    'description' => 'The {users}.uid that owns this booking.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  ));
  db_add_index('rooms_bookings', 'uid', array(
    'uid',
  ));

  // Set all the bookings owned by admin user.
  db_update('rooms_bookings')
    ->fields(array(
    'uid' => 1,
  ))
    ->execute();
  return t('New columns added to rooms_units table');
}

/**
 * Update permission names for rooms_booking entity management.
 */
function rooms_booking_update_7005() {

  // Load utility functions.
  module_load_install('rooms');
  $map = array(
    'administer booking types' => 'administer rooms_booking_type entities',
    'administer bookings' => 'bypass rooms_booking entities access',
  );
  $entity_info = entity_get_info('rooms_booking');
  foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
    $map['view any ' . $bundle_name . ' booking'] = 'view any rooms_booking entity of bundle ' . $bundle_name;
    $map['edit any ' . $bundle_name . ' booking'] = 'update any rooms_booking entity of bundle ' . $bundle_name;
  }
  commerce_update_rename_permissions($map);
  return t('Role and custom View permissions updated for order entity management. Access checks in modules and permissions on default Views must be updated manually.');
}

/**
 * Update permission name for administer rooms_booking entities.
 */
function rooms_booking_update_7006() {
  $map = array(
    'administer rooms_booking entities' => 'bypass rooms_booking entities access',
  );

  // Easy part: rename the permissions in {role_permission}.
  foreach ($map as $old_name => $new_name) {
    $roles = db_select('role_permission', 'r')
      ->fields('r', array(
      'rid',
      'permission',
    ))
      ->condition('permission', $old_name)
      ->execute()
      ->fetchAllKeyed();
    foreach (array_keys($roles) as $rid) {
      db_merge('role_permission')
        ->key(array(
        'rid' => $rid,
        'permission' => $new_name,
      ))
        ->fields(array(
        'rid' => $rid,
        'permission' => $new_name,
        'module' => 'rooms_booking',
      ))
        ->execute();
      db_delete('role_permission')
        ->condition('rid', $rid)
        ->condition('permission', $old_name)
        ->execute();
    }
  }

  // Trickier: rename the permission for the in-database Views.
  foreach (views_get_all_views() as $view) {
    if ($view->type == t('Default')) {
      continue;
    }
    $save_view = FALSE;
    foreach ($view->display as $display_name => $display) {
      if (!empty($display->display_options['access']['type']) && $display->display_options['access']['type'] == 'perm') {
        $permission_name = $display->display_options['access']['perm'];
        if (isset($map[$permission_name])) {
          $display->display_options['access']['perm'] = $map[$permission_name];
          $save_view = TRUE;
        }
      }
    }
    if ($save_view) {
      $view
        ->save();
    }
  }
}

/**
 * Give the new permission 'create customer profiles on bookings'
 * to all roles with 'bypass rooms_booking entities access' permission.
 */
function rooms_booking_update_7007() {
  $roles = db_select('role_permission', 'r')
    ->fields('r', array(
    'rid',
    'permission',
  ))
    ->condition('permission', 'bypass rooms_booking entities access')
    ->execute()
    ->fetchAllKeyed();
  foreach (array_keys($roles) as $rid) {
    db_merge('role_permission')
      ->key(array(
      'rid' => $rid,
      'permission' => 'create customer profiles on bookings',
    ))
      ->fields(array(
      'rid' => $rid,
      'permission' => 'create customer profiles on bookings',
      'module' => 'rooms_booking',
    ))
      ->execute();
  }
}

Functions

Namesort descending Description
rooms_booking_create_standard_booking_type Creates the default standard booking type.
rooms_booking_install Implements hook_install().
rooms_booking_schema Implements hook_schema().
rooms_booking_update_7001 Create booking_status field in rooms_booking table.
rooms_booking_update_7002 Create customers table.
rooms_booking_update_7003 Create price field in rooms_booking table.
rooms_booking_update_7004 Create owner field in rooms_booking table.
rooms_booking_update_7005 Update permission names for rooms_booking entity management.
rooms_booking_update_7006 Update permission name for administer rooms_booking entities.
rooms_booking_update_7007 Give the new permission 'create customer profiles on bookings' to all roles with 'bypass rooms_booking entities access' permission.