--- /Users/alex/Desktop/domain/domain.module	2009-11-01 13:20:19.000000000 -0500
+++ domain.module	2009-11-18 10:57:34.000000000 -0500
@@ -1147,6 +1147,48 @@ function domain_nodeapi(&$node, $op, $a3
     case 'update':
       $_SESSION['domain_nid'] = $node->nid;
       break;
+    case 'presave':
+      if($node->devel_generate){
+        // Only do this for Devel Generate.
+        $existing_domains = domain_domains();
+        
+        // Build $domains array based on
+        // domains checked in the generate form
+        // and shuffle for randomization
+        $checked = array_intersect(
+          array_keys($node->devel_generate['domains']),
+          array_values($node->devel_generate['domains'])
+        );
+        array_walk($checked,create_function('&$v,$k','$v = str_replace("id-","",$v);'));
+        shuffle($checked);
+        $domains = array_combine(array_values($checked),array_values($checked));
+        
+        // Add the domains and supporting data to the node
+        if(!empty($domains)){
+          // Remove some domains from the shuffled array
+          // for randomization (-1 garuntees at least one domain).
+          $howmany = rand(0,count($domains)-1);
+          for($i=0; $i<$howmany; $i++){
+            array_pop($domains);
+          }
+          
+          // Add the domains to the node and grab the first domain as the source.
+          // The source is random because the array has been shuffled
+          $node->domains = $domains;
+          $node->domain_source = current($domains);
+          // domain_site is set to TRUE or FALSE based on "all", "never" or "random flag"
+          $node->domain_site = $node->devel_generate['domain_site'] == 'all' ? TRUE : ($node->devel_generate['domain_site'] == 'random' ? rand(0,1) == 1 : FALSE);
+          // Set subdomains according to the domains in $domains
+          $node->subdomains = array();
+          if($node->domain_site){
+            $node->subdomains[] = 'All affiliates'; 
+          }
+          foreach($domains as $id){
+            $node->subdomains[] = $existing_domains[$id]['sitename'];
+          }
+        }
+      }
+      break;
   }
 }
 
@@ -1480,6 +1522,7 @@ function domain_form_alter(&$form, &$for
   if (in_array($form_id, $forms)) {
     return;
   }
+  
   // Set a message if we are on an admin page.
   domain_warning_check($form_id);
   // If SEO is turned on, then form actions need to be absolute paths
@@ -1501,6 +1544,38 @@ function domain_form_alter(&$form, &$for
     }
   }
 
+  // If this is the devel generate form, get some info
+  if($form_id == "devel_generate_content_form"){
+    $form['submit']['#weight'] = 10;
+    $form['domain'] = array(
+      '#type'         => 'fieldset',
+      '#title'        => t('Domain Access Options'),
+      '#collapsible'  => TRUE,
+      '#collapsed'    => FALSE,
+      '#weight'       => 9,
+    );
+    $form['domain']['domain_site'] = array(
+      '#type'         => 'select',
+      '#title'        => t('Send to all affiliates'),
+      '#options'      => array(
+        'none'    => 'Never',
+        'all'     => 'Always',
+        'random'  => 'Randomly decide',
+      ),
+      '#description'  => t('If you choose "always" or "randomly" you must select at least one domain below.'),
+    );
+    $form['domain']['domains'] = array(
+      '#type'         => 'checkboxes',
+      '#title'        => t('Publish to'),
+      '#options'      => array(),
+      '#description'  => t('Generated content will be accessible on any or all of the domains checked above.'),
+    );
+    foreach(domain_domains() as $id=>$d){
+      $form['domain']['domains']['#options']['id-'.$id] = $d['sitename'];
+    }
+    $form['#submit'][] = 'domain_devel_generate_submit';
+  }
+
   // Apply to all node editing forms, but make sure we are not on the CCK field configuration form.
   if ($form['#id'] == 'node-form' && !isset($form['#node']->cck_dummy_node_form)) {
     global $_domain, $user;
