You are here

domain_settings.module in Domain Access 7.3

Allows domain-specific use of Drupal system settings forms.

File

domain_settings/domain_settings.module
View source
<?php

/**
 * @defgroup domain_settings Domain Settings : expanded site configuration options for Domain Access
 *
 * Makes standard system settings forms domain-sensitive.
 */

/**
 * @file
 * Allows domain-specific use of Drupal system settings forms.
 *
 * @ingroup domain_settings
 */

/**
 * Forms filtering method constants.
 *
 * Used to specify how to filter forms on which we want to add the
 * domain-specific settings form element.
 */
define('DOMAIN_SETTINGS_SHOW_EXCEPT_LISTED', 0);
define('DOMAIN_SETTINGS_SHOW_ONLY_LISTED', 1);
define('DOMAIN_SETTINGS_ALL_DOMAINS', -5);

/**
 * Implements hook_permission().
 */
function domain_settings_permission() {
  $permissions = array(
    'access domain settings form' => array(
      'title' => t('Access domain settings form'),
      'restrict access' => TRUE,
    ),
  );
  return $permissions;
}

/**
 * Implements hook_form_alter()
 *
 * This function checks the #submit value of the form to see
 * if system_settings_form() has been invoked. If so, we may
 * attach domain-specific settings to the form, if the user is
 * permitted to use this module and the form is not on the
 * administrator's denied list.
 */
function domain_settings_form_alter(&$form, $form_state, $form_id) {
  $_domain = domain_get_domain();

  // Remove settings that cannot be used reliably.
  if (!user_access('access domain settings form') || !isset($form['#submit']) || !in_array('system_settings_form_submit', $form['#submit']) || !domain_settings_add_element($form_id)) {

    // Is this form_id allowed?
    return;
  }

  // Set our drop-down's weight to be one lighter than the submit button's,
  // ensuring that it always appears right above it (assuming nobody else
  // form_alters us out.)
  drupal_set_message(t('This form is domain-sensitive, be sure you select the proper domain before saving.'), 'warning', FALSE);
  $weight = isset($form['buttons']['#weight']) ? $form['buttons']['#weight'] : 0;
  $form['buttons']['#weight'] = $weight + 2;
  $domain_weight = $weight + 1;
  $form['#submit'][] = 'domain_settings_form_submit';
  $options = array(
    DOMAIN_SETTINGS_ALL_DOMAINS => t('All domains'),
  );

  // Get the display format of the form element.
  $format = domain_select_format();
  foreach (domain_domains() as $data) {

    // The domain must be valid.
    if ($data['valid'] || user_access('access inactive domains')) {

      // Checkboxes must be filtered, select lists should not.
      $options[$data['domain_id']] = empty($format) ? check_plain($data['sitename']) : $data['sitename'];
    }
  }
  $form['domain_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Domain-specific settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => $domain_weight,
  );

  // Set the proper default.
  $behavior = variable_get('domain_settings_behavior', 0);
  if ($behavior == 1) {
    $default = $_domain['domain_id'];
  }
  elseif ($behavior == 2) {
    $default = DOMAIN_SETTINGS_ALL_DOMAINS;
  }
  else {
    $default = domain_default_id();
  }
  $form['domain_settings']['domain_id'] = array(
    '#type' => empty($format) ? 'radios' : 'select',
    '#title' => t('Save settings for'),
    '#options' => $options,
    '#required' => TRUE,
    '#description' => t('Select the domain to which these settings apply. If you select <em>All domains</em>, domain-specific settings will be removed.'),
    '#default_value' => $default,
  );
  if ($format) {
    $form['domain_settings']['domain_id']['#multiple'] = FALSE;
    $form['domain_settings']['domain_id']['#size'] = count($options) > 10 ? 10 : count($options);
  }
  if (isset($form['site_name'])) {
    $form['site_name']['#description'] = ' <em>' . t('This value will change the registered name of the selected domain.') . '</em>';
  }
  foreach ($form['#submit'] as $key => $value) {
    if ($value == 'system_settings_form_submit') {
      unset($form['#submit'][$key]);
    }
  }
}

/**
 * Submit handler for domain-specific settings.
 */
function domain_settings_form_submit($form, &$form_state) {
  $domain_id = $form_state['values']['domain_id'];
  $values = array();
  foreach ($form_state['values'] as $key => $value) {
    if (!in_array($key, array(
      'op',
      'submit',
      'reset',
      'form_build_id',
      'form_token',
      'form_id',
      'domain_id',
    ))) {
      $values[$key] = $value;
    }
  }

  // Get the primary domain.
  $default = domain_default_id();
  foreach ($values as $name => $value) {

    // Handle checkboxes when array_filter is set. (Do not return empty checkboxes at all.)
    if (is_array($value) && isset($form_state['values']['array_filter'])) {
      $value = array_keys(array_filter($value));
    }
    if ($domain_id == $default || $domain_id == DOMAIN_SETTINGS_ALL_DOMAINS) {
      variable_set($name, $value);

      // Update the site name of the primary domain, if needed.
      if ($name == 'site_name') {
        db_update('domain')
          ->condition('domain_id', $default)
          ->fields(array(
          'sitename' => $form_state['values']['site_name'],
        ))
          ->execute();
      }
      if ($domain_id == DOMAIN_SETTINGS_ALL_DOMAINS) {
        foreach (domain_domains() as $id => $value) {
          domain_conf_delete_variable($id, $name);
        }
      }
    }
    else {
      domain_conf_variable_save($domain_id, $name, $value);
      if ($name == 'site_name') {
        variable_set('domain_sitename', $value);
        db_update('domain')
          ->condition('domain_id', $domain_id)
          ->fields(array(
          'sitename' => $form_state['values']['site_name'],
        ))
          ->execute();
      }
    }
  }

  // Set a message about the affected domains.
  $extra = '';
  if ($domain_id > 0) {
    $domain = domain_lookup($domain_id);
    $affected = $domain['sitename'];
    if ($domain_id == domain_default_id()) {
      $extra = t('These settings will be inherited unless overridden per domain.');
    }
  }
  else {
    $affected = t('all domains');
  }
  drupal_set_message(t('The configuration options have been saved for %domains. !extra', array(
    '%domains' => $affected,
    '!extra' => $extra,
  )));
  module_invoke_all('domain_settings', $domain_id, $form_state['values']);
  cache_clear_all();
}

/**
 * Implements hook_domain_form().
 */
function domain_settings_domain_form(&$form) {

  // Add the form element to the main screen.
  $form['domain_settings_module'] = array(
    '#type' => 'fieldset',
    '#title' => t('Domain-specific settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['domain_settings_module']['domain_settings_behavior'] = array(
    '#type' => 'radios',
    '#title' => t('Domain settings behavior'),
    '#options' => array(
      0 => t('Use the default domain'),
      1 => t('Use the active domain'),
      2 => t('All domains'),
    ),
    '#default_value' => variable_get('domain_settings_behavior', 0),
    '#description' => t('Default form value when submitting system settings.'),
  );
  $form['domain_settings_module']['domain_settings_form_visibility'] = array(
    '#type' => 'radios',
    '#title' => t('Visibility of domain-specific settings on forms'),
    '#options' => array(
      DOMAIN_SETTINGS_SHOW_EXCEPT_LISTED => t('Show on every system settings form, except those listed below.'),
      DOMAIN_SETTINGS_SHOW_ONLY_LISTED => t('Show only on system settings forms listed below.'),
    ),
    '#default_value' => variable_get('domain_settings_form_visibility', DOMAIN_SETTINGS_SHOW_EXCEPT_LISTED),
  );
  $form['domain_settings_module']['domain_settings_forms'] = array(
    '#type' => 'textarea',
    '#title' => t('Forms'),
    '#rows' => 5,
    '#cols' => 40,
    '#default_value' => variable_get('domain_settings_forms', ''),
    '#description' => t('Allow or disallow specific forms by entering a list of form_ids, one per line.'),
  );
}

/**
 * Helper function to test if a form has to display the domain-specific
 * settings, based on 'domain_settings' settings the user defined.
 *
 * By design, the configuration for Domain Access itself is disallowed
 * (and some other forms known to be problematic).
 *
 * @param $form_id
 *   The form_id string that we want to test.
 * @return
 *    Boolean TRUE if the given form_id has to display the domain-
 *    specific settings. FALSE otherwise.
 */
function domain_settings_add_element($form_id) {

  // Cache results.
  $forms_visibility =& drupal_static(__FUNCTION__);
  if (isset($forms_visibility[$form_id])) {
    return $forms_visibility[$form_id];
  }

  // These forms are known to be problematic, so omit them.
  $disallow = array(
    'domain_configure_form',
    'system_file_system_settings',
    // Changing file paths is dangerous.
    'system_performance_settings',
  );

  // Retrieve the form filtering method:
  $filter_method = variable_get('domain_settings_form_visibility', DOMAIN_SETTINGS_SHOW_EXCEPT_LISTED);

  // Retrieve the user-listed forms:
  $user_listed_forms = array();
  $setting_var = trim(variable_get('domain_settings_forms', ''));
  if (!empty($setting_var)) {
    $match = preg_replace('/(\\r\\n?|\\n)/', '|', $setting_var);
    $user_listed_forms = explode("|", $match);
  }

  // If we want to show the setting on all forms except those listed:
  if ($filter_method == DOMAIN_SETTINGS_SHOW_EXCEPT_LISTED) {
    $forms_to_ignore = array_merge($user_listed_forms, $disallow);
    $forms_visibility[$form_id] = !in_array($form_id, $forms_to_ignore);
  }
  else {
    if ($filter_method == DOMAIN_SETTINGS_SHOW_ONLY_LISTED) {
      $forms_to_authorize = array_diff($user_listed_forms, $disallow);
      $forms_visibility[$form_id] = in_array($form_id, $forms_to_authorize);
    }
  }
  return $forms_visibility[$form_id];
}

/**
 * Implements hook_domain_warning_alter().
 */
function domain_settings_domain_warning_alter(&$forms) {

  // Forms which Domain Settings handles and are set as warnings.
  $core_forms = array(
    'system_admin_theme_settings',
    'system_site_information_settings',
    'system_site_maintenance_settings',
    'menu_configure',
  );
  foreach ($core_forms as $form_id) {
    if (isset($forms[$form_id])) {
      unset($forms[$form_id]);
    }
  }
}

/**
 * Handle the erasure of Domain Conf settings properly.
 */
function domain_settings_form_domain_conf_reset_form_alter(&$form, &$form_state) {
  $form['#submit'] = array(
    'domain_settings_reset_form_submit',
  );
  $form['domain_settings_erase'] = array(
    '#type' => 'checkbox',
    '#title' => t('Retain values from Domain Settings module.'),
    '#default_value' => 1,
    '#weight' => 1,
  );
  $form['actions']['#weight'] = 2;
}

/**
 * Form submit handler to batch reset custom settings.
 *
 * @see domain_conf_reset_form()
 */
function domain_settings_reset_form_submit($form, &$form_state) {
  if (empty($form_state['values']['domain_settings_erase'])) {
    domain_conf_reset_form_submit($form, $form_state);
    return;
  }
  drupal_set_message(t('Domain configuration settings have been reset. Custom module settings have been retained.'));
  $form_state['redirect'] = 'admin/structure/domain/conf/' . $form_state['values']['domain_id'];
  domain_settings_reset($form_state['values']['domain_id']);

  // Clear the cache.
  cache_clear_all();
}

/**
 * Handle the batch erasure of Domain Conf settings properly.
 */
function domain_settings_form_domain_batch_form_alter(&$form, &$form_state) {
  if (!isset($form['variable']['#value']) || $form['variable']['#value'] != 'domain_conf') {
    return;
  }
  $form['#submit'] = array(
    'domain_settings_batch_form_submit',
  );
  $form['domain_settings_erase'] = array(
    '#type' => 'checkbox',
    '#title' => t('Retain values from Domain Settings module.'),
    '#default_value' => 1,
    '#weight' => 1,
  );
  $form['submit']['#weight'] = 2;
}

/**
 * Submit handler for batch domain settings.
 */
function domain_settings_batch_form_submit($form, &$form_state) {
  if (empty($form_state['values']['domain_settings_erase'])) {
    domain_batch_form_submit($form, $form_state);
    return;
  }
  foreach (array_filter($form_state['values']['domain_batch']) as $domain_id => $value) {
    domain_settings_reset($domain_id);
  }
  drupal_set_message(t('Domain configuration settings have been reset. Custom module settings have been retained.'));

  // Clear the cache.
  cache_clear_all();
}

/**
 * Erase Domain Conf and Batch settings but keep those set by this module.
 *
 * @param $domain_id
 *   The domain_id to reset.
 */
function domain_settings_reset($domain_id) {

  // Get the conf elements and erase them from the record.
  $batch = module_invoke_all('domain_batch');
  $conf = module_invoke_all('domain_conf');
  $list = array_merge(array_keys($batch), array_keys($conf));

  // Get the existing data.
  $settings = domain_conf_data_get($domain_id, TRUE);

  // Erase the settings by key.
  foreach ($list as $item) {
    if (isset($settings[$item])) {
      unset($settings[$item]);
    }
  }

  // Write the update.
  domain_conf_data_set($domain_id, $settings, FALSE);
}

Related topics

Functions

Namesort descending Description
domain_settings_add_element Helper function to test if a form has to display the domain-specific settings, based on 'domain_settings' settings the user defined.
domain_settings_batch_form_submit Submit handler for batch domain settings.
domain_settings_domain_form Implements hook_domain_form().
domain_settings_domain_warning_alter Implements hook_domain_warning_alter().
domain_settings_form_alter Implements hook_form_alter()
domain_settings_form_domain_batch_form_alter Handle the batch erasure of Domain Conf settings properly.
domain_settings_form_domain_conf_reset_form_alter Handle the erasure of Domain Conf settings properly.
domain_settings_form_submit Submit handler for domain-specific settings.
domain_settings_permission Implements hook_permission().
domain_settings_reset Erase Domain Conf and Batch settings but keep those set by this module.
domain_settings_reset_form_submit Form submit handler to batch reset custom settings.

Constants