Index: uc_attribute.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ubercart/uc_attribute/uc_attribute.module,v
retrieving revision 1.12.2.2
diff -u -r1.12.2.2 uc_attribute.module
--- uc_attribute.module	14 Aug 2008 21:08:47 -0000	1.12.2.2
+++ uc_attribute.module	4 Sep 2008 21:05:22 -0000
@@ -452,7 +452,19 @@
     '#description' => t('Multiple attributes on an add to cart form are sorted by this value and then by their name.<br />May be overridden at the product level.'),
     '#default_value' => isset($attribute->ordering) ? $attribute->ordering : 0,
   );
-
+  
+  // get a list of the product classes
+  $class_query = db_query("SELECT uca.pcid, uca.name FROM {uc_product_classes} uca INNER JOIN {node} n ON uca.pcid = n.type");
+  while ($class = db_fetch_array($class_query)) {
+    $classes[$class['pcid']] = $class['name'];
+  }
+  
+  $form['classes'] = array( 
+    '#type' => 'checkboxes', 
+    '#title' => t('Push this attribute to these product classes'), 
+    '#options' => $classes, 
+  );
+  
   $form['submit'] = array(
     '#type' => 'submit',
     '#value' => t('Submit'),
@@ -463,9 +475,94 @@
 }
 
 /**
+ * Batch function for applying attributes by node type.
+ *
+ * @param $values
+ *   (array) The $form['values'] array
+ * @param $type
+ *   (string) The node type
+ */
+function uc_batch_attributes_push($values, $type, &$context) {
+  
+  if (!isset($context['sandbox']['progress'])) {
+    $context['sandbox']['progress'] = 0;
+    $context['sandbox']['current_node'] = 0;
+    $context['sandbox']['max'] = db_result(db_query("SELECT COUNT(DISTINCT nid) FROM {node} WHERE type = '%s'", $type));
+  }
+  
+  $limit = 25;
+
+  $result = db_query_range("SELECT nid FROM {node} WHERE (nid > %d) AND (type = '%s') ORDER BY nid ASC", $context['sandbox']['current_node'], $type, 0, $limit);
+  while ($row = db_fetch_array($result)) {
+
+    db_query("INSERT INTO {uc_product_attributes} ( nid, aid, ordering, default_option, required, display ) VALUES ( %d, %d, %d, %d, %d, %d ) ON DUPLICATE KEY UPDATE nid = nid", 
+      (integer) $row['nid'], 
+      (integer) $values['aid'], 
+      (integer) $values['ordering'], 
+      0, // default option can't be set, since this is at the attribute level(?)
+      (integer) $values['required'], 
+      (integer) $values['display']
+    );
+    
+    $title = db_result(db_query("SELECT title FROM {node} WHERE nid = '%d'", $row['nid']));
+    
+    // Store some result for post-processing in the finished callback.
+    $context['results'][] = $title;
+
+    // Update our progress information.
+    $context['sandbox']['progress']++;
+    $context['sandbox']['current_node'] = $row['nid'];
+    $context['message'] = t('Adding the attribute to %node', array('%node' => $title));
+  }
+
+  // Inform the batch engine that we are not finished,
+  // and provide an estimation of the completion level we reached.
+  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
+    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
+  }
+}
+
+/**
+* Batch 'finished' callback
+*/
+function uc_batch_attributes_finished($success, $results, $operations) {
+  
+  if ($success) {
+    $message = count($results) .' products were updated with the new attribute settings:';
+    $message .= theme('item_list', $results);
+  }
+  else {
+    $error_operation = reset($operations);
+    $message = 'An error occurred while processing '. $error_operation[0] .' with arguments :'. print_r($error_operation[0], TRUE);
+  }
+  
+  drupal_set_message($message);
+}
+
+/**
  * Submit function for uc_attribute_add_form().
  */
 function uc_attribute_form_submit($form, &$form_state) {
+  
+  $func_array = array();
+  
+  // create a batch job for each class that was selected
+  foreach ($form_state['values']['classes'] as $pcid => $type) {
+    if ($type !== 0) {
+      $func_array = array_merge($func_array, array(array('uc_batch_attributes_push', array($form_state['values'], $type))));
+    }
+  }
+  
+  $batch = array(
+    'operations' => $func_array, 
+    'finished' => 'uc_batch_attributes_finished',
+    'title' => t('Pushing attributes...'),
+    'init_message' => t('Starting up.'),
+    'progress_message' => t('Processed @current out of @total.'),
+    'error_message' => t('Oh, no! There has been an error.'),
+  );
+  batch_set($batch);
+
   if (!empty($form_state['values']['aid'])) {
     db_query("UPDATE {uc_attributes} SET name = '%s', ordering = %d, required = %d, display = %d, description = '%s' WHERE aid = %d", $form_state['values']['name'], $form_state['values']['ordering'], $form_state['values']['required'], $form_state['values']['display'], $form_state['values']['description'], $form_state['values']['aid']);
   }
@@ -1548,4 +1645,3 @@
   }
   return $aids;
 }
-

