Index: auction.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ecommerce/contrib/auction/auction.module,v
retrieving revision 1.16
diff -u -F^function -r1.16 auction.module
--- auction.module	7 May 2006 16:11:08 -0000	1.16
+++ auction.module	10 May 2006 02:58:23 -0000
@@ -9,18 +9,18 @@
  * Implementation of hook_help().
  */
 function auction_help($section = 'admin/help#auction') {
-  $output = "";
-
   switch ($section) {
     case 'admin/modules#description':
       return t('Create auction products. Dependency: product.module');
+    case 'node/add/product#auction':
+      return t('A product that can be auctioned for a fixed period, and then sold to the highest bidder.');
   }
 }

 /**
- * Implementation of hook_settings().
+ * Implementation of hook_ec_settings().
  */
-function auction_settings() {
+function auction_ec_settings() {

   $form['auction_days'] = array(
     '#type' => 'textfield',
@@ -118,13 +118,8 @@ function auction_productapi(&$node, $op,
         if (!$node->expires) {
           form_set_error('expires', t('Please enter an expiration date.'));
         }
-        if (!is_numeric($node->expires)) {
-          if (strtotime($node->expires) != -1) {
-            $node->expires = strtotime($node->expires);
-          }
-          else {
-            form_set_error('expires', t('Invalid date specified.'));
-          }
+        elseif (strtotime($node->expires) < 1) {
+          form_set_error('expires', t('Invalid date specified.'));
         }
       }
       break;
@@ -133,10 +128,11 @@ function auction_productapi(&$node, $op,
       return array('auction' => t('auction item'));

     case 'adjust_price':
-      $high_bidder = auction_get_current_bid($node->nid);
-      return (($high_bidder->bid) ? $high_bidder->bid : $node->price);
+      $high_bidder = auction_bid_current($node->nid);
+      return ($high_bidder['bid']) ? $high_bidder['bid'] : $node->price;

     case 'cart add item':
+      /* This is not currently called internally. */
       global $user;
       $expired = (time() > $node->expires) ? true : false;
       $buyer = db_result(db_query_range('SELECT uid FROM {ec_auction_bid} WHERE uid = %d ORDER BY created DESC', $user->uid, 0, 1));
@@ -163,13 +159,14 @@ function auction_productapi(&$node, $op,

     /* Node has been saved, write to product tables. */
     case 'insert':
-//      var_dump ($node->expires); die();
+      $node->expires = strtotime($node->expires);
       return db_query('INSERT INTO {ec_product_auction} (nid, expires) VALUES (%d, %d)', $node->nid, $node->expires);

     case 'attributes':
       return array('in_stock', 'is_shippable');

     case 'update':
+      $node->expires = strtotime($node->expires);
       return db_query('UPDATE {ec_product_auction} SET expires = %d WHERE nid = %d', $node->expires, $node->nid);

     case 'delete':
@@ -241,25 +238,13 @@ function auction_checkout_page() {
     $title = t('Checkout');
     switch ($op) {
       default:
-        $result = db_query('SELECT DISTINCT nid FROM {ec_auction_bid} ORDER BY nid');
-        while ($data = db_fetch_object($result)) {
-          $item = db_fetch_object(db_query('SELECT nid, bid, uid FROM {ec_auction_bid} WHERE nid = %d ORDER BY created DESC', $data->nid));
-          if ($item->uid == $user->uid) {
-            $nids[] = $item->nid;
-            if (db_result(db_query("SELECT qty FROM {ec_cart} WHERE cookie_id = '%s' AND nid = '%d'", cart_get_id(), $item->nid)) == 0) {
-              cart_add_item($item->nid, 1, null);
-            }
-            $add_to_cart[] = $item;
+        $result = db_query('SELECT bid.* FROM {ec_auction_bid} bid INNER JOIN {ec_product_auction} auc ON bid.nid = auc.nid WHERE bid.uid = %d AND auc.expires < %d', $user->$uid, time());
+        while ($item = db_fetch_object($result)) {
+          if (!db_result(db_query("SELECT qty FROM {ec_cart} WHERE cookie_id = '%s' AND nid = '%d'", cart_get_id(), $item->nid))) {
+            cart_add_item($item->nid, 1, null);
           }
         }
-        $paid_for = db_result(db_query('SELECT COUNT(t.txnid) FROM {ec_transaction_product} tp, {ec_transaction} t WHERE t.payment_status = %d AND t.txnid = tp.txnid AND t.uid = %d AND nid IN (%s)', payment_get_status_id('completed'), $user->uid, implode(',', $nids)));
-        if ($add_to_cart && !$paid_for) {
-          drupal_goto('cart/checkout');
-        }
-        else {
-          $title = t('Auction');
-          $output = t('You have no winning items to pay for.');
-        }
+        drupal_goto('cart/checkout');
         break;
     }
     return $output;
@@ -273,13 +258,25 @@ function auction_item_paid_for($nid) {
   global $user;

   $result = db_query('SELECT DISTINCT nid FROM {ec_auction_bid} WHERE nid = %d ORDER BY nid', $nid);
-  while ($data = db_fetch_object($result)) {
-    $item = db_fetch_object(db_query('SELECT nid, bid, uid FROM {ec_auction_bid} WHERE nid = %d ORDER BY created DESC', $data->nid));
-    if ($item->uid == $user->uid) {
-      $nids[] = $item->nid;
+  if (!$result) {
+    while ($data = db_fetch_object($result)) {
+      $item = db_fetch_object(db_query('SELECT nid, bid, uid FROM {ec_auction_bid} WHERE nid = %d ORDER BY created DESC', $data->nid));
+      if ($item->uid == $user->uid) {
+        $nids[] = $item->nid;
+      }
     }
+    return db_result(db_query('SELECT COUNT(t.txnid) FROM {ec_transaction_product} tp, {ec_transaction} t WHERE t.payment_status = %d AND t.txnid = tp.txnid AND t.uid = %d AND nid IN (%s)', payment_get_status_id('completed'), $user->uid, implode(',', $nids)));
+  }
+  else {
+    return FALSE;
   }
-  return db_result(db_query('SELECT COUNT(t.txnid) FROM {ec_transaction_product} tp, {ec_transaction} t WHERE t.payment_status = %d AND t.txnid = tp.txnid AND t.uid = %d AND nid IN (%s)', payment_get_status_id('completed'), $user->uid, implode(',', $nids)));
+}
+
+function auction_bid_current($nid) {
+  $result = db_query('SELECT * FROM {ec_auction_bid} WHERE nid = %d ORDER BY created DESC', $nid);
+  $high_bid = db_fetch_array($result);
+  $high_bid['count'] = db_num_rows($result);
+  return $high_bid;
 }

 function auction_bid_history($nid) {
@@ -298,10 +295,6 @@ function auction_bid_history($nid) {
   return $output;
 }

-function auction_bid_history_count($nid) {
-  return db_result(db_query('SELECT COUNT(bid) FROM {ec_auction_bid} WHERE nid = %d', $nid));
-}
-
 function auction_bid_form($edit) {
   $product = node_load($edit['nid']);

@@ -310,16 +303,16 @@ function auction_bid_form($edit) {
     drupal_goto("node/$product->nid");
   }

-  $form['data'] = array();
-  $form['data']['#high_bid'] = (array)auction_get_current_bid($edit['nid']);
-  $form['data']['#current_bid'] = ($form['data']['#high_bid']['bid']) ? payment_format($form['data']['#high_bid']['bid']) : payment_format($product->price);
+  $form['theme'] = array();
+  $form['theme']['#high_bid'] = auction_bid_current($edit['nid']);
+  $form['theme']['#current_bid'] = ($form['theme']['#high_bid']['bid']) ? $form['theme']['#high_bid']['bid'] : $product->price;
   $form['bid'] = array(
     '#type' => 'textfield',
     '#title' => '',
     '#default_value' => $edit['bid'],
     '#size' => 10,
     '#maxlength' => 50,
-    '#description' => t('Enter an amount above %base-bid-amount.', array('%base-bid-amount' => $form['data']['#current_bid'])),
+    '#description' => t('Enter an amount above %base-bid-amount.', array('%base-bid-amount' => $form['theme']['#current_bid'])),
     '#attributes' => null,
     '#required' => true,
   );
@@ -329,7 +322,7 @@ function auction_bid_form($edit) {
   );
   $form['price'] = array(
     '#type' => 'hidden',
-    '#value' => $form['data']['#current_bid'],
+    '#value' => $form['theme']['#current_bid'],
   );
   $form['submit'] = array(
     '#type' => 'submit',
@@ -344,7 +337,7 @@ function theme_auction_bid_form($form) {
   drupal_set_title(t('Place a Bid'));

   $header = array();
-  $rows[] = array(t('Current bid:'), $form['data']['#current_bid']);
+  $rows[] = array(t('Current bid:'), payment_format($form['theme']['#current_bid']));
   $rows[] = array(t('Your bid:'), form_render($form['bid']));
   $rows[] = array('&nbsp;', form_render($form['submit']));

@@ -355,8 +348,10 @@ function theme_auction_bid_form($form) {
 }

 function auction_bid_validate($edit) {
+
   $errors = array();
   if (isset($edit['bid'])) {
+
     if (is_numeric($edit['bid'])) {
       if ($edit['price']) {
         if ($edit['bid'] <= $edit['price']) {
@@ -383,44 +378,71 @@ function auction_bid_save($edit) {
   drupal_set_message(t('Your bid of %bid-amount has been placed.', array('%bid-amount' => payment_format($edit['bid']))));
 }

-function auction_get_current_bid($nid) {
-  return db_fetch_object(db_query_range('SELECT * FROM {ec_auction_bid} WHERE nid = %d ORDER BY created DESC', $nid, 0, 1));
-}
-
 function theme_product_auction_view($node, $teaser = 0, $page = 0) {

+  global $user;
   if ($teaser) { return $node; }

-  // need to put the formatting in theme_theme_product_auction_view, so add additional info to the $form array
-  $form['data'] = array();
-  $form['data']['#nid'] = $node->nid;
-  $form['data']['#count'] = auction_bid_history_count($node->nid);
-  $form['data']['#expire_time'] = $node->expires;
-  $form['data']['#high_bid'] = (array)auction_get_current_bid($node->nid);
-  $form['data']['#current_bid'] = ($form['data']['#high_bid']['bid']) ? payment_format($form['data']['#high_bid']['bid']) : payment_format($node->price);
-
-  if (auction_productapi($node, 'cart add item')) {
-    //and is this item is not already paid for
-    if (!auction_item_paid_for($node->nid)) {
-       $form['submit'] = array('#type' => 'submit', '#value' => t('Pay for your items'));
-       $form['nid'] =  array('#type' => 'hidden', '#value' => $node->nid);
+  /* Build some variables for theming. */
+  $high_bid = auction_bid_current($node->nid);
+  $form['theme'] = array();
+  $form['theme']['#nid'] = $node->nid;
+  $form['theme']['#expire_time'] = $node->expires;
+  $form['theme']['#high_bid'] = $high_bid;
+  if ($high_bid['bid']) {
+    $form['theme']['#high_bid_amount'] = payment_format($high_bid['bid']);
+    $form['theme']['#high_bid_name'] = db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $high_bid['uid']));
+  }
+  else {
+    $form['theme']['#high_bid_amount'] = payment_format($node->price);
+    $form['theme']['#high_bid_name'] = t('No bids');
+  }
+  /* Determine the required function. */
+  if (time() < $node->expires) {
+    /* Auction is active. */
+    if ($high_bid['uid'] == $user->uid) {
+      /* User is the highest bidder. */
+      $form['theme']['#form'] = FALSE;
+      $form['theme']['#message'] = t('You are currently the highest bidder.');
+    }
+    elseif (user_access('place bids')) {
+      /* User is allowed to bid. */
+      $form['theme']['#form'] = TRUE;
+      $form['theme']['#message'] = t('You may place a bid.');
+      $form['submit'] = array('#type' => 'submit', '#value' => t('Place Bid'));
+      $form['nid'] =  array('#type' => 'hidden', '#value' => $node->nid);
+      $form['#action'] = url("auction/$node->nid/bid");
     }
     else {
-       $form['data']['#button'] = t('You\'ve already paid for this item.');
+      /* User is not allowed to bid. */
+      $form['theme']['#form'] = FALSE;
+      $form['theme']['#message'] = t('You are not authorised to bid.');
     }
   }
-  elseif ($node->expires > time()) {
-     $form['submit'] = array('#type' => 'submit', '#value' => t('Place Bid'));
-  }
-
-  if (time() > $node->expires) {
-    $form['#action'] = url("auction/checkout");
-  }
   else {
-    $form['nid'] =  array('#type' => 'hidden', '#value' => $node->nid);
-    $form['#action'] = url("auction/$node->nid/bid");
+    /* Auction has expired. */
+    if ($high_bid['uid'] != $user->uid) {
+      /* User is NOT the highest bidder. */
+      $form['theme']['#form'] = FALSE;
+      $form['theme']['#message'] = t('This auction is over.');
+    }
+    elseif (auction_item_paid_for($node->nid)) {
+      /* User IS the highest bidder AND has paid. */
+      $form['theme']['#form'] = FALSE;
+      $form['theme']['#message'] = t('You have paid for this item.');
+    }
+    else {
+      /* User IS the highest bidder AND still has to pay */
+      $form['theme']['#form'] = TRUE;
+      $form['theme']['#message'] = t('You\'ve won this auction!');
+      $form['submit'] = array('#type' => 'submit', '#value' => t('Proceed to Checkout'));
+      $form['nid'] =  array('#type' => 'hidden', '#value' => $node->nid);
+      $form['#action'] = url("auction/checkout");
+    }
   }
-  $node->body = drupal_get_form('product_auction_form', $form) .'<p>&nbsp;</p><h3>'. t('Description') .'</h3>'. $node->body;
+  $output = '<h3>'. $node->title .'</h3>';
+  $output .= drupal_get_form('product_auction_form', $form);
+  $node->body = $output . $node->body;
   return $node;
 }

@@ -428,27 +450,17 @@ function theme_product_auction_form($for

   drupal_set_title(t('Bidding History'));

-  $header = array();
-  $rows[] = array(t('Current bid:'), $form['data']['#current_bid']);
-  if (isset($form['data']['#button'])) {
-    $rows[] = array('&nbsp;', $button);
-  }
-  else {
-    $rows[] = array('&nbsp',form_render($form['submit']));
-  }
-  $rows[] = array(t('Time left:'), format_interval($form['data']['#expire_time'] - time()));
+  $output = '<p class="auction-message">'. $form['theme']['#message'] .'</p>';

-  //$rows[] = array(t('Start time:'), format_date($node->created));
-  if ($form['data']['#current_bid']) {
-    $rows[] = array(t('High bidder:'), db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $form['data']['#high_bid']['uid'])));
-    $bid_history = t('(<a href="%bid-history-link">%num-bids</a>)', array('%bid-history-link' => url('auction/'. $form['data']['#nid'] .'/bid/history'), '%num-bids' => format_plural($form['data']['#count'], '1 previous bid', '%count previous bids')));
-    $rows[] = array(t('Bid history'), $bid_history);
-  }
-  else {
-    $rows[] = array(t('High bidder:'), t('none'));
-  }
-
-  $output = theme('table', $header, $rows);
+  $header = array();
+  $rows[] = array(t('Time left:'), format_interval($form['theme']['#expire_time'] - time()));
+  $rows[] = array(t('Current bid:'), $form['theme']['#high_bid_amount']);
+  $rows[] = array(t('High bidder:'), $form['theme']['#high_bid_name']);
+  $bid_history = t('(<a href="%bid-history-link">%num-bids</a>)', array('%bid-history-link' => url('auction/'. $form['theme']['#nid'] .'/bid/history'), '%num-bids' => format_plural($form['theme']['#high_bid']['count'], '1 bid', '%count bids')));
+  $rows[] = array(t('Bid history'), $bid_history);
+  if ($form['theme']['#form']) {$rows[] = array(NULL,form_render($form['submit'])); }

+  $output .= theme('table', $header, $rows);
+  $output .= form_render($form);
   return $output;
 }
