<?
//v1.8 www.exoops.info 2006/03
// 
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// Original Author: Pascal Le Boustouller
// Author Website : http://www.perso-search.com/e-xoopsien/
// Licence Type   : GPL
// ------------------------------------------------------------------------- //
include("header.php");
include(XOOPS_ROOT_PATH."/modules/MyAnnonces/cache/config.php");
function AnnoncesDel($lid, $ok) {
    global $db, $xoopsUser, $bcoosConfig, $xoopsTheme;
	
	$result = $db->query("SELECT usid, photo FROM ".$db->prefix("ann_annonces")." where lid=$lid");
    list($usid, $photo) = $db->fetchRow($result);
	
	  if ($xoopsUser) {
		 $calusern = $xoopsUser->getVar("uid", "E");
		if ($usid == $calusern) {
	
	if($ok==1) {
    $db->query("delete from ".$db->prefix("ann_annonces")." where lid=$lid");
	
	if($photo){
	$destination = XOOPS_ROOT_PATH."/modules/MyAnnonces/images_ann";
	
	if (file_exists("$destination/$photo")) {
			 	 unlink("$destination/$photo");
				}
	}
	
	
redirect_header("index.php",1,_CLA_ANNDEL);
exit();
} else {
include(XOOPS_ROOT_PATH."/header.php");
	OpenTable();
	echo "<br><center>";
	echo "<b>"._CLA_SURDELANN."</b><br><br>";
    }
	echo "[ <a href=\"supprann.php?op=AnnoncesDel&lid=$lid&ok=1\">"._YES."</a> | <a href=\"index.php\">"._NO."</a> ]<br><br>";
	CloseTable();
include(XOOPS_ROOT_PATH."/footer.php");
}
}
}
function ModAnnonce($lid) {
    global $db, $xoopsModule, $bcoosConfig, $xoopsUser, $monnaie, $moderated, $photomax, $ynprice, $xoopsTheme, $myts;
	
include(XOOPS_ROOT_PATH."/header.php");
echo "<script language=\"javascript\">\nfunction CLA(CLA) { var MainWindow = window.open (CLA, \"_blank\",\"width=500,height=300,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no\");}\n</script>";
include_once(XOOPS_ROOT_PATH."/modules/MyAnnonces/class/arbre.php");
$mytree = new XoopsArbre($db->prefix("ann_categories"),"cid","pid");
	$photomax1=$photomax/1024;
    $result = $db->query("select lid, cid, title, type, description, tel, price, typeprix, date, email, submitter, usid, town, country, valid, photo from ".$db->prefix("ann_annonces")." where lid=$lid");
    list($lid, $cide, $title, $type, $description, $tel, $price, $typeprix, $date, $email, $submitter, $usid, $town, $country, $valid, $photo_old) = $db->fetchRow($result);
	
	  if ($xoopsUser) {
		 $calusern = $xoopsUser->uid();
		if ($usid == $calusern) {
    OpenTable();
    echo "<b>"._CLA_MODIFANN."</b><br><br>";
	
	$title = $myts->makeTboxData4Edit($title);
	$type = $myts->makeTboxData4Edit($type);
	$description = $myts->makeTboxData4Edit($description);
	$tel = $myts->makeTboxData4Edit($tel);
	$price = $myts->makeTboxData4Edit($price);
	$typeprix = $myts->makeTboxData4Edit($typeprix);
	$submitter = $myts->makeTboxData4Edit($submitter);	
	$town = $myts->makeTboxData4Edit($town);
	$country = $myts->makeTboxData4Edit($country);
				
	$useroffset = "";
    if($xoopsUser) {
		$timezone = $xoopsUser->timezone();
	if(isset($timezone)){
		$useroffset = $xoopsUser->timezone();
	}else{
		$useroffset = $bcoosConfig['default_TZ'];
	}
	}
	$dates = ($useroffset*3600) + $date;	
	$dates = formatTimestamp($date,"s");
		
    	echo "<form action=\"supprann.php\" method=post ENCTYPE=\"multipart/form-data\">
	    <TABLE><TR>
		<TD>"._CLA_NUMANNN." </TD><TD>$lid "._CLA_DU." $dates</TD>
		</TR><TR>
		<TD>"._CLA_SENDBY." </TD><TD>$submitter</TD>
		</TR><TR>
		<TD>"._CLA_EMAIL." </TD><TD><input type=\"text\" name=\"email\" size=\"50\" value=\"$email\"></TD>
		</TR><TR>
		<TD>"._CLA_TEL." </TD><TD><input type=\"text\" name=\"tel\" size=\"50\" value=\"$tel\"></TD>
		</TR><TR>
		<TD>"._CLA_TOWN." </TD><TD><input type=\"text\" name=\"town\" size=\"50\" value=\"$town\"></TD>
		</TR><TR>
		<TD>"._CLA_COUNTRY." </TD><TD><input type=\"text\" name=\"country\" size=\"50\" value=\"$country\"></TD>
		</TR><TR>
		<TD>"._CLA_TITLE2." </TD><TD><input type=\"text\" name=\"title\" size=\"50\" value=\"$title\"></TD>
		</TR><TR>
		<TD>"._CLA_TYPE." </TD><TD><select name=\"type\">";
		
		$result5=$db->query("select nom_type from ".$db->prefix("ann_type")." order by nom_type");
		while(list($nom_type) = $db->fetchRow($result5)) {
    	    	    $sel = "";
		    if ($nom_type == $type) {
			$sel = "selected";
		    }
		    echo "<option value=\"$nom_type\" $sel>$nom_type</option>";
		}
		echo "</select></TD>";
		echo "</TR><TR>
		<TD>"._CLA_CAT2." </TD><TD>";
		$mytree->makeMySelBox("title", "title", $cide);
	    echo "</TD>
		</TR><TR>
		<TD>"._CLA_ANNONCE." </TD><TD><textarea name=\"description\" cols=\"60\" rows=\"10\">$description</textarea></TD>
		</TR><TR>";
		
		if ($ynprice == 1) {
		echo "<TR><TD>"._CLA_PRICE2." </TD><TD><input type=\"text\" name=\"price\" size=\"20\" value=\"$price\"> $monnaie";
		$result3 = $db->query("select nom_prix from ".$db->prefix("ann_prix")." order by id_prix");
	echo " <select name=\"typeprix\"><option value=\"$typeprix\">$typeprix</option>";
	while(list($nom_prix) = $db->fetchRow($result3)) {
	echo "<option value=\"$nom_prix\">$nom_prix</option>";
	    }
	echo "</select></TD></TR>";
		}
		
		if ($photo_old) {
		echo "</TR><TD>"._CLA_ACTUALPICT." </TD><TD><A href=\"javascript:CLA('display-image.php?lid=$lid')\">$photo_old</A> <input type=\"hidden\" name=\"photo_old\" value=\"$photo_old\"> <INPUT TYPE=\"checkbox\" NAME=\"supprim\" VALUE=\"yes\"> "._CLA_DELPICT."</TD>
		</TR><TR>";
		echo "<TD>"._CLA_NEWPICT." </TD><TD><INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$photomax\"><input type=file name=\"photo\"> (<  ";
		printf ("%.2f ko",$photomax1);
		echo ")</TD>";
		} else {
		echo "<TD>"._CLA_IMG." </TD><TD><INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$photomax\"><input type=file name=\"photo\"> (<  ";
		printf ("%.2f ko",$photomax1);
		echo ")</TD>";
		}
		echo"</TR><TR>
		<TD colspan=2><input type=\"submit\" value=\""._CLA_MODIFANN."\"></TD>
		</TR></TABLE>";
		echo "<input type=\"hidden\" name=\"op\" value=\"ModAnnonceS\">";
		
	if ($moderated == '1') {
	    echo "<input type=\"hidden\" name=\"valid\" value=\"No\">";
		echo "<BR>"._CLA_MODIFBEFORE."<BR>";
		} else {
		echo "<input type=\"hidden\" name=\"valid\" value=\"Yes\">";
		}
		
	    echo "<input type=\"hidden\" name=\"lid\" value=\"$lid\">";
	    echo "<input type=\"hidden\" name=\"date\" value=\"$date\">";
	    echo "<input type=\"hidden\" name=\"submitter\" value=\"$submitter\">
		</form><br>";
    CloseTable();
include(XOOPS_ROOT_PATH."/footer.php");
         }    
		 }
}
function ModAnnonceS($lid, $cat, $title, $type, $description, $tel, $price, $typeprix, $date, $email, $submitter, $town, $country, $valid, $photo, $photo_old, $photoS_size, $photoS_name, $_FILES, $supprim) {
    global $db, $bcoosConfig, $photomax, $myts;
	
	$destination = XOOPS_ROOT_PATH."/modules/MyAnnonces/images_ann";
	
	if($supprim == "yes"){
		if (file_exists("$destination/$photo_old")) {
			 unlink("$destination/$photo_old");
		}
		
		$photo_old = "";
	}
	
	$title = $myts->makeTboxData4Save($title);
	$type = $myts->makeTboxData4Save($type);
	$description = $myts->makeTboxData4Save($description);
	$tel = $myts->makeTboxData4Save($tel);
	$price = $myts->makeTboxData4Save($price);
	$typeprix = $myts->makeTboxData4Save($typeprix);
	$submitter = $myts->makeTboxData4Save($submitter);	
	$town = $myts->makeTboxData4Save($town);
	$country = $myts->makeTboxData4Save($country);
	
	
if ( !empty($_FILES['photo']['name']) ) {
	include_once(XOOPS_ROOT_PATH."/class/fileupload.php");
	$upload = new fileupload();
	$upload->set_upload_dir("$destination/", 'photo');
	$upload->set_accepted("gif|jpg|png", 'photo');
	$upload->set_overwrite(1, 'photo');
	$result = $upload->upload();
	if (!$result['photo']['filename']) {	
			redirect_header("supprann.php?op=ModAnnonce&lid=$lid", 3, $upload->errors());
			exit();
		} else {
		if ($photo_old) {
		if (@file_exists("$destination/$photo_old")) {
			 unlink("$destination/$photo_old");
			}
		}
		
		$photo_old = $result['photo']['filename'];
		
		}		
	}
		
    $db->query("update ".$db->prefix("ann_annonces")." set cid='$cat', title='$title', type='$type', description='$description', tel='$tel', price='$price', typeprix='$typeprix',  email='$email', submitter='$submitter', town='$town', country='$country', valid='$valid', photo='$photo_old' where lid=$lid");
			redirect_header("index.php",1,_CLA_ANNMOD2);
			exit();
}
switch ($op) {
    case "ModAnnonce":
    ModAnnonce($lid);
    break;
	
	case "ModAnnonceS":
    ModAnnonceS($lid, $cid, $title, $type, $description, $tel, $price, $typeprix, $date, $email, $submitter, $town, $country, $valid, $photo, $photo_old, $photo_size, $photo_name, $_FILES, $supprim);
    break;
    case "AnnoncesDel":
    AnnoncesDel($lid, $ok);
    break;
    default:
	redirect_header("index.php",1,""._RETURNANN."");
	break;
}
?>
