<?php
/*
Lite version of update price

- pull out all relevant code into a stand alone snippet

*/

// cater for stand-alone calls
if (!function_exists('get_option')) {
	//require_once('../../../wp-config.php');
	
	define( 'SHORTINIT', true );
 
	// Build the wp-load.php path from a plugin/theme
	$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
	require_once( $parse_uri[0] . 'wp-load.php' );
}
 
include 'wpa_constants.php';

 
if (isset($_GET['update_price'])):

  global $wpdb;

  $options = get_option('wp_auctions');
  $anonymizebidder = $options['anonymizebidder'];
  $engine = $options['engine'];
  $currencysymbol = $options['currencysymbol'];

  $auction_id = isset($_POST['auction_id']) ? intval($_POST['auction_id']) : false;
  if ($auction_id) {
  
    // get bids (use same code as display)
    $table_name = $wpdb->prefix . "wpa_bids";
    if ($engine != 3) {  // 3 = REVERSE_ENGINE
        $strSQL = "SELECT bidder_name, bidder_url ,date,current_bid_price, bid_type FROM $table_name WHERE auction_id=".$auction_id." ORDER BY current_bid_price DESC, bid_type";
    } else {
        $strSQL = "SELECT bidder_name, bidder_url ,date,current_bid_price, bid_type FROM $table_name WHERE auction_id=".$auction_id." ORDER BY current_bid_price, bid_type";
    }
    $rows = $wpdb->get_results ($strSQL);
    
    $topbidder = "";
    foreach ($rows as $bid) {
		if ($topbidder == "") {
        	$topbidder = anon_bidder_name($bid->bidder_name, $anonymizebidder);
			break;  // perf help
      }
    }

  	// prepare result
  	$table_name = $wpdb->prefix . "wpa_auctions";
  	$strSQL = "SELECT current_price,start_price, BIN_price FROM $table_name WHERE id=".$auction_id;
  	$row = $wpdb->get_row ($strSQL);

    if ($row != null) {
       $result = wpa_get_price($row->current_price,$row->start_price,$row->BIN_price,$currencysymbol," ");
       $result .= "|";
       if (!empty($topbidder)) {
          $result .= "<strong>".__('Winning','wpauctions').":</strong> ".$topbidder;
       } else {
          $result .= "&nbsp;";
       }
    } else {
        $result = 0;
    }

    echo $result;
  } else {
     echo "AJAX ERROR: No auction specified";
  }
  
  // exit so we don't have to take additional load
  exit;

endif;

?>