View source
<?php
define('DOMAIN_SETTINGS_SHOW_EXCEPT_LISTED', 0);
define('DOMAIN_SETTINGS_SHOW_ONLY_LISTED', 1);
define('DOMAIN_SETTINGS_ALL_DOMAINS', -5);
function domain_settings_permission() {
$permissions = array(
'access domain settings form' => array(
'title' => t('Access domain settings form'),
'restrict access' => TRUE,
),
);
return $permissions;
}
function domain_settings_form_alter(&$form, $form_state, $form_id) {
$_domain = domain_get_domain();
if (!user_access('access domain settings form') || !isset($form['#submit']) || !in_array('system_settings_form_submit', $form['#submit']) || !domain_settings_add_element($form_id)) {
return;
}
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'),
);
$format = domain_select_format();
foreach (domain_domains() as $data) {
$data['domain_id'] == 0 ? $key = -1 : ($key = $data['domain_id']);
if ($data['valid'] || user_access('access inactive domains')) {
$options[$key] = 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,
);
$behavior = variable_get('domain_settings_behavior', 0);
$default = -1;
if ($behavior == 1) {
$default = $_domain['domain_id'] == 0 ? -1 : $_domain['domain_id'];
}
if ($behavior == 2) {
$default = DOMAIN_SETTINGS_ALL_DOMAINS;
}
$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]);
}
}
}
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;
}
}
foreach ($values as $name => $value) {
if (is_array($value) && isset($form_state['values']['array_filter'])) {
$value = array_keys(array_filter($value));
}
if ($domain_id == -1 || $domain_id == DOMAIN_SETTINGS_ALL_DOMAINS) {
variable_set($name, $value);
if ($name == 'site_name') {
variable_set('domain_sitename', $value);
db_update('domain')
->condition('domain_id', 0)
->fields(array(
'sitename' => $form_state['values']['site_name'],
))
->execute();
}
if ($domain_id == DOMAIN_SETTINGS_ALL_DOMAINS) {
foreach (domain_domains() as $id => $value) {
domain_conf_variable_delete($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();
}
}
}
$extra = '';
if ($domain_id > 0) {
$domain = domain_lookup($domain_id);
$affected = $domain['sitename'];
}
elseif ($domain_id == -1) {
$default = domain_default();
$affected = $default['sitename'];
$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();
}
function domain_settings_domainform(&$form) {
$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.'),
);
}
function domain_settings_add_element($form_id) {
static $forms_visibility;
if (isset($forms_visibility[$form_id])) {
}
$disallow = array(
'domain_configure_form',
'system_file_system_settings',
'system_performance_settings',
);
$filter_method = variable_get('domain_settings_form_visibility', DOMAIN_SETTINGS_SHOW_EXCEPT_LISTED);
$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 ($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];
}
function domain_settings_domain_warnings_alter(&$forms) {
$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]);
}
}
}
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;
}
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']);
cache_clear_all();
}
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;
}
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.'));
cache_clear_all();
}
function domain_settings_reset($domain_id) {
$batch = module_invoke_all('domainbatch');
$conf = module_invoke_all('domainconf');
$list = array_merge(array_keys($batch), array_keys($conf));
$result = db_query("SELECT domain_id, settings FROM {domain_conf} WHERE domain_id = :domain_id", array(
':domain_id' => $domain_id,
))
->fetchObject();
if (!empty($result->settings)) {
return;
}
$settings = domain_unserialize($result->settings);
foreach ($list as $item) {
if (isset($settings[$item])) {
unset($settings[$item]);
}
}
db_update('domain_conf')
->fields(array(
'settings' => serialize($settings),
))
->condition('domain_id', $result->domain_id)
->execute();
}