You are here

domain_conf.module in Domain Access 7.2

Domain manager configuration options.

File

domain_conf/domain_conf.module
View source
<?php

/**
 * @defgroup domain_conf Domain Conf: configuration extension
 * Functions for the Domain Conf module.
 */

/**
 * @file
 * Domain manager configuration options.
 *
 * @ingroup domain_conf
 */

/**
 * Implements hook_domain_bootstrap_full().
 */
function domain_conf_domain_bootstrap_full($domain) {
  static $check;

  // If running domain_set_domain(), we have issues with the variables
  // from the primary domain, which need to be loaded from cache.
  // @link http://drupal.org/node/412156
  if ($check) {
    $_domain = domain_get_domain();
    if ($domain['domain_id'] == 0 && $check != $_domain['domain_id']) {
      _domain_conf_load_primary(TRUE);
    }
  }

  // Flag that we have already loaded.
  $check = $domain['domain_id'];
  $data = db_query("SELECT settings FROM {domain_conf} WHERE domain_id = :domain_id", array(
    ':domain_id' => $domain['domain_id'],
  ))
    ->fetchField();
  if (!empty($data)) {
    global $conf;
    $settings = domain_unserialize($data);

    // Overwrite the $conf variables.
    foreach ($settings as $key => $value) {
      if ($value === 'domain-conf-ignore') {
        continue;
      }

      // Language handling is a special case.
      if ($key == 'language_default') {
        $table = domain_get_primary_table('system');
        $language = (bool) db_query("SELECT status FROM {$table} WHERE name = 'locale' AND type = 'module'")
          ->fetchField();
        if ($language) {
          $table = domain_get_primary_table('languages');
          $temp = db_query("SELECT * FROM {$table} WHERE language = :language", array(
            ':language' => $value,
          ))
            ->fetchObject();
          if (!empty($temp)) {
            $value = $temp;
            $GLOBALS['language'] = $temp;
            $conf[$key] = $value;
          }

          // Ensure we store the default language in case of reset.
          // See domain_conf_js_maintain().
          $table = domain_get_primary_table('variable');
          $result = db_query("SELECT value FROM {$table} WHERE name = :name", array(
            ':name' => $key,
          ))
            ->fetchField();
          if (!empty($result)) {
            domain_conf_default_language(unserialize($result));
          }
        }
      }
      else {
        $conf[$key] = $value;
      }
    }
  }
}

/**
 * Implements hook_init()
 */
function domain_conf_init() {

  // Allow sites to add implementations of hook_domainconf() without hacking.
  // See http://drupal.org/node/236877.
  if (arg(0) == 'admin') {
    $extra = drupal_get_path('module', 'domain_conf') . '/domain_conf.inc';
    if (file_exists($extra)) {
      include $extra;
    }
  }
}

/**
 * Implements hook_menu()
 */
function domain_conf_menu() {
  $items = array();
  $items['admin/structure/domain/conf/%domain'] = array(
    'title' => 'Domain site settings',
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
    'access arguments' => array(
      'administer domains',
    ),
    'page callback' => 'domain_conf_page',
    'page arguments' => array(
      4,
    ),
    'file' => 'domain_conf.admin.inc',
  );
  $items['admin/structure/domain/conf-reset/%domain'] = array(
    'title' => 'Domain site settings',
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
    'access arguments' => array(
      'administer domains',
    ),
    'page callback' => 'domain_conf_reset',
    'page arguments' => array(
      4,
    ),
    'file' => 'domain_conf.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_theme()
 */
function domain_conf_theme() {
  $themes = array(
    'domain_conf_reset' => array(
      'variables' => array(
        'domain' => array(),
      ),
    ),
  );
  return $themes;
}

/**
 * Implements hook_domainlinks()
 */
function domain_conf_domainlinks($domain) {
  static $extra;
  if (!isset($extra)) {
    $extra = domain_conf_api();
  }
  if ($domain['domain_id'] > 0 || !empty($extra)) {
    $links[] = array(
      'title' => t('settings'),
      'path' => 'admin/structure/domain/conf/' . $domain['domain_id'],
    );
    return $links;
  }
  return FALSE;
}

/**
 * Implements hook_domainwarnings()
 */
function domain_conf_domainwarnings() {

  // These are the forms for variables set by Domain Conf.
  $forms = array(
    'system_admin_theme_settings',
    'system_performance_settings',
    'system_regional_settings',
    'system_site_information_settings',
    'system_site_maintenance_mode',
    'locale_languages_overview_form',
    'menu_configure',
  );
  $return = array();
  foreach ($forms as $form) {
    $return[$form] = 'admin/structure/domain/conf/%domain_id';
  }
  return $return;
}

/**
 * Implements hook_domainbatch()
 */
function domain_conf_domainbatch() {
  $batch = array();

  // Allows the deletion of all Domain Configuration rows.
  $batch['domain_conf'] = array(
    '#form' => array(
      '#title' => t('Reset configurations'),
      '#type' => 'checkbox',
      '#options' => array(
        0 => 1,
        1 => t('Reset'),
      ),
      '#description' => t('Delete custom settings for this domain.'),
    ),
    '#domain_action' => 'domain_delete',
    '#system_default' => 0,
    '#variable' => 'domain_conf',
    '#meta_description' => t('Delete custom settings for domains as supplied by Domain Configuration.'),
    '#table' => 'domain_conf',
    '#weight' => -10,
  );

  // Change the email address.
  $batch['site_mail'] = array(
    '#form' => array(
      '#title' => t('Email address'),
      '#type' => 'textfield',
      '#size' => 40,
      '#maxlength' => 255,
      '#description' => t('Set the email address for this domain.'),
    ),
    '#permission' => 'administer site configuration',
    '#domain_action' => 'domain_conf',
    '#system_default' => variable_get('site_mail', ''),
    '#variable' => 'site_mail',
    '#meta_description' => t('Set the email address for all domains.'),
    '#data_type' => 'string',
    '#weight' => -8,
    '#update_all' => TRUE,
    '#group' => t('Site configuration'),
  );

  // Change the site slogan.
  $batch['site_slogan'] = array(
    '#form' => array(
      '#title' => t('Site slogan'),
      '#type' => 'textfield',
      '#size' => 60,
      '#maxlength' => 255,
      '#description' => t('The slogan of this domain. Some themes display a slogan when available.'),
    ),
    '#permission' => 'administer site configuration',
    '#domain_action' => 'domain_conf',
    '#system_default' => variable_get('site_slogan', ''),
    '#variable' => 'site_slogan',
    '#meta_description' => t('Set the site slogan for all domains.'),
    '#data_type' => 'string',
    '#weight' => -8,
    '#update_all' => TRUE,
    '#group' => t('Site configuration'),
  );

  // Change the site frontpage.
  $batch['site_frontpage'] = array(
    '#form' => array(
      '#title' => t('Site frontpage'),
      '#type' => 'textfield',
      '#size' => 30,
      '#maxlength' => 255,
      '#description' => t('The home page displays content from this relative URL. If unsure, specify "node".'),
    ),
    '#permission' => 'administer site configuration',
    '#domain_action' => 'domain_conf',
    '#system_default' => variable_get('site_frontpage', 'node'),
    '#variable' => 'site_frontpage',
    '#meta_description' => t('Set the site frontpage for all domains.'),
    '#data_type' => 'string',
    '#weight' => -8,
    '#update_all' => TRUE,
    '#group' => t('Site configuration'),
  );

  // Change the anonymous user name.
  $batch['anonymous'] = array(
    '#form' => array(
      '#title' => t('Anonymous user'),
      '#type' => 'textfield',
      '#size' => 30,
      '#maxlength' => 255,
      '#description' => t('The name used to indicate anonymous users for this domain.'),
    ),
    '#permission' => 'administer site configuration',
    '#domain_action' => 'domain_conf',
    '#system_default' => variable_get('anonymous', 'Anonymous'),
    '#variable' => 'anonymous',
    '#meta_description' => t('Set the anonymous user label for all domains.'),
    '#data_type' => 'string',
    '#weight' => -8,
    '#update_all' => TRUE,
    '#group' => t('Site configuration'),
  );

  // Change the administrative theme.
  $themes = list_themes();
  ksort($themes);
  $options[] = t('Use domain default theme');
  foreach ($themes as $key => $value) {
    $options[$key] = $key;
  }
  $batch['admin_theme'] = array(
    '#form' => array(
      '#title' => t('Administrative theme'),
      '#type' => 'select',
      '#options' => $options,
      '#description' => t('Select the administrative theme for this domain.'),
    ),
    '#permission' => 'administer themes',
    '#domain_action' => 'domain_conf',
    '#system_default' => variable_get('admin_theme', 0),
    '#variable' => 'admin_theme',
    '#meta_description' => t('Set the administrative theme for all domains.'),
    '#data_type' => 'string',
    '#weight' => -8,
    '#update_all' => TRUE,
    '#group' => t('Administrative theme'),
  );

  // Change the timezone.
  $zones = system_time_zones();
  $batch['date_default_timezone'] = array(
    '#form' => array(
      '#title' => t('Timezone default'),
      '#type' => 'select',
      '#options' => $zones,
      '#description' => t('Select the default site time zone.'),
    ),
    '#permission' => 'administer site configuration',
    '#domain_action' => 'domain_conf',
    '#system_default' => variable_get('date_default_timezone', 0),
    '#variable' => 'date_default_timezone',
    '#meta_description' => t('Set the default timezone for all domains.'),
    '#data_type' => 'string',
    '#weight' => -6,
    '#update_all' => TRUE,
    '#group' => t('Timezone settings'),
  );

  // Change the caching mode.
  $cache = variable_get('cache', 0);
  $batch['cache'] = array(
    '#form' => array(
      '#type' => 'checkbox',
      '#title' => t('Cache pages for anonymous users'),
      '#default_value' => $cache,
    ),
    '#permission' => 'administer site configuration',
    '#domain_action' => 'domain_conf',
    '#system_default' => $cache,
    '#variable' => 'cache',
    '#meta_description' => t('Set the page cache options for all domains.'),
    '#data_type' => 'integer',
    '#weight' => -5,
    '#update_all' => TRUE,
    '#group' => t('Performance'),
  );

  // Change the cache lifetime.
  $period = drupal_map_assoc(array(
    0,
    60,
    180,
    300,
    600,
    900,
    1800,
    2700,
    3600,
    10800,
    21600,
    32400,
    43200,
    86400,
  ), 'format_interval');
  $period[0] = '<' . t('none') . '>';
  $batch['cache_lifetime'] = array(
    '#form' => array(
      '#type' => 'select',
      '#title' => t('Minimum cache lifetime'),
      '#default_value' => variable_get('cache_lifetime', 0),
      '#options' => $period,
      '#description' => t('The minimum amount of time that will elapse before the caches are recreated.'),
    ),
    '#permission' => 'administer site configuration',
    '#domain_action' => 'domain_conf',
    '#system_default' => variable_get('cache_lifetime', 0),
    '#variable' => 'cache_lifetime',
    '#meta_description' => t('Set the minimum cache lifetime for all domains.'),
    '#data_type' => 'integer',
    '#weight' => -5,
    '#update_all' => TRUE,
    '#group' => t('Performance'),
  );

  // Toggle the site offline status.
  $batch['maintenance_mode'] = array(
    '#form' => array(
      '#type' => 'checkbox',
      '#title' => t('Put site into maintenance mode'),
      '#default_value' => variable_get('maintenance_mode', 0),
      '#description' => t('When enabled, only users with the "Access site in maintenance mode" <a href="@permissions-url">permission</a> are able to access your site to perform maintenance; all other visitors see the maintenance mode message configured below. Authorized users can log in directly via the <a href="@user-login">user login</a> page.', array(
        '@permissions-url' => url('admin/people/permissions'),
        '@user-login' => url('user'),
      )),
    ),
    '#permission' => 'administer site configuration',
    '#domain_action' => 'domain_conf',
    '#system_default' => variable_get('maintenance_mode', 0),
    '#variable' => 'maintenance_mode',
    '#meta_description' => t('Set maintenance for all domains.'),
    '#data_type' => 'integer',
    '#weight' => -4,
    '#update_all' => TRUE,
    '#group' => t('Maintenance mode'),
  );

  // Change the site offline message.
  $batch['maintenance_mode_message'] = array(
    '#form' => array(
      '#title' => t('Site offline message'),
      '#type' => 'textarea',
      '#cols' => 30,
      '#rows' => 5,
      '#description' => t('Message to show visitors when this domain is in off-line mode.'),
    ),
    '#permission' => 'administer site configuration',
    '#domain_action' => 'domain_conf',
    '#system_default' => t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array(
      '@site' => variable_get('site_name', 'Drupal'),
    )),
    '#variable' => 'maintenance_mode_message',
    '#meta_description' => t('Set the site offline message for all domains.'),
    '#data_type' => 'string',
    '#weight' => -2,
    '#update_all' => TRUE,
    '#group' => t('Maintenance mode'),
  );

  // Change the default language.
  if (module_exists('locale')) {
    $languages = domain_conf_language_options();
    $default = language_default();
    $batch['language_default'] = array(
      '#form' => array(
        '#title' => t('Default language'),
        '#type' => 'select',
        '#options' => $languages,
        '#description' => t('The default language to use for this domain. <em>Note: This setting only works with path-based language switching.</em>'),
      ),
      '#permission' => 'administer languages',
      '#domain_action' => 'domain_conf',
      '#system_default' => $default->language,
      '#override_default' => TRUE,
      '#variable' => '',
      // deliberately empty, since this is an array and cannot be set.
      '#meta_description' => t('Set the default language for all domains.'),
      '#data_type' => 'string',
      '#weight' => 6,
      '#update_all' => TRUE,
      '#group' => t('Language settings'),
      '#module' => t('Language'),
    );
  }

  // Change the menus
  if (module_exists('menu')) {
    $menus = menu_get_menus();
    $menu_options = $menus;
    $menus[0] = t('Do not use for this site');
    $main = variable_get('menu_main_links_source', 'main-menu');
    $batch['menu_main_links_source'] = array(
      '#form' => array(
        '#title' => t('Source for the Main links'),
        '#type' => 'select',
        '#options' => $menus,
        '#description' => t('Select what should be displayed as the Main links (typically at the top of the page).'),
      ),
      '#permission' => 'administer menu',
      '#domain_action' => 'domain_conf',
      '#system_default' => $main,
      '#variable' => 'menu_main_links_source',
      '#meta_description' => t('Select what should be displayed as the Main links (typically at the top of the page).'),
      '#data_type' => 'string',
      '#weight' => 2,
      '#update_all' => TRUE,
      '#group' => t('Menu settings'),
      '#module' => t('Menu'),
    );
    $batch['menu_secondary_links_source'] = array(
      '#form' => array(
        '#title' => t('Source for the Secondary links'),
        '#type' => 'select',
        '#options' => $menus,
        '#description' => t("Select the source for the Secondary links. An advanced option allows you to use the same source for both Main links (currently %main) and Secondary links: if your source menu has two levels of hierarchy, the top level menu links will appear in the Main links, and the children of the active link will appear in the Secondary links.", array(
          '%main' => isset($menus[$main]) ? $menus[$main] : t('No main links'),
        )),
      ),
      '#permission' => 'administer menu',
      '#domain_action' => 'domain_conf',
      '#system_default' => variable_get('menu_secondary_links_source', 0),
      '#variable' => 'menu_secondary_links_source',
      '#meta_description' => t('Set the secondary links menu in all domains.'),
      '#data_type' => 'string',
      '#weight' => 4,
      '#update_all' => TRUE,
      '#group' => t('Menu settings'),
      '#module' => t('Menu'),
    );
    $types = node_type_get_types();

    // Node menu handling is much more complex now.
    // Call Drupal.menu_update_parent_list() to filter the list of
    // available default parent menu items based on the selected menus.
    // This function will not work with multiple form elements on the page.
    // TODO: Replace or extend the core JS.

    /*
    drupal_add_js(
      '(function ($) { Drupal.menu_update_parent_list(); })(jQuery);',
      array('scope' => 'footer', 'type' => 'inline')
    );
    drupal_add_js(drupal_get_path('module', 'menu') . '/menu.admin.js'));
    */

    // To avoid an 'illegal option' error after saving the form we have to load
    // all available menu items. Otherwise it is not possible to dynamically
    // add options to the list.
    $options = menu_parent_options(menu_get_menus(), array(
      'mlid' => 0,
    ));
    foreach ($types as $type) {
      $form['menu']['menu_options'] = array(
        '#type' => 'checkboxes',
        '#title' => t('@type: Available menus', array(
          '@type' => $type->name,
        )),
        '#options' => $menu_options,
      );
      $form['menu']['menu_parent'] = array(
        '#type' => 'select',
        '#title' => t('@type: Default parent item', array(
          '@type' => $type->name,
        )),
        '#options' => array(),
        '#attributes' => array(
          'class' => array(
            'menu-title-select',
          ),
        ),
        '#process' => array(
          'domain_conf_get_menu_parents',
        ),
      );
      $batch['menu_options_' . $type->type] = array(
        '#form' => $form['menu']['menu_options'],
        '#permission' => 'administer menu',
        '#domain_action' => 'domain_conf',
        '#system_default' => variable_get('menu_options_' . $type->type, array(
          'main-menu',
        )),
        '#variable' => 'menu_options_' . $type->type,
        '#meta_description' => t('The menus available to place links in for this content type.'),
        '#data_type' => 'string',
        '#weight' => 6,
        '#update_all' => TRUE,
        '#collapsed' => TRUE,
        '#group' => t('@type menu settings', array(
          '@type' => $type->name,
        )),
        '#module' => t('Menu'),
      );
      $batch['menu_parent_' . $type->type] = array(
        '#form' => $form['menu']['menu_parent'],
        '#permission' => 'administer menu',
        '#domain_action' => 'domain_conf',
        '#system_default' => variable_get('menu_parent_' . $type->type, 'main-menu:0'),
        '#variable' => 'menu_parent_' . $type->type,
        '#meta_description' => t('Choose the menu item to be the default parent for a new link in the content authoring form.'),
        '#data_type' => 'string',
        '#weight' => 6,
        '#update_all' => TRUE,
        '#collapsed' => TRUE,
        '#group' => t('@type menu settings', array(
          '@type' => $type->name,
        )),
        '#module' => t('Menu'),
      );
    }
  }
  foreach ($batch as $key => $value) {
    if (!isset($batch[$key]['#module'])) {
      $batch[$key]['#module'] = t('Domain Configuration');
    }
  }
  return $batch;
}

/**
 * Form process function to load available menus.
 *
 * If we try to load the menus in the batch hook, it can create a race
 * condition with menu_rebuild() that leads to a fatal call stack error.
 *
 * So we use this process callback to populate the menus safely, after they
 * have been rebuilt.
 */
function domain_conf_get_menu_parents($element, &$form_state) {
  static $options;
  if (!isset($options)) {
    $options = menu_parent_options(menu_get_menus(), array(
      'mlid' => 0,
    ));
  }
  $element['#options'] += $options;
  return $element;
}

/**
 * Implements hook_domainupdate().
 */
function domain_conf_domainupdate($op, $domain, $form_state = array()) {
  if ($op == 'delete') {
    db_delete('domain_conf')
      ->condition('domain_id', $domain['domain_id'])
      ->execute();
    cache_clear_all('variables', 'cache_bootstrap');
  }
}

/**
 * Retrieves elements from hook_domainconf() and formats them
 * as needed.
 *
 * @param $all
 *   Should the function return all hook implementations or just those marked
 *   with the domain_settings flag.  Defaults to FALSE.  Used to determine if
 *   we are loading configuration options specific to the Domain Access module.
 * @return
 *   An array of form elements according to the FormsAPI or an empty array.
 */
function domain_conf_api($all = FALSE, $settings = array()) {
  global $_domain;
  $options = array();
  $extra = module_invoke_all('domainconf', $_domain);
  if (!empty($extra)) {
    foreach ($extra as $key => $value) {
      foreach (element_children($value) as $element) {
        if (isset($value[$element]['#default_value']) && isset($settings[$element])) {
          $value[$element]['#default_value'] = $settings[$element];
        }
      }
      if (!empty($value['#domain_setting']) || $all == TRUE) {

        // Discard the #domain_setting flag; it is not needed.
        unset($value['#domain_setting']);

        // Set the $options array.
        $options[$key] = $value;
      }
    }
  }
  return $options;
}

/**
 * Implements hook_block_info().
 */
function domain_conf_block_info() {
  $blocks = array();
  $blocks['domain-main-links'] = array(
    'info' => t('Domain main links'),
  );
  $blocks['domain-secondary-links'] = array(
    'info' => t('Domain secondary links'),
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function domain_conf_block_view($delta = '') {

  // Dispatch to sub-function.
  if (empty($delta)) {
    return;
  }

  // Get the menu information.
  $menus = menu_get_menus();
  $string = str_replace('domain-', '', $delta);
  $name = str_replace('-links', '-menu', $string);
  $source = 'menu_' . str_replace('-', '_', $string) . '_source';
  $data = variable_get($source, $string);

  // Some domains can disable primary and secondary links.
  if (empty($data)) {
    return;
  }

  // In D7, secondary links may be children of main links.
  if ($delta == 'domain-secondary-links' && variable_get('menu_secondary_links_source', 'user-menu') == variable_get('menu_main_links_source', 'main-menu')) {

    // This does not work right now.
  }
  if (!isset($menus[$data])) {
    return;
  }

  // Output the block.
  $output = menu_tree($data);

  // Build the block.
  $block = array(
    'subject' => check_plain($menus[$data]),
    'content' => $output,
  );
  return $block;
}

/**
 * Change the variable setting for a domain.
 * This function is called by external modules that wish
 * to alter Domain Conf settings.
 *
 * Note that this function saves the value to the database
 * and changes the active $conf array.
 *
 * @link http://drupal.org/node/367963
 * @see domain_conf_variable_save()
 *
 * @param $domain_id
 *   The unique domain ID that is being edited.
 * @param $variable
 *   The name of the variable you wish to set.
 * @param $value
 *   The value of the variable to set. You may leave this
 *   value blank in order to unset the custom variable.
 */
function domain_conf_variable_set($domain_id, $variable, $value = NULL) {
  global $conf, $_domain;
  domain_conf_variable_save($domain_id, $variable, $value);

  // Clear the cache.
  cache_clear_all('variables', 'cache');

  // If we are on the active domain, set the active variable.
  if ($domain_id == $_domain['domain_id']) {
    $conf[$variable] = $value;
  }
}

/**
 * Load a variable specific to a domain.
 *
 * @param $domain_id
 *   The unique domain ID that is being edited.
 * @param $variable
 *   The name of the variable you wish to get.
 * @param $all
 *   A boolean flag indicating whether to return the entire variable array.
 * @param $reset
 *   A boolean flag to reset the static variable array for the domain. Useful
 *   if you are changing variables during a page request.
 * @return
 *   The value of the variable for that domain, or NULL if not set,
 *   or an array of variables, in the case of $all.
 */
function domain_conf_variable_get($domain_id, $variable = '', $all = FALSE, $reset = FALSE) {
  global $_domain;
  static $settings, $base;
  if (empty($base)) {
    $base = _domain_conf_load_primary(FALSE);
  }
  if (!isset($settings[$domain_id]) || $reset) {

    // Get the current settings for this domain, if any.
    $data = domain_unserialize(db_query("SELECT settings FROM {domain_conf} WHERE domain_id = :domain_id", array(
      ':domain_id' => $domain_id,
    ))
      ->fetchField());
    if (empty($data)) {
      $data = array();
    }
    $settings[$domain_id] = array_merge($base, $data);
  }
  if ($all) {
    return $settings[$domain_id];
  }
  if (isset($settings[$domain_id][$variable])) {
    return $settings[$domain_id][$variable];
  }
  return NULL;
}

/**
 * Get the language options for use in forms.
 */
function domain_conf_language_options() {
  $languages = language_list('language', TRUE);
  $options = array();
  foreach ($languages as $key => $lang) {
    $extra = '';
    if ($lang->native != $lang->name) {
      $extra = ' (' . $lang->name . ')';
    }
    $options[$key] = check_plain($lang->native . $extra);
  }
  return $options;
}

/**
 * Load the variables from the primary domain.
 *
 * We run this special handler when not able to trust variable_get()
 * during domain switching.
 *
 * @see domain_set_domain()
 *
 * @param $unset
 *   If TRUE, this will reset the global $conf array.
 * @return
 *   If set to TRUE, no return, just modify the global $conf array.
 *   Otherwise, return the settings data for the primary domain.
 */
function _domain_conf_load_primary($unset = FALSE) {
  static $settings;
  if (!isset($settings)) {

    // Account for table prefixing.
    $cache_table = domain_get_primary_table('cache');

    // Load the query.
    $data = db_query("SELECT data FROM {$cache_table} WHERE cid = 'variables'")
      ->fetchField();
    if (!empty($data)) {
      $settings = domain_unserialize($data);
    }
    else {
      $variable_table = domain_get_primary_table('variable');
      $result = db_query("SELECT name, value FROM {$variable_table}");
      foreach ($result as $vars) {
        $data[$vars->name] = domain_unserialize($vars->value);
      }
      $settings = $data;
    }
  }

  // Do we reset the global or just return data?
  if ($unset) {
    global $conf;
    $conf = $settings;
    return;
  }
  return $settings;
}

/**
 * Delete a setting from {domain_conf}.
 *
 * @param $domain_id
 *   The unique domain ID that is being edited.
 * @param $variable
 *   The name of the variable you wish to delete.
 */
function domain_conf_variable_delete($domain_id, $variable) {

  // Get the current settings for this domain, if any.
  $settings = domain_unserialize(db_query("SELECT settings FROM {domain_conf} WHERE domain_id = :domain_id", array(
    ':domain_id' => $domain_id,
  ))
    ->fetchField());

  // Settings found, remove them.
  if (!empty($settings)) {
    unset($settings[$variable]);
    db_update('domain_conf')
      ->condition('domain_id', $domain_id)
      ->fields(array(
      'settings' => serialize($settings),
    ))
      ->execute();
  }
}

/**
 * Store a single variable in {domain_conf}.
 *
 * @link http://drupal.org/node/367963
 * @see domain_conf_variable_set()
 *
 * @param $domain_id
 *   The unique domain ID that is being edited.
 * @param $variable
 *   The name of the variable you wish to set.
 * @param $value
 *   The value of the variable to set. You may leave this
 *   value blank in order to unset the custom variable.
 */
function domain_conf_variable_save($domain_id, $variable, $value = NULL) {

  // Get the current settings for this domain, if any.
  $serialized = db_query("SELECT settings FROM {domain_conf} WHERE domain_id = :domain_id", array(
    ':domain_id' => $domain_id,
  ))
    ->fetchField();

  // Settings found, update them.
  if ($serialized) {
    $settings = domain_unserialize($serialized);
    $settings[$variable] = $value;
    db_update('domain_conf')
      ->condition('domain_id', $domain_id)
      ->fields(array(
      'settings' => serialize($settings),
    ))
      ->execute();
  }
  elseif (domain_lookup($domain_id) != -1) {
    $settings = array(
      $variable => $value,
    );
    db_insert('domain_conf')
      ->fields(array(
      'domain_id' => $domain_id,
      'settings' => serialize($settings),
    ))
      ->execute();
  }
}

/**
 * Ensure that language changes do not affect default settings.
 *
 * When using multiple languages with JavaScript translations, the invocation
 * of _locale_rebuild_js() can force the default domain setting to be assigned
 * to the active domain. Hilarity foes not ensue.
 *
 * We try to account for this issue by ensuring that the default setting is not
 * changed when this function is invoked, which can happen in two places:
 * locale_js_alter() and locale_languages_delete_form_submit(). Fortunately,
 * both function are tied to hook that we can monitor in order to ensure that
 * the settings are preserved properly.
 * @link http://drupal.org/node/1271810
 * @see _locale_rebuild_js()
 * @see locale_languages_delete_form_submit()
 * @see hook_multilingual_settings_changed()
 * @see domain_conf_js_alter()
 * @see domain_conf_multilingual_settings_changed()
 */
function domain_conf_js_maintain() {

  // Only a factor if using the Locale module.
  if (!module_exists('locale')) {
    return;
  }
  if ($default_language = domain_conf_default_language()) {
    drupal_register_shutdown_function('domain_conf_set_default_language', $default_language);
  }
}

/**
 * Implements hook_js_alter().
 */
function domain_conf_js_alter(&$javascript) {
  domain_conf_js_maintain();
}

/**
 * Implements hook_multilingual_settings_changed().
 */
function domain_conf_multilingual_settings_changed() {
  domain_conf_js_maintain();
}

/**
 * Store the default language setting for later use.
 *
 * @param $value
 *  If set, the $langauage object to store.
 *
 * @return
 *  The language object or FALSE if not set.
 */
function domain_conf_default_language($value = NULL) {
  $language =& drupal_static(__FUNCTION__);
  if (!is_null($value)) {
    $language = $value;
  }
  return isset($language) ? $language : FALSE;
}

/**
 * Shutdown function to reset the default language variable.
 *
 * @param $default_language
 *  A language object, stored by domain_conf_default_language().
 */
function domain_conf_set_default_language($default_language) {
  if (!empty($default_language) && is_object($default_language)) {
    variable_set('language_default', $default_language);
  }
}

Related topics

Functions

Namesort descending Description
domain_conf_api Retrieves elements from hook_domainconf() and formats them as needed.
domain_conf_block_info Implements hook_block_info().
domain_conf_block_view Implements hook_block_view().
domain_conf_default_language Store the default language setting for later use.
domain_conf_domainbatch Implements hook_domainbatch()
domain_conf_domainlinks Implements hook_domainlinks()
domain_conf_domainupdate Implements hook_domainupdate().
domain_conf_domainwarnings Implements hook_domainwarnings()
domain_conf_domain_bootstrap_full Implements hook_domain_bootstrap_full().
domain_conf_get_menu_parents Form process function to load available menus.
domain_conf_init Implements hook_init()
domain_conf_js_alter Implements hook_js_alter().
domain_conf_js_maintain Ensure that language changes do not affect default settings.
domain_conf_language_options Get the language options for use in forms.
domain_conf_menu Implements hook_menu()
domain_conf_multilingual_settings_changed Implements hook_multilingual_settings_changed().
domain_conf_set_default_language Shutdown function to reset the default language variable.
domain_conf_theme Implements hook_theme()
domain_conf_variable_delete Delete a setting from {domain_conf}.
domain_conf_variable_get Load a variable specific to a domain.
domain_conf_variable_save Store a single variable in {domain_conf}.
domain_conf_variable_set Change the variable setting for a domain. This function is called by external modules that wish to alter Domain Conf settings.
_domain_conf_load_primary Load the variables from the primary domain.