You are here

availability_calendar.install in Availability Calendars 7.5

Same filename and directory in other branches
  1. 7.3 availability_calendar.install
  2. 7.4 availability_calendar.install

File

availability_calendar.install
View source
<?php

/**
 * Install, update and uninstall functions for the Availability Calendar module.
 */

/**
 * Implements hook_schema()
 * @link http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_schema/7
 */
function availability_calendar_schema() {
  $schema = array();

  // Singular table names (though [#140860] isn't clear about the conclusion).
  $schema['availability_calendar_calendar'] = array(
    'description' => 'Stores availability calendars.',
    'fields' => array(
      'cid' => array(
        'description' => 'The primary identifier for an availability calendar.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the calendar was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the calendar was most recently saved.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'cid',
    ),
  );
  $schema['availability_calendar_availability'] = array(
    'description' => 'Stores availability per availability calendar per day.',
    'fields' => array(
      'cid' => array(
        'description' => 'The primary identifier for an availability calendar.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      // [#747036]: using date instead of separate fields allows for better querying.
      // [#1083198]: Mysql, pgsql, mssql, and Oracle support the DATE type, but
      //   this is not a type supported by Drupal, so specify per supported
      //   database. Sqlite uses TEXT as type but has a lot of date functions.
      // 'yyyy-mm-dd' (iso 8601) is accepted by mysql, pgsql, mssql, and sqlite
      //   to specify DATE's. For Oracle I could not find this information.
      // The 'between' operator is inclusive on both sides on all databases:
      //   date between from and to  <=>  from <= date and date <= to.
      'date' => array(
        'description' => 'Date of availability state.',
        'mysql_type' => 'DATE',
        'pgsql_type' => 'DATE',
        'sqlsrv_type' => 'DATE',
        'oracle_type' => 'DATE',
        'sqlite_type' => 'TEXT',
        'type' => 'char',
        'length' => '10',
        'not null' => TRUE,
      ),
      'sid' => array(
        'description' => 'The availability state.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'cid',
      'date',
    ),
    'indexes' => array(
      'availability_calendar_availability_date' => array(
        'date',
        'sid',
      ),
    ),
    'foreign keys' => array(
      'availability_calendar_calendar' => array(
        'table' => 'availability_calendar_calendar',
        'columns' => array(
          'cid' => 'cid',
        ),
      ),
      'availability_calendar_state' => array(
        'table' => 'availability_calendar_state',
        'columns' => array(
          'sid' => 'sid',
        ),
      ),
    ),
  );
  $schema['availability_calendar_state'] = array(
    'description' => 'Stores defined availability calendar states.',
    'fields' => array(
      'sid' => array(
        'description' => 'The identifier for a state.',
        'type' => 'serial',
        'size' => 'tiny',
        'not null' => TRUE,
      ),
      'css_class' => array(
        'description' => 'The CSS class used for this state.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ),
      'label' => array(
        'description' => 'The label for this state.',
        'type' => 'varchar',
        'length' => 64,
      ),
      'weight' => array(
        'description' => 'The weight of this state.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'is_available' => array(
        'description' => 'Boolean indicating whether this state is to be treated as available.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'sid',
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 * @link http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_install/7
 */
function availability_calendar_install() {
  $t = get_t();

  // Fill schema: add a default set of states to the database.
  $states = array(
    array(
      'sid' => 1,
      'css_class' => 'cal-nc',
      'label' => 'Not communicated',
      'weight' => 1,
      'is_available' => 0,
    ),
    array(
      'sid' => 2,
      'css_class' => 'cal-av',
      'label' => 'Available',
      'weight' => 2,
      'is_available' => 1,
    ),
    array(
      'sid' => 3,
      'css_class' => 'cal-na',
      'label' => 'Fully booked',
      'weight' => 3,
      'is_available' => 0,
    ),
    array(
      'sid' => 4,
      'css_class' => 'cal-opt',
      'label' => 'Provisionally booked',
      'weight' => 4,
      'is_available' => 0,
    ),
  );

  // Keep localize.drupal.org satisfied.
  if (FALSE) {
    t('Not Communicated');
    t('Available');
    t('Fully booked');
    t('Provisionally booked');
  }
  foreach ($states as $state) {
    db_insert('availability_calendar_state')
      ->fields($state)
      ->execute();
  }

  // Define a set of default style settings.
  variable_set('availability_calendar_styles_generate', 1);
  $styles = array(
    'table' => array(
      'font-size' => 'smaller',
      'color' => '#000000',
      'background-color' => '',
      'border-width' => '1px',
      'border-color' => '#000000',
    ),
    'caption' => array(
      'font-weight' => 'bold',
      'font-style' => 'inherit',
      'font-size' => 'smaller',
    ),
    'header' => array(
      'height' => '',
      'font-weight' => 'bold',
      'font-style' => 'inherit',
      'font-size' => 'inherit',
      'text-align' => 'center',
    ),
    'week_notes' => array(
      'width' => '90px',
    ),
    'days' => array(
      'width' => '28px',
      'height' => '28px',
      'text-align' => 'center',
      'vertical-align' => 'middle',
    ),
    'states' => array(
      'split-day' => '/',
    ),
  );

  // Fill default states
  $styles['states']['cal-nc'] = '#909090';
  $styles['states']['cal-av'] = '#90ee90';
  $styles['states']['cal-na'] = '#ffb6c1';
  $styles['states']['cal-opt'] = '#ffffe0';
  variable_set('availability_calendar_styles', $styles);
  module_load_include('inc', 'availability_calendar', 'availability_calendar.styles');
  availability_calendar_styles_generate();
  $link = l($t('Availability Calendar') . ' ' . $t('Settings'), 'admin/config/content/availability-calendar/settings');
  drupal_set_message($t("Please visit the '!link' page to define the set of availability states.", array(
    '!link' => $link,
  )), 'warning');
  $link = l($t('Availability Calendar') . ' ' . $t('Styling'), 'admin/config/content/availability-calendar/styling');
  drupal_set_message($t("Please visit the '!link' page to generate your own custom CSS file for Availability Calendar.", array(
    '!link' => $link,
  )), 'warning');
  $link1 = l($t('Date and time'), 'admin/config/regional/date-time');
  $link2 = l($t('Date and time') . ' - ' . $t('Localize'), 'admin/config/regional/date-time/locale');
  drupal_set_message($t("Please visit the '!link1' and !link2 pages to define date formats for Availability Calendar.", array(
    '!link1' => $link1,
    '!link2' => $link2,
  )), 'warning');
}

/**
 * Implements hook_field_schema().
 * @link http://api.drupal.org/api/drupal/modules--field--field.api.php/function/hook_field_schema/7
 */
function availability_calendar_field_schema($field) {
  if ($field['type'] == 'availability_calendar') {
    return array(
      'columns' => array(
        'enabled' => array(
          'type' => 'int',
          'size' => 'tiny',
          'default' => 1,
          'not null' => TRUE,
        ),
        'name' => array(
          'type' => 'varchar',
          'length' => 255,
          'default' => '',
          'not null' => TRUE,
        ),
        'cid' => array(
          'type' => 'int',
          'not null' => TRUE,
        ),
      ),
      'indexes' => array(
        // Can be used to find out what fields refer to a given calendar.
        'cid' => array(
          'cid',
        ),
      ),
      'foreign keys' => array(
        'cid' => array(
          'table' => 'availability_calendar_calendar',
          'columns' => array(
            'cid' => 'cid',
          ),
        ),
      ),
    );
  }
  return array();
}

/**
 * Implements hook_uninstall().
 * @link http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_uninstall/7
 */
function availability_calendar_uninstall() {
  availability_calendar_variable_del_multiple('availability_calendar_');
  file_unmanaged_delete_recursive('public://availability_calendar');
}

/**
 * Utility function that is an altered version of variable_del, it will delete
 * a set of variables set by a module.
 *
 * @param string $name
 *   The variables' namespace to delete.
 */
function availability_calendar_variable_del_multiple($name) {
  db_delete('variable')
    ->condition('name', addcslashes($name, '%_') . '%', 'LIKE')
    ->execute();
  cache_clear_all('variables', 'cache');
}

/**
 * Regenerate custom styles file.
 */
function availability_calendar_update_7301(&$sandbox) {
  module_load_include('inc', 'availability_calendar', 'availability_calendar.styles');
  availability_calendar_styles_generate();
}

/**
 * Regenerate custom styles file and display warning.
 */
function availability_calendar_update_7302(&$sandbox) {
  $t = get_t();
  module_load_include('inc', 'availability_calendar', 'availability_calendar.styles');
  availability_calendar_styles_generate();
  $link = l('CHANGELOG.txt', drupal_get_path('module', 'availability_calendar') . "/CHANGELOG.txt", array(
    'language' => LANGUAGE_NONE,
  ));
  drupal_set_message($t("Availability Calendar: please read the 'Incompatibilities with 7.x-3.x' section in '!link' for a list of actions (like clear caches, set date formats) you have to perform right now to keep this module working.", array(
    '!link' => $link,
  )), 'warning');
}

/**
 * Clear theme cache.
 */
function availability_calendar_update_7402(&$sandbox) {
  system_rebuild_theme_data();
  drupal_theme_rebuild();
}