=== modified file 'uc_attribute/uc_attribute.admin.inc' (properties changed: -x to +x)
--- uc_attribute/uc_attribute.admin.inc	2009-09-23 18:21:45 +0000
+++ uc_attribute/uc_attribute.admin.inc	2009-10-04 18:30:42 +0000
@@ -769,6 +769,7 @@
   }
 
   foreach ($attributes as $aid => $attribute) {
+    $default_options = array();
     $form['attributes'][$aid]['name'] = array(
       '#value' => check_plain($attribute->name),
     );
@@ -788,10 +789,13 @@
     if ($base_attr->options) {
       $options = array();
 
-      $result = db_query("SELECT ao.aid, ao.oid, ao.name, ao.cost AS default_cost, ao.price AS default_price, ao.weight AS default_weight, ao.ordering AS default_ordering, po.cost, po.price, po.weight, po.ordering, po.ordering IS NULL AS null_order FROM {uc_attribute_options} AS ao LEFT JOIN $table AS po ON ao.oid = po.oid AND po.". $id_type ." = ". $sql_type ." WHERE aid = %d ORDER BY null_order, po.ordering, default_ordering, ao.name", $id, $attribute->aid);
+      $result = db_query("SELECT ao.aid, ao.oid, ao.name, ao.cost AS default_cost, ao.price AS default_price, ao.weight AS default_weight, ao.ordering AS default_ordering, po.cost, po.price, po.weight, po.ordering, po.default_option, po.ordering IS NULL AS null_order FROM {uc_attribute_options} AS ao LEFT JOIN $table AS po ON ao.oid = po.oid AND po.". $id_type ." = ". $sql_type ." WHERE aid = %d ORDER BY null_order, po.ordering, default_ordering, ao.name", $id, $attribute->aid);
       while ($option = db_fetch_object($result)) {
         $oid = $option->oid;
         $options[$oid] = '';
+        if ($option->default_option==1) {
+          $default_options[] = $oid;
+        }
 
         $form['attributes'][$aid]['options'][$oid]['select'] = array(
           '#type' => 'checkbox',
@@ -820,11 +824,19 @@
           '#attributes' => array('class' => 'uc-attribute-option-table-ordering'),
         );
       }
+      $default_form_type = 'radios';
+      if ($attribute->display == 3) {
+        $default_form_type = 'checkboxes';
+        $default_option = $default_options;
+      }
+      else {
+        $default_option = $default_options[0];
+      }
 
       $form['attributes'][$aid]['default'] = array(
-        '#type' => 'radios',
+        '#type' => $default_form_type,
         '#options' => $options,
-        '#default_value' => /* $attribute->required ? NULL : */ $attribute->default_option,
+        '#default_value' => /* $attribute->required ? NULL : */ $default_option,
         //'#disabled' => $attribute->required,
       );
     }
@@ -966,23 +978,25 @@
 function uc_object_options_form_validate($form, &$form_state) {
   if (isset($form_state['values']['attributes'])) {
     foreach ($form_state['values']['attributes'] as $aid => $attribute) {
-      $selected_opts = array();
-      if (is_array($attribute['options'])) {
-        foreach ($attribute['options'] as $oid => $option) {
-          if ($option['select'] == 1) {
-            $selected_opts[] = $oid;
+      if (!is_array($attribute['default'])) {
+        $selected_opts = array();
+        if (is_array($attribute['options'])) {
+          foreach ($attribute['options'] as $oid => $option) {
+            if ($option['select'] == 1) {
+              $selected_opts[] = $oid;
+            }
           }
         }
-      }
-      if (!empty($selected_opts) && !$form['attributes'][$aid]['default']['#disabled'] && !in_array($attribute['default'], $selected_opts)) {
-        form_set_error($attribute['default']);
-        $error = TRUE;
+        if (!empty($selected_opts) && !$form['attributes'][$aid]['default']['#disabled'] && !in_array($attribute['default'], $selected_opts)) {
+          form_set_error($attribute['default']);
+          $error = TRUE;
+        }
       }
     }
   }
 
   if ($error) {
-    drupal_set_message(t('All attributes with enabled options must specify an enabled option as default.'), 'error');
+    drupal_set_message(t('All attributes with enabled options (except checkboxes) must specify an enabled option as default.'), 'error');
   }
 }
 
@@ -1004,15 +1018,27 @@
   }
 
   foreach ($form_state['values']['attributes'] as $attribute) {
-    db_query("UPDATE $attr_table SET default_option = %d WHERE $id = $sql_type AND aid = %d", $attribute['default'], $form_state['values']['id'], $attribute['aid']);
-
+// Absolete
+//    db_query("UPDATE $attr_table SET default_option = %d WHERE $id = $sql_type AND aid = %d", $attribute['default'], $form_state['values']['id'], $attribute['aid']);
     if (is_array($attribute['options'])) {
       foreach ($attribute['options'] as $oid => $option) {
+
+        // assign a 1 to all default options
+        $default_option = 0;
+        if (is_array($attribute['default'])) {
+          if (in_array($oid, $attribute['default'])) {
+            $default_option =1;
+          }
+        }
+        else {
+          if ($oid == $attribute['default']) {
+            $default_option = 1;
+          }
+        }
         db_query("DELETE FROM $opt_table WHERE $id = $sql_type AND oid = %d", $form_state['values']['id'], $oid);
-
         if ($option['select']) {
-          db_query("INSERT INTO $opt_table ($id, oid, cost, price, weight, ordering) VALUES ($sql_type, %d, %f, %f, %f, %d)",
-                   $form_state['values']['id'], $oid, $option['cost'], $option['price'], $option['weight'], $option['ordering']);
+          db_query("INSERT INTO $opt_table ($id, oid, cost, price, weight, ordering, default_option) VALUES ($sql_type, %d, %f, %f, %f, %d, %d)",
+                   $form_state['values']['id'], $oid, $option['cost'], $option['price'], $option['weight'], $option['ordering'], $default_option);
         }
         elseif ($form_state['values']['type'] == 'product') {
           $aid = $attribute['aid'];

=== modified file 'uc_attribute/uc_attribute.install' (properties changed: -x to +x)
--- uc_attribute/uc_attribute.install	2009-09-23 15:41:04 +0000
+++ uc_attribute/uc_attribute.install	2009-10-04 16:55:48 +0000
@@ -229,6 +229,13 @@
         'not null' => TRUE,
         'default' => 0,
       ),
+      'default_option' => array(
+        'description' => t('Is the option enabled by default.'),
+        'type' => 'int',
+        'size' => 'tiny',
+        'not null' => TRUE,
+        'default' => 0,
+      ),
     ),
     'primary key' => array('pcid', 'oid'),
   );
@@ -337,6 +344,13 @@
         'not null' => TRUE,
         'default' => 0,
       ),
+      'default_option' => array(
+        'description' => t('Is the option enabled by default.'),
+        'type' => 'int',
+        'size' => 'tiny',
+        'not null' => TRUE,
+        'default' => 0,
+      ),
     ),
     'primary key' => array('nid', 'oid'),
   );
@@ -719,3 +733,32 @@
 
   return $ret;
 }
+
+function uc_attribute_update_6006() {
+  $ret = array();
+  $spec = array(
+    'description' => t('Is the option enabled by default.'),
+    'type' => 'int',
+    'size' => 'tiny',
+    'not null' => TRUE,
+    'default' => 0,
+  );
+  
+  db_add_field($ret, 'uc_class_attribute_options',  'default_option', $spec);
+  db_add_field($ret, 'uc_product_options',  'default_option', $spec);
+
+  $ret[] = update_sql("UPDATE {uc_class_attributes} AS a LEFT JOIN {uc_class_attribute_options} AS o ON a.default_option=o.oid AND a.pcid=o.pcid SET o.default_option=1");
+  $ret[] = update_sql("UPDATE {uc_product_attributes} AS a LEFT JOIN {uc_product_options} AS o ON a.default_option=o.oid AND a.nid=o.nid SET o.default_option=1");
+
+  return $ret;
+}
+//  Cleaning out the trash, might be a good idea to wait to the next update
+//  also need to change scheme
+//
+//  function uc_attribute_update_6007() {
+//    $ret = array();
+//    db_drop_field(&$ret, 'uc_class_attributes', 'default_option');
+//    db_drop_field(&$ret, 'uc_product_attributes', 'default_option');
+//  
+//    return $ret;
+//  }

=== modified file 'uc_attribute/uc_attribute.module' (properties changed: -x to +x)
--- uc_attribute/uc_attribute.module	2009-09-29 16:15:09 +0000
+++ uc_attribute/uc_attribute.module	2009-10-04 16:55:58 +0000
@@ -552,12 +552,12 @@
   if ($nid) {
     switch ($type) {
       case 'product':
-        $attribute = db_fetch_object(db_query("SELECT a.aid, a.name, a.label AS default_label, a.ordering AS default_ordering, a.required AS default_required, a.display AS default_display, a.description, pa.label, pa.default_option, pa.required, pa.ordering, pa.display FROM {uc_attributes} AS a LEFT JOIN {uc_product_attributes} AS pa ON a.aid = pa.aid AND pa.nid = %d WHERE a.aid = %d", $nid, $attr_id));
-        $result = db_query("SELECT po.nid, po.oid, po.cost, po.price, po.weight, po.ordering, ao.name, ao.aid FROM {uc_product_options} AS po LEFT JOIN {uc_attribute_options} AS ao ON po.oid = ao.oid AND nid = %d WHERE aid = %d ORDER BY po.ordering, ao.name", $nid, $attr_id);
+        $attribute = db_fetch_object(db_query("SELECT a.aid, a.name, a.label AS default_label, a.ordering AS default_ordering, a.required AS default_required, a.display AS default_display, a.description, pa.label, pa.required, pa.ordering, pa.display FROM {uc_attributes} AS a LEFT JOIN {uc_product_attributes} AS pa ON a.aid = pa.aid AND pa.nid = %d WHERE a.aid = %d", $nid, $attr_id));
+        $result = db_query("SELECT po.nid, po.oid, po.cost, po.price, po.weight, po.ordering, po.default_option, ao.name, ao.aid FROM {uc_product_options} AS po LEFT JOIN {uc_attribute_options} AS ao ON po.oid = ao.oid AND nid = %d WHERE aid = %d ORDER BY po.ordering, ao.name", $nid, $attr_id);
         break;
       case 'class':
-        $attribute = db_fetch_object(db_query("SELECT a.aid, a.name, a.label AS default_label, a.ordering AS default_ordering, a.required AS default_required, a.display AS default_display, a.description, ca.default_option, ca.label, ca.required, ca.ordering, ca.display FROM {uc_attributes} AS a LEFT JOIN {uc_class_attributes} AS ca ON a.aid = ca.aid AND ca.pcid = '%s' WHERE a.aid = %d", $nid, $attr_id));
-        $result = db_query("SELECT co.pcid, co.oid, co.cost, co.price, co.weight, co.ordering, ao.name, ao.aid FROM {uc_class_attribute_options} AS co LEFT JOIN {uc_attribute_options} AS ao ON co.oid = ao.oid AND co.pcid = '%s' WHERE ao.aid = %d ORDER BY co.ordering, ao.name", $nid, $attr_id);
+        $attribute = db_fetch_object(db_query("SELECT a.aid, a.name, a.label AS default_label, a.ordering AS default_ordering, a.required AS default_required, a.display AS default_display, a.description, ca.label, ca.required, ca.ordering, ca.display FROM {uc_attributes} AS a LEFT JOIN {uc_class_attributes} AS ca ON a.aid = ca.aid AND ca.pcid = '%s' WHERE a.aid = %d", $nid, $attr_id));
+        $result = db_query("SELECT co.pcid, co.oid, co.cost, co.price, co.weight, co.ordering, co.default_option, ao.name, ao.aid FROM {uc_class_attribute_options} AS co LEFT JOIN {uc_attribute_options} AS ao ON co.oid = ao.oid AND co.pcid = '%s' WHERE ao.aid = %d ORDER BY co.ordering, ao.name", $nid, $attr_id);
         break;
       default:
         $attribute = db_fetch_object(db_query("SELECT * FROM {uc_attributes} WHERE aid = %d", $attr_id));
@@ -716,7 +716,11 @@
     $context['subject']['attribute'] = $attribute;
     // Build the attribute's options array.
     $options = array();
+    $default_option = array();
     foreach ($attribute->options as $option) {
+      if ($option->default_option == 1) {
+        $default_option[] = $option->oid;
+      }
       $context['subject']['option'] = $option;
       switch (variable_get('uc_attribute_option_price_format', 'adjustment')) {
         case 'total':
@@ -756,7 +760,6 @@
         if ($attribute->display == 1) {
           $options = array('' => t('Please select')) + $options;
         }
-        unset($attribute->default_option);
       }
       switch ($attribute->display) {
         case 1:
@@ -772,7 +775,7 @@
       $form_attributes[$attribute->aid] = array(
         '#type' => $attr_type,
         '#description' => check_markup($attribute->description),
-        '#default_value' => ($attr_type == "checkboxes" ? array() : $attribute->default_option),
+        '#default_value' => ($attr_type == "checkboxes" ? $default_option : $default_option[0]),
         '#options' => $options,
         '#required' => $attribute->required,
       );

