<?
function csvform()
{
global $userfile, $html_header, $html_footer, $ct,$ads_fields,$categories, $ads_count, 
$cat_fields,$fields_sets,$allcatfields, $err_csv_inf;
st_adm();

$tbclr_2="#dddddd";
$tbl_col="#ffffff";
echo $html_header;
echo "
<center>
<table   width='550' border=0 bgcolor='#ffffff' cellspacing=1 cellpadding=0>
<tr><td>
<font FACE='ARIAL, HELVETICA' COLOR='#000099' size='-1'>
<a href='admin.php'>Admin:</a>
</td></tr><tr><td bgcolor='#eeeeee'>
<font FACE='ARIAL, HELVETICA' COLOR='#000099'>
<b>Uploading ads from *.csv file</b>
</font>
</td></tr></table>
<p> $err_csv_inf
<form action='admin.php?md=inputcsv&ct=$ct' method='post' ENCTYPE='multipart/form-data'>
<table   width='550' border=0 bgcolor='$tbclr_2' cellspacing=1 cellpadding=0>
<tr><td>
<table width='100%' border=0 cellspacing=1 cellpadding=1>
<tr>
<td bgcolor='#eeeeee' height='$td_height' width='30%' >
<font FACE='ARIAL, HELVETICA' COLOR='#000099' size='-1'> 
&nbsp;<b>Ads Fields</b>          
</font>
</td><td bgcolor='#eeeeee' width='70%'>
<font FACE='ARIAL, HELVETICA' COLOR='#000099' size='-1'>
&nbsp; <b> Column number in csv file or default value </b> 
</font>
</td></tr>
<td bgcolor='$tbl_col' height='$td_height' width='30%' >
<font FACE='ARIAL, HELVETICA' COLOR='#000099' size='-1'> 
ID#
</font>
</td><td bgcolor='$tbl_col' width='70%'>
<font FACE='ARIAL, HELVETICA' COLOR='#000099' size='-1'>
<input type='text' name='cidnum' size='3' value='1'> 
&nbsp; or autoincrement start from:
<input type='text' name='didnum' size='5'> 
</font>
</td></tr>
";

$ilmm=1;
foreach ($cat_fields as $key => $value )
{
$ilmm++;
$colnmb="c".$key;
$defltnm="d".$key;

echo "
<td bgcolor='$tbl_col' height='$td_height' width='30%' >
<font FACE='ARIAL, HELVETICA' COLOR='#000099' size='-1'> 
".$cat_fields[$key][0]."
</font>
</td><td bgcolor='$tbl_col' width='70%'>
<font FACE='ARIAL, HELVETICA' COLOR='#000099' size='-1'>
<input type='text' name='$colnmb' size='3' value='$ilmm'> 
&nbsp; or default value:
<input type='text' name='$defltnm' size='20'> 
</font>
</td></tr>
";
}
$ilmm++;
echo "
<td bgcolor='$tbl_col' height='$td_height' width='30%' >
<font FACE='ARIAL, HELVETICA' COLOR='#000099' size='-1'> 
Member's login
</font>
</td><td bgcolor='$tbl_col' width='70%'>
<font FACE='ARIAL, HELVETICA' COLOR='#000099' size='-1'>
<input type='text' name='cmlogin' size='3' > 
&nbsp; or default value:
<input type='text' name='dmlogin' size='20'>
</font>
</td></tr>
<td bgcolor='$tbl_col' height='$td_height' width='30%' >
<font FACE='ARIAL, HELVETICA' COLOR='#000099' size='-1'> 
Photos option
</font>
</td><td bgcolor='$tbl_col' width='70%'>
<font FACE='ARIAL, HELVETICA' COLOR='#000099' size='-1'>
<input type='text' name='cphto' size='3'> 
&nbsp; or default value:
<select name='dphto' size='1'><option selected><option>y<option>n</select> 
</font>
</td></tr>
<td bgcolor='#eeeeee' height='$td_height' width='30%' >
<font FACE='ARIAL, HELVETICA' COLOR='#000099' size='-1'> 
<b>Load csv file:</b>
</font>
</td><td bgcolor='#eeeeee' width='70%'>
 <input type='file' name='userfile'>
</td></tr>
</table>
</td></tr></table>
<font FACE='ARIAL, HELVETICA' COLOR='#000099' size='-1'>
 
<input type='submit' value='Load'> 
</form>
</center>
";

echo $html_footer;
exit;

}

function inputcsv()
{
global $userfile, $ct, $err_csv_inf, $table_ads, $html_header, $html_footer, $HTTP_POST_VARS;

if (filesize($userfile) < 1){
$err_csv_inf="
<font FACE='ARIAL, HELVETICA' COLOR='#990000' >
<b>Input correct *.csv file </b></font>
";
csvform();
}

st_adm();

echo $html_header; 
echo "<font FACE='ARIAL, HELVETICA' COLOR='#000099' size='-1'><center>";

connect_csv_db();

$fcsv=fopen($userfile, 'r');

$csvid=0;
if ($HTTP_POST_VARS['didnum']!=''){$csvid=$HTTP_POST_VARS['didnum']-1;}

while (!feof($fcsv))
{
$fmss=fgetcsv($fcsv,8000, ';');

if ($HTTP_POST_VARS['didnum']!=''){$csvid++;
$idnum1=$csvid;
}
else {
$val=$HTTP_POST_VARS['cidnum']-1; $idnum1=$fmss[$val];
} 

if ($idnum1!=""){
$sql_query="select idnum from $table_ads where idnum=$idnum1";
$sql_res=mysql_query("$sql_query");
$row=mysql_fetch_row($sql_res);
$count1=$row[0];
if ($count1!=""){upd_csv_entry($idnum1, $fmss);}
else {add_csv_entry($idnum1, $fmss);}
}
}

fclose($fcsv);
unlink($userfile);

echo "
</font><p>
<font FACE='ARIAL, HELVETICA' COLOR='#000099'>
<b>Ads has been added or updated
</b></center></font> 
";

echo $html_footer;
exit;
}


function form_csv_entry($idnum,$fmss)
{
global $ct,  $table_ads, $visible_val, $cat_fields,
$HTTP_POST_VARS, $moderating, $adphotos, $cook_login, $expperiod;

if ($HTTP_POST_VARS['cmlogin']!=''){
$val=$HTTP_POST_VARS['cmlogin']-1; 
$db_entry['login']=$fmss[$val];
}
if ($HTTP_POST_VARS['dmlogin']!=''){
$val=$HTTP_POST_VARS['dmlogin']; 
$db_entry['login']=$val;
}

$time_now=time();
 
$db_entry['idnum']=$idnum;

$db_entry['exptime']=$time_now + $expperiod*86400;
$db_entry['time']=$time_now;
$db_entry['catname']=$ct;
$db_entry['visible']=1;

if ($HTTP_POST_VARS['cphto']!=''){
$val=$HTTP_POST_VARS['cphto']-1; 
$db_entry['adphotos']=$fmss[$val];
}
if ($HTTP_POST_VARS['dphto']!=''){
$val=$HTTP_POST_VARS['dphto']; 
$db_entry['adphotos']=$val;
}
 

foreach ($cat_fields as $key => $value)
{
$ckey='c'.$key;
$dkey='d'.$key;
if ($HTTP_POST_VARS[$ckey]!=''){
$val=$HTTP_POST_VARS[$ckey]-1; 
$db_entry[$key]=$fmss[$val];
}
if ($HTTP_POST_VARS[$dkey]!=''){
$val=$HTTP_POST_VARS[$dkey]; 
$db_entry[$key]=$val;
}

}
 
return $db_entry;
}
 
function add_csv_entry($idnum, $fmss)
{
global $ct, $db_entry, $table_ads, $visible_val, $cat_fields,
$HTTP_POST_VARS, $ad_idnum, $oldvrsn, $REMOTE_ADDR;

 
$ad_idnum=$idnum;
$db_entry=form_csv_entry($idnum,$fmss);

$db_entry['cntemll']="0";
$db_entry['cntvstr']="0";
$db_entry['ratevtcn']="0";
$db_entry['ratevtrt']="0";
$db_entry['adcommkey']=0;

$db_sql_string="";
$db_sql_var="";
foreach ($db_entry as $db_key => $db_value)
{

if ($db_key !="")
{
if ($db_entry[$db_key] == "" )
{
$db_sql_string=$db_sql_string."NULL, ";
}
else{
$db_sql_string=$db_sql_string."'$db_entry[$db_key]', ";
}
$db_sql_var=$db_sql_var."$db_key, ";
}
}

$db_sql_string=$db_sql_string."'$REMOTE_ADDR', ";
$db_sql_var=$db_sql_var."ipaddr1, "; 

$db_sql_string=corr_sqlstring($db_sql_string);
$db_sql_var=corr_sqlstring($db_sql_var);
$sql_query="insert into $table_ads ( $db_sql_var ) values( $db_sql_string )";

$msql_rs=mysql_query("$sql_query");
if ($msql_rs){$mssg1="ad # $idnum has been inserted";}
else {$mssg1="ad # $idnum does not inserted. ".mysql_error();}
echo  "$mssg1 <br>";

} 


function upd_csv_entry($idnum, $fmss)
{

global $ct, $db_entry, $table_ads, $visible_val, $cat_fields,
$HTTP_POST_VARS, $ad_idnum, $oldvrsn, $REMOTE_ADDR;


$db_entry=form_csv_entry($idnum,$fmss);
$db_entry['idnum']="";
$sql_string="";
foreach ($db_entry as $db_key =>$db_value)
{
 
if ($db_entry[$db_key]!=""){
$sql_string=$sql_string."$db_key='$db_entry[$db_key]', ";
}
}
$sql_string=corr_sqlstring($sql_string);

$sql_query="update $table_ads set $sql_string where idnum=$idnum";
$msql_rs=mysql_query("$sql_query");
if ($msql_rs){$mssg1="ad # $idnum has been updated  ";}
else {$mssg1="ad # $idnum does not updated ".mysql_error() ;}
echo  "$mssg1 <br>";
}


function st_adm()
{
global $ct,$ads_fields,$categories, $ads_count, $cat_fields,$fields_sets,$allcatfields;
connect_to_db();
$c_res1=0;
foreach ($categories as $key => $value)
{
if ($key == $ct ){$c_res1=1;}
}
if ($c_res1==0){
if ($ct !=""){
echo $html_header;
echo "<h3> Incorrect Cateogry </h3>";
echo $html_footer;
exit;
}
}
if ($ct!="")
{
$a1=$categories[$ct][1];
$a2=$fields_sets[$a1];
}
else 
{$a2=$allcatfields;}
foreach ($a2 as $a2_value)
{
$cat_fields[$a2_value]=$ads_fields[$a2_value];
}
}

function connect_csv_db()
{
global $host_name, $db_user,$db_password, $html_header, 
$html_footer, $db_name, $table_ads;

mysql_connect("$host_name","$db_user","$db_password");
mysql_select_db("$db_name");

}

?>