<?php
// $Id: domain.install,v 1.21 2009/08/17 19:23:40 agentken Exp $

/**
 * @file
 * Install file.
 */

/**
 * Implement hook_install()
 */
function domain_install() {
  drupal_install_schema('domain');
  $root = strtolower(rtrim($_SERVER['SERVER_NAME']));
  $site = variable_get('site_name', 'Drupal');
  $scheme = 'http';
  if (!empty($_SERVER['HTTPS'])) {
    $scheme = 'https';
  }
  db_query("UPDATE {domain} SET subdomain = '%s', sitename = '%s', scheme = '%s', valid = 1 WHERE domain_id = 0", $root, $site, $scheme);
  if (!db_affected_rows()) {
    db_query("INSERT INTO {domain} (subdomain, sitename, scheme, valid) VALUES ('%s', '%s', '%s', %d)", $root, $site, $scheme, 1);
    // MySQL won't let us insert row 0 into an autoincrement table.
    // Similar to the {users} table, this leaves us with no row 1.
    db_query("UPDATE {domain} SET domain_id = domain_id - 1");
  }
  // Set the default domain variables.
  variable_set('domain_root', $root);
  variable_set('domain_scheme', $scheme);
  variable_set('domain_sitename', $site);
}

/**
 * Implement hook_schema()
 */
function domain_schema() {
  $schema['domain'] = array(
    'fields' => array(
      'domain_id' => array('type' => 'serial', 'not null' => TRUE),
      'subdomain' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
      'sitename' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
      'scheme' => array('type' => 'varchar', 'length' => '8', 'not null' => TRUE, 'default' => 'http'),
      'valid' => array('type' => 'varchar', 'length' => '1', 'not null' => TRUE, 'default' => '1')),
    'primary key' => array('domain_id'),
    'indexes' => array(
      'subdomain' => array('subdomain')),
  );
  $schema['domain_access'] = array(
    'fields' => array(
      'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'gid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'realm' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => '')),
    'primary key' => array('nid', 'gid', 'realm'),
    'indexes' => array(
      'nid' => array('nid')),
  );
  $schema['domain_editor'] = array(
    'fields' => array(
      'uid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'domain_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0)),
    'primary key' => array('uid', 'domain_id'),
  );
  return $schema;
}

/**
 * Implement hook_uninstall()
 */
function domain_uninstall() {
  drupal_uninstall_schema('domain');
  db_query("DELETE from {variable} WHERE name LIKE '%s%%'", 'domain_');
}

/**
 * Update note.
 *
 * Since versions prior to 5.x.1.0 are not supported, and ther are no schema changes from
 * 5.x.1 to 6.x.1, no update functions have been provided for Drupal 6.
 *
 * To upgrade from a release candidate, first upgrade the module to 5.x.1.0.  Then upgrade
 * to Drupal 6.
 *
 */

/**
 * Updates from 5.x.1.2 to 5.x.1.3.  This change affected the size
 * of the columns for 'subdomain' and 'sitename'.  See http://drupal.org/node/244142
 */
function domain_update_6100() {
  $ret = array();
  db_drop_index($ret, 'domain', 'subdomain');
  db_change_field($ret, 'domain', 'subdomain', 'subdomain', array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''));
  db_add_index($ret, 'domain', 'subdomain', array('subdomain'));
  db_change_field($ret, 'domain', 'sitename', 'sitename', array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''));
  return $ret;
}

/**
 * Updates to 6.x.2.
 *
 * Deprecates the domain_editor grant.
 * Adds the {domain_editor} table.
 * Installs the new Domain Boostrap routine.
 * Moves records from {user} (data) to {domain_editor}.
 */
function domain_update_6200() {
  $ret = array();

  // Remove the old editors information.
  variable_del('domain_editors');
  // Promopt the user to rebuild node access.
  node_access_needs_rebuild(TRUE);

  // Update the new bootstrap information.
  domain_bootstrap_register();

  // Try to register the default domain.
  $root = variable_get('domain_root', rtrim($_SERVER['HTTP_HOST']));
  db_query("INSERT INTO {domain} (subdomain, sitename, scheme, valid) VALUES ('%s', '%s', '%s', %d)", $root, variable_get('domain_sitename', variable_get('site_name', 'Drupal')), variable_get('domain_scheme', 'http://'), 1);
  // MySQL won't let us insert row 0 into an autoincrement table.
  db_query("UPDATE {domain} SET domain_id = 0 WHERE subdomain = '%s'", $root);

  // Install the {domain_editor} table.
  $schema = domain_schema();
  db_create_table($ret, 'domain_editor', $schema['domain_editor']);

  // Move records from $user->data to {domain_editor}.
  if (!db_table_exists('domain_editor')) {
    return;
  }
  $result = db_query("SELECT uid, data FROM {users}");
  while ($account = db_fetch_object($result)) {
    $data = unserialize($account->data);
    if (!empty($data['domain_user'])) {
      foreach ($data['domain_user'] as $domain_id => $status) {
        // A zero flag indicated not selected.
        if ($status != 0) {
          // Convert the -1 checkbox to a zero.
          if ($domain_id == -1) {
            $domain_id = 0;
          }
          db_query("INSERT INTO {domain_editor} (uid, domain_id) VALUES (%d, %d)", $account->uid, $domain_id);
        }
      }
    }
  }
  return $ret;
}

/**
 * Updates to 6.x.2.
 *
 * Deletes entries from {domain_editor} for domains that no longer exist.
 *
 * This update is needed by people who have been running 6.x.2rc versions.
 *
 */
function domain_update_6201() {
  $ret = array();
  db_query("DELETE FROM {domain_editor} WHERE NOT EXISTS (SELECT domain_id FROM {domain} WHERE {domain}.domain_id={domain_editor}.domain_id)");
  return $ret;
}

/**
 * Updates to 6.x.2.
 * 
 * Removes the authoring and menu settings for the node form.
 * 
 */
function domain_update_6202() {
  $options = variable_get('domain_form_elements', array());
  if (isset($options['author'])) {
    unset($options['author']);
  }
  if (isset($options['menu'])) {
    unset($options['menu']);
  }
  variable_set('domain_form_elements', $options);
}
