? 303406-theme-settings.patch
? 333.patch
Index: domain_theme.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain_theme/domain_theme.admin.inc,v
retrieving revision 1.2
diff -u -p -r1.2 domain_theme.admin.inc
--- domain_theme.admin.inc	25 Mar 2009 17:46:34 -0000	1.2
+++ domain_theme.admin.inc	1 May 2009 20:21:38 -0000
@@ -52,14 +52,23 @@ function domain_theme_form_alter(&$form,
     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 use the default
+        // If the theme was disabled, then we have to set a new default
         if ($key == $theme['theme']) {
-          $form['theme_default']['#default_value'] = variable_get('theme_default', 'garland');
+          $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]);
@@ -68,6 +77,12 @@ function domain_theme_form_alter(&$form,
       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');
@@ -76,7 +91,7 @@ function domain_theme_form_alter(&$form,
     }
     // Message to users.
     $form['intro'] = array(
-      '#value' => t('<p>Select the default theme for this domain. Theme-specific settings must be configured at <a href="!url">the system theme settings page</a>.</p>', array('!url' => url('admin/build/themes'))),
+      '#value' => t('<p>Select the default theme for this domain.</p>'),
     );
     // Which domain are we editing?
     $form['domain_id'] = array(
@@ -90,6 +105,55 @@ function domain_theme_form_alter(&$form,
       '#value' => t('Set domain theme'),
     );
   }
+
+  if ($form_id == 'system_theme_settings') {
+    $domain_id = arg(5);
+    $theme = arg(4);
+    $themes = list_themes();
+
+    $domain = domain_load($domain_id);
+    if ($domain == -1 || !array_key_exists($theme, $themes)) {
+      return drupal_access_denied();
+    }
+    drupal_set_title(t('!site : %theme settings', array('!site' => $domain['sitename'], '%theme' => $themes[$theme]->info['name'])));
+    // 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'][] = 'domain_theme_settings_submit';
+    foreach ($form['#submit'] as $key => $value) {
+      if ($value == 'system_theme_settings_submit') {
+        unset($form['#submit'][$key]);
+      }
+    }
+    // Check for the presence of color.module.
+    $info = color_get_info($theme);
+    if (empty($info)) {
+      return;
+    }
+    // We have to fool Color module to not delete files.
+    global $conf;
+    $conf['color_'. $theme .'_files'] = array();
+    $form['domain_color'] = array('#type' => 'value', '#value' => TRUE);
+    // 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');
+    $color_settings = array();
+    foreach ($vars as $variable) {
+      $name = 'color_'. $theme .'_'. $variable;
+      $color_settings[$name] = db_result(db_query("SELECT value FROM {variable} WHERE name = '%s'", $name));
+    }
+    $form['domain_color_defaults'] = array('#type' => 'value', '#value' => $color_settings);
+  }
 }
 
 /**
@@ -97,17 +161,18 @@ function domain_theme_form_alter(&$form,
  */
 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 +230,66 @@ 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));
+
+  // 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'];
+
+  if (isset($values['domain_color'])) {
+    $vars = array('palette', 'stylesheets', 'logo', 'files', 'screenshot');
+    foreach ($vars as $variable) {
+      $values['color_'. $values['theme'] .'_'. $variable] = variable_get('color_'. $values['theme'] .'_'. $variable, '');
+    }
+    foreach ($values['domain_color_defaults'] as $key => $value) {
+      db_query("UPDATE {variable} SET value = '%s' WHERE name = '%s'", $value, $key);
+    }
+  }
+
+  $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_query("UPDATE {domain_theme} SET settings = 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 WHERE domain_id = %d AND theme = '%s'", $settings, $domain_id, $theme);
+    }
+    else {
+      db_query("INSERT INTO {domain_theme} (domain_id, theme, settings, status) VALUES (%d, '%s', %b, 0)", $domain_id, $theme, $settings);
+    }
+    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.4
diff -u -p -r1.4 domain_theme.install
--- domain_theme.install	30 Mar 2008 17:51:47 -0000	1.4
+++ domain_theme.install	1 May 2009 20:21:38 -0000
@@ -21,8 +21,9 @@ 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)),
+    'primary key' => array('domain_id', 'theme'),
   );
   return $schema;
 }
@@ -34,3 +35,15 @@ 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));
+  $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.13
diff -u -p -r1.13 domain_theme.module
--- domain_theme.module	25 Mar 2009 17:46:34 -0000	1.13
+++ domain_theme.module	1 May 2009 20:21:38 -0000
@@ -22,14 +22,36 @@
 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');
+     foreach ($vars as $variable) {
+      $name = 'color_'. $theme['theme'] .'_'. $variable;
+      if (isset($settings[$name])) {
+        $conf[$name] = $settings[$name];
+       }
+    }
   }
 }
 
@@ -55,6 +77,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 +104,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;
