View source
<?php
module_load_include('inc', 'site_banner', 'site_banner_options');
function site_banner_admin_settings_form() {
$form = array();
$form = array_merge(site_banner_generate_banner_status_form_elements(variable_get('site_banner_status', FALSE), variable_get('site_banner_debug_status', FALSE), variable_get('site_banner_top_banner_active', TRUE), variable_get('site_banner_bottom_banner_active', TRUE)), site_banner_generate_banner_text_form_elements(variable_get('site_banner_text', site_banner_get_default_text())), site_banner_generate_bg_color_form_elements(variable_get('site_banner_background_color', site_banner_get_default_background_color())), site_banner_generate_text_color_form_elements(variable_get('site_banner_text_color', site_banner_get_default_text_color())));
$form['site_banner_text']['#description'] = t('The text to be displayed on the site banner. If the !context_link module is being used, this will be the default text used when no context alters the site banner text.', array(
'!context_link' => l(t('Context'), 'https://drupal.org/project/context'),
));
$bg_suffix_description_text = '<br/>' . t('If the !context_link module is being used, this will be the default background color used when no context alters the site banner background color.', array(
'!context_link' => l(t('Context'), 'https://drupal.org/project/context'),
));
$text_suffix_description_text = '<br/>' . t('If the !context_link module is being used, this will be the default text color used when no context alters the site banner text color.', array(
'!context_link' => l(t('Context'), 'https://drupal.org/project/context'),
));
$form['site_banner_background_color_form']['#description'] .= $bg_suffix_description_text;
$form['site_banner_text_color_form']['#description'] .= $text_suffix_description_text;
$form['site_banner_background_custom_color_form']['#description'] .= $bg_suffix_description_text;
$form['site_banner_text_custom_color_form']['#description'] .= $text_suffix_description_text;
$form['#submit'] = array(
'site_banner_admin_settings_form_submit',
);
return system_settings_form($form);
}
function site_banner_admin_settings_form_validate($form, &$form_state) {
site_banner_validate_background_color_code_form($form_state);
site_banner_validate_text_color_code_form($form_state);
}
function site_banner_admin_settings_form_submit($form, &$form_state) {
$banner_status = $form_state['values']['site_banner_status'];
$banner_debug_status = $form_state['values']['site_banner_debug_status'];
$top_banner_active = $form_state['values']['site_banner_top_banner_enabled'];
$bottom_banner_active = $form_state['values']['site_banner_bottom_banner_enabled'];
$background_color_code = site_banner_get_background_color_from_form($form_state);
$text_color_code = site_banner_get_text_color_from_form($form_state);
variable_set('site_banner_status', $banner_status);
variable_set('site_banner_debug_status', $banner_debug_status);
variable_set('site_banner_top_banner_active', $top_banner_active);
variable_set('site_banner_bottom_banner_active', $bottom_banner_active);
variable_set('site_banner_background_color', $background_color_code);
variable_set('site_banner_text_color', $text_color_code);
}
function site_banner_generate_banner_text_form_elements($default_text) {
$form = array();
$form['site_banner_text'] = array(
'#title' => t('Banner text'),
'#description' => t('The text to be displayed on the site banner.'),
'#type' => 'textfield',
'#size' => 50,
'#maxlength' => 100,
'#default_value' => $default_text,
);
return $form;
}
function site_banner_generate_banner_status_form_elements($default_value_status, $default_value_debug_status, $default_value_top_banner_enabled, $default_value_bottom_banner_enabled) {
$form = array();
$form['site_banner_status'] = array(
'#title' => t('Banner active'),
'#description' => t('Whether the site banner is active.'),
'#type' => 'checkbox',
'#default_value' => $default_value_status,
);
$form['site_banner_debug_status'] = array(
'#title' => t('Banner debug mode active'),
'#description' => t('Whether the debug mode is active: in this mode hyperlinks to administration pages are added to banner texts with links to active contexts to users with the "Administer site banner settings" permissions.'),
'#type' => 'checkbox',
'#default_value' => $default_value_debug_status,
);
$form['site_banner_top_banner_enabled'] = array(
'#title' => t('Top banner enabled'),
'#description' => t('Whether the top site banner is visible.'),
'#type' => 'checkbox',
'#default_value' => $default_value_top_banner_enabled,
);
$form['site_banner_bottom_banner_enabled'] = array(
'#title' => t('Bottom banner enabled'),
'#description' => t('Whether the bottom site banner is visible.'),
'#type' => 'checkbox',
'#default_value' => $default_value_bottom_banner_enabled,
);
return $form;
}
function site_banner_generate_bg_color_form_elements($existing_background_color) {
$form = array();
$background_color_array = site_banner_get_background_colors();
$background_color_array_names = array_values($background_color_array);
$selected_background_index = 1;
$using_custom_background_color = TRUE;
if (in_array($existing_background_color, array_keys($background_color_array), TRUE)) {
$using_custom_background_color = FALSE;
$background_color_to_search = $background_color_array[$existing_background_color];
$selected_background_index = array_search($background_color_to_search, $background_color_array_names, TRUE);
}
$form['site_banner_background_color_select'] = array(
'#type' => 'checkbox',
'#title' => t('Select a custom background color'),
'#description' => t('Checking this box will allow you to define a custom background color for the site banner.'),
'#default_value' => $using_custom_background_color,
);
$form['site_banner_background_color_form'] = array(
'#title' => t('Banner background color'),
'#description' => t('The background color for the site banner.'),
'#type' => 'select',
'#options' => $background_color_array_names,
'#default_value' => $selected_background_index,
'#required' => FALSE,
'#states' => array(
'visible' => array(
':input[name="site_banner_background_color_select"]' => array(
'checked' => FALSE,
),
),
),
);
$form['site_banner_background_custom_color_form'] = array(
'#title' => t('Custom background color'),
'#description' => t("A custom color code in hexadecimal format (for example #000000 for black) for the banner's custom background color."),
'#type' => 'textfield',
'#default_value' => $existing_background_color,
'#size' => 7,
'#maxlength' => 7,
'#required' => FALSE,
'#states' => array(
'visible' => array(
':input[name="site_banner_background_color_select"]' => array(
'checked' => TRUE,
),
),
),
);
return $form;
}
function site_banner_generate_text_color_form_elements($existing_text_color) {
$text_color_array = site_banner_get_text_colors();
$text_color_array_names = array_values($text_color_array);
$selected_text_index = 1;
$using_custom_text_color = TRUE;
if (in_array($existing_text_color, array_keys($text_color_array), TRUE)) {
$using_custom_text_color = FALSE;
$text_color_to_search = $text_color_array[$existing_text_color];
$selected_text_index = array_search($text_color_to_search, $text_color_array_names, TRUE);
}
$form['site_banner_text_color_select'] = array(
'#type' => 'checkbox',
'#title' => t('Select a custom text color'),
'#description' => t('Checking this box will allow you to define a custom text color for the site banner.'),
'#default_value' => $using_custom_text_color,
);
$form['site_banner_text_color_form'] = array(
'#title' => t('Banner text color'),
'#description' => t('The site banner text color.'),
'#type' => 'select',
'#options' => $text_color_array_names,
'#default_value' => $selected_text_index,
'#required' => FALSE,
'#states' => array(
'visible' => array(
':input[name="site_banner_text_color_select"]' => array(
'checked' => FALSE,
),
),
),
);
$form['site_banner_text_custom_color_form'] = array(
'#title' => t('Custom text color'),
'#description' => t("A custom color code in hexadecimal format (for example #000000 for black) for the banner's custom text color."),
'#type' => 'textfield',
'#default_value' => $existing_text_color,
'#size' => 7,
'#maxlength' => 7,
'#required' => FALSE,
'#states' => array(
'visible' => array(
':input[name="site_banner_text_color_select"]' => array(
'checked' => TRUE,
),
),
),
);
return $form;
}
function site_banner_get_background_color_from_form($form_state) {
return site_banner_get_background_color_code($form_state['values']['site_banner_background_color_form'], $form_state['values']['site_banner_background_custom_color_form'], $form_state['values']['site_banner_background_color_select']);
}
function site_banner_get_text_color_from_form($form_state) {
return site_banner_get_text_color_code($form_state['values']['site_banner_text_color_form'], $form_state['values']['site_banner_text_custom_color_form'], $form_state['values']['site_banner_text_color_select']);
}
function site_banner_validate_background_color_code_form($form_state) {
$is_custom_background_color = $form_state['values']['site_banner_background_color_select'];
$background_color_code_as_text = $form_state['values']['site_banner_background_custom_color_form'];
if ($is_custom_background_color) {
if (preg_match(site_banner_get_html_color_regex_pattern(), $background_color_code_as_text) != 1) {
form_set_error('site_banner_background_custom_color_form', t('Please enter a valid hexadecimal color code for the background color (for example #ff00ff) or select an existing color.'));
}
}
}
function site_banner_validate_text_color_code_form($form_state) {
$is_custom_text_color = $form_state['values']['site_banner_text_color_select'];
$text_color_code_as_text = $form_state['values']['site_banner_text_custom_color_form'];
if ($is_custom_text_color) {
if (preg_match(site_banner_get_html_color_regex_pattern(), $text_color_code_as_text) != 1) {
form_set_error('site_banner_text_custom_color_form', t('Please enter a valid hexadecimal color code for the text color (for example #ff00ff) or select an existing color.'));
}
}
}