Index: auction.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ecommerce/contrib/auction/auction.module,v
retrieving revision 1.19.2.3.2.1.2.8
diff -u -r1.19.2.3.2.1.2.8 auction.module
--- auction.module	11 Feb 2007 19:11:52 -0000	1.19.2.3.2.1.2.8
+++ auction.module	12 Feb 2007 10:31:49 -0000
@@ -102,14 +102,14 @@
   if ($type == 'node' && user_access('access content') && $teaser && $node->ptype == 'auction') {
     if (time() > $node->expires) {
       $links['bid'] = array(
-        'title' => t('Biding closed'),                   
+                    'title' => t('Biding closed'),                    
         'attributes' => array('class' => 'bid-link'),
       );
     }
     else {
       $links['bid'] = array(
-        'title' => t('Bid now'),
-        'href' => "node/$node->nid",
+                    'title' => t('Bid now'),
+                    'href' => "node/$node->nid",
         'attributes' => array('title' => t('Place your bid for this item'), 'class' => 'bid-link'),
       );
     }
@@ -131,7 +131,10 @@
   }
 }
 
-function auction_productapi(&$node, $op, $a3 = null, $a4 = null) {
+/*
+ * Implemetation of ecommerce' hook_productapi() 
+ */
+function auction_productapi(&$node, $op, $a3 = null, $a4 = null) {    
 
   switch ($op) {
     /* Due to the way we have to build our node, we have to pass an array of values this module collects. */
@@ -164,7 +167,7 @@
       $buyer = db_result(db_query_range('SELECT uid FROM {ec_auction_bid} WHERE uid = %d ORDER BY created DESC', $user->uid, 0, 1));
       return (($buyer && $expired) ? true : false);
 
-    case 'form':
+    case 'form':     
       if (!$node->expires) {
         $node->expires = time() + (variable_get('auction_days', 1) * 24 * 60 * 60);
       }
@@ -203,10 +206,32 @@
     case 'delete':
       db_query('DELETE FROM {ec_product_auction} WHERE nid = %d', $node->nid);
       db_query('DELETE FROM {ec_auction_bid} WHERE nid = %d', $node->nid);
+      break;    
   }
 }
 
-function auction_page($nid, $action) {
+/*
+ * Implemetation of hook_nodeapi()   
+ * NOTE: product_view() missing since e-commerce v.3* 
+ * 
+ */
+function auction_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {   
+  switch ($op) {
+    case 'view':
+     // $a3 is teaser, $a4 is page attr. in $op='view' case
+     global $user;
+     if ($a3) { return $node; }
+     $form = drupal_get_form('product_auction_view_form',$node,$user);
+     $node->content['body']['#value'] .= $form; // it appends rendered form to node body (node->body go through filters)
+     break;
+   }
+}
+
+/**
+ * Auction page
+ * $_POST['edit'], $_POST['op'] maybe not needed in D5 after small rework 
+ */
+function auction_page($nid, $action) {  
   $edit = $_POST['edit'];
   $op   = $_POST['op'];
 
@@ -218,16 +243,7 @@
 
   if (user_access('place bids')) {
     switch ($op ? $op : $action) {
-      case t('Confirm bid'):
-        if (auction_bid_validate($edit)) {
-          auction_bid_save($edit);
-          drupal_goto('node/'. $edit['nid']);
-        }
-        else {
-          $output = auction_bid_form($edit);
-        }
-        break;
-
+      //case t('Confirm bid') control of this part moved to auction_bid_form_validate & submit
       case t('Pay for your items'):
         drupal_goto('cart/'. $edit['nid']. '/add');
         break;
@@ -238,13 +254,13 @@
         }
         else {
           $edit['nid'] = $nid;
-          $output = auction_bid_form($edit);
+          $output = drupal_get_form('auction_bid_form',$edit);
         }
         break;
 
       default:
         $edit['nid'] = $nid;
-        $output = auction_bid_form($edit);
+        $output =  drupal_get_form('auction_bid_form',$edit);
         break;
     }
     return $output;
@@ -293,17 +309,17 @@
  * But between you and me, I am suspicious of the logic.
  *
  */
-function auction_item_paid_for($nid) {
+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);
-  if (!$result) {
+  if ($result) { // NOTE: strange, there was (!$result) before, I've missed something?
     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 {
@@ -339,7 +355,8 @@
 
   if ($rows) {
     $output = theme('table', $header, $rows);
-  }
+  }    
+  $output.= l(t('Back'),'node/'.$nid);  
   return $output;
 }
 
@@ -348,15 +365,13 @@
  * is a number and is higher than the previous bid.
  *
  */
-function auction_bid_validate($edit) {
-
-  $errors = array();
-  if (isset($edit['bid'])) {
+function auction_bid_form_validate($form_id,$form_values) {  
+  if (isset($form_values['bid'])) {
 
-    if (is_numeric($edit['bid'])) {
-      if ($edit['price']) {
-        if ($edit['bid'] <= $edit['price']) {
-          $errors['bid'] = t('You must bid more than %current-price.', array('%current-price' => payment_format($edit['price'])));
+    if (is_numeric($form_values['bid'])) {
+      if ($form_values['price']) {
+        if ($form_values['bid'] <= $form_values['price']) {
+          $errors['bid'] = t('You must bid more than %current-price.', array('%current-price' => payment_format($form_values['price'])));
         }
       }
       else {
@@ -369,8 +384,15 @@
   }
   foreach ($errors as $name => $message) {
     form_set_error($name, $message);
-  }
-  return count($errors) == 0;
+  }  
+}
+
+/**
+ * Submit form, which passed validation, save bid to database
+ */
+function auction_bid_form_submit($form_id,$form_values) {  
+  auction_bid_save($form_values);
+  drupal_goto('node/'. $form_values['nid']);
 }
 
 function auction_bid_save($edit) {
@@ -387,7 +409,7 @@
  *   Object. A replica of the current node for editing.
  */
 function auction_bid_form($edit) {
-  $product = node_load($edit['nid']);
+  $product = node_load($edit['nid']);  
 
   if (time() > $product->expires) {
     drupal_set_message(t('This auction has ended.'));
@@ -419,7 +441,7 @@
     '#type' => 'submit',
     '#value' => t('Confirm bid'),
   );
-  return drupal_get_form('auction_bid_form', $form);
+  return $form;
 }
 
 /**
@@ -447,26 +469,47 @@
 }
 
 /**
- * Called via product_view() which is an implementation of hook_view().
- * Generates the HTML elements required to view an auction. Contrary to
- * its name, do not retheme this function. Please theme theme_product_auction_form()
- * instead.
- *
- * @param $node
- *   Object. The node being displayed.
- * @param $tease
- *   Boolean. Whether we are to generate a "teaser" or summary of the node, rather than
- *   display the whole thing.
- * @param $page
- *   Boolean. Whether the node is being displayed as a standalone page. If this is TRUE,
- *   the node title should not be displayed, as it will be printed automatically by the
- *   theme system. Also, the module may choose to alter the default breadcrumb trail in this case.
+ * Formats the HTML elements that you see when you _View_ an auction.
+ * You can override this function. Refer to Theming at http://drupal.org/node/55126.
+ * Called indirectly by theme_product_auction_view()
+ *
+ * @param $form
+ *   A formapi array which contains the form elements (eg. the 'Place Bid'
+ *   submit element). $form['theme'] contains the non-form data.
+ *
  */
-function theme_product_auction_view($node, $teaser = 0, $page = 0) {
+function theme_product_auction_view_form($form) {
+  
+  drupal_set_title(t('Bidding History'));
+  $output = '<p class="auction-message">'. $form['theme']['#message'] .'</p>';
 
-  global $user;
-  if ($teaser) { return $node; }
+  $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 = l(t('%num-bids',array('%num-bids' => format_plural($form['theme']['#high_bid']['count'], '1 bid', '@count bids'))),'auction/'. $form['theme']['#nid'] .'/bid/history',array(),NULL,NULL,FALSE,TRUE);
+  //WTF? $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,drupal_render($form['submit'])); }
 
+  $output .= theme('table', $header, $rows);
+  $output .= drupal_render($form);
+  return $output;
+}
+
+/**
+ * Create form array and load some useful information for later theming
+ *
+ * @param 
+ *   Object $node
+ * @param
+ *   Object $user
+ * @return
+ *   A formapi array which contains the form elements (eg. the 'Place Bid'
+ *   submit element). Array['theme'] contains the non-form data.
+ */
+
+function product_auction_view_form($node,$user){
   /* Build some variables for theming. */
   $high_bid = auction_bid_current($node->nid);
   $form['theme'] = array();
@@ -523,37 +566,6 @@
       $form['nid'] =  array('#type' => 'hidden', '#value' => $node->nid);
       $form['#action'] = url("auction/checkout");
     }
-  }
-  $output = '<h3>'. check_plain($node->title) .'</h3>';
-  $output .= drupal_get_form('product_auction_form', $form);
-  $node->body = $output . $node->body;
-  return $node;
-}
-
-/**
- * Formats the HTML elements that you see when you _View_ an auction.
- * You can override this function. Refer to Theming at http://drupal.org/node/55126.
- * Called indirectly by theme_product_auction_view()
- *
- * @param $form
- *   A formapi array which contains the form elements (eg. the 'Place Bid'
- *   submit element). $form['theme'] contains the non-form data.
- *
- */
-function theme_product_auction_form($form) {
-
-  drupal_set_title(t('Bidding History'));
-  $output = '<p class="auction-message">'. $form['theme']['#message'] .'</p>';
-
-  $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,drupal_render($form['submit'])); }
-
-  $output .= theme('table', $header, $rows);
-  $output .= drupal_render($form);
-  return $output;
-}
+  }  
+  return $form;
+}
\ No newline at end of file
