From e6bf2a70628e5389dba6fbabb51a1e83163168d6 Mon Sep 17 00:00:00 2001
From: Dave Long <dave@longwaveconsulting.com>
Date: Fri, 18 Mar 2011 14:36:34 +0000
Subject: [PATCH] Backport UcOrder object from D7, allowing other modules to extend the order schema.

---
 uc_cart/uc_cart.pages.inc |   46 ++-------------------
 uc_order/uc_order.module  |   96 ++++++++++++++++++++++++++++++---------------
 2 files changed, 68 insertions(+), 74 deletions(-)

diff --git a/uc_cart/uc_cart.pages.inc b/uc_cart/uc_cart.pages.inc
index 6008a69..6a290a2 100644
--- a/uc_cart/uc_cart.pages.inc
+++ b/uc_cart/uc_cart.pages.inc
@@ -134,60 +134,22 @@ function uc_cart_checkout_form() {
     $order = uc_order_load($_SESSION['cart_order']);
   }
   else {
-    $order = NULL;
+    $order = new UcOrder();
   }
 
   // Check the referer URI to clear order details and prevent identity theft.
   if (uc_referer_check(array('cart/checkout', 'cart/checkout/review'))) {
     if ($order == FALSE || uc_order_status_data($order->order_status, 'state') != 'in_checkout') {
       unset($_SESSION['cart_order']);
-      $order = NULL;
+      $order = new UcOrder();
     }
     elseif (uc_order_status_data($order->order_status, 'state') != 'in_checkout' || ($user->uid > 0 && $user->uid != $order->uid)) {
-      $order = NULL;
+      $order = new UcOrder();
     }
   }
   else {
     unset($_SESSION['cart_order']);
-    $order = NULL;
-  }
-
-  if (is_null($order)) {
-    $order = new stdClass();
-    $order->order_id = 0;
-    $order->uid = 0;
-    $order->order_status = '';
-    $order->order_total = 0;
-    $order->primary_email = '';
-
-    $order->delivery_first_name = '';
-    $order->delivery_last_name = '';
-    $order->delivery_phone = '';
-    $order->delivery_company = '';
-    $order->delivery_street1 = '';
-    $order->delivery_street2 = '';
-    $order->delivery_city = '';
-    $order->delivery_zone = 0;
-    $order->delivery_postal_code = '';
-    $order->delivery_country = 0;
-    $order->billing_first_name = '';
-    $order->billing_last_name = '';
-    $order->billing_phone = '';
-    $order->billing_company = '';
-    $order->billing_street1 = '';
-    $order->billing_street2 = '';
-    $order->billing_city = '';
-    $order->billing_zone = 0;
-    $order->billing_postal_code = '';
-    $order->billing_country = 0;
-
-    $order->products = array();
-    $order->line_items = array();
-
-    $order->payment_method = '';
-    $order->data = array();
-    $order->created = 0;
-    $order->modified = 0;
+    $order = new UcOrder();
   }
 
   $form['panes'] = array('#tree' => TRUE);
diff --git a/uc_order/uc_order.module b/uc_order/uc_order.module
index 3de59a5..e1a8723 100644
--- a/uc_order/uc_order.module
+++ b/uc_order/uc_order.module
@@ -14,6 +14,53 @@ require_once('uc_order.order_pane.inc');
 require_once('uc_order.line_item.inc');
 require_once('uc_order.ca.inc');
 
+/**
+ * Defines an order object.
+ */
+class UcOrder {
+
+  public $order_id = 0;
+  public $uid = 0;
+  public $order_status = '';
+  public $order_total = 0;
+  public $primary_email = '';
+
+  public $delivery_first_name = '';
+  public $delivery_last_name = '';
+  public $delivery_phone = '';
+  public $delivery_company = '';
+  public $delivery_street1 = '';
+  public $delivery_street2 = '';
+  public $delivery_city = '';
+  public $delivery_zone = 0;
+  public $delivery_postal_code = '';
+  public $delivery_country = 0;
+
+  public $billing_first_name = '';
+  public $billing_last_name = '';
+  public $billing_phone = '';
+  public $billing_company = '';
+  public $billing_street1 = '';
+  public $billing_street2 = '';
+  public $billing_city = '';
+  public $billing_zone = 0;
+  public $billing_postal_code = '';
+  public $billing_country = 0;
+
+  public $products = array();
+  public $line_items = array();
+
+  public $payment_method = '';
+  public $data = array();
+  public $created = 0;
+  public $modified = 0;
+
+  function __construct() {
+    $this->order_status = uc_order_state_default('in_checkout');
+  }
+
+}
+
 /*******************************************************************************
  * Hook Functions (Drupal)
  ******************************************************************************/
@@ -1007,7 +1054,7 @@ function op_products_edit_table() {
  * Generate a new order for user $uid.
  */
 function uc_order_new($uid = 0, $state = 'in_checkout') {
-  $order = new stdClass();
+  $order = new UcOrder();
 
   if ($uid > 0) {
     $user = user_load(array('uid' => $uid));
@@ -1017,19 +1064,10 @@ function uc_order_new($uid = 0, $state = 'in_checkout') {
 
   $order->uid = $uid;
   $order->order_status = uc_order_state_default($state);
-  $order->products = array();
+  $order->created = time();
+  $order->modified = time();
 
-  db_query("INSERT INTO {uc_orders} (uid, order_status, order_total, "
-          ."primary_email, delivery_first_name, delivery_last_name, delivery_phone, "
-          ."delivery_company, delivery_street1, delivery_street2, delivery_city, "
-          ."delivery_zone, delivery_postal_code, delivery_country, billing_first_name, "
-          ."billing_last_name, billing_phone, billing_company, billing_street1, "
-          ."billing_street2, billing_city, billing_zone, billing_postal_code, "
-          ."billing_country, payment_method, data, created, modified) VALUES "
-          ."(%d, '%s', 0, '%s', '', '', '', '', '', '', '', 0, '', 0, '', "
-          ."'', '', '', '', '', '', 0, 0, 0, '', '', %d, %d)",
-           $uid, $order->order_status, $email, time(), time());
-  $order->order_id = db_last_insert_id('uc_orders', 'order_id');
+  drupal_write_record('uc_orders', $order);
 
   uc_order_module_invoke('new', $order, NULL);
 
@@ -1046,24 +1084,18 @@ function uc_order_save($order) {
 
   uc_order_module_invoke('presave', $order, NULL);
 
-  db_query("UPDATE {uc_orders} SET uid = %d, order_status = '%s', order_total = %f, product_count = %d, primary_email = '%s', "
-          ."delivery_first_name = '%s', delivery_last_name = '%s', delivery_phone = '%s', "
-          ."delivery_company = '%s', delivery_street1 = '%s', delivery_street2 = '%s', "
-          ."delivery_city = '%s', delivery_zone = %d, delivery_postal_code = '%s', delivery_country = %d, "
-          ."billing_first_name = '%s', billing_last_name = '%s', billing_phone = '%s', "
-          ."billing_company = '%s', billing_street1 = '%s', billing_street2 = '%s', "
-          ."billing_city = '%s', billing_zone = %d, billing_postal_code = '%s', billing_country = %d, "
-          ."payment_method = '%s', data = '%s', host = '%s', modified = %d WHERE order_id = %d",
-           $order->uid, $order->order_status, uc_order_get_total($order), uc_order_get_product_count($order),
-           $order->primary_email, $order->delivery_first_name, $order->delivery_last_name, $order->delivery_phone,
-           $order->delivery_company, $order->delivery_street1, $order->delivery_street2,
-           $order->delivery_city, $order->delivery_zone, $order->delivery_postal_code,
-           ((is_null($order->delivery_country) || $order->delivery_country == 0) ? variable_get('uc_store_country', 840) : $order->delivery_country),
-           $order->billing_first_name, $order->billing_last_name, $order->billing_phone,
-           $order->billing_company, $order->billing_street1, $order->billing_street2,
-           $order->billing_city, $order->billing_zone, $order->billing_postal_code,
-           ((is_null($order->billing_country) || $order->billing_country == 0) ? variable_get('uc_store_country', 840) : $order->billing_country),
-           $order->payment_method, serialize($order->data), ip_address(), time(), $order->order_id);
+  $order->order_total = uc_order_get_total($order);
+  $order->product_count = uc_order_get_product_count($order);
+  if (is_null($order->delivery_country) || $order->delivery_country == 0) {
+    $order->delivery_country = variable_get('uc_store_country', 840);
+  }
+  if (is_null($order->billing_country) || $order->billing_country == 0) {
+    $order->billing_country = variable_get('uc_store_country', 840);
+  }
+  $order->host = ip_address();
+  $order->modified = time();
+
+  drupal_write_record('uc_orders', $order, 'order_id');
 
   if (isset($order->products) && is_array($order->products)) {
     foreach ($order->products as $product) {
-- 
1.6.0.4

