Index: multidomain.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/multidomain/multidomain.info,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 multidomain.info
--- multidomain.info	7 Mar 2007 23:20:58 -0000	1.1.2.1
+++ multidomain.info	20 Apr 2007 17:18:50 -0000
@@ -1,4 +1,4 @@
 ; $Id: multidomain.info,v 1.1.2.1 2007/03/07 23:20:58 robroy Exp $
 name = Multiple domains 
 description = Allows site administrator to configure which domains content appears on.
-dependencies = singlesignon
+dependencies = singlesignon taxonomy
Index: multidomain.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/multidomain/multidomain.module,v
retrieving revision 1.1.2.4
diff -u -r1.1.2.4 multidomain.module
--- multidomain.module	7 Mar 2007 23:20:58 -0000	1.1.2.4
+++ multidomain.module	21 Apr 2007 00:49:12 -0000
@@ -107,7 +107,56 @@
     '#description' => t("Always use this domain if no matching domains were found? This option is recommended, but it might be necessary to disable this for certain configurations of shared tables.")
   );
 
-  return system_settings_form($form);
+  $vid = variable_get('multidomain_vocabulary', '');
+  $vocabulary = taxonomy_get_vocabulary($vid);
+
+  $form['multidomain_nodes'] = array('#type' => 'checkboxes',
+    '#title' => t('Domain vocabulary node types'),
+    '#default_value' => $vocabulary->nodes,
+    '#options' => node_get_types('names'),
+    '#description' => t('A list of node types you want to be associated with domains.'),
+    '#required' => TRUE,
+  );
+
+  $form = system_settings_form($form);
+  // Don't use the default form handling.
+  unset($form['#base']);
+  return $form;
+}
+
+/**
+ * Form submit callback. Save additional data not saved by default.
+ */
+function multidomain_default_domain_settings_submit($form_id, $form_values) {
+  $op = isset($form_values['op']) ? $form_values['op'] : '';
+  if ($op != t('Reset to defaults')) {
+    $vid = variable_get('multidomain_vocabulary', '');
+    $vocabulary = (array) taxonomy_get_vocabulary($vid);
+    $vocabulary['nodes'] = array_filter($form_values['multidomain_nodes']);
+    taxonomy_save_vocabulary($vocabulary);
+    // Create or update a term associated with this domain.
+    if ($form_values['singlesignon_master_url'] != variable_get('singlesignon_master_url', 'http://')) {     
+      if ($tid = variable_get('multidomain_master_domain_tid', 0)) {
+        $term = (array) taxonomy_get_term($tid);
+      }
+      else {
+        $term = array(
+          'description' => '',
+          'vid' => $vid,
+          // Set a low weigth as this is the primary domain.
+          'weight' => -10,
+        );
+      }
+      // In either case, set the new name.
+      $term['name'] = $form_values['singlesignon_master_url'];
+      taxonomy_save_term($term);
+      variable_set('multidomain_master_domain_tid', $term['tid']);
+    }
+  }
+  // We don't want this saved as a variable.
+  unset($form_values['multidomain_nodes']);
+  // Save the remaining settings.
+  system_settings_form_submit($form_id, $form_values);
 }
 
 function multidomain_domain_delete($url) {
@@ -116,6 +165,11 @@
   return confirm_form($form, t('Do you want to delete the @domain domain?', array('@domain' => $url)), 'admin/settings/multidomain');
 }
 
+/**
+ * Menu callback. Delete a domain.
+ *
+ * %todo Delete the taxonomy term associated with this domain?
+ */
 function multidomain_domain_delete_submit($form_id, $values) {
   $domains = variable_get('multidomain_sites', array());
   unset($domains[$values['url']]);
@@ -133,6 +187,7 @@
   $edit = $domains[$url];
 
   $form = array();
+  $form['tid'] = array('#type' => 'value', '#value' => isset($edit['tid']) ? $edit['tid'] : '');
   $form['url'] = array('#type' => 'hidden', '#value' => $url);
   $form['domain'] = array(
     '#type' => 'textfield',
@@ -205,16 +260,27 @@
 
 function multidomain_domain_settings_validate($form_id, $form_values) {
   $domains = variable_get('multidomain_sites', array());
-  if (isset($domains[$form_values['url']]) && ($form_values['url'] != $form_values['domain'])) {
+  if (isset($domains[$form_values['domain']]) && ($form_values['url'] != $form_values['domain'])) {
     form_set_error('domain', t('This domain has already been created.'));
   }
 }
 
 function multidomain_domain_settings_submit($form_id, $form_values) {
   $domains = variable_get('multidomain_sites', array());
+  // If domain or weight has changed, update the term tied to this domain.
+  if (($form_values['domain'] != $form_values['url']) || ($form_values['weight'] != $domains[$form_values['url']]['weight'])) {
+    // Load the existing term.
+    $term = (array) taxonomy_get_term($form_values['tid']);
+    $term['name'] = $form_values['domain'];
+    $term['weight'] = $form_values['weight'];
+    taxonomy_save_term($term);
+    drupal_set_message(t('Domain term updated.'));
+  }
+  // If the domain has been changed, unset the previous data.
   if ($form_values['domain'] != $form_values['url']) {
     unset($domains[$form_values['url']]);
   }
+
   $domains[$form_values['domain']] = $form_values;
   uasort($domains, '_multidomain_domain_sort');
   variable_set('multidomain_sites', $domains);
@@ -275,7 +341,15 @@
 
 function multidomain_settings_submit($form_id, $form_values) {
   $domains = variable_get('multidomain_sites', array());
-  $domains[$form_values['new_domain']] = array();
+  // Create a term for the new domain.
+  $term = array(
+    'name' => $form_values['new_domain'],
+    'description' => '',
+    'vid' => variable_get('multidomain_vocabulary', ''),
+    'weight' => 0,
+  );
+  taxonomy_save_term($term);
+  $domains[$form_values['new_domain']] = array('tid' => $term['tid']);
   variable_set('multidomain_sites', $domains);
 }
 
@@ -319,8 +393,35 @@
   }
 }
 
+/**
+ * Implementation of hook_db_rewrite_sql().
+ *
+ * Limit nodes returned by domain term.
+ */
+function multidomain_db_rewrite_sql($query, $primary_table, $primary_field) {
+  static $domains;
+  $return = array();
+  if ($primary_table == 'nid') {
+    if ($domains === NULL) {
+      $domains = variable_get('multidomain_sites', array());
+    }
+    $protocol = ( $_SERVER['HTTPS'] ) ? 'https://' : 'http://' ;
+    // Determine if we are on a registered domain.
+    if ($domain = $domains[$protocol . $_SERVER['HTTP_HOST']]) {
+      $return['join'] = 'INNER JOIN {term_node} tn ON tn.nid = '. $primary_table;
+      $return['where'] = 'tn.tid = '. $domain['tid'];
+    }
+  }
+  return $return;
+}
+
 if (!function_exists('custom_url_rewrite')) {
   function custom_url_rewrite($type, $alias, $real_path) {
+    global $clean_url;
+    // Cache the clean_url variable to improve performance.
+    if (!isset($clean_url)) {
+      $clean_url = (bool)variable_get('clean_url', '0');
+    }
     static $default = null;
     static $force_default = null;
     static $domains = null;
@@ -339,6 +440,13 @@
     }
     $base = $force_default ? $default : NULL;
     foreach ($domains as $domain => $info) {
+      // Match path by taxonomy.
+      // Determine if this is a node.
+      $source = $type == 'alias' ? $path : drupal_lookup_path($path);
+      $arg = explode('/', $source);
+      if ($arg[0] == 'node' && is_numeric($arg[1])) {
+        $page_match = db_num_rows(db_query('SELECT tn.* FROM {term_node} tn WHERE tn.nid = %d AND tn.tid = %d', $arg[1], $domain['tid']));
+      }
       // Match path if necessary
       if ($info['pages']) {
         if ($info['visibility'] < 2) {
@@ -375,14 +483,14 @@
     }
     if ($type == 'alias') {
       if (($protocol . $_SERVER['HTTP_HOST'] != $base) && !is_null($base) && (strpos($path, '://') === FALSE)) {
-        $path = $base .'/'. $path;
+        $path = $base .'/'. (!$clean_url ? '?q=' : '') . $path;
       }
     }
     else {
       // Permanent redirect on old pages.
       #if ($protocol . $_SERVER['HTTP_HOST'] != $base) {
       #  header("HTTP/1.1 301 Moved Permanently");
-      #  header("Location: " . $base .'/'. $path);
+      #  header("Location: " . $base .'/'. (!$clean_url ? '?q=' : '') . $path;);
       #  exit();
       #}
     }
--- multidomain.install
+++ multidomain.install
@@ -0,0 +1,48 @@
+<?php
+// $Id: $
+
+/** 
+ * Implementation of hook_install(). 
+ */
+function multidomain_install() {
+  $vid = variable_get('multidomain_vocabulary', '');
+  if (empty($vid)) {
+    // Check to see if a forum vocabulary exists
+    $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module = '%s'", 'multidomain'));
+    if (!$vid) {
+      // Create the forum vocabulary. Assign the vocabulary a low weight so
+      // it will appear first in node create and edit forms.
+      // Assign it all existing node types.
+      $nodes = array();
+      foreach (array_keys(node_get_types('names')) as $type) {
+        $nodes[$type] = 1;
+      }
+      $vocabulary = array('name' => 'Domain', 'multiple' => 0, 'required' => 1, 'hierarchy' => 1, 'relations' => 0, 'module' => 'forum', 'weight' => -10, 'nodes' => $nodes);
+      taxonomy_save_vocabulary($vocabulary);
+      drupal_set_message('A Domain vocabulary was created. To assign content to a domain, add it to this vocabulary.');
+      $vid = $vocabulary['vid'];
+    }
+    variable_set('multidomain_vocabulary', $vid);
+  } 
+}
+
+/** 
+ * Create and populate a vocabulary. 
+ */
+function multidomain_update_1() {
+  // Initialize.
+  multidomain_install();
+  // Create a term for each existing domain.
+  $vid = variable_get('multidomain_vocabulary', '');
+  $domains = variable_get('multidomain_sites', array());
+  foreach ($domains as $domain => $settings) {
+    $term = array(
+      'name' => $domain,
+      'description' => '',
+      'vid' => $vid,
+      'weight' => $settings['weight'],
+    );
+    taxonomy_save_term($term);
+  }
+  return array();
+}
