You are here

availability_calendars.install in Availability Calendars 6

Install, update and uninstall functions for the Availability Calendars module.

@author Dan Karran (geodaniel) <dan at karran dot net> @author Nicholas Alipaz (nicholas.alipaz) @author Erwin Derksen (http://drupal.org/user/750928)

File

availability_calendars.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the Availability Calendars module.
 *
 * @author Dan Karran (geodaniel) <dan at karran dot net>
 * @author Nicholas Alipaz (nicholas.alipaz)
 * @author Erwin Derksen (http://drupal.org/user/750928)
 */

/**
 * Implements hook_schema().
 */
function availability_calendars_schema() {
  $schema = array();
  $schema['availability_calendars_day'] = array(
    'description' => 'The table for calendar days.',
    'fields' => array(
      'nid' => array(
        'description' => 'The primary identifier for a node.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'year' => array(
        'description' => 'The number of the year.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'month' => array(
        'description' => 'The number of the month.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'day' => array(
        'description' => 'The number of the day.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'status' => array(
        'description' => 'The status.',
        'type' => 'text',
        'size' => 'medium',
      ),
      // [#747036]: this duplication allows for better querying
      // @todo: make this a date field instead of datetime?
      // @todo: remove year,month,day fields? what about cross-db handling (mysql, postgres, sqlite, mssql, ...)
      'date' => array(
        'description' => 'Datetime representation of availability',
        'type' => 'datetime',
      ),
    ),
  );
  $schema['availability_calendars_week'] = array(
    'description' => 'The table for calendar days.',
    'fields' => array(
      'nid' => array(
        'description' => 'The primary identifier for a node.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'year' => array(
        'description' => 'The number of the year.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'month' => array(
        'description' => 'The number of the month.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'week' => array(
        'description' => 'The number of the week.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'note' => array(
        'description' => 'The status.',
        'type' => 'varchar',
        'length' => 64,
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function availability_calendars_install() {
  drupal_install_schema('availability_calendars');
  drupal_set_message(t('Availability Calendars module installed successfully.'), 'warning');
}

/**
 * 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 for which to delete
 */
function availability_calendars_variable_del_all($name) {
  db_query("DELETE FROM {variable} WHERE name LIKE '%s%%'", $name);
  cache_clear_all('variables', 'cache');
}

/**
 * Implements hook_uninstall().
 */
function availability_calendars_uninstall() {
  drupal_uninstall_schema('availability_calendars');
  _availability_calendars_variable_del_all('availability_calendars_');
  drupal_set_message(t('Availability Calendars module uninstalled successfully.'), 'warning');
}

/**
 * Update availability_calendars_day table to add date field.
 */
function availability_calendars_update_6101() {
  $ret = array();
  db_add_field($ret, 'availability_calendars_day', 'date', array(
    'type' => 'datetime',
    'not null' => FALSE,
  ));
  $ret[] = update_sql("UPDATE {availability_calendars_day} SET date = CAST(CONCAT(year, '-', month, '-', day, ' 12:00:00') as DATETIME)", $date);
  $ret[] = array(
    'success' => TRUE,
    'query' => 'Recalculated date column for ' . db_affected_rows() . ' availability calendar entries.',
    'rows' => db_affected_rows(),
  );
  return $ret;
}

/**
 * Update the settings system.
 */
function availability_calendars_update_6102(&$sandbox) {
  $ret = array();

  // system wide settings
  $ret[] = update_sql("UPDATE {variable} SET name = 'availability_calendars_settings_system_nodeview' WHERE name = 'availability_calendars_display_nodeview'");
  $ret[] = update_sql("UPDATE {variable} SET name = 'availability_calendars_settings_system_hideold' WHERE name = 'availability_calendars_display_hideallold'");
  $ret[] = update_sql("UPDATE {variable} SET name = 'availability_calendars_settings_system_monthcount' WHERE name = 'availability_calendars_display_monthcount'");

  // content type settings
  $types = node_get_types();
  foreach ($types as $id => $val) {
    if (variable_get("availability_calendars_{$val->type}", 'marker') !== 'marker') {
      $ret[] = update_sql("UPDATE {variable} SET name = 'availability_calendars_settings_system-type_{$val->type}' WHERE name = 'availability_calendars_{$val->type}'");
    }
  }

  // Node specific settings.
  // Rename all node specific settings, by inserting settings_node_, select by looking at
  // the format (availability_calendar_%_%) and if the 24th character is a number.
  // strlen('availability_calendars_') = 23
  global $db_type;
  $update_query = "UPDATE {variable} " . ($db_type === 'pgsql' ? "SET name = substring(name from 1 for 23) || 'settings_node_' || substring(name from 24) " : "SET name = concat(substring(name from 1 for 23), 'settings_node_', substring(name from 24)) ") . "WHERE name LIKE 'availability\\_calendars\\_%\\_%' " . "  AND position(substring(name from 24 for 1) in '123456789') > 0";
  $ret[] = update_sql($update_query);
  return $ret;
}

/**
 * Change statuses to now use class strings instead of integers and alter defaultstatus setting if set.
 */
function availability_calendars_update_6103(&$sandbox) {
  $ret = array();
  if (!isset($sandbox['progress'])) {

    // changing statuses
    $ret[] = update_sql("ALTER TABLE {availability_calendars_day} CHANGE status status MEDIUMTEXT NULL DEFAULT NULL");
    $ret[] = update_sql("UPDATE {availability_calendars_day} SET status = 'calavailable' WHERE status = '0';");
    $ret[] = update_sql("UPDATE {availability_calendars_day} SET status = 'calnotavailable' WHERE status = '1';");
    $ret[] = update_sql("UPDATE {availability_calendars_day} SET status = 'calnotavailableprov' WHERE status = '2';");
    $ret[] = update_sql("UPDATE {variable} SET value = 's:12:\"calavailable\";' WHERE name = 'availability_calendars_settings_system_defaultstatus' AND value = 'i:0;'");
    $ret[] = update_sql("UPDATE {variable} SET value = 's:12:\"calavailable\";' WHERE name = 'availability_calendars_settings_system_defaultstatus' AND value = 's:1:\"0\";'");
    $ret[] = update_sql("UPDATE {variable} SET value = 's:15:\"calnotavailable\";' WHERE name = 'availability_calendars_settings_system_defaultstatus' AND value = 'i:1;'");
    $ret[] = update_sql("UPDATE {variable} SET value = 's:15:\"calnotavailable\";' WHERE name = 'availability_calendars_settings_system_defaultstatus' AND value = 's:1:\"1\";'");
    $ret[] = update_sql("UPDATE {variable} SET value = 's:19:\"calnotavailableprov\";' WHERE name = 'availability_calendars_settings_system_defaultstatus' AND value = 'i:2;'");
    $ret[] = update_sql("UPDATE {variable} SET value = 's:19:\"calnotavailableprov\";' WHERE name = 'availability_calendars_settings_system_defaultstatus' AND value = 's:1:\"2\";'");

    // content type settings
    $types = node_get_types();
    $update_types = array();
    foreach ($types as $id => $val) {
      if (variable_get('availability_calendars_settings_system-type_' . $val->type, '0') == '1' || variable_get('availability_calendars_' . $val->type, '0') == '1') {
        $update_types[] = "'{$val->type}'";
      }
    }
    $sandbox['progress'] = 0;
    $sandbox['current_nid'] = 0;
    $sandbox['update_types'] = implode(', ', $update_types);
    $sandbox['max'] = db_result(db_query('SELECT COUNT(DISTINCT nid) FROM {node} WHERE type IN (' . $sandbox['update_types'] . ')'));
  }

  // node specific updates: do not rewrite (i18n might restrict to a certain language)
  $nodes = db_query_range('SELECT nid FROM {node} WHERE nid > ' . $sandbox['current_nid'] . ' AND type IN (' . $sandbox['update_types'] . ') ORDER BY nid ASC', 0, 1);
  while ($node = db_fetch_object($nodes)) {
    $nid = $node->nid;
    $sql = '';
    $sql = "WHEN (name = 'availability_calendars_settings_node_" . $nid . "_defaultstatus' AND value = 'i:0;') THEN 's:12:\"calavailable\";'";
    $sql = "WHEN (name = 'availability_calendars_settings_node_" . $nid . "_defaultstatus' AND value = 's:1:\"0\";') THEN 's:12:\"calavailable\";'";
    $sql .= " WHEN (name = 'availability_calendars_settings_node_" . $nid . "_defaultstatus' AND value = 'i:1;') THEN 's:15:\"calnotavailable\";'";
    $sql .= " WHEN (name = 'availability_calendars_settings_node_" . $nid . "_defaultstatus' AND value = 's:1:\"1\";') THEN 's:15:\"calnotavailable\";'";
    $sql .= " WHEN (name = 'availability_calendars_settings_node_" . $nid . "_defaultstatus' AND value = 'i:2;') THEN 's:19:\"calnotavailableprov\";'";
    $sql .= " WHEN (name = 'availability_calendars_settings_node_" . $nid . "_defaultstatus' AND value = 's:1:\"1\";') THEN 's:19:\"calnotavailableprov\";'";
    $ret[] = update_sql("UPDATE {variable} SET value = CASE {$sql} ELSE value END;");
    $sandbox['progress']++;
    $sandbox['current_nid'] = $nid;
  }
  $ret['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  $ret[] = array(
    'success' => TRUE,
    'query' => 'Fixed variables for ' . $sandbox['max'] . ' availability calendar variable settings.',
  );
  return $ret;
}

Functions

Namesort descending Description
availability_calendars_install Implements hook_install().
availability_calendars_schema Implements hook_schema().
availability_calendars_uninstall Implements hook_uninstall().
availability_calendars_update_6101 Update availability_calendars_day table to add date field.
availability_calendars_update_6102 Update the settings system.
availability_calendars_update_6103 Change statuses to now use class strings instead of integers and alter defaultstatus setting if set.
availability_calendars_variable_del_all Utility function that is an altered version of variable_del, it will delete a set of variables set by a module.