Index: uc_add_donation.module
===================================================================
--- uc_add_donation.module	(revision 189)
+++ uc_add_donation.module	(working copy)
@@ -37,7 +37,7 @@
       $donation = FALSE;
       $items = uc_cart_get_contents();
       foreach( $items as $item ) {
-        if ($item->module == 'uc_donation') {
+        if (is_numeric(strpos($item->model, "DONATION"))) {
           $donation = TRUE;
           break;
         }
@@ -137,8 +137,29 @@
 
     case 'process':
       // Add the donation amount to the order object.
-      $arg1->add_donation_amount = _uc_add_donation_get_donation_amount($arg2['uc_donation_amount'], $arg2['uc_add_donation_custom']);
-      $arg1->add_donation_custom = $arg2['uc_add_donation_custom'];
+      // Check for the line item in the line item db.. If it already exists on this order object, update it.
+      // This particular piece of code was pretty much ripped right from uc_quote.module
+      // The 4 in the uc_order_line_item add is the weight.
+      $donation_amount = _uc_add_donation_get_donation_amount($arg2['uc_donation_amount'], $arg2['uc_add_donation_custom']);
+      if($donation_amount > 0){
+      $result = db_query("SELECT line_item_id FROM {uc_order_line_items} WHERE order_id = %d AND type = 'uc_add_donation'", $arg1->order_id);
+      if ($lid = db_result($result)) {
+        uc_order_update_line_item($lid,
+        t("Donation"),
+        $donation_amount
+        );
+      }
+      else {
+        uc_order_line_item_add($arg1->order_id, 'uc_add_donation',
+        t("Donation"),
+        $donation_amount,
+        4
+        );
+      }
+      }
+      //      $arg1->add_donation_amount = _uc_add_donation_get_donation_amount($arg2['uc_donation_amount'], $arg2['uc_add_donation_custom']);
+      //      $arg1->add_donation_custom = $arg2['uc_add_donation_custom'];
+      return TRUE;
       break;
     case 'settings':
 
@@ -146,19 +167,19 @@
         '#type' => 'textfield',
         '#title' => t('Enter the field name for the donation field.'),
         '#default_value' => variable_get('uc_add_donation_field', DONATION_AMOUNTS_TITLE),
-      );       
-      
+      );
+
       $form['uc_add_donation_title'] = array(
         '#type' => 'textfield',
         '#title' => t('Enter the title for the donation field.'),
         '#default_value' => variable_get('uc_add_donation_title', DONATION_AMOUNTS_TITLE),
-      );       
-      
+      );
+
       $form['uc_add_donation_help'] = array(
         '#type' => 'textarea',
         '#title' => t('Enter the help text that they will see when they add a donation.'),
         '#default_value' => variable_get('uc_add_donation_help', DONATION_AMOUNTS_HELP),
-      );  
+      );
 
       // Allow them to change what donations they would like to accept.
       $form['uc_add_donation_amounts'] = array(
@@ -166,18 +187,14 @@
         '#title' => t('Enter the available donation amounts.'),
         '#default_value' => variable_get('uc_add_donation_amounts', DONATION_AMOUNTS_STRING),
       );
-      
+
       // Allow then to enter in a custom donation
       $form['uc_add_donation_custom'] = array(
-        '#title'        => t('Custom Donation Amount'),
+        '#title'        => t('Allow custom donation amount'),
         '#description'  => t('Allow the user to enter in their custom donation amount.'),
-        '#type'         => 'radios',
-        '#options'      => array(
-          0   => t('Deny'),
-          1   => t('Allow'),
-        ),
+        '#type'         => 'checkbox',
         '#default_value' => variable_get('uc_add_donation_custom', DONATIONS_CUSTOM_DONATION),
-      );      
+      );
       return $form;
       break;
   }
@@ -223,8 +240,11 @@
     case 'customer':
     case 'invoice':
       // Add the output of this donation to the order view.
+      // This should check the line_items on the order, not the 
+      if ($arg1->add_donation_amount > 0){
       $output = t('Your Donation: @donation - THANK YOU!', array('@donation' => uc_currency_format($arg1->add_donation_amount != 'other' ? $arg1->add_donation_amount : $arg1->add_donation_custom)));
       return $output;
+      }
       break;
   }
 }
@@ -234,6 +254,12 @@
  * 
  * @return The items added to the line item array.
  */
+/*
+ * We no longer need to have this live as a declared line item if
+ * its a normal line item in the database.
+ * 
+ */
+
 function uc_add_donation_line_item() {
   $items[] = array(
      'id' => 'uc_add_donation',
@@ -244,8 +270,9 @@
      'stored' => FALSE,  
      'calculated' => TRUE,
      'display_only' => FALSE,  
-     'callback' => 'uc_add_donation_get_line_item',
   );
+  //     'callback' => 'uc_add_donation_get_line_item',
+  
   return $items;
 }
 
@@ -256,21 +283,26 @@
  * @param $arg1 - The order.
  * @return unknown_type
  */
-function uc_add_donation_get_line_item($op, $arg1) {
-  switch ($op) {
-    case 'load':
-      $donation = uc_add_donation_get_donation_amount( $arg1 );
-      $lines = array();
-      $lines[] = array(
-        'id' => 'uc_add_donation',
-        'title' => t('Donation'),
-        'amount' => $donation,
-        'weight' => 1
-      );
-      return $lines;
-  }
-}
+/* 
+ * And therefore we dont need the loader callback either.
+ * 
+ */
 
+//function uc_add_donation_get_line_item($op, $arg1) {
+//  switch ($op) {
+//    case 'load':
+//      $donation = uc_add_donation_get_donation_amount( $arg1 );
+//      $lines = array();
+//      $lines[] = array(
+//        'id' => 'uc_add_donation',
+//        'title' => t('Donation'),
+//        'amount' => $donation,
+//        'weight' => 1
+//      );
+//      return $lines;
+//  }
+//}
+
 /**
  * Implementation of hook_order().
  * 
@@ -279,50 +311,63 @@
  * @param $arg2 - The second argument whose value is based on the operation.
  * @return unknown_type
  */
-function uc_add_donation_order($op, $arg1, $arg2) {
-  switch ($op) {
-    case 'load':
-      
-      // Load the donation given this order ID.
-      $result = db_query("SELECT * FROM {uc_add_donation} WHERE order_id = %d", $arg1->order_id);
-      if ($data = db_fetch_object($result)) {
-        $arg1->add_donation_amount = $data->donation;
-      }
-      break;
-    case 'delete':
-      
-      // Delete the donation from the database.
-      db_query("DELETE FROM {uc_add_donation} WHERE order_id = %d", $arg1->order_id);
-      break;
-    case 'save':
-      
-      // Save or Update the donation in the database.
-      db_query("UPDATE {uc_add_donation} SET donation=%d WHERE order_id=%d", $arg1->add_donation_amount, $arg1->order_id);
-      if (db_affected_rows() == 0) {
-         db_query("INSERT INTO {uc_add_donation} ( order_id, donation) VALUES (%d, %d)", $arg1->order_id, $arg1->add_donation_amount);
-      }
-      break;
-  }
-}
 
+/*
+ * The load is handled by hook_line_item_alter
+ * The save happens as part of the pane 'process' operation
+ * And the delete is handled by default. Line items get removed when an order does.
+ */
+
+//function uc_add_donation_order($op, $arg1, $arg2) {
+//  switch ($op) {
+//    case 'load':
+//      
+//      // Load the donation given this order ID.
+//      $result = db_query("SELECT * FROM {uc_add_donation} WHERE order_id = %d", $arg1->order_id);
+//      if ($data = db_fetch_object($result)) {
+//        $arg1->add_donation_amount = $data->donation;
+//      }
+//      break;
+//    case 'delete':
+//      
+//      // Delete the donation from the database.
+//      db_query("DELETE FROM {uc_add_donation} WHERE order_id = %d", $arg1->order_id);
+//      break;
+//    case 'save':
+//      
+//      // Save or Update the donation in the database.
+//      db_query("UPDATE {uc_add_donation} SET donation=%d WHERE order_id=%d", $arg1->add_donation_amount, $arg1->order_id);
+//      if (db_affected_rows() == 0) {
+//         db_query("INSERT INTO {uc_add_donation} ( order_id, donation) VALUES (%d, %d)", $arg1->order_id, $arg1->add_donation_amount);
+//      }
+//      break;
+//  }
+//}
+
 /**
  * Returns the donation amount given the order object.
  * 
  * @param $order
  * @return unknown_type
  */
-function uc_add_donation_get_donation_amount( $order ) {
-   // If the donation variable is set, then just return that...
-   if ( isset($order->add_donation_amount) && ($order->add_donation_amount != 'other')) {
-      return $order->add_donation_amount;
-   }
-   else if (isset($order->add_donation_amount) && ($order->add_donation_amount == 'other') && isset($order->add_donation_custom)) {
-      return $order->add_donation_custom;
-   }
-   // Otherwise, get it from the database.
-   return db_result( db_query("SELECT donation FROM {uc_add_donation} WHERE order_id = %d", $order->order_id) );
-}
+/*
+ * And if we dont need the line item loader callback, 
+ * we dont need this function either, as its only called from there.
+ * 
+ */
 
+//function uc_add_donation_get_donation_amount( $order ) {
+//   // If the donation variable is set, then just return that...
+//   if ( isset($order->add_donation_amount) && ($order->add_donation_amount != 'other')) {
+//      return $order->add_donation_amount;
+//   }
+//   else if (isset($order->add_donation_amount) && ($order->add_donation_amount == 'other') && isset($order->add_donation_custom)) {
+//      return $order->add_donation_custom;
+//   }
+//   // Otherwise, get it from the database.
+//   return db_result( db_query("SELECT donation FROM {uc_add_donation} WHERE order_id = %d", $order->order_id) );
+//}
+
 /**
  * Returns the donation amount given the Radio control index.
  * 
@@ -336,4 +381,12 @@
    $options = preg_split( "/[\n\r]/", variable_get('uc_add_donation_amounts', DONATION_AMOUNTS_STRING), -1, PREG_SPLIT_NO_EMPTY );
    return $options[$index];
 }
-?>
\ No newline at end of file
+
+function uc_add_donation_line_item_alter(&$item, $order){
+    // When an order gets loaded, its line items are loaded, and then passed through line item alter.
+    // We can use this built in loop to avoid looping over the line items everywhere we want to check 
+    // the value of the add_donation_amount.
+    if ($item['type'] == 'uc_add_donation'){
+      $order->add_donation_amount = $item['amount'];
+    }
+}
