You are here

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

Install, update and uninstall functions for the rooms module.

File

rooms.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the rooms module.
 */

/**
 * Implements hook_field_schema().
 */
function rooms_field_schema($field) {
  if ($field['type'] == 'rooms_options') {
    return array(
      'columns' => array(
        'name' => array(
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
        ),
        'quantity' => array(
          'type' => 'int',
          'not null' => FALSE,
        ),
        'operation' => array(
          'type' => 'varchar',
          'length' => 255,
          'not null' => FALSE,
        ),
        'value' => array(
          'type' => 'float',
          'not null' => FALSE,
        ),
        'type' => array(
          'type' => 'varchar',
          'length' => 255,
          'not null' => FALSE,
        ),
      ),
    );
  }
}

/**
 * Implements hook_requirements().
 */
function rooms_requirements($phase) {
  $requirements = array();
  $t = get_t();
  switch ($phase) {
    case 'runtime':
      if (!rooms_library_loaded('fullcalendar', 'minified')) {
        $description = array();
        $fullcalendar_library = libraries_detect('fullcalendar');
        if (!$fullcalendar_library['installed']) {
          $description[] = $t('Could not load the FullCalendar Library');
        }
        $requirements['fullcalendar'] = array(
          'title' => $t('FullCalendar'),
          'description' => implode('<br />', $description),
          'value' => 'FullCalendar Missing',
          'severity' => REQUIREMENT_ERROR,
        );
      }
      else {
        $requirements['fullcalendar'] = array(
          'title' => $t('FullCalendar'),
          'description' => $t('The FullCalendar Library is installed'),
          'value' => 'FullCalendar Installed',
          'severity' => REQUIREMENT_OK,
        );
      }
  }
  return $requirements;
}

/**
 * Utility function: rename a set of permissions.
 */
function rooms_update_rename_permissions($map) {

  // Easy part: rename the permissions in {role_permission}.
  foreach ($map as $old_name => $new_name) {
    db_update('role_permission')
      ->fields(array(
      'permission' => $new_name,
    ))
      ->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();
    }
  }
}

/**
 * Converts the booking option price value columns from integer to float.
 */
function rooms_update_7001() {
  $spec = array(
    'type' => 'float',
    'not null' => FALSE,
  );
  $fields = array_filter(field_info_fields(), function ($item) {
    return $item['type'] == 'rooms_options';
  });
  foreach ($fields as $field_definition) {
    $data_table = 'field_data_' . $field_definition['field_name'];
    $revision_table = 'field_revision_' . $field_definition['field_name'];
    $field = $field_definition['field_name'] . '_value';
    db_change_field($data_table, $field, $field, $spec);
    db_change_field($revision_table, $field, $field, $spec);
  }
  return t('Field structure updated.');
}

/**
 * Add the booking option Type column.
 */
function rooms_update_7002() {
  $fields = field_info_fields();
  foreach ($fields as $field) {
    if ($field['type'] == 'rooms_options') {
      $revision_table = key($field['storage']['details']['sql']['FIELD_LOAD_REVISION']);
      $field_name = $field['storage']['details']['sql']['FIELD_LOAD_REVISION'][$revision_table]['type'];
      if (!db_field_exists($revision_table, $field_name)) {
        $field_spec = $field['columns']['type'];
        $field_spec['initial'] = 'optional';
        db_add_field($revision_table, $field_name, $field_spec);
      }
      $current_table = key($field['storage']['details']['sql']['FIELD_LOAD_CURRENT']);
      $field_name = $field['storage']['details']['sql']['FIELD_LOAD_CURRENT'][$current_table]['type'];
      if (!db_field_exists($current_table, $field_name)) {
        $field_spec = $field['columns']['type'];
        $field_spec['initial'] = 'optional';
        db_add_field($current_table, $field_name, $field_spec);
      }
    }
  }
}

Functions

Namesort descending Description
rooms_field_schema Implements hook_field_schema().
rooms_requirements Implements hook_requirements().
rooms_update_7001 Converts the booking option price value columns from integer to float.
rooms_update_7002 Add the booking option Type column.
rooms_update_rename_permissions Utility function: rename a set of permissions.