You are here

opening_hours.install in Opening hours 7

Same filename and directory in other branches
  1. 6 opening_hours.install

Database schema, installation and upgrade hooks for the opening hours module.

File

opening_hours.install
View source
<?php

/**
 * @file
 * Database schema, installation and upgrade hooks for the opening hours module.
 */

/**
 * Implements hook_schema().
 */
function opening_hours_schema() {
  $schema = array();
  $schema['opening_hours'] = array(
    'description' => 'A single opening hours instance, ie. an open period on a specific date.',
    'fields' => array(
      'instance_id' => array(
        'description' => 'The primary identifier for an opening hours instance.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'nid' => array(
        'description' => 'The {node}.nid for the content this opening hours instance belongs to.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'date' => array(
        'description' => 'The date this opening hours instance is valid for.',
        'type' => 'text',
        'pgsql_type' => 'date',
        'mysql_type' => 'date',
        'not null' => TRUE,
      ),
      'start_time' => array(
        'description' => 'The time this opening hours instance starts.',
        'type' => 'text',
        'pgsql_type' => 'time',
        'mysql_type' => 'time',
        'not null' => TRUE,
      ),
      'end_time' => array(
        'description' => 'The time this opening hours instance ends.',
        'type' => 'text',
        'pgsql_type' => 'time',
        'mysql_type' => 'time',
        'not null' => TRUE,
      ),
      'category_tid' => array(
        'description' => 'The {taxonomy_term_data}.tid for the category selected for this instance, if any.',
        'type' => 'int',
        'unsigned' => TRUE,
      ),
      'notice' => array(
        'description' => 'An optional note that signifies something special about this opening hours instance',
        'type' => 'varchar',
        'length' => 255,
      ),
      'repeat_rule' => array(
        'description' => 'What repeat rule (if any) was used for this opening hours instance.',
        'type' => 'varchar',
        'length' => 255,
      ),
      'repeat_end_date' => array(
        'description' => 'The date the repeating rule ends.',
        'type' => 'text',
        'pgsql_type' => 'date',
        'mysql_type' => 'date',
      ),
      'original_instance_id' => array(
        'description' => 'The {opening_hours}.instance_id for the original instance, which this instance is a repeat of.',
        'type' => 'int',
        'unsigned' => TRUE,
      ),
      'customised' => array(
        'description' => 'Boolean indicating whether this instance has been modified, so it differs from its original instance. Not applicable (NULL) for instances that have no original instance.',
        'type' => 'int',
        'size' => 'tiny',
      ),
    ),
    'primary key' => array(
      'instance_id',
    ),
    'indexes' => array(
      'nid_date_category_tid' => array(
        'nid',
        'date',
        'category_tid',
      ),
      'original_custom' => array(
        'original_instance_id',
        'customised',
      ),
    ),
  );
  return $schema;
}

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

  // By default, we want to use the admin theme for our interface. This
  // requires admin_theme module to work properly in Drupal 6.
  variable_set('admin_theme_opening_hours_opening_hours', 1);
}

/**
 * Implements hook_uninstall().
 */
function opening_hours_uninstall() {

  // Delete any variables we have set.
  variable_del('admin_theme_opening_hours_opening_hours');
  db_query("DELETE FROM {variable} WHERE name LIKE 'opening_hours_%'");
}

/**
 * Implements hook_update_N().
 */
function opening_hours_update_7000(&$sandbox) {

  // Remove the old index, since we create a new one below.
  // The new one also includes the category tid.
  db_drop_index('opening_hours', 'nid_date');
  db_add_field('opening_hours', 'category_tid', array(
    'description' => 'The {taxonomy_term_data}.tid for the category selected for this instance, if any.',
    'type' => 'int',
    'unsigned' => TRUE,
  ), array(
    'indexes' => array(
      'nid_date_category_tid' => array(
        'nid',
        'date',
        'category_tid',
      ),
    ),
  ));
}

/**
 * Implements hook_update_N().
 *
 * Remove unique key, so that “duplicate” opening hours are allowed.
 *
 * Enforcing uniqueness doesn't make as much sense after we got 
 * categories.
 */
function opening_hours_update_7001(&$sandbox) {
  db_drop_unique_key('opening_hours', 'nid_date_time');
}

/**
 * Implements hook_update_N().
 *
 * Setup field settings for opening hours extra field.
 *
 * @see _opening_hours_openings_hours_field_setup()
 */
function opening_hours_update_7002() {
  _opening_hours_openings_hours_field_setup();
}

/**
 * Setup field settings for opening hours extra field.
 *
 * When we add the extra field we don't want it enabled on existing node types
 * as it may conflict with existing implementations.
 */
function _opening_hours_openings_hours_field_setup() {
  $field_machine_name = 'opening_hours_week';
  $info = entity_get_info('node');
  $view_modes = array_merge(array(
    'default',
  ), array_keys($info['view modes']));
  $node_types = node_type_get_types();
  foreach ($node_types as $node_type) {
    if (variable_get('opening_hours_enabled_' . $node_type->type, FALSE)) {
      $settings = field_bundle_settings('node', $node_type->type);
      foreach ($view_modes as $view_mode) {
        $settings['extra_fields']['display'][$field_machine_name][$view_mode] = array(
          'visible' => FALSE,
          'weight' => 0,
          'label' => 'above',
        );
      }
      field_bundle_settings('node', $node_type->type, $settings);
    }
  }
}