commit 76f0050b83fd0cf79e167b7eeee9a021e4067180
Author: Chris Burgess <chris@giantrobot.co.nz>
Date:   Fri Apr 12 15:54:41 2013 +1200

    Issue #795094. Squash lots of undefined variable / property errors.

diff --git a/modules/uc_recurring_order/uc_recurring_order.module b/modules/uc_recurring_order/uc_recurring_order.module
index e553061..a9636a7 100644
--- a/modules/uc_recurring_order/uc_recurring_order.module
+++ b/modules/uc_recurring_order/uc_recurring_order.module
@@ -220,14 +220,15 @@ function uc_recurring_order_pane_checkout($op, &$arg1, $arg2) {
       return TRUE;
 
     case 'review':
-      if ($arg1->data['recurring_option']) {
+      if (!empty($arg1->data['recurring_option'])) {
         $next_renewal = strtotime('+'. $arg1->data['recurring_option']);
         $review[] = array(
           'title' => t('Recurring Order'),
           'data' => t('Your next order after this will occur on @next.', array('@recurring' => $arg1->data['recurring_option'], '@next' => format_date($next_renewal, 'short')))
         );
+        return $review;
       }
-      return $review;
+
   }
 }
 
@@ -238,7 +239,7 @@ function uc_recurring_order_order($op, $arg1, $arg2) {
   switch ($op) {
     // TODO: Allow admin to create a recurring order from "create order" page.
     case 'submit':
-      $recurring = $arg1->data['recurring_option'];
+      $recurring = isset($arg1->data['recurring_option']) ? $arg1->data['recurring_option'] : 0;
       $next_renewal = strtotime('+'. $recurring);
       if ($next_renewal > time() && variable_get('uc_recurring_checkout_process', TRUE)) {
         if (uc_recurring_order_process_order($arg1, $recurring) === FALSE) {
@@ -252,7 +253,7 @@ function uc_recurring_order_order($op, $arg1, $arg2) {
         if (db_affected_rows() !== 0) {
           uc_order_comment_save($arg1->order_id, 0, t('The recurring order amount was updated.'), 'admin');
         }
-			}
+      }
     break;
   }
 }
@@ -261,6 +262,7 @@ function uc_recurring_order_order($op, $arg1, $arg2) {
  * Process a recurring order.
  */
 function uc_recurring_order_process_order($order, $recurring) {
+  global $user;
   if (variable_get('uc_recurring_order_enabled', TRUE)) {
     $payment_method = !empty($order->payment_method) ? $order->payment_method : 'default';
     if (!($fee_handler = uc_recurring_get_recurring_info($payment_method))) {
@@ -315,7 +317,6 @@ function uc_recurring_order_process_order($order, $recurring) {
       return FALSE;
     }
   }
-  return $return;
 }
 
 /**
diff --git a/modules/uc_recurring_product/uc_recurring_product.module b/modules/uc_recurring_product/uc_recurring_product.module
index abad0e3..ce85a7c 100644
--- a/modules/uc_recurring_product/uc_recurring_product.module
+++ b/modules/uc_recurring_product/uc_recurring_product.module
@@ -31,9 +31,6 @@ function uc_recurring_product_feature_form($form_state, $node, $feature) {
   drupal_add_css(drupal_get_path('module', 'uc_recurring') .'/uc_recurring.css');
   drupal_add_js(drupal_get_path('module', 'uc_recurring') .'/uc_recurring.js', 'module');
 
-  if (!empty($feature)) {
-    $product = uc_recurring_product_fee_load($feature['pfid']);
-  }
   $options = uc_product_get_models($node);
   $form['nid'] = array(
     '#type' => 'hidden',
@@ -44,7 +41,7 @@ function uc_recurring_product_feature_form($form_state, $node, $feature) {
     '#title' => t('Applicable SKU'),
     '#description' => t('Select the applicable product model/SKU for this fee.'),
     '#options' => $options,
-    '#default_value' => $product->model,
+    '#default_value' => NULL,
   );
 
   $form['fee'] = array(
@@ -54,14 +51,10 @@ function uc_recurring_product_feature_form($form_state, $node, $feature) {
     '#description' => t('Specify the amount that is charged on each renewal date.'),
   );
 
-  $attributes = array();
-  if ($product->fee_amount == 0) {
-    $attributes['checked'] = 'checked';
-  }
   $form['fee']['fee_same_product'] = array(
     '#type' => 'checkbox',
     '#title' => t('Set the recurring fee amount to the same as selling price of the product at the time of purchase.'),
-    '#attributes' => $attributes,
+    '#attributes' => array(),
   );
 
   $form['fee']['product_price'] = array(
@@ -72,11 +65,11 @@ function uc_recurring_product_feature_form($form_state, $node, $feature) {
     '#type' => 'textfield',
     '#title' => t('Recurring fee amount'),
     '#description' => t('Charge this amount each billing period.<br />The product price is still charged at checkout.'),
-    '#default_value' => $product->fee_amount == 0 ? $node->sell_price : $product->fee_amount,
+    '#default_value' => $node->sell_price,
     '#size' => 16,
     '#field_prefix' => variable_get('uc_sign_after_amount', FALSE) ? '' : variable_get('uc_currency_sign', '$'),
     '#field_suffix' => variable_get('uc_sign_after_amount', FALSE) ? variable_get('uc_currency_sign', '$') : '',
-    '#attributes' => $product->fee_amount == 0 ? array('disabled' => 'disabled') : array(),
+    '#attributes' => array(),
   );
 
   $form['interval'] = array(
@@ -95,7 +88,7 @@ function uc_recurring_product_feature_form($form_state, $node, $feature) {
   $form['interval']['initial']['initial_charge_value'] = array(
     '#type' => 'select',
     '#options' => drupal_map_assoc(uc_range(0, 52)),
-    '#default_value' => $product->initial_charge_value,
+    '#default_value' => NULL,
   );
   $form['interval']['initial']['initial_charge_unit'] = array(
     '#type' => 'select',
@@ -105,7 +98,7 @@ function uc_recurring_product_feature_form($form_state, $node, $feature) {
       'months' => t('month(s)'),
       'years' => t('year(s)'),
     ),
-    '#default_value' => $product->initial_charge_unit,
+    '#default_value' => NULL,
   );
 
   $form['interval']['regular'] = array(
@@ -118,7 +111,7 @@ function uc_recurring_product_feature_form($form_state, $node, $feature) {
   $form['interval']['regular']['regular_interval_value'] = array(
     '#type' => 'select',
     '#options' => drupal_map_assoc(uc_range(1, 52)),
-    '#default_value' => $product->regular_interval_value,
+    '#default_value' => NULL,
   );
   $form['interval']['regular']['regular_interval_unit'] = array(
     '#type' => 'select',
@@ -128,7 +121,7 @@ function uc_recurring_product_feature_form($form_state, $node, $feature) {
       'months' => t('month(s)'),
       'years' => t('year(s)'),
     ),
-    '#default_value' => $product->regular_interval_unit,
+    '#default_value' => NULL,
   );
 
   $form['num_interval'] = array(
@@ -138,23 +131,37 @@ function uc_recurring_product_feature_form($form_state, $node, $feature) {
     '#description' => t('Specify how many times the recurring fee will be charged.'),
   );
 
-  $attributes = array();
-  if ($product->number_intervals < 0) {
-    $attributes['checked'] = 'checked';
-  }
   $form['num_interval']['unlimited_intervals'] = array(
     '#type' => 'checkbox',
     '#title' => t('Unlimited rebillings.'),
-    '#attributes' => $attributes,
+    '#attributes' => array(),
   );
   $form['num_interval']['number_intervals'] = array(
     '#type' => 'textfield',
     '#title' => t('Number of billing periods'),
     '#size' => 16,
-    '#default_value' => $product->number_intervals < 0 ? '' : $product->number_intervals,
-    '#attributes' => $product->number_intervals < 0 ? array('disabled' => 'disabled') : array(),
+    '#default_value' => NULL,
+    '#attributes' => array(),
   );
 
+  if (!empty($feature) && $product = uc_recurring_product_fee_load($feature['pfid'])) {
+    $form['model']['#default_value'] = isset($product) ? $product->model : '';
+    if (isset($product->fee_amount) && $product->fee_amount == 0) {
+      $form['fee']['fee_same_product']['#attributes']['checked'] = 'checked';
+      $form['fee']['product_price']['#attributes']['disabled'] = 'disabled';
+    }
+    $form['fee']['fee_amount']['#default_value'] = (!isset($product->fee_amount) || $product->fee_amount == 0) ? $node->sell_price : $product->fee_amount;
+    $form['interval']['initial']['initial_charge_value']['#default_value'] =  isset($product->initial_charge_value) ? $product->initial_charge_value : NULL;
+    $form['interval']['initial']['initial_charge_unit']['#default_value'] = isset($product->initial_charge_unit) ? $product->initial_charge_unit : NULL;
+    $form['interval']['regular']['regular_interval_value']['#default_value'] = isset($product->regular_interval_value) ? $product->regular_interval_value : NULL;
+    $form['interval']['regular']['regular_interval_unit']['#default_value'] = $product->regular_interval_unit;
+    if ($product->number_intervals < 0) {
+      $form['num_interval']['unlimited_intervals']['#attributes']['checked'] = 'checked';
+      $form['num_interval']['number_intervals']['#attributes']['disabled'] = 'disabled';
+      $form['num_interval']['number_intervals']['#default_value'] = $product->number_intervals;
+    }
+  }
+
   return uc_product_feature_form($form);
 }
 
@@ -168,13 +175,14 @@ function uc_recurring_product_feature_form_validate($form, &$form_state) {
  * Submit handler for the recurring feature.
  */
 function uc_recurring_product_feature_form_submit($form, &$form_state) {
+  // Build the recurring fee's product object.
+  $product = new stdClass;
+  $product->pfid = NULL;
+
   // Use the form specified pfid if available.
   if (!empty($form_state['values']['pfid'])) {
-    $pfid = $form_state['values']['pfid'];
+    $product->pfid = $form_state['values']['pfid'];
   }
-
-  // Build the recurring fee's product object.
-  $product->pfid = $pfid;
   $product->model = $form_state['values']['model'];
   $product->fee_amount = $form_state['values']['fee_amount'];
   $product->initial_charge = $form_state['values']['initial_charge_value'] .' '. $form_state['values']['initial_charge_unit'];
@@ -364,7 +372,6 @@ function uc_recurring_product_process_order($order, $data = array()) {
       $fee = drupal_clone($fee_template);
 
       $product_fee = $product['recurring product'];
-      $order_product_id = $product['product']->order_product_id;
 
       // If the product fee amount is 0, it means we need to use the product
       // price. This allows recurring fees to be adjusted by attributes.
@@ -387,7 +394,7 @@ function uc_recurring_product_process_order($order, $data = array()) {
       ) + $data;
       $fee->attempts = 0;
       $fee->pfid = $product_fee->pfid;
-      $fee->order_product_id = $order_product_id;
+      $fee->order_product_id = isset($product['product']->order_product_id) ? $product['product']->order_product_id : NULL;
       $fee->own_handler = !empty($fee_handler['own handler']);
 
       drupal_alter('recurring_fee_user_create', $fee);
@@ -432,7 +439,7 @@ function uc_recurring_product_recurring_renewal_pending(&$order, &$fee) {
 
   // Set a single product - the recurring fee.
   $product = new stdClass();
-  $product->order_id = $new_id;
+  $product->order_id = NULL;
   $product->nid = $fee->data['nid'];
   $product->model = $fee->data['model'];
   $product->title = !empty($fee->fee_title) ? $fee->fee_title : t('Renewal of product @model', array('@model' => $product->model));
@@ -559,5 +566,3 @@ function uc_recurring_product_fee_product_delete($pfid) {
   module_invoke_all('recurring_product_delete', $pfid);
   db_query("DELETE FROM {uc_recurring_product} WHERE pfid = %d", $pfid);
 }
-
-
diff --git a/uc_recurring.admin.inc b/uc_recurring.admin.inc
index 11160a6..da663d2 100644
--- a/uc_recurring.admin.inc
+++ b/uc_recurring.admin.inc
@@ -12,7 +12,7 @@ function uc_recurring_admin($filter = NULL, $id = NULL) {
   $item = menu_get_item();
 
   // Add the filter form to the top of the table.
-  $output .= drupal_get_form('uc_recurring_admin_filter_form', $item);
+  $output = drupal_get_form('uc_recurring_admin_filter_form', $item);
 
   // Build an array of fees to include in the admin table.
   $fees = array();
@@ -436,9 +436,8 @@ function uc_recurring_order_information($order_id) {
   }
 
   // Recurring fee details.
-  if (!empty($order)) {
-    $fees = uc_recurring_get_fees($order);
-    $output .= '<h2>'. t('Original order ID: @order_id', array('@order_id' => $order->order_id)) .'</h2>';
+  if (!empty($order) && $fees = uc_recurring_get_fees($order)) {
+    $output = '<h2>'. t('Original order ID: @order_id', array('@order_id' => $order->order_id)) .'</h2>';
     $output .= theme('uc_recurring_admin_table', $fees);
 
     $header = array(
diff --git a/uc_recurring.module b/uc_recurring.module
index 6591646..85834ee 100644
--- a/uc_recurring.module
+++ b/uc_recurring.module
@@ -408,10 +408,12 @@ function uc_recurring_uc_message() {
  *   The new order ID or FALSE if unable to renew fee.
  */
 function uc_recurring_renew($fee) {
+  global $user;
   if ($fee->attempts==0) {
     // create a new order for the renewal
     $order = uc_recurring_create_renewal_order($fee);
-  } else {
+  }
+  else {
     // renewal order already created on first charge attempt
     $order = FALSE;
     if (isset($fee->data['retry_order_id']) && is_numeric($fee->data['retry_order_id'])) {
@@ -461,6 +463,8 @@ function uc_recurring_renew($fee) {
     // Add comment to both new and original orders.
     uc_order_comment_save($fee->order_id, $user->uid, $message);
     uc_order_comment_save($order->order_id, $user->uid, $message);
+    // @TODO These two watchdog statements could go up in the previous
+    // if/else?
     if ($fee->module == 'uc_recurring_product') {
       watchdog('uc_recurring', 'Failed to capture recurring fee of @amount for product @model on order @order_id.', array('@amount' => $fee->fee_amount, '@model' => $fee->data['model'], '@order_id' => $fee->order_id), WATCHDOG_ERROR, l(t('order !order_id', array('!order_id' => $fee->order_id)), 'admin/store/orders/'. $fee->order_id));
     }
@@ -607,8 +611,7 @@ function uc_recurring_extend_fee(&$fee, $extend_seconds) {
 function uc_recurring_get_extension($fee_id, $attempt) {
   $result = db_query("SELECT * FROM {uc_recurring_extensions} WHERE (pfid = %d OR pfid IS NULL) AND rebill_attempt = %d ORDER BY pfid DESC", $fee_id, $attempt);
   $extend_seconds = 0;
-  if ($result != FALSE) {
-    $extension = db_fetch_object($result);
+  while ($extension = db_fetch_object($result)) {
     $extend_seconds = $extension->time_to_extend;
   }
   return $extend_seconds;
@@ -868,22 +871,25 @@ function uc_recurring_fee_cancel($rfid, $fee = NULL) {
  */
 function uc_recurring_get_fees($order, $reset = FALSE) {
   static $fees = array();
-  if ($reset || empty($fees[$order->order_id])) {
+  if ($reset || !isset($order->order_id) || empty($fees[$order->order_id])) {
     if (!empty($order->products)) {
       $products = array();
       foreach ($order->products as $value) {
-        $products[$value->order_product_id] = $value->order_product_id;
+        if (isset($value->order_product_id)) {
+          $products[$value->order_product_id] = $value->order_product_id;
+        }
       }
 
-      $result = db_query("SELECT ru.*, u.name FROM {uc_recurring_users} ru LEFT JOIN {users} u ON u.uid=ru.uid WHERE order_product_id IN (". db_placeholders($products) .")", $products);
-
-      while ($fee = db_fetch_object($result)) {
-        $fee->data = unserialize($fee->data);
-        $fees[$order->order_id][] = $fee;
+      if (!empty($products)) {
+        $result = db_query("SELECT ru.*, u.name FROM {uc_recurring_users} ru LEFT JOIN {users} u ON u.uid=ru.uid WHERE order_product_id IN (". db_placeholders($products) .")", $products);
+        while ($fee = db_fetch_object($result)) {
+          $fee->data = unserialize($fee->data);
+          $fees[$order->order_id][] = $fee;
+        }
       }
     }
   }
-  return !empty($fees[$order->order_id]) ? $fees[$order->order_id] : array();
+  return (isset($order->order_id) && !empty($fees[$order->order_id])) ? $fees[$order->order_id] : array();
 }
 
 /**
@@ -1069,6 +1075,10 @@ function theme_uc_recurring_admin_table($fees) {
   $recurring_states = uc_recurring_fee_status_label();
 
   foreach ($fees as $fee) {
+    if (!isset($fee->name)) {
+      $account = user_load($fee->uid);
+      $fee->name = $account->name;
+    }
     if ($fee->remaining_intervals < 0) {
       $fee_remaining = t('Until cancelled');
     }
@@ -1098,7 +1108,7 @@ function theme_uc_recurring_admin_table($fees) {
   }
 
   // Add the table and pager to the page output.
-  $output .= theme('table', $header, $rows);
+  $output = theme('table', $header, $rows);
   $output .= theme('pager', NULL, variable_get('uc_order_number_displayed', 30), 0);
   
   return $output;
