<?php

/**
 * @file
 * Install file for Domain Conf.
 */

/**
 * Implements hook_schema().
 */
function domain_conf_schema() {
  $schema['domain_conf'] = array(
    'description' => 'Stores custom settings for each domain.',
    'fields' => array(
      'domain_id' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
      'settings' => array('type' => 'blob', 'size' => 'big', 'not null' => FALSE)),
    'primary key' => array('domain_id'),
    'foreign_keys' => array(
      'domain_id' => array('domain' => 'domain_id'),
    ),
  );
  return $schema;
}

/**
 * Update block deltas to Drupal 7.
 */
function domain_conf_update_7000() {
  // Get an array of the renamed block deltas, organized by module.
  $renamed_deltas = array(
    'domain' => array(
      'domain-primary-links' => 'domain-main-links',
    ),
  );
  $moved_deltas = array();
  update_fix_d7_block_deltas($sandbox, $renamed_deltas, $moved_deltas);
  return t('Domain Conf blocks updated.');
}

/**
 * Some variable names changed in Drupal 7.
 */
function domain_conf_update_7001() {
  $changes = domain_conf_d7_variable_changes();
  $domains = db_query("SELECT * FROM {domain}")->FetchAllAssoc('domain_id');
  $result = db_query("SELECT domain_id, settings FROM {domain_conf}");
  foreach ($result as $object) {
    if (!isset($domains[$object->domain_id])) {
      // Remove cruft from the table.
      db_delete('domain_conf')
        ->condition('domain_id', $object->domain_id)
        ->execute();
    }
    elseif (!empty($object->settings)) {
      // Update the record with the new variables.
      $settings = unserialize($object->settings);
      foreach ($changes['removed'] as $key) {
        if (isset($settings[$key])) {
          unset($settings[$key]);
        }
      }
      foreach ($changes['renamed'] as $key => $value) {
        if (isset($settings[$key])) {
          $settings[$value] = $settings[$key];
          unset($settings[$key]);
        }
      }
      db_update('domain_conf')
        ->fields(array('settings' => serialize($settings)))
        ->condition('domain_id', $object->domain_id)
        ->execute();
    }
  }
  return t('Domain Conf variables updated.');
}

/**
 * Return an array of variable changes from Drupal 6 to 7.
 */
function domain_conf_d7_variable_changes() {
  $removed = array(
    'menu_default_node_menu',
    'site_mission',
    'site_footer',
    'page_compression',
  );
  $renamed = array(
    'menu_primary_links_source' => 'menu_main_links_source',
    'site_offline' => 'maintenance_mode',
    'site_offline_message' => 'maintenance_mode_message',
  );
  return array('removed' => $removed, 'renamed' => $renamed);
}

/**
 * Implements hook_dependencies().
 */
function domain_conf_update_dependencies() {
  $dependencies['domain_conf'][7300] = array(
    'domain' => 7303,
  );
  return $dependencies;
}

/**
 * Remove references to row 0.
 */
function domain_conf_update_7300(&$sandbox) {
  $default_id = db_query("SELECT domain_id FROM {domain} WHERE is_default = 1")->fetchField();
  db_update('domain_conf')
    ->fields(array('domain_id' => $default_id))
    ->condition('domain_id', 0)
    ->execute();
  return t('Domain Configuration zero records removed.');
}

/**
 * Increases the size of the {domain_conf}.settings to long blob.
 */
function domain_conf_update_7301(&$sandbox) {
  $updated_field = array(
    'type' => 'blob',
    'size' => 'big',
    'not null' => FALSE,
  );
  db_change_field('domain_conf', 'settings', 'settings', $updated_field);
}
