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.12
diff -u -r1.1.2.12 multidomain.module
--- multidomain.module	3 May 2007 23:32:22 -0000	1.1.2.12
+++ multidomain.module	4 May 2007 00:11:41 -0000
@@ -87,6 +87,38 @@
   return $items;
 }
 
+/**
+ * Implementation of hook_term_path().
+ *
+ * Link to the home page of the relevant domain.
+ * Because of the hook_nodeapi() implementation, these links shouldn't
+ * normally show up in any case in regular node views.
+ */
+function multidomain_term_path($term) {
+  return $term->name;
+}
+
+/**
+ * Implementation of hook_nodeapi().
+ *
+ * Remove terms in the multiple domain vocabulary from the node's
+ * array before viewing.
+ */
+function multidomain_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
+  switch($op) {
+    case 'view':
+      if (isset($node->taxonomy)) {
+        $vid = variable_get('multidomain_vocabulary', '');
+        foreach ($node->taxonomy as $index => $term) {
+          if ($term->vid == $vid) {
+            unset($node->taxonomy[$index]);
+          }
+        }
+      }
+      break;
+  }
+}
+
 function multidomain_default_domain_settings() {
   $form = array();
   $form['singlesignon_master_url'] = array(
@@ -107,7 +139,70 @@
     '#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);
+  $form['taxonomy'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Taxonomy settings'),
+  );
+
+  $vid = variable_get('multidomain_vocabulary', '');
+  $vocabulary = taxonomy_get_vocabulary($vid);
+
+  $form['taxonomy']['multidomain_taxonomy_primary'] = array(
+    '#type' => 'radios',
+    '#title' => t('Content visibility from primary domain'),
+    '#default_value' => variable_get('multidomain_taxonomy_primary', 1),
+    '#options' => array(t('All content'), t('Only content in this domain\'s category')),
+    '#description' => t('Content may be assigned to a domain by category. Select whether you want to see content from all domains\' categories on the master domain or only content specifically assigned to the master domain.'),
+  );
+
+  $form['taxonomy']['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 +211,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 +233,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',
@@ -253,14 +354,48 @@
 
 function multidomain_domain_settings_submit($form_id, $form_values) {
   $domains = variable_get('multidomain_sites', array());
+  $vid = variable_get('multidomain_vocabulary', '');
+  // Load an existing term by id.
+  if (isset($form_values['tid']) && $form_values['tid']) {
+    $term = (array) taxonomy_get_term($form_values['tid']);
+  }
+  // Or by name.
+  else {
+    $result = db_query("SELECT * FROM {term_data} WHERE name = '%s' AND vid = %d", $form_values['url'], $vid);
+    if (db_num_rows($result)) {
+      $term = db_fetch_array($result);
+      $form_values['tid'] = $term['tid'];
+    }
+  }
+  // If no existing term, create one.
+  if (!isset($term)) {
+    $term = array(
+      'name' => $form_values['domain'],
+      'description' => '',
+      'vid' => $vid,
+      'weight' => $form_values['weight'],
+    );
+    taxonomy_save_term($term);
+    $form_values['tid'] = $term['tid'];
+    drupal_set_message(t('Domain term created.'));
+  }
+  // If domain or weight has changed, update the term tied to this domain.
+  elseif (($form_values['domain'] != $form_values['url']) || ($form_values['weight'] != $domains[$form_values['url']]['weight'])) {
+    $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']]);
   }
+
   // If the value is the same as the main domain's default, don't save it.
   // This ensures that later changes to the main domain's settings will
   // apply also to other domains, unless that other domain has already
   // had a custom value set.
-    foreach ($form_values['variables'] as $name => $value) {
+  foreach ($form_values['variables'] as $name => $value) {
     // Determine if there is an existing value.
     if ($existing_value = db_result(db_query("SELECT value FROM {variable} WHERE name = '%s'", $name))) {
       // If so, unserialize it and compare it to the value submitted.
@@ -330,7 +465,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);
 }
 
@@ -383,6 +526,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_field == 'nid') {
+    if ($domains === NULL) {
+      $domains = variable_get('multidomain_sites', array());
+      if (variable_get('multidomain_taxonomy_primary', 1)) {
+        $domains[variable_get('singlesignon_master_url', 'http://')] = array(
+          'tid' => variable_get('multidomain_master_domain_tid', 0),
+        );
+      }
+    }
+    $protocol = ( $_SERVER['HTTPS'] ) ? 'https://' : 'http://' ;
+    // Determine if we are on a registered domain.
+    if ($domain = $domains[$protocol . $_SERVER['HTTP_HOST']]) {
+      if (isset($domain['tid'])) {
+        $return['join'] = 'INNER JOIN {term_node} tn ON tn.nid = '. $primary_table .'.nid';
+        $return['where'] = 'tn.tid = '. $domain['tid'];
+      }
+    }
+  }
+  return $return;
+}
+
+/**
  * Return specified elements from an array of forms.
  */
 function multidomain_get_form_bits($forms) {
@@ -433,14 +605,23 @@
     }
     $base = $force_default ? $default : NULL;
     foreach ($domains as $domain => $info) {
+      $source = $type == 'alias' ? $path : drupal_lookup_path($path);
+      $arg = explode('/', $source);
+      // Determine if this is a node.
+      $is_node = ($arg[0] == 'node' && is_numeric($arg[1]));
       $page_match = FALSE;
+
       // If this is a link to the configuration of this domain, match it.
       // This is needed because we need the variables to be set to this domain's
       // versions, so that the form elements load with the correct defaults.
       if ($path == 'admin/settings/multidomain/settings/'. str_replace('://', '_', $domain)) {
         $page_match = TRUE;
       }
-      // Match path if necessary
+      // Match path by taxonomy.
+      if (!$page_match && $is_node) {
+        $page_match = db_num_rows(db_query('SELECT tn.* FROM {term_node} tn WHERE tn.nid = %d AND tn.tid = %d', $arg[1], $info['tid'])) ? TRUE : FALSE;
+      }
+      // Match path if necessary.
       if (!$page_match && $info['pages']) {
         if ($info['visibility'] < 2) {
           $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($info['pages'], '/')) .')$/';
@@ -459,12 +640,9 @@
         }
       }
 
-      if (!$page_match && sizeof($info['types'])) {
-        $parts = explode('/', $real_path);
-        if ($parts[0] == 'node' && is_numeric($parts[1])) {
-          $node = node_load($parts[1]);
-          $page_match = in_array($node->type, array_keys($info['types']) && $info['types'][$node->type]);
-        }
+      if (!$page_match && !empty($info['types']) && $is_node) {
+        $node = node_load($arg[1]);
+        $page_match = array_key_exists($node->type, $info['types']) && $info['types'][$node->type];
       }
       if ($page_match) {
         $base = $domain;
@@ -480,7 +658,7 @@
       // 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,63 @@+<?php+// $Id: $++/** + * Implementation of hook_install(). + */+function multidomain_install() {+  $vid = variable_get('multidomain_vocabulary', '');+  if (empty($vid)) {+    // Check to see if a multiple domain vocabulary exists+    $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module = '%s'", 'multidomain'));+    if (!$vid) {+      // Create the multiple domain 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' => 'multiple domain', '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'];+      // Add a term for the master domain if that domain is already registered.+      if ($master_domain = variable_get('singlesignon_master_url', FALSE)) {+        $term = array(+          'name' => $master_domain,+          'description' => '',+          'vid' => $vid,+          // Set a low weigth as this is the primary domain.+          'weight' => -10,+        );+        taxonomy_save_term($term);+        variable_set('multidomain_master_domain_tid', $term['tid']);+      }+    }+    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);+    // Set the term id for this domain.+    $domains[$domain]['tid'] = $term['tid'];+  }+  variable_set('multidomain_sites', $domains);+  return array();+}

