<?php
    
include('./vars.php');
    include(
'./admin-password.php');
    
$bNavHome true;
    include(
'./header.php');

    
$cTitle 'Upload ShareASale Datafeed';
?>
<div class="post">
<h2 class="title"><?= $cTitle ?></h2>
<div class="entry">
<?php
if (is_uploaded_file($_FILES['sas']['tmp_name'])) {
    
// Unzip the file
    
move_uploaded_file($_FILES['sas']['tmp_name'], './temp/' $_FILES['sas']['name']);
    
chdir('./temp/');
    
exec("/usr/bin/unzip " $_FILES['sas']['name'], $aOutput);
    
// print_r($aOutput);

    
$cTxtFile str_replace('.zip''.txt'$_FILES['sas']['name']);

    
// Remove the zip file
    
unlink($_FILES['sas']['name']);

    
// Set up a map to easily reference the data
    
$rsMap[''] = count($rsMap);

    
// Read through the .txt file
    
$fp fopen($cTxtFile"r");
    
$bFirst true;

    
$rsMap['ProductID'] = 0;
    
$rsMap['Name'] = 1;
    
$rsMap['MerchantID'] = 2;
    
$rsMap['Merchant'] = 3;
    
$rsMap['Link'] = 4;
    
$rsMap['Thumbnail'] = 5;
    
$rsMap['BigImage'] = 6;
    
$rsMap['Price'] = 7;
    
$rsMap['RetailPrice'] = 8;
    
$rsMap['Category'] = 9;
    
$rsMap['Subcategory'] = 10;
    
$rsMap['Description'] = 11;
    
$rsMap['Custom1'] = 12;
    
$rsMap['Custom2'] = 13;
    
$rsMap['Custom3'] = 14;
    
$rsMap['Custom4'] = 15;
    
$rsMap['Custom5'] = 16;
    
$rsMap['Lastupdated'] = 17;
    
$rsMap['Status'] = 18;
    
$rsMap['Manufacturer'] = 19;
    
$rsMap['PartNumber'] = 20;
    
$rsMap['MerchantCategory'] = 21;
    
$rsMap['MerchantSubcategory'] = 22;
    
$rsMap['ShortDescription'] = 23;
    
$rsMap['ISBN'] = 24;
    
$rsMap['UPC'] = 25;
    
$rsMap['SKU'] = 26;
    
$rsMap['CrossSell'] = 27;
    
$rsMap['MerchantGroup'] = 28;
    
$rsMap['MerchantSubgroup'] = 29;
    
$rsMap['CompatibleWith'] = 30;
    
$rsMap['CompareTo'] = 31;
    
$rsMap['QuantityDiscount'] = 32;
    
$rsMap['Bestseller'] = 33;
    
$rsMap['AddToCartURL'] = 34;
    
$rsMap['ReviewsRSSURL'] = 35;
    
$rsMap['Option1'] = 36;
    
$rsMap['Option2'] = 37;
    
$rsMap['Option3'] = 38;
    
$rsMap['Option4'] = 39;
    
$rsMap['Option5'] = 40;

    while ((
$rsItem fgetcsv($fp100000"|")) !== FALSE) {
        
$rsItem[$rsMap['Link']] = str_replace('YOURUSERID'$nSASID$rsItem[$rsMap['Link']]);
        if (
$bFirst) {
            
// We'll use this later to determine which products can be deleted
            
mysql_query("update products set bActiveProduct=0 where MerchantID=" . (int)$rsItem[$rsMap['MerchantID']] . "");
            
$bFirst false;    // Only runs once
        
// ends if ($bFirst)

        
if (mysql_num_rows(mysql_query("select * from products where ProductID=" . (int)$rsItem[$rsMap['ProductID']] . " and MerchantID=" . (int)$rsItem[$rsMap['MerchantID']] . " limit 1")) == 0) {
            
// This is a new record
            
$nFPCol 0;
            
$cQuery "insert into products (ProductID, Name, MerchantID, Merchant, Link, Thumbnail, BigImage, Price, RetailPrice, Category, Subcategory, Description, Custom1, Custom2, Custom3, Custom4, Custom5, Lastupdated, Status, Manufacturer, PartNumber, MerchantCategory, MerchantSubcategory, ShortDescription, ISBN, UPC, SKU, CrossSell, MerchantGroup, MerchantSubgroup, CompatibleWith, CompareTo, QuantityDiscount, Bestseller, AddToCartURL, ReviewsRSSURL, Option1, Option2, Option3, Option4, Option5, bActiveProduct) values (" . (int)$rsItem[0] . ", '" myres($rsItem[1]) . "', " . (int)$rsItem[2] . ", '" myres($rsItem[3]) . "', '" myres($rsItem[4]) . "', '" myres($rsItem[5]) . "', '" myres($rsItem[6]) . "', " . (float)$rsItem[7] . ", " . (float)$rsItem[8] . ", '" myres($rsItem[9]) . "', '" myres($rsItem[10]) . "', '" myres($rsItem[11]) . "', '" myres($rsItem[12]) . "', '" myres($rsItem[13]) . "', '" myres($rsItem[14]) . "', '" myres($rsItem[15]) . "', '" myres($rsItem[16]) . "', now(), '" myres($rsItem[18]) . "', '" myres($rsItem[19]) . "', '" myres($rsItem[20]) . "', '" myres($rsItem[21]) . "', '" myres($rsItem[22]) . "', '" myres($rsItem[23]) . "', '" myres($rsItem[24]) . "', '" myres($rsItem[25]) . "', '" myres($rsItem[26]) . "', '" myres($rsItem[27]) . "', '" myres($rsItem[28]) . "', '" myres($rsItem[29]) . "', '" myres($rsItem[30]) . "', '" myres($rsItem[31]) . "', '" myres($rsItem[32]) . "', '" . (int)$rsItem[33] . "', '" myres($rsItem[34]) . "', '" myres($rsItem[35]) . "', '" myres($rsItem[36]) . "', '" myres($rsItem[37]) . "', '" myres($rsItem[38]) . "', '" myres($rsItem[39]) . "', '" myres($rsItem[40]) . "', 1)";
        } 
// ends <insert new record>
        
else {
            
// This is an existing record
            
$cQuery "update products set Name='" myres($rsItem[$rsMap['Name']]) . "', MerchantID='" myres($rsItem[$rsMap['MerchantID']]) . "', Merchant='" myres($rsItem[$rsMap['Merchant']]) . "', Link='" myres($rsItem[$rsMap['Link']]) . "', Thumbnail='" myres($rsItem[$rsMap['Thumbnail']]) . "', BigImage='" myres($rsItem[$rsMap['BigImage']]) . "', Price='" myres($rsItem[$rsMap['Price']]) . "', RetailPrice='" myres($rsItem[$rsMap['RetailPrice']]) . "', Category='" myres($rsItem[$rsMap['Category']]) . "', Subcategory='" myres($rsItem[$rsMap['Subcategory']]) . "', Description='" myres($rsItem[$rsMap['Description']]) . "', Custom1='" myres($rsItem[$rsMap['Custom1']]) . "', Custom2='" myres($rsItem[$rsMap['Custom2']]) . "', Custom3='" myres($rsItem[$rsMap['Custom3']]) . "', Custom4='" myres($rsItem[$rsMap['Custom4']]) . "', Custom5='" myres($rsItem[$rsMap['Custom5']]) . "', Lastupdated=now(), Status='" myres($rsItem[$rsMap['Status']]) . "', Manufacturer='" myres($rsItem[$rsMap['Manufacturer']]) . "', PartNumber='" myres($rsItem[$rsMap['PartNumber']]) . "', MerchantCategory='" myres($rsItem[$rsMap['MerchantCategory']]) . "', MerchantSubcategory='" myres($rsItem[$rsMap['MerchantSubcategory']]) . "', ShortDescription='" myres($rsItem[$rsMap['ShortDescription']]) . "', ISBN='" myres($rsItem[$rsMap['ISBN']]) . "', UPC='" myres($rsItem[$rsMap['UPC']]) . "', SKU='" myres($rsItem[$rsMap['SKU']]) . "', CrossSell='" myres($rsItem[$rsMap['CrossSell']]) . "', MerchantGroup='" myres($rsItem[$rsMap['MerchantGroup']]) . "', MerchantSubgroup='" myres($rsItem[$rsMap['MerchantSubgroup']]) . "', CompatibleWith='" myres($rsItem[$rsMap['CompatibleWith']]) . "', CompareTo='" myres($rsItem[$rsMap['CompareTo']]) . "', QuantityDiscount='" myres($rsItem[$rsMap['QuantityDiscount']]) . "', Bestseller='" myres($rsItem[$rsMap['Bestseller']]) . "', AddToCartURL='" myres($rsItem[$rsMap['AddToCartURL']]) . "', ReviewsRSSURL='" myres($rsItem[$rsMap['ReviewsRSSURL']]) . "', Option1='" myres($rsItem[$rsMap['Option1']]) . "', Option2='" myres($rsItem[$rsMap['Option2']]) . "', Option3='" myres($rsItem[$rsMap['Option3']]) . "', Option4='" myres($rsItem[$rsMap['Option4']]) . "', Option5='" myres($rsItem[$rsMap['Option5']]) . "', bActiveProduct=1 where ProductID=" . (int)$rsItem[$rsMap['ProductID']] . " and MerchantID=" . (int)$rsItem[$rsMap['MerchantID']] . " limit 1";
        } 
// ends else from <updating existing record>
        
mysql_query($cQuery);
        if (
mysql_error()) {
            echo(
"<p><b>MySQL Error: " mysql_error() . "<br />\n");
            echo(
"$cQuery</p>");
            echo(
"<pre>");
            
print_r($rsItem);
            echo(
"</pre>");
            break;
        } 
// ends if (mysql_error())

    
// ends while (($rsDeal = fgetcsv($fp, 100000, "|")) !== FALSE)

    // Remove products from this merchant that were not in this feed
    
mysql_query("delete from products where bActiveProduct=0 and MerchantID=" . (int)$rsItem[$rsMap['MerchantID']] . "");

    
fclose($fp);

    
// Remove the txt file
    
unlink($cTxtFile);

    
// Go back to the working directory
    
chdir('../');

    echo(
"<p>The products have been updated.</p>");

// ends else from if (!is_uploaded_file($_FILES['sas']['tmp_name']))

?>
<form method="post" enctype="multipart/form-data" action="<?= $_SERVER['PHP_SELF'?>">
    Share A Sale Zip File: <input type="file" name="sas"><br />
    <input type="submit" name="cAction" value="Upload" />
</form>

</div>
</div><!-- ends class="post" -->

<p align="center">[ <a href="<?= $_SERVER['PHP_SELF'?>"><?= $cTitle  ?> Home</a> | <a href="./admin.php" target="_top">Admin Home</a> ]</p>
<?php
    
include('./footer.php');
?>