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	13 Feb 2007 02:09:45 -0000
@@ -117,6 +117,19 @@
   return $links;
 }
 
+/*
+ * Implemetation of hook_links_alter()
+ * 
+ * It removes "Add to cart" link from links array even if it is set up to ON on node edit page.
+ * "This item is in Your shopping cart" link is not affected. Only user who won the auction should
+ * add auction to the cart - and there is special form for it.  
+ */
+function auction_link_alter(&$node, &$links) {
+	if (isset($node->ptype) && $node->ptype == 'auction' && $links['add_to_cart']['title'] == t('Add to cart')) {
+	  unset($links['add_to_cart']);
+	}  
+}
+
 /**
  * Implementation of hook_user().
  */
@@ -131,7 +144,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. */
@@ -203,9 +219,30 @@
     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;
   }
 }
 
+/*
+ * Implemetation of hook_nodeapi()
+ * NOTE: product_view() missing since e-commerce v.3* 
+ * 
+ */
+function auction_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
+switch ($op && isset($node->ptype) && $node->ptype == 'auction') {
+  case 'view':
+    global $user;
+    if (!$teaser) {
+      $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)  	 
+    }  	 
+  }   
+}
+
+/**
+ * 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 +255,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 +266,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;
@@ -275,7 +303,7 @@
     $title = t('Checkout');
     switch ($op) {
       default:
-        $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());
+        $result = db_query('SELECT nid, uid, bid FROM {ec_auction_bid} WHERE (nid, uid, bid) IN (SELECT bid.nid, bid.uid, MAX(bid.bid) FROM {ec_auction_bid} bid INNER JOIN {ec_product_auction} auc ON bid.nid = auc.nid WHERE auc.expires < %d GROUP BY nid) AND uid = %d;', time(), $user->uid);                 
         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);
@@ -297,7 +325,7 @@
   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) {
@@ -339,7 +367,8 @@
 
   if ($rows) {
     $output = theme('table', $header, $rows);
-  }
+  } 
+  $output.= l(t('Back'),'node/'.$nid);
   return $output;
 }
 
@@ -348,15 +377,13 @@
  * is a number and is higher than the previous bid.
  *
  */
-function auction_bid_validate($edit) {
+function auction_bid_form_validate($form_id,$form_values) {
+  if (isset($form_values['bid'])) {
 
-  $errors = array();
-  if (isset($edit['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 +396,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) {
@@ -419,7 +453,7 @@
     '#type' => 'submit',
     '#value' => t('Confirm bid'),
   );
-  return drupal_get_form('auction_bid_form', $form);
+  return $form;
 }
 
 /**
@@ -447,26 +481,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 +578,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
