? 540684-domain-source.patch
? 561282-views-filtering.patch
? 564070-source.patch
? 609600-sitename.patch
? big.redirect.patch
? test.patch
Index: domain.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain.module,v
retrieving revision 1.122
diff -u -p -r1.122 domain.module
--- domain.module	11 Oct 2009 17:44:21 -0000	1.122
+++ domain.module	20 Oct 2009 19:02:48 -0000
@@ -1097,25 +1097,20 @@ function domain_nodeapi(&$node, $op, $a3
         return;
       }
       // Append the domain grants to the node for editing.
-      $node->domains = array();
-      $node->domain_site = FALSE;
-      $result = db_query("SELECT gid, realm FROM {domain_access} WHERE nid = %d AND (realm = '%s' OR realm = '%s')", $node->nid, 'domain_id', 'domain_site');
-      while ($data = db_fetch_object($result)) {
-        // Transform the 0 to -1, since {domain_access} is unsigned.
-        ($data->gid == 0) ? $gid = -1 : $gid = $data->gid;
-        if ($data->realm == 'domain_id') {
-          $node->domains[$gid] = $gid;
-          if ($gid > 0) {
-            $domain = domain_lookup($gid);
-            $node->subdomains[] = $domain['sitename'];
-          }
-          else {
-            $node->subdomains[] = variable_get('domain_sitename', variable_get('site_name', 'Drupal'));
-          }
+      $domains = domain_get_node_domains($node->nid);
+      $node->domains = $domains['domain_id'];
+      $node->domain_site = $domains['domain_site'];
+      $node->subdomains = array();
+      if ($node->domain_site) {
+        $node->subdomains[] = t('All affiliates');
+      }
+      foreach ($node->domains as $gid) {
+        if ($gid > 0) {
+          $domain = domain_lookup($gid);
+          $node->subdomains[] = $domain['sitename'];
         }
-        else if ($data->realm == 'domain_site') {
-          $node->domain_site = TRUE;
-          $node->subdomains[] = t('All affiliates');
+        else {
+          $node->subdomains[] = variable_get('domain_sitename', variable_get('site_name', 'Drupal'));
         }
       }
       break;
@@ -1149,6 +1144,36 @@ function domain_nodeapi(&$node, $op, $a3
 }
 
 /**
+ * Get the domains for a node.
+ *
+ * @param $nid
+ *   The node id.
+ * @return
+ *   An array, consisting of two parts. 'domain_id' is an array of active domain ids. 'domain_site'
+ *   is a TRUE/FALSE boolean indicating affiliate status.
+ */
+function domain_get_node_domains($nid) {
+  static $lookup = array();
+  if (isset($lookup[$nid])) {
+    return $lookup[$nid];
+  }
+  $domains = array('domain_id' => array(), 'domain_site' => FALSE);
+  $result = db_query("SELECT gid, realm FROM {domain_access} WHERE nid = %d AND (realm = '%s' OR realm = '%s')", $nid, 'domain_id', 'domain_site');
+  while ($data = db_fetch_object($result)) {
+    // Transform the 0 to -1, since {domain_access} is unsigned.
+    ($data->gid == 0) ? $gid = -1 : $gid = $data->gid;
+    if ($data->realm == 'domain_id') {
+      $domains['domain_id'][$gid] = $gid;
+    }
+    else if ($data->realm == 'domain_site') {
+       $domains['domain_site'] = TRUE;
+    }
+  }
+  $lookup[$nid] = $domains;
+  return $lookup[$nid];
+}
+
+/**
  * Implement hook_node_grants.
  *
  * Informs the node access system what permissions the user has.  By design
Index: settings_custom_url.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/settings_custom_url.inc,v
retrieving revision 1.23
diff -u -p -r1.23 settings_custom_url.inc
--- settings_custom_url.inc	22 Aug 2009 22:30:36 -0000	1.23
+++ settings_custom_url.inc	20 Oct 2009 19:02:49 -0000
@@ -48,7 +48,7 @@ function domain_url_alter_outbound(&$pat
   $check = domain_grant_all();
   $seo = variable_get('domain_seo', 0);
   // If using Domain Source, we force links to a specific domain.
-  $use_source = function_exists('domain_source_domainupdate');
+  $use_source = function_exists('domain_source_lookup');
 
   if ($check || $seo || $use_source) {
     // Check to see if this is a node or comment link and set $nid accordingly.
@@ -89,7 +89,7 @@ function domain_url_alter_outbound(&$pat
           // purpose of this check.
           if ($use_source) {
             $source = db_result(db_query("SELECT domain_id FROM {domain_source} WHERE nid = %d", $nid));
-            $domain[$nid] = domain_lookup($source);
+            $domain[$nid] = domain_source_lookup($source);
           }
           else {
             // Load the domain data for this node -- but only take the first match.
Index: domain_source/domain_source.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain_source/domain_source.module,v
retrieving revision 1.15
diff -u -p -r1.15 domain_source.module
--- domain_source/domain_source.module	15 Oct 2009 13:56:47 -0000	1.15
+++ domain_source/domain_source.module	20 Oct 2009 19:02:50 -0000
@@ -14,6 +14,8 @@
  * @ingroup domain_source
  */
 
+define('DOMAIN_SOURCE_USE_ACTIVE', -5);
+
 /**
  * Implement hook_form_alter()
  */
@@ -21,8 +23,21 @@ function domain_source_form_alter(&$form
   // Apply to all node editing forms, but make sure we are not on the CCK field configuration form.
   // See http://drupal.org/node/186624.
   if ($form['#id'] == 'node-form' && !isset($form['#node']->cck_dummy_node_form)) {
-    global $_domain;
+    global $_domain, $user;
+    if (!empty($form['#node']->nid)) {
+      $default_source = db_result(db_query("SELECT domain_id FROM {domain_source} WHERE nid = %d", $form['#node']->nid));
+    }
+    if (!isset($default_source)) {
+      $default_source = $_domain['domain_id'];
+    }
+    $account = $user;
+    domain_get_user_domains($account);
+    // Only uses with 'set domain access' can assign content to all affilaites, so they get a new option.
+    // This option allows domain source to be ignored on a per-node basis.
     $options = array();
+    if (user_access('set domain access')) {
+      $options[DOMAIN_SOURCE_USE_ACTIVE] = t('Use active domain');
+    }
     $domains = domain_domains();
     $show = FALSE;
     if (user_access('set domain access')) {
@@ -32,24 +47,28 @@ function domain_source_form_alter(&$form
       }
     }
     // In the case of users with limited permissions, option 3 is the "show options" value.
-    else if (user_access('publish to any assigned domain')) {
-      global $user;
+    else if (user_access('publish to any assigned domain') && !empty($account->domain_user)) {
       // Get the user's allowed domains.
       foreach ($domains as $domain) {
-        if ($user->domain_user[$domain['domain_id']] != 0 && $domain['domain_id'] == $user->domain_user[$domain['domain_id']]) {
-          $options[$domain['domain_id']] = $domain['sitename'];
+        $key = $domain['domain_id'];
+        if ($domain['domain_id'] == 0) {
+          $key = -1;
+        }
+        if ($account->domain_user[$key] != 0 && $key == $account->domain_user[$key]) {
+          $options[$key] = $domain['sitename'];
         }
       }
       // Is this node assigned to a source that the user can control?
       $source = $form['#node']->domain_source;
-      if (isset($source)) {
-        if (isset($user->domain_user[$source]) && $user->domain_user[$source] == $source) {
+      if (isset($source) && isset($account->domain_user[$source])) {
+        if ($account->domain_user[$source] == $source) {
           $show = TRUE;
         }
         else {
+          $name = ($source != -5) ? $domains[$source]['sitename'] : t('the active domain');
           $form['domain']['domain_source_notes'] = array(
             '#value' => '<label><b>'. t('Source domain:') .'</b></label><div class="description">'.
-              t('This content is assigned to %domain and cannot be reassigned.', array('%domain' => $domains[$source]['sitename'])) .'</div>',
+              t('This content is assigned to %domain and cannot be reassigned.', array('%domain' => $name)) .'</div>',
           );
         }
       }
@@ -60,7 +79,7 @@ function domain_source_form_alter(&$form
         '#type' => 'select',
         '#title' => t('Source domain'),
         '#options' => $options,
-        '#default_value' => isset($form['#node']->domain_source) ? $form['#node']->domain_source : $_domain['domain_id'],
+        '#default_value' => $default_source,
         '#description' => t('This affiliate will be used to write links to this content from other affiliates, as needed.')
       );
     }
@@ -68,7 +87,7 @@ function domain_source_form_alter(&$form
     else {
       $form['domain']['domain_source'] = array(
         '#type' => 'value',
-        '#value' => (isset($form['#node']->domain_source)) ? $form['#node']->domain_source : $_domain['domain_id'],
+        '#value' => $default_source,
       );
     }
   }
@@ -98,6 +117,7 @@ function domain_source_form_alter(&$form
  * Implement hook_nodeapi()
  */
 function domain_source_nodeapi(&$node, $op, $a3, $a4) {
+  global $_domain;
   switch ($op) {
     case 'validate':
       // If not set, we ignore.
@@ -119,7 +139,7 @@ function domain_source_nodeapi(&$node, $
         // I find this code easier to read than a compound IF statement.
       }
       // Here we account for both the 'domains_raw' and 'domains' options.
-      else if (!in_array($key, $allowed)) {
+      else if ($node->domain_source != DOMAIN_SOURCE_USE_ACTIVE && !in_array($key, $allowed)) {
         form_set_error('domain_source', t('The source affiliate must be selected as a publishing option.'));
       }
       break;
@@ -138,13 +158,7 @@ function domain_source_nodeapi(&$node, $
       break;
     case 'load':
       if ($node->nid) {
-        $source = array();
-        $source = db_fetch_array(db_query("SELECT domain_id FROM {domain_source} WHERE nid = %d", $node->nid));
-      }
-      if (empty($source)) {
-        $node->domain_source = variable_get('domain_default_source', 0);
-      }
-      else {
+        $source = domain_source_lookup($node, $node->domains);
         $node->domain_source = $source['domain_id'];
       }
       break;
@@ -154,8 +168,13 @@ function domain_source_nodeapi(&$node, $
       // calls that are neither a teaser nor a page view.
       if ($a3 !== FALSE || $a4 !== FALSE) {
         if (variable_get('domain_debug', 0) && user_access('set domain access') && isset($node->domain_source)) {
-        $source = domain_lookup($node->domain_source);
-        $node->content['domain_source'] = array('#value' => '<p>'. t('<b>Source domain</b>: %source', array('%source' => $source['sitename'])) .'</b></p>', '#weight' => 25);
+          $source = domain_lookup($node->domain_source);
+          $extra = ' ';
+          $use_active = db_result(db_query("SELECT domain_id FROM {domain_source} WHERE nid = %d", $node->nid));
+          if ($use_active == DOMAIN_SOURCE_USE_ACTIVE) {
+            $extra .= t('using active domain');
+          }
+          $node->content['domain_source'] = array('#value' => '<p>'. t('<b>Source domain</b>: %source', array('%source' => $source['sitename'] . $extra)) .'</b></p>', '#weight' => 25);
         }
       }
       break;
@@ -163,6 +182,37 @@ function domain_source_nodeapi(&$node, $
 }
 
 /**
+ * Find the proper source domain for a node.
+ *
+ * @param $nid
+ *   The node id.
+ * @param $domains
+ *   An optional array of domain ids, which is present when this function is called from hook_nodeapi().
+ * @return
+ *   The active source domain to use; an array returned by domain_lookup().
+ */
+function domain_source_lookup($nid, $domains = array()) {
+  global $_domain;
+  $source = db_result(db_query("SELECT domain_id FROM {domain_source} WHERE nid = %d", $nid));
+  if (empty($source)) {
+    $source = variable_get('domain_default_source', 0);
+  }
+  // DOMAIN_SOURCE_USE_ACTIVE is the status for 'Use active domain.'
+  else if ($source == DOMAIN_SOURCE_USE_ACTIVE){
+    $source = $_domain['domain_id'];
+  }
+  if (empty($domains)) {
+    $lookup = domain_get_node_domains($nid);
+    $domains = $lookup['domain_id'];
+  }
+  if (!in_array($source, $domains)) {
+    $source = current($domains);
+  }
+  $domain_id = ($source == -1) ? 0 : $source;
+  return domain_lookup($domain_id);
+}
+
+/**
  * FormAPI function that lets us update access rules.
  */
 function domain_source_update_nodes($form, &$form_state) {
