You are here

rooms_unit.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_unit/rooms_unit.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_unit_install() {
  rooms_description_content_type_install();
  rooms_unit_install_create_options_field();
}

/**
 * Implements hook_uninstall().
 */
function rooms_unit_uninstall() {
  rooms_description_content_type_uninstall();

  // Delete options field.
  field_delete_field('rooms_booking_unit_options');

  // Delete other rooms fields.
  foreach (_rooms_description_installed_fields() as $field) {
    field_delete_field($field['field_name']);
  }
  $limit = variable_get('field_purge_batch_size', 10);
  field_purge_batch($limit);
}

/**
 * Implements hook_schema().
 */
function rooms_unit_schema() {
  $schema = array();
  $schema['rooms_units'] = array(
    'description' => 'The base table for Rooms.',
    'fields' => array(
      'unit_id' => array(
        'description' => 'Primary Key: Identifier for a Bookable Unit.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'type' => array(
        'description' => 'The {unit_type}.type of this Bookable Unit.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'language' => array(
        'description' => 'The language of the Unit.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'name' => array(
        'description' => 'The name of the Unit - 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,
      ),
      'base_price' => array(
        'description' => 'The default cost per night for the unit',
        'type' => 'numeric',
        'not null' => TRUE,
        'precision' => 10,
        'scale' => 2,
        'default' => 0,
      ),
      'weekly_discount' => array(
        'description' => 'The default weekly discount for the unit',
        'type' => 'numeric',
        'not null' => TRUE,
        'precision' => 10,
        'scale' => 2,
        'default' => 0,
      ),
      'monthly_discount' => array(
        'description' => 'The default monthly discount for the unit',
        'type' => 'numeric',
        'not null' => TRUE,
        'precision' => 10,
        'scale' => 2,
        'default' => 0,
      ),
      'default_state' => array(
        'description' => 'The default booking state for the room',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'bookable' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
        'size' => 'tiny',
        'description' => 'Whether the unit is available for booking or not.',
      ),
      'min_sleeps' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
        'size' => 'tiny',
        'description' => 'How many people can sleep in a unit (MIN)',
      ),
      'max_sleeps' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
        'size' => 'tiny',
        'description' => 'How many people can sleep in a unit (MAX)',
      ),
      'min_children' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'How many children can sleep in a unit (MIN)',
      ),
      'max_children' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'How many children can sleep in a unit (MAX)',
      ),
      'status' => array(
        'description' => 'Boolean indicating whether the booking unit is published (visible to non-administrators).',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'uid' => array(
        'description' => 'The {users}.uid that created this booking.',
        '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(
      'unit_id',
    ),
    'indexes' => array(
      'type' => array(
        'type',
      ),
    ),
  );
  $schema['rooms_unit_type'] = array(
    'description' => 'Stores information about defined unit types.',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique unit type identifier.',
      ),
      'type' => array(
        'description' => 'The machine-readable name of this unit type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'label' => array(
        'description' => 'The human-readable name of this unit 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 room 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',
      ),
    ),
  );
  return $schema;
}

/**
 * Creates content type for rooms unit type description.
 */
function rooms_description_content_type_install() {
  $t = get_t();

  // We define the node type as an associative array.
  if (!in_array('unit_description', node_type_get_names())) {
    $unit_description = array(
      'type' => 'unit_description',
      'name' => $t('Unit Description'),
      'base' => 'node_content',
      'description' => $t('Bookable Unit Description.'),
      'title_label' => $t('Title'),
      'custom' => TRUE,
    );
  }
  $content_type = node_type_set_defaults($unit_description);
  foreach (_rooms_description_installed_fields() as $field) {
    field_create_field($field);
  }
  foreach (_rooms_description_installed_instances() as $instance) {
    $instance['entity_type'] = 'node';
    $instance['bundle'] = $unit_description['type'];
    field_create_instance($instance);
  }
  node_type_save($content_type);
}

/**
 * Creates rooms_booking_unit_options field for bookable unit type entities.
 */
function rooms_unit_install_create_options_field() {
  if (field_read_field('rooms_booking_unit_options') === FALSE) {

    // Create field 'rooms_booking_unit_options'.
    $field = array(
      'field_name' => 'rooms_booking_unit_options',
      'type' => 'rooms_options',
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    );
    field_create_field($field);
  }
  cache_clear_all();
  field_cache_clear();

  // Attach field 'rooms_booking_unit_options' to instances of rooms_unit_type.
  if (field_info_instance('rooms_unit_type', 'rooms_booking_unit_options', 'rooms_unit_type') === NULL) {
    $instance = array(
      'field_name' => 'rooms_booking_unit_options',
      'entity_type' => 'rooms_unit_type',
      'label' => 'Options',
      'bundle' => 'rooms_unit_type',
      'required' => FALSE,
      'widget' => array(
        'type' => 'rooms_options',
      ),
    );
    field_create_instance($instance);
  }
}

/**
 * Helps to create field types necessary for this module.
 *
 * @return array
 *   Field types to create.
 */
function _rooms_description_installed_fields() {
  $t = get_t();
  return array(
    'unit_description_description' => array(
      'field_name' => 'unit_description_description',
      'type' => 'text',
      'label' => $t('Unit type description'),
    ),
    'unit_description_image' => array(
      'field_name' => 'unit_description_image',
      'type' => 'image',
      'cardinality' => 1,
      'label' => 'Image',
    ),
  );
}

/**
 * Helps to create field instances necessary for this module.
 *
 * @return array
 *   Instances to create.
 */
function _rooms_description_installed_instances() {
  $t = get_t();
  return array(
    'unit_description_description' => array(
      'field_name' => 'unit_description_description',
      'label' => $t('A description of this unit type.'),
      'widget' => array(
        'type' => 'text_textarea',
      ),
      'display' => array(
        'unit_description_list' => array(
          'label' => 'hidden',
          'type' => 'unit_description_description',
        ),
      ),
    ),
    'unit_description_image' => array(
      'field_name' => 'unit_description_image',
      'label' => $t('Image'),
      'required' => FALSE,
      'widget' => array(
        'type' => 'image_image',
        'weight' => 2.1,
      ),
      'display' => array(
        'unit_description_list' => array(
          'label' => 'hidden',
          'type' => 'image_link_content__thumbnail',
        ),
      ),
    ),
  );
}

/*
 * Removes content type Unit Description.
 */
function rooms_description_content_type_uninstall() {
  $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  $result = db_query($sql, array(
    ':type' => 'unit_description',
  ));
  $nids = array();
  foreach ($result as $row) {
    $nids[] = $row->nid;
  }
  node_delete_multiple($nids);
  foreach (array_keys(_rooms_description_installed_fields()) as $field) {
    field_delete_field($field);
  }
  $instances = field_info_instances('node', 'unit_description');
  foreach ($instances as $instance_name => $instance) {
    field_delete_instance($instance);
  }
  node_type_delete('unit_description');
  field_purge_batch(1000);
}

/**
 * Changes base_price field to numeric storage.
 */
function rooms_unit_update_7001() {
  $base_price_field_spec = array(
    'description' => 'The default cost per night for the unit',
    'type' => 'numeric',
    'not null' => TRUE,
    'precision' => 10,
    'scale' => 2,
    'default' => 0,
  );
  db_change_field('rooms_units', 'base_price', 'base_price', $base_price_field_spec);
  return t('Schema for the rooms_unit table has been updated.');
}

/**
 * Creates rooms_description content type.
 */
function rooms_unit_update_7002() {
  rooms_description_content_type_install();
}

/**
 * Adds fields for children.
 */
function rooms_unit_update_7003() {
  $cot_surcharge = array(
    'description' => 'The default cot_surcharge',
    'type' => 'numeric',
    'not null' => TRUE,
    'precision' => 10,
    'scale' => 2,
    'default' => 0,
  );
  db_add_field('rooms_units', 'cot_surcharge', $cot_surcharge);
  $children = array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'size' => 'tiny',
    'description' => 'How many children can sleep in a unit',
  );
  db_add_field('rooms_units', 'children', $children);
}

/**
 * Removes field cot_surcharge.
 */
function rooms_unit_update_7004() {
  db_drop_field('rooms_units', 'cot_surcharge');
}

/**
 * Adds max and min sleeps, add max and min children.
 */
function rooms_unit_update_7005() {
  $max_sleeps = array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 1,
    'size' => 'tiny',
    'description' => 'How many people can sleep in a unit (MAX)',
  );
  db_change_field('rooms_units', 'sleeps', 'max_sleeps', $max_sleeps);
  $max_children = array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'size' => 'tiny',
    'description' => 'How many children can sleep in a unit (MAX)',
  );
  db_change_field('rooms_units', 'children', 'max_children', $max_children);
  $min_sleeps = array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 1,
    'size' => 'tiny',
    'description' => 'How many people can sleep in a unit (MIN)',
  );
  db_add_field('rooms_units', 'min_sleeps', $min_sleeps);
  $min_children = array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'size' => 'tiny',
    'description' => 'How many children can sleep in a unit (MIN)',
  );
  db_add_field('rooms_units', 'min_children', $min_children);
  db_update('rooms_units')
    ->expression('min_sleeps', 'max_sleeps')
    ->execute();
  db_update('rooms_units')
    ->expression('min_children', 'max_children')
    ->execute();
}

/**
 * Adds max and min sleeps for Bookable Unit Types.
 */
function rooms_unit_update_7006() {
  foreach (rooms_unit_types_ids() as $type => $label) {
    $unit_type = rooms_unit_type_load($type);
    $unit_type->data = array(
      'base_price' => $unit_type->data['base_price'],
      'min_sleeps' => $unit_type->data['sleeps'],
      'max_sleeps' => $unit_type->data['sleeps'],
      'min_children' => $unit_type->data['children_sleeps'],
      'max_children' => $unit_type->data['children_sleeps'],
      'rooms_description_source' => $unit_type->data['$unit_type->data'],
    );
    rooms_unit_type_save($unit_type);
  }
}

/**
 * Renames 'field_options' to 'rooms_booking_unit_options' and attach this field
 * to any bundle of rooms_unit_type.
 */
function rooms_unit_update_7007() {
  if (field_info_field('field_options') !== NULL) {

    // Rename old field 'field_options' to 'rooms_booking_unit_options'
    db_query("UPDATE field_config SET field_name = 'rooms_booking_unit_options' WHERE field_name = 'field_options'");
    db_query("UPDATE field_config_instance SET field_name = 'rooms_booking_unit_options' WHERE field_name = 'field_options'");
    db_query("RENAME TABLE field_data_field_options TO field_data_rooms_booking_unit_options");
    db_query("ALTER TABLE field_data_rooms_booking_unit_options CHANGE field_options_name rooms_booking_unit_options_name VARCHAR(255) NOT NULL");
    db_query("ALTER TABLE field_data_rooms_booking_unit_options CHANGE field_options_quantity rooms_booking_unit_options_quantity INT(11) DEFAULT NULL");
    db_query("ALTER TABLE field_data_rooms_booking_unit_options CHANGE field_options_operation rooms_booking_unit_options_operation varchar(255) DEFAULT NULL");
    db_query("ALTER TABLE field_data_rooms_booking_unit_options CHANGE field_options_value rooms_booking_unit_options_value INT(11) DEFAULT NULL");
    db_query("RENAME TABLE field_revision_field_options TO field_revision_rooms_booking_unit_options");
    db_query("ALTER TABLE field_revision_rooms_booking_unit_options CHANGE field_options_name rooms_booking_unit_options_name VARCHAR(255) NOT NULL");
    db_query("ALTER TABLE field_revision_rooms_booking_unit_options CHANGE field_options_quantity rooms_booking_unit_options_quantity INT(11) DEFAULT NULL");
    db_query("ALTER TABLE field_revision_rooms_booking_unit_options CHANGE field_options_operation rooms_booking_unit_options_operation varchar(255) DEFAULT NULL");
    db_query("ALTER TABLE field_revision_rooms_booking_unit_options CHANGE field_options_value rooms_booking_unit_options_value INT(11) DEFAULT NULL");
  }
  else {

    // Create field 'rooms_booking_unit_options'
    $field = array(
      'field_name' => 'rooms_booking_unit_options',
      'type' => 'rooms_options',
      'cardinality' => -1,
    );
    field_create_field($field);
  }
  cache_clear_all();
  field_cache_clear();
  foreach (rooms_unit_types_ids() as $type => $value) {

    // Attach field 'rooms_booking_unit_options' to any instance of rooms_unit.
    if (field_info_instance('rooms_unit', 'rooms_booking_unit_options', $type) === NULL) {
      $instance = array(
        'field_name' => 'rooms_booking_unit_options',
        'entity_type' => 'rooms_unit',
        'label' => 'Options',
        'bundle' => $type,
        'required' => FALSE,
        'widget' => array(
          'type' => 'rooms_options',
        ),
      );
      field_create_instance($instance);
    }
  }
}

/**
 * Updates all data saved in Rooms Unit Type to change max/min_children_sleeps
 * to max/min_children.
 */
function rooms_unit_update_7008() {
  foreach (rooms_unit_types_ids() as $type => $label) {
    $unit_type = rooms_unit_type_load($type);
    $unit_type->data = array(
      'base_price' => $unit_type->data['base_price'],
      'min_sleeps' => $unit_type->data['min_sleeps'],
      'max_sleeps' => $unit_type->data['max_sleeps'],
      // Rename min/max_children_sleeps to  min/max_children.
      'min_children' => isset($unit_type->data['min_children_sleeps']) ? $unit_type->data['min_children_sleeps'] : $unit_type->data['min_children'],
      'max_children' => isset($unit_type->data['max_children_sleeps']) ? $unit_type->data['max_children_sleeps'] : $unit_type->data['max_children'],
      'rooms_description_source' => $unit_type->data['$unit_type->data'],
    );
    rooms_unit_type_save($unit_type);
  }
}

/**
 * Adds status and uid properties to Bookable units.
 */
function rooms_unit_update_7009() {
  db_add_field('rooms_units', 'status', array(
    'description' => 'Boolean indicating whether the booking unit is published (visible to non-administrators).',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 1,
  ));
  db_add_field('rooms_units', 'uid', array(
    'description' => 'The {users}.uid that created this booking.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  ));

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

/**
 * Update permission names for rooms_unit entity management.
 */
function rooms_unit_update_7010() {

  // Load utility functions.
  module_load_install('rooms');
  $map = array(
    'administer bookable unit types' => 'administer rooms_unit_type entities',
    'administer bookable units' => 'bypass rooms_unit entities access',
  );
  $entity_info = entity_get_info('rooms_booking');
  foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
    $map['view any ' . $bundle_name . ' bookable unit'] = 'view any rooms_unit entity of bundle ' . $bundle_name;
    $map['edit any ' . $bundle_name . ' bookable unit'] = 'update any rooms_unit 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.');
}

/**
 * Adds options field to Rooms unit type entity.
 */
function rooms_unit_update_7011() {
  rooms_unit_install_create_options_field();
}

/**
 * Adds weekly_price and monthly_price to Bookable units.
 */
function rooms_unit_update_7012() {
  db_add_field('rooms_units', 'weekly_price', array(
    'description' => 'The default cost per week for the unit',
    'type' => 'numeric',
    'not null' => TRUE,
    'precision' => 10,
    'scale' => 2,
    'default' => 0,
  ));
  db_add_field('rooms_units', 'monthly_price', array(
    'description' => 'The default cost per month for the unit',
    'type' => 'numeric',
    'not null' => TRUE,
    'precision' => 10,
    'scale' => 2,
    'default' => 0,
  ));
}

/**
 * Update permission name for administer rooms_unit entities.
 */
function rooms_unit_update_7013() {
  $map = array(
    'administer rooms_unit entities' => 'bypass rooms_unit 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_unit',
      ))
        ->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();
    }
  }
}

/**
 * Rename columns 'weekly_price' in 'weekly_discount'
 * and 'monthly_price' in 'monthly_discount'.
 */
function rooms_unit_update_7014() {
  db_change_field('rooms_units', 'weekly_price', 'weekly_discount', array(
    'description' => 'The default weekly discount for the unit',
    'type' => 'numeric',
    'not null' => TRUE,
    'precision' => 10,
    'scale' => 2,
    'default' => 0,
  ));
  db_change_field('rooms_units', 'monthly_price', 'monthly_discount', array(
    'description' => 'The default monthly discount for the unit',
    'type' => 'numeric',
    'not null' => TRUE,
    'precision' => 10,
    'scale' => 2,
    'default' => 0,
  ));
}

Functions

Namesort descending Description
rooms_description_content_type_install Creates content type for rooms unit type description.
rooms_description_content_type_uninstall
rooms_unit_install Implements hook_install().
rooms_unit_install_create_options_field Creates rooms_booking_unit_options field for bookable unit type entities.
rooms_unit_schema Implements hook_schema().
rooms_unit_uninstall Implements hook_uninstall().
rooms_unit_update_7001 Changes base_price field to numeric storage.
rooms_unit_update_7002 Creates rooms_description content type.
rooms_unit_update_7003 Adds fields for children.
rooms_unit_update_7004 Removes field cot_surcharge.
rooms_unit_update_7005 Adds max and min sleeps, add max and min children.
rooms_unit_update_7006 Adds max and min sleeps for Bookable Unit Types.
rooms_unit_update_7007 Renames 'field_options' to 'rooms_booking_unit_options' and attach this field to any bundle of rooms_unit_type.
rooms_unit_update_7008 Updates all data saved in Rooms Unit Type to change max/min_children_sleeps to max/min_children.
rooms_unit_update_7009 Adds status and uid properties to Bookable units.
rooms_unit_update_7010 Update permission names for rooms_unit entity management.
rooms_unit_update_7011 Adds options field to Rooms unit type entity.
rooms_unit_update_7012 Adds weekly_price and monthly_price to Bookable units.
rooms_unit_update_7013 Update permission name for administer rooms_unit entities.
rooms_unit_update_7014 Rename columns 'weekly_price' in 'weekly_discount' and 'monthly_price' in 'monthly_discount'.
_rooms_description_installed_fields Helps to create field types necessary for this module.
_rooms_description_installed_instances Helps to create field instances necessary for this module.