You are here

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

Install for Rooms Booking Manager module

File

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

/**
 * @file
 * Install for Rooms Booking Manager module
 */

/**
 * Implements hook_install().
 */
function rooms_booking_manager_install() {
  user_role_grant_permissions(1, array(
    'book units',
  ));
  user_role_grant_permissions(2, array(
    'book units',
  ));
  variable_set('rooms_unit_type_selector', array());
}

/**
 * Implements hook_enable().
 *
 * Creates a product that can be referenced from line items.
 */
function rooms_booking_manager_enable() {

  // Reset product types cache to have our rooms_product available.
  commerce_product_types_reset();
  commerce_product_configure_product_type('rooms_product');
  $types = commerce_product_types();
  if (isset($types['rooms_product']) && !empty($types['rooms_product'])) {
    $previous_product_id = variable_get('rooms_booking_manager_booking_product_id', 0);
    if (!($previous_product_id && commerce_product_load($previous_product_id))) {
      $product = commerce_product_new('rooms_product');
      $product->sku = 'ROOMS-BASIC-BOOKING';
      $product->title = t('Rooms Basic Booking');

      // We will change the price later, but for now set the price to 100 (=$1)
      // to give tax module something to work with.
      $product->commerce_price[LANGUAGE_NONE][0]['amount'] = 100;
      $product->commerce_price[LANGUAGE_NONE][0]['currency_code'] = commerce_default_currency();
      commerce_product_save($product);
      variable_set('rooms_booking_manager_booking_product_id', $product->product_id);
    }
  }
  else {
    drupal_set_message(t('There was an error creating the rooms product to handle bookings.'));
  }
}

/**
 * Implements hook_uninstall().
 */
function rooms_booking_manager_uninstall() {
  commerce_product_delete(variable_get('rooms_booking_manager_booking_product_id', 0));
  variable_del('rooms_date_format');
  variable_del('rooms_booking_manager_booking_product_id');
  variable_del('rooms_unit_type_selector');
  module_load_include('inc', 'rooms_booking_manager', 'rooms_booking_manager.variable');
  $vars = rooms_booking_manager_variable_info(array());
  foreach ($vars as $var_name => $var) {
    variable_del($var_name);
  }
  $vars = array(
    'rooms_booking_manager_deposit_enabled',
    'rooms_booking_manager_deposit_type',
    'rooms_booking_manager_deposit_fixed',
    'rooms_booking_manager_deposit_multiply',
  );
  foreach ($vars as $var_name => $var) {
    variable_del($var_name);
  }

  // Delete fields created by this module.
  field_delete_field('rooms_booking_options');
  field_delete_field('rooms_booking_number_people');
  field_delete_field('rooms_booking_options');
  field_delete_field('rooms_booking_reference');

  // Delete line item fields.
  module_load_include('inc', 'rooms_booking_manager', 'includes/rooms_booking_manager.fields');
  $field_data = _rooms_booking_manager_line_item_type_fields();
  foreach ($field_data['fields'] as $field_name => $field_info) {
    field_delete_field($field_name);
  }
  field_purge_batch(20);
}

/**
 * Adds "Booking Number People" field to the rooms_booking_manager_line_item.
 */
function rooms_booking_manager_update_7001() {
  $field = array(
    'field_name' => 'rooms_booking_number_people',
    'label' => t('Booking Number People'),
    'cardinality' => 2,
    'type' => 'number_integer',
    'module' => 'number',
    'active' => 1,
    'locked' => 1,
    'settings' => array(
      'size' => 8,
      'max_length' => 10,
      'text_processing' => 0,
    ),
  );
  field_create_field($field);
  $instance = array(
    'field_name' => 'rooms_booking_number_people',
    'label' => t('Booking Number People'),
    'entity_type' => 'commerce_line_item',
    'bundle' => 'rooms_booking',
    'required' => TRUE,
    'settings' => array(
      'size' => 8,
      'max_length' => 10,
      'text_processing' => 0,
    ),
    'widget' => array(
      'type' => 'text_textfield',
    ),
    'display' => array(
      'default' => array(
        'label' => 'above',
        'module' => 'number',
        'settings' => array(
          'decimal_separator' => '.',
          'prefix_suffix' => TRUE,
          'scale' => 0,
          'thousand_separator' => ' ',
        ),
        'type' => 'number_integer',
        'weight' => 11,
      ),
      'teaser' => array(
        'label' => 'above',
        'settings' => array(),
        'type' => 'hidden',
        'weight' => 0,
      ),
    ),
  );
  field_create_instance($instance);
}

/**
 * Sets price for ROOMS-BASIC-BOOKING to 100 (=$1).
 */
function rooms_booking_manager_update_7002() {
  commerce_product_types_reset();
  commerce_product_configure_product_type('rooms_product');
  $rooms_product = commerce_product_load(variable_get('rooms_booking_manager_booking_product_id', 0));
  if ($rooms_product != NULL) {
    $rooms_product->commerce_price[LANGUAGE_NONE][0]['amount'] = 100;
    $rooms_product->commerce_price[LANGUAGE_NONE][0]['currency_code'] = commerce_default_currency();
    commerce_product_save($rooms_product);
  }
}

/**
 * Adds "Booking Options" field to the rooms_booking_manager_line_item.
 */
function rooms_booking_manager_update_7003() {

  // Clear the field cache so that the rooms_options field type will be
  // available.
  field_cache_clear();
  $field = array(
    'field_name' => 'rooms_booking_options',
    'label' => t('Booking Options'),
    'cardinality' => -1,
    'required' => 0,
    'type' => 'rooms_options',
    'module' => 'rooms',
    'settings' => array(
      'user_register_form' => FALSE,
    ),
    'widget' => array(
      'active' => 0,
      'module' => 'rooms',
      'settings' => array(),
      'type' => 'rooms_options_combined',
      'weight' => '0',
    ),
  );
  field_create_field($field);
  $instance = array(
    'field_name' => 'rooms_booking_options',
    'label' => t('Booking Options'),
    'entity_type' => 'commerce_line_item',
    'bundle' => 'rooms_booking',
    'default_value' => NULL,
    'deleted' => '0',
    'description' => '',
    'display' => array(
      'default' => array(
        'label' => 'above',
        'module' => NULL,
        'settings' => array(),
        'type' => 'rooms_options_default',
        'weight' => 12,
      ),
      'teaser' => array(
        'label' => 'above',
        'settings' => array(),
        'type' => 'hidden',
        'weight' => 0,
      ),
    ),
    'required' => 0,
    'settings' => array(
      'user_register_form' => FALSE,
    ),
    'widget' => array(
      'module' => 'rooms_unit',
      'type' => 'rooms_options_combined',
    ),
  );
  field_create_instance($instance);
}

/**
 * Adds "Booking Reference" field to the rooms_booking_manager_line_item.
 */
function rooms_booking_manager_update_7004() {

  // We need to first ensure that the entityreference module is enabled.
  module_enable(array(
    'entityreference',
  ));
  field_cache_clear();
  $field = array(
    'field_name' => 'rooms_booking_reference',
    'label' => t('Booking Reference'),
    'cardinality' => 1,
    'required' => 0,
    'type' => 'entityreference',
    'module' => 'entityreference',
    'settings' => array(
      'handler' => 'base',
      'handler_settings' => array(
        'behaviors' => array(
          'views-select-list' => array(
            'status' => 0,
          ),
        ),
        'sort' => array(
          'type' => 'none',
        ),
        'target_bundles' => array(),
      ),
      'target_type' => 'rooms_booking',
    ),
    'widget' => array(
      'active' => 1,
      'module' => 'entityreference',
      'settings' => array(
        'match_operator' => 'CONTAINS',
        'path' => '',
        'size' => '60',
      ),
      'type' => 'entityreference_autocomplete',
      'weight' => '9',
    ),
  );
  field_create_field($field);
  $instance = array(
    'field_name' => 'rooms_booking_reference',
    'label' => t('Booking Reference'),
    'entity_type' => 'commerce_line_item',
    'bundle' => 'rooms_booking',
    'default_value' => NULL,
    'display' => array(
      'default' => array(
        'label' => 'above',
        'module' => 'entityreference',
        'settings' => array(
          'link' => FALSE,
        ),
        'type' => 'entityreference_label',
        'weight' => 13,
      ),
    ),
    'required' => 0,
    'settings' => array(
      'user_register_form' => FALSE,
    ),
    'widget' => array(
      'active' => 1,
      'module' => 'entityreference',
      'settings' => array(
        'match_operator' => 'CONTAINS',
        'path' => '',
        'size' => '60',
      ),
      'type' => 'entityreference_autocomplete',
      'weight' => '9',
    ),
  );
  field_create_instance($instance);
}

/**
 * Adds "Booking Children Ages" field to the rooms_booking_manager_line_item.
 */
function rooms_booking_manager_update_7005() {
  $field = array(
    'field_name' => 'rooms_booking_children_ages',
    'label' => t('Booking Children Ages'),
    'cardinality' => -1,
    'type' => 'number_integer',
    'module' => 'number',
    'active' => 1,
    'locked' => 1,
    'settings' => array(
      'size' => 8,
      'max_length' => 10,
      'text_processing' => 0,
    ),
  );
  field_create_field($field);
  $instance = array(
    'field_name' => 'rooms_booking_children_ages',
    'label' => t('Booking Children Ages'),
    'entity_type' => 'commerce_line_item',
    'bundle' => 'rooms_booking',
    'required' => FALSE,
    'settings' => array(
      'size' => 8,
      'max_length' => 10,
      'text_processing' => 0,
    ),
    'widget' => array(
      'type' => 'text_textfield',
    ),
    'display' => array(
      'default' => array(
        'label' => 'above',
        'module' => 'number',
        'settings' => array(
          'decimal_separator' => '.',
          'prefix_suffix' => TRUE,
          'scale' => 0,
          'thousand_separator' => ' ',
        ),
        'type' => 'number_integer',
        'weight' => 13,
      ),
      'teaser' => array(
        'label' => 'above',
        'settings' => array(),
        'type' => 'hidden',
        'weight' => 0,
      ),
    ),
  );
  field_create_instance($instance);
}

/**
 * Renames 'rooms_enquiry_form_confirmation' variable to
 * 'rooms_booking_manager_enquiry_form_confirmation'.
 */
function rooms_booking_manager_update_7006() {
  variable_set_value('rooms_booking_manager_enquiry_form_confirmation', variable_get_value('rooms_enquiry_form_confirmation'));
  variable_delete('rooms_enquiry_form_confirmation');
}

/**
 * Update line item "Booking Dates" value to include time.
 */
function rooms_booking_manager_update_7007() {
  foreach (commerce_line_item_load_multiple(FALSE, array(
    'type' => 'rooms_booking',
  )) as $line_item) {
    if (isset($line_item->rooms_booking_dates[LANGUAGE_NONE][0]['value'])) {
      $value = new DateTime($line_item->rooms_booking_dates[LANGUAGE_NONE][0]['value']);
      $value2 = new DateTime($line_item->rooms_booking_dates[LANGUAGE_NONE][0]['value2']);
      $line_item->rooms_booking_dates[LANGUAGE_NONE][0]['value'] = $value
        ->format('Y-m-d') . 'T00:00:00';
      $line_item->rooms_booking_dates[LANGUAGE_NONE][0]['value2'] = $value2
        ->format('Y-m-d') . 'T00:00:00';
      field_attach_update('commerce_line_item', $line_item);
    }
  }
}

Functions

Namesort descending Description
rooms_booking_manager_enable Implements hook_enable().
rooms_booking_manager_install Implements hook_install().
rooms_booking_manager_uninstall Implements hook_uninstall().
rooms_booking_manager_update_7001 Adds "Booking Number People" field to the rooms_booking_manager_line_item.
rooms_booking_manager_update_7002 Sets price for ROOMS-BASIC-BOOKING to 100 (=$1).
rooms_booking_manager_update_7003 Adds "Booking Options" field to the rooms_booking_manager_line_item.
rooms_booking_manager_update_7004 Adds "Booking Reference" field to the rooms_booking_manager_line_item.
rooms_booking_manager_update_7005 Adds "Booking Children Ages" field to the rooms_booking_manager_line_item.
rooms_booking_manager_update_7006 Renames 'rooms_enquiry_form_confirmation' variable to 'rooms_booking_manager_enquiry_form_confirmation'.
rooms_booking_manager_update_7007 Update line item "Booking Dates" value to include time.