? 303406-theme-settings_6.patch Index: domain_theme.admin.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain_theme/domain_theme.admin.inc,v retrieving revision 1.3 diff -u -p -r1.3 domain_theme.admin.inc --- domain_theme.admin.inc 31 May 2009 18:16:41 -0000 1.3 +++ domain_theme.admin.inc 31 May 2009 20:35:16 -0000 @@ -38,76 +38,154 @@ function domain_theme_page($domain) { } /** - * Implement hook_form_alter() + * Implement hook_form_alter(). + * + * Since we have this in an include file, it is only called + * by our module's invocation of this form. */ -function domain_theme_form_alter(&$form, &$form_state, $form_id) { - if ($form_id == 'system_themes_form') { - $domain_id = arg(4); - $domain = domain_load($domain_id); - if ($domain == -1) { - return drupal_access_denied(); - } - // Get the current $theme for this domain, if available. - $theme = domain_theme_lookup($domain['domain_id']); - if ($theme['theme']) { - $form['theme_default']['#default_value'] = $theme['theme']; - } - // Unset options that are not allowed. - $available = $form['status']['#options']; - $allowed = $form['status']['#default_value']; - foreach ($available as $key => $value) { - if (!in_array($key, $allowed)) { - // If the theme was disabled, then we have to use the default - if ($key == $theme['theme']) { - $form['theme_default']['#default_value'] = variable_get('theme_default', 'garland'); +function domain_theme_form_system_themes_form_alter(&$form, &$form_state) { + $domain_id = arg(4); + $domain = domain_load($domain_id); + if ($domain == -1) { + return drupal_access_denied(); + } + // Get the current $theme for this domain, if available. + $theme = domain_theme_lookup($domain['domain_id']); + if ($theme['theme']) { + $form['theme_default']['#default_value'] = $theme['theme']; + } + else { + $form['theme_default']['#default_value'] = ''; + if (empty($_POST)) { + drupal_set_message(t('No theme has been set for this domain.')); + } + } + // Unset options that are not allowed. + $available = $form['status']['#options']; + $allowed = $form['status']['#default_value']; + foreach ($available as $key => $value) { + if (!in_array($key, $allowed)) { + // If the theme was disabled, then we have to set a new default + if ($key == $theme['theme']) { + $form['theme_default']['#default_value'] = ''; + if (empty($_POST)) { + drupal_set_message(t('The chosen theme is no longer available for this domain.'), 'status', FALSE); } - unset($form[$key]); - unset($form['status']['#options'][$key]); - unset($form['theme_default']['#options'][$key]); } - else { - $form['status']['#disabled'] = TRUE; - } - } - // Use our own submit buttons. - $unset = array('buttons', '#submit'); - foreach ($unset as $key) { unset($form[$key]); + unset($form['status']['#options'][$key]); + unset($form['theme_default']['#options'][$key]); + } + else { + $form['status']['#disabled'] = TRUE; + } + + if($form[$key] && $form[$key]['operations']) { + $form[$key]['operations']= array( + '#value' => l(t('configure'), 'admin/build/domain/theme/'. $key .'/'. $domain['domain_id'] .'/theme-settings'), + ); + } + } + // Use our own submit buttons. + $unset = array('buttons', '#submit'); + foreach ($unset as $key) { + unset($form[$key]); + } + // Message to users. + $form['intro'] = array( + '#value' => t('
Select the default theme for this domain.
'), + ); + // Which domain are we editing? + $form['domain_id'] = array( + '#type' => 'value', + '#value' => $domain['domain_id'], + ); + // Our submit handlers. + $form['#submit'][] = 'domain_theme_submit'; + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Set domain theme'), + ); +} + +/** + * Implement hook_form_alter(). + * + * This function is a helper to a normal hook_form_alter implementation, + * where we add additonal form elemtns if we are dealing with domain-specific + * form settings. + */ +function domain_theme_form_alter(&$form, &$form_state, $form_id) { + // We cannot use a named form_alter here because of color module. + // Color submit must happen first, and a named function destroys that. + if ($form_id != 'system_theme_settings') { + return; + } + $theme = arg(4); + $domain_id = arg(5); + $themes = list_themes(); + + $domain = domain_load($domain_id); + if ($domain == -1 || !array_key_exists($theme, $themes)) { + return drupal_access_denied(); + } + + $color_info = color_get_info($theme); + + drupal_set_title(t('!site : %theme settings', array('!site' => $domain['sitename'], '%theme' => $theme))); + // Which domain are we editing? + $form['domain_id'] = array( + '#type' => 'value', + '#value' => $domain['domain_id'], + ); + $form['theme'] = array( + '#type' => 'value', + '#value' => $theme, + ); + + // We have to remove the core submit handler, but keep other handlers. + $form['#submit'][100] = 'domain_theme_settings_submit'; + foreach ($form['#submit'] as $key => $value) { + if ($value == 'system_theme_settings_submit') { + unset($form['#submit'][$key]); } - // Message to users. - $form['intro'] = array( - '#value' => t('Select the default theme for this domain. Theme-specific settings must be configured at the system theme settings page.
', array('!url' => url('admin/build/themes'))), - ); - // Which domain are we editing? - $form['domain_id'] = array( - '#type' => 'value', - '#value' => $domain['domain_id'], - ); - // Our submit handlers. - $form['#submit'][] = 'domain_theme_submit'; - $form['submit'] = array( - '#type' => 'submit', - '#value' => t('Set domain theme'), - ); } + ksort($form['#submit']); + // Check for the presence of color.module. + if (empty($color_info)) { + return; + } + + // Color module will reset the values in {variable}. which we don't + // want to happen. So we have to grab the existing values and store + // them so that we can set the {variable} table correctly. + // TODO: Make this work with Domain Prefix. + $vars = array('palette', 'stylesheets', 'logo', 'files', 'screenshot'); + foreach ($vars as $variable) { + $name = 'color_'. $theme .'_'. $variable; + $value = db_result(db_query("SELECT value FROM {variable} WHERE name = '%s'", $name)); + $color_settings[$name] = isset($value) ? $value : NULL; + } + $form['domain_color_defaults'] = array('#type' => 'value', '#value' => $color_settings); } /** - * FormsAPI submut handler for the theme settings + * FormsAPI submit handler for the theme settings */ function domain_theme_submit($form, &$form_state) { $theme = $form_state['values']['theme_default']; - $id = $form_state['values']['domain_id']; + $domain_id = $form_state['values']['domain_id']; $settings = NULL; // This is a placeholder for advanced functions. - $check = domain_theme_lookup($id); + db_query("UPDATE {domain_theme} SET status = 0 WHERE domain_id = %d", $domain_id); + $check = domain_theme_lookup($domain_id, $theme); if ($check == -1) { - db_query("INSERT INTO {domain_theme} (domain_id, theme, settings) VALUES (%d, '%s', %b)", $id, $theme, $settings); + db_query("INSERT INTO {domain_theme} (domain_id, theme, settings, status) VALUES (%d, '%s', %b, 1)", $domain_id, $theme, $settings); } else { - db_query("UPDATE {domain_theme} SET theme = '%s', settings = %b WHERE domain_id = %d", $theme, $settings, $id); + db_query("UPDATE {domain_theme} SET status = 1 WHERE domain_id = %d AND theme = '%s'", $domain_id, $theme); } // Return to the correct page. - $form_state['redirect'] = 'admin/build/domain/theme/'. $id; + $form_state['redirect'] = 'admin/build/domain/theme/'. $domain_id; } /** @@ -165,3 +243,99 @@ function theme_domain_theme_reset($domai } return $output; } + +/** + * The domain theme page callback router. + * + * @param $theme + * The theme being configured. + * @param $domain + * The $domain object created by domain_lookup(). + */ +function domain_theme_settings($theme, $domain) { + // Load the system form file. + include_once(drupal_get_path('module', 'system') . '/system.admin.inc'); + + $theme = db_fetch_array(db_query("SELECT theme, settings FROM {domain_theme} WHERE domain_id = %d AND theme = '%s'", $domain['domain_id'], $theme)); + + // Write uploads to the proper directory? + if ($domain['domain_id'] > 0) { + global $conf; + $conf['file_directory_path'] .= '/domain-'. $domain['domain_id']; + } + + // If there are settings, we have to load ours. + if (!empty($theme)) { + domain_theme_set_variables($theme); + return drupal_get_form('system_theme_settings', $theme['theme']); + } + else { + return drupal_get_form('system_theme_settings'); + } +} + +/** + * Process domain_theme_settings form submissions. + */ +function domain_theme_settings_submit($form, &$form_state) { + $values = $form_state['values']; + $filepath = NULL; + $reset = FALSE; + if ($values['op'] == $values['reset']) { + db_query("DELETE FROM {domain_theme} WHERE domain_id = %d AND theme = '%s'", $values['domain_id'], $values['theme']); + $domain = domain_lookup($values['domain_id']); + drupal_set_message(t('The %theme settings for %domain have been reset.', array('%theme' => $values['theme'], '%domain' => $domain['sitename']))); + $reset = TRUE; + } + + $vars = array('palette', 'stylesheets', 'logo', 'files', 'screenshot'); + foreach ($vars as $variable) { + $preset = variable_get('color_'. $values['theme'] .'_'. $variable, ''); + if (!empty($preset)) { + $values['color_'. $values['theme'] .'_'. $variable] = $preset; + } + } + // If our domain uses different schemes, we have to ensure that the {variable} table stays accurate + // for the primary domain. + if (isset($values['domain_color_defaults'])) { + foreach ($values['domain_color_defaults'] as $key => $value) { + if (!empty($value)) { + variable_set($key, unserialize($value)); + } + else { + variable_del($key); + } + } + } + // Set the filepath for color module. + if (!empty($values['color_'. $values['theme'] .'_stylesheets'][0])) { + $filepath = domain_theme_get_color_path($values['color_'. $values['theme'] .'_stylesheets'][0]); + } + + // If a reset, stop here. + if ($reset) { + return; + } + + $key = $values['var']; + $domain_id = $values['domain_id']; + $theme = $values['theme']; + // If no record exists, we behave differently. + $check = db_result(db_query("SELECT COUNT(*) FROM {domain_theme} WHERE domain_id = %d AND theme = '%s'", $domain_id, $theme)); + if ($values['op'] == $values['reset'] && $check > 0) { + db_queryd("UPDATE {domain_theme} SET settings = NULL, filepath = NULL WHERE domain_id = %d AND theme = '%s'", $domain_id, $theme); + drupal_set_message(t('The configuration options have been reset to their default values.')); + } + else { + // Exclude unnecessary elements before saving. + unset($values['var'], $values['submit'], $values['reset'], $values['form_id'], $values['op'], $values['form_build_id'], $values['form_token'], $values['domain_id'], $values['domain_color'], $values['domain_color_defaults']); + $settings = serialize($values); + if ($check > 0) { + db_query("UPDATE {domain_theme} SET settings = %b, filepath = '%s' WHERE domain_id = %d AND theme = '%s'", $settings, $filepath, $domain_id, $theme); + } + else { + db_query("INSERT INTO {domain_theme} (domain_id, theme, settings, status, filepath) VALUES (%d, '%s', %b, 0, '%s')", $domain_id, $theme, $settings, $filepath); + } + drupal_set_message(t('The configuration options have been saved.')); + } +} Index: domain_theme.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain_theme/domain_theme.install,v retrieving revision 1.5 diff -u -p -r1.5 domain_theme.install --- domain_theme.install 31 May 2009 18:16:41 -0000 1.5 +++ domain_theme.install 31 May 2009 20:35:16 -0000 @@ -21,8 +21,10 @@ function domain_theme_schema() { 'fields' => array( 'domain_id' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), 'theme' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''), - 'settings' => array('type' => 'blob', 'not null' => FALSE)), - 'primary key' => array('domain_id'), + 'settings' => array('type' => 'blob', 'not null' => FALSE), + 'status' => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0), + 'filepath' => array('type' => 'varchar', 'length' => '255', 'not null' => FALSE)), + 'primary key' => array('domain_id', 'theme'), ); return $schema; } @@ -34,3 +36,16 @@ function domain_theme_uninstall() { drupal_uninstall_schema('domain_theme'); variable_del('domain_theme_weight'); } + +/** + * Update the table structure + */ +function domain_theme_update_6200() { + $ret = array(); + db_drop_primary_key(&$ret, 'domain_theme'); + db_add_primary_key(&$ret, 'domain_theme', array('domain_id', 'theme')); + db_add_field(&$ret, 'domain_theme', 'status', array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0)); + db_add_field(&$ret, 'domain_theme', 'filepath', array('type' => 'varchar', 'length' => '255', 'not null' => FALSE)); + $ret[] = update_sql("UPDATE {domain_theme} SET status = 1"); + return $ret; +} Index: domain_theme.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain_theme/domain_theme.module,v retrieving revision 1.14 diff -u -p -r1.14 domain_theme.module --- domain_theme.module 31 May 2009 18:16:41 -0000 1.14 +++ domain_theme.module 31 May 2009 20:35:17 -0000 @@ -22,14 +22,47 @@ function domain_theme_init() { // Assign the theme selected, based on the active domain. global $_domain, $custom_theme; - // If the theme has already been set (such as an admin theme), ignore. - if (!empty($custom_theme)) { - return; + // If the theme has already been set (such as an admin theme), only load settings. + if (empty($custom_theme)) { + $theme = domain_theme_lookup($_domain['domain_id']); + } + else { + $theme = domain_theme_lookup($_domain['domain_id'], $custom_theme); } - $theme = domain_theme_lookup($_domain['domain_id']); // The above returns -1 on failure. if ($theme != -1) { - $custom_theme = $theme['theme']; + // Set our domain-specific theme. + if (empty($custom_theme)) { + $custom_theme = $theme['theme']; + } + domain_theme_set_variables($theme); + } +} + +function domain_theme_set_variables($theme) { + global $conf; + if (!empty($theme['settings'])) { + $settings = unserialize($theme['settings']); + $conf['theme_' . $theme['theme'] . '_settings'] = $settings; + // Account for color module. + $vars = array('palette', 'stylesheets', 'logo', 'files', 'screenshot'); + // In some cases, where the domain uses the default color palette + // and the primary theme does not, we may only have the palette + // stored, in which case, we have to load that data and ignore the rest. + $palette_var = 'color_'. $theme['theme'] .'_palette'; + if (!isset($settings[$palette_var])) { + if (isset($settings['palette'])) { + $conf[$palette_var] = $settings['palette']; + } + } + else { + foreach ($vars as $variable) { + $name = 'color_'. $theme['theme'] .'_'. $variable; + if (isset($settings[$name])) { + $conf[$name] = $settings[$name]; + } + } + } } } @@ -55,6 +88,14 @@ function domain_theme_menu() { 'page arguments' => array(4), 'file' => 'domain_theme.admin.inc', ); + $items['admin/build/domain/theme/%/%domain/theme-settings'] = array( + 'title' => 'Configure domain theme settings', + 'type' => MENU_CALLBACK, + 'access arguments' => array('administer domains'), + 'page callback' => 'domain_theme_settings', + 'page arguments' => array(4, 5), + 'file' => 'domain_theme.admin.inc', + ); return $items; } @@ -74,21 +115,22 @@ function domain_theme_theme() { * Get domain theme information * * @param $domain_id - * The domain_id taken from {domain}. Optional. + * The domain_id taken from {domain}. * @param $theme * The string representation of a {domain_theme} entry. Optional. - * @param $clear + * If this value is NULL, the default theme for this domain will be returned. + * @param $reset * A boolean flag to clear the static variable if necessary. Not used. Here for consistency. * @return * An array containing the requested row from the {domain_theme} table. * Returns -1 on failure. */ -function domain_theme_lookup($domain_id = NULL, $theme = NULL, $clear = FALSE) { - if (!is_null($domain_id)) { - $return = db_fetch_array(db_query("SELECT domain_id, theme, settings FROM {domain_theme} WHERE domain_id = %d", $domain_id)); +function domain_theme_lookup($domain_id, $theme = NULL, $reset = FALSE) { + if (!is_null($theme)) { + $return = db_fetch_array(db_query("SELECT domain_id, theme, settings, status FROM {domain_theme} WHERE domain_id = %d AND theme= '%s'", $domain_id, $theme)); } - else if (!is_null($theme)) { - $return = db_fetch_array(db_query("SELECT domain_id, theme, settings FROM {domain_theme} WHERE theme= '%s'", $theme)); + else { + $return = db_fetch_array(db_query("SELECT domain_id, theme, settings, status FROM {domain_theme} WHERE domain_id = %d AND status = 1", $domain_id)); } if (empty($return)) { $return = -1; @@ -215,3 +257,16 @@ function domain_theme_domainwarnings() { 'system_theme_settings', ); } + +/** + * Return the unique string path element used by color.module. + * + * @param $path + * A path to a color module file, such as 'default/files/garland-00123451/style.css'. + * @ return + * A string indicating the color module's filepath. + */ +function domain_theme_get_color_path($path) { + return current(array_slice(array_reverse(explode('/', $path)), 1, 1)); +} +