Member Classifieds PDF Print E-mail



/**
* Database Configuration
*/
define(PT_DB_SERVER, 'localhost');
define(PT_DB_NAME, 'puttertalk');
define(PT_DB_USER, 'puttertalk');
define(PT_DB_PASSWORD, 'Igot0ne');


/**
* CJ and eBay Configuration
*/
define('TRACKING_PARTNER_CODE', '9'); // 1 for CJ, 9 is new ebay one
define('TRACKING_ID', '2793737'); // PID for CJ
define('AFFILIATE_USER_ID', 'PTW'); // shopping ID for CJ
define('EBAY_APP_ID', 'PutterTa-c153-44f2-b04f-1b0dd3f23ca9'); // eBay appID
define('EBAY_CAMPAIGN_ID', '5335876545'); // campaign id for PTW

/**
* Page Configuration vars
*/
define('SMF_AVATAR_PATH', 'http://www.puttertalk.com/community/attachments/');
define('EBAY_ENDPOINT', 'http://open.api.ebay.com/shopping?');
define('SMF_PROFILE_PATH', 'http://www.puttertalk.com/community/index.php?action=profile;u=');


try {
$db = new PDO('mysql:host=' . PT_DB_SERVER . ';dbname=' . PT_DB_NAME,
PT_DB_USER, PT_DB_PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, TRUE);

}
catch(PDOException $e) {
echo $e->getMessage();
//error_log($e->getMessage());
exit('Database is temporarily unavailable.');
}




/**
* Function to do the magic -- eventaully make a nice class to build the queries
*/
function constructPostCallAndGetResponse($endpoint, $query, $sellerID = "", $page = 1)
{

if (empty($sellerID)){
$sellerID = "mrdoug";
}

(!empty($_GET['page'])) ? $page = intval($_GET['page']) : $page = '1';

// create the xml request that will be POSTed
$xmlRequest = "";
$xmlRequest .= "";
$xmlRequest .= $sellerID ;
$xmlRequest .= "1513";
$xmlRequest .= "100";
$xmlRequest .= "" . $page . "";
$xmlRequest .= "EndTime";
$xmlRequest .= "SellerInfo";
$xmlRequest .= "1000000";
$xmlRequest .= "
";

// debug request
//echo '
'.str_replace(array('<','>'),array('< ', ' >'),$xmlRequest).'
';

$session = curl_init($endpoint); // create a curl session

curl_setopt($session, CURLOPT_POST, true); // POST request type
curl_setopt($session, CURLOPT_POSTFIELDS, $xmlRequest); // set the body of the POST
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // return values as a string
$headers = array(
'X-EBAY-API-CALL-NAME: FindItemsAdvanced',
'X-EBAY-API-SITE-ID: 0', // Site 0 is for US
'X-EBAY-API-APP-ID: ' . EBAY_APP_ID,
'X-EBAY-API-VERSION: 515',
'X-EBAY-API-REQUEST-ENCODING: XML', // for a POST request, the response is same format
'X-EBAY-API-TRACKING-ID: ' . EBAY_CAMPAIGN_ID,
'X-EBAY-API-TRACKING-PARTNER-CODE: ' . TRACKING_PARTNER_CODE,
'Content-Type: text/xml;charset=utf-8',
);
curl_setopt($session, CURLOPT_HTTPHEADER, $headers); //set headers using the above array
$responseXML = curl_exec($session); // send the request
curl_close($session);

// debug response
//echo '
'.str_replace(array('<','>'),array('< ', ' >'),$responseXML).'
';

return $responseXML; // returns a string of XML
} // END function constructPostCallAndGetResponse

/**
* Convert PnYnMnDTnHnMnS format into easily read time value
*/
function formatEbayTime ( $value ) {
$format = "";

preg_match('@P(\d+)D@', $value, $matches);
if (!empty($matches)) { $format = $matches[1] . " D "; }
preg_match('@T(\d+)H@', $value, $matches);
if (!empty($matches)) { $format .= $matches[1] . " H "; }
preg_match('@H(\d+)M@', $value, $matches);
if (!empty($matches)) { $format .= $matches[1] . " M "; }

return $format;
} // END function formatEbayTime


try {
if(empty($_GET['id'])){
$select_ebay_ids = $db->prepare('SELECT value '
. 'FROM ptc_themes '
. 'WHERE variable = :var '
. 'AND value <> ""');
if (FALSE !== $select_ebay_ids) {
if ($select_ebay_ids->execute(array('var' => 'PT_Ebay'))) {
$ebay_ids = $select_ebay_ids->fetchAll(PDO::FETCH_COLUMN);
}
}

// debeg.. members with ebay info entered
//echo '
';
//print_r($ebay_ids);
//echo '
';
$seller = '';
foreach($ebay_ids as $key => $value) {
$seller .= '' . $value . '';
}
}

else{
// Set single user for display
$seller = '' . ($_GET['id']) . '';
}// END IF(empty($_GET['id']))

// Construct the FindItems Shopping API POST call.
// Load the call and capture the document returned.
$resp = simplexml_load_string(constructPostCallAndGetResponse(EBAY_ENDPOINT, $query, $seller));

// Check to see if the response was loaded, else print an error
if (!empty($resp)) {
// build our result table
$results = '





';

// counter for table backgrounds
$i = 0;
if($resp->TotalItems > 0){
// If the response has valid items parse it and build links
foreach($resp->SearchResult->ItemArray->Item as $item) {
// the following is the information we want to pull out of the response
$link = $item->ViewItemURLForNaturalSearch;
$title = $item->Title;
$price = '$'.str_replace('.', '',number_format($item->ConvertedCurrentPrice, 2)).'';
$thumb = $item->GalleryURL;
$bids = $item->BidCount;
$timeLeft = $item->TimeLeft;
$seller = $item->Seller->UserID;

// grab seller's avatar and forum name from SMF
// CONSIDER: Clean up and optimize.. 200 db queries are possible here
$select_member_info = $db->prepare('SELECT m.ID_MEMBER, m.memberName '
. 'FROM ptc_members AS m '
. 'LEFT JOIN ptc_themes AS t ON m.ID_MEMBER = t.ID_MEMBER '
. 'WHERE t.value = :var');
if (FALSE !== $select_member_info) {
if ($select_member_info->execute(array('var' => $seller))) {
$member_info = $select_member_info->fetch(PDO::FETCH_ASSOC);
}
}

$select_avatar = $db->prepare('SELECT filename '
. 'FROM ptc_attachments '
. 'WHERE ID_MEMBER = :id '
. 'AND ID_MSG = 0');
if (FALSE !== $select_avatar) {
if ($select_avatar->execute(array('id' => $member_info['ID_MEMBER']))) {
$avatar = $select_avatar->fetchColumn();
}
}


// For each SearchResultItem node, build a link and append it to $results
($i % 2 == 0) ? $style = '1' : $style = '2';
$results .= ""
. ""
. ""
. "";

// increment our table stripe counter
$i++;
} // END FOR EACH
// check to build durty navigation
// TODO: build in single user into nav.. in case someone has more than 10 listings
if($resp->TotalPages > 1) {
$nav = 'page ';
for($i = 1; $i <= $resp->TotalPages; $i++) {
$nav .= ''.$i.' ';
}
}
$results .= "
  Image & Description Current Price Time Left
";
if(file_exists('/home/sites/puttertalk.com/community/attachments/' . $avatar)
&& (strlen($avatar) > 3)){
$results .= ""
. "
"
. $member_info['memberName'] ."
";
}
else{
$results .= "
 

"
. "". $member_info['memberName'] ."";
}
$results .= "
"
. ""
. "
"
. "
$title" . $price . "" . formatEbayTime($timeLeft)
. ''
. "
"
. $nav . "
";


}
else{
// No items returned so set message
$results = "Sorry, no items are currently listed...";
} // END IF ($resp->TotalItems > 0)
}
else {
// There was no response, set an error message
$results = "We're sorry... there was no response from eBay!";
} // END IF (!empty($resp))

}
catch(PDOException $e) {
echo('These listings are temporarily unavailable. Please try again later today.
' .
$e->getMessage());
}


?>