<?
//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");
function index($cid) {
global $myts, $xoopsUser, $annoadd, $xoopsModule;
include(XOOPS_ROOT_PATH."/modules/MyAnnonces/cache/config.php");
include(XOOPS_ROOT_PATH."/modules/MyAnnonces/include/functions.php");
	if (!$xoopsUser && $annoadd == 0) {
redirect_header(XOOPS_URL."/user.php",3,_CLA_FORMEMBERS2);
exit();
	} else {
include(XOOPS_ROOT_PATH."/header.php");
    OpenTable();
	
echo "<B><A HREF=\"index.php\">"._CLA_MAIN."</A> &raquo; "._CLA_ADDANNONCE3."</B><br /><br />\n";
$condition = join('', file("./cache/condition.txt"));
echo  "".$myts->makeTareaData4Show($condition, 1, 1, 1)."<BR /><BR />";
echo "<BR /><BR /><CENTER>[ <A HREF=\"addannonces.php?op=addindex&cid=$cid\">"._CLA_CONDITIONOK."</A> | <A HREF=\"index.php\">"._CLA_CONDITIONNO."</A> ]</CENTER>";
	CloseTable();
	copyright();
include(XOOPS_ROOT_PATH."/footer.php");
	}
}
function addindex($cid) {
    global $db, $bcoosConfig, $xoopsUser, $xoopsTheme, $photomax, $xoopsModule, $ynprice;
include(XOOPS_ROOT_PATH."/modules/MyAnnonces/cache/config.php");
include(XOOPS_ROOT_PATH."/modules/MyAnnonces/include/functions.php");
include_once(XOOPS_ROOT_PATH."/modules/MyAnnonces/class/arbre.php");
$mytree = new XoopsArbre($db->prefix("ann_categories"),"cid","pid");
	  if ($cid =="") {     
redirect_header("index.php",1,_CLA_ADDANNONCE);
exit();
	}	
	if (!$xoopsUser && $annoadd == 0) {
redirect_header(XOOPS_URL."/user.php",3,_CLA_FORMEMBERS2);
exit();
	} else {
	
	$photomax1=$photomax/1024;
include(XOOPS_ROOT_PATH."/header.php");
echo "<script>
          function verify() {
                var msg = \""._CLA_VALIDERORMSG."\\n__________________________________________________\\n\\n\";
                var errors = \"FALSE\";
                if (document.Add.type.value == \"0\") {
                        errors = \"TRUE\";
                        msg += \""._CLA_VALIDTYPE."\\n\";
                }
				
                if (document.Add.cid.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._CLA_VALIDCAT."\\n\";
                }
				
                if (document.Add.titre.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._CLA_VALIDTITLE."\\n\";
                }
				
				if (document.Add.description.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._CLA_VALIDANN."\\n\";
                }
				
				if (document.Add.submitter.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._CLA_VALIDSUBMITTER."\\n\";
                }
				
				if (document.Add.email.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._CLA_VALIDEMAIL."\\n\";
                }
				
				if (document.Add.town.value == \"\" && document.Add.country.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._CLA_VALIDTOWNCOUNTRY."\\n\";
                }
				
  
                if (errors == \"TRUE\") {
                        msg += \"__________________________________________________\\n\\n"._CLA_VALIDMSG."\\n\";
                        alert(msg);
                        return false;
                }
          }
          </script>";
list($numrows) = $db->fetchRow($db->query("select cid, title from ".$db->prefix("ann_categories").""));
    
	if ($numrows>0) {
	OpenTable();
	
	echo "<B><A HREF=\"index.php\">"._CLA_MAIN."</A> &raquo; "._CLA_ADDANNONCE3."</b><br><br>";
	
	if ($moderated == '1') {
	    echo ""._CLA_ANNMODERATE." $claday "._CLA_DAY."<br><br>";
		} else {
		echo ""._CLA_ANNNOMODERATE." $claday "._CLA_DAY."<br><br>";
		}
		
		echo "<form method=\"post\" action=\"addannonces.php\" ENCTYPE=\"multipart/form-data\" NAME=\"Add\" onSubmit=\"return verify();\">";
		echo "<TABLE><TR>";
	$result2 = $db->query("select nom_type from ".$db->prefix("ann_type")." order by nom_type");
	echo "<TD valign=top>"._CLA_TYPE." </TD><TD><select name=\"type\"  class=\"select\"><option value=\"0\">"._CLA_SELECTYPE."</option>";
	while(list($nomtyp) = $db->fetchRow($result2)) {
	echo "<option value=\"$nomtyp\">$nomtyp</option>";
	    }
	echo "</select></TD>
		</TR><TR>";
	
	echo "<TD valign=top>"._CLA_CAT2." </TD><TD>";
	
$requete = $db->query("select title, affprix from ".$db->prefix("ann_categories")." where  cid=".$cid."");
list($title, $affprix) = $db->fetchRow($requete);
	echo "<B>&raquo; $title</B><input type=\"hidden\" name=\"cid\" value=\"$cid\"></TD>
		</TR><TR>
		<TD>"._CLA_TITLE2." </TD><TD><input type=\"text\" class=\"text\" name=\"titre\" size=\"50\" maxlength=\"100\"></TD>
		</TR><TR>
		<TD>"._CLA_ANNONCE." <BR>"._CLA_CHARMAX."</TD><TD><textarea name=\"description\" cols=\"50\" rows=\"5\" class=\"textarea\"></textarea></TD>
		</TR><TR>
		<td>"._CLA_IMG."</td><td><INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$photomax\">";
		include_once(XOOPS_ROOT_PATH."/class/upload.class.php");
		$upload = new Upload();
		$upload->printFormField("photo");
		echo " (<  ";
		printf ("%.2f ko",$photomax1); 
		echo ")</td></TR>";
		
		if ($ynprice == 1) {
		if ($affprix == 1) {
		echo "<TR><TD>"._CLA_PRICE2." </TD><TD><input type=\"text\" class=\"text\" name=\"price\" size=\"20\"> $monnaie";
	$result3 = $db->query("select nom_prix from ".$db->prefix("ann_prix")." order by id_prix");
	echo " <select name=\"typeprix\" class=\"select\">";
	while(list($nom_prix) = $db->fetchRow($result3)) {
	echo "<option value=\"$nom_prix\">$nom_prix</option>";
	    }
	echo "</select></TD>";
		}
		}
    if($xoopsUser) {
	$iddd =$xoopsUser->getVar("uid", "E");
	$idd =$xoopsUser->getVar("name", "E");
	$idde =$xoopsUser->getVar("email", "E");
	}
	
	$time = time();
	
	echo "</TR><TR>
		<TD>"._CLA_SURNAME." </TD><TD><input type=\"text\" class=\"text\" name=\"submitter\" size=\"30\" value=\"$idd\"></TD>
		</TR><TR>
		<TD>"._CLA_EMAIL." </TD><TD><input type=\"text\" class=\"text\" name=\"email\" size=\"30\" value=\"$idde\"></TD>
		</TR><TR>
		<TD>"._CLA_TEL." </TD><TD><input type=\"text\" class=\"text\" name=\"tel\" size=\"30\"></TD>
		</TR><TR>
		<TD>"._CLA_TOWN." </TD><TD><input type=\"text\" class=\"text\" name=\"town\" size=\"30\"></TD>
		</TR><TR>
		<TD>"._CLA_COUNTRY." </TD><TD><input type=\"text\" class=\"text\" name=\"country\" size=\"30\"></TD>
		</TR></TABLE>
		<input type=\"hidden\" name=\"usid\" value=\"$iddd\">
		<input type=\"hidden\" name=\"op\" value=\"AddAnnoncesOk\">";
		
	if ($moderated == '1') {
	if ($xoopsUser && $xoopsUser->isAdmin($xoopsModule->mid())) {
	echo "<input type=\"hidden\" name=\"valid\" value=\"Yes\">";
	} else {
	    echo "<input type=\"hidden\" name=\"valid\" value=\"No\">";
	}
		} else {
		echo "<input type=\"hidden\" name=\"valid\" value=\"Yes\">";
		}
		
		echo "<input type=\"hidden\" name=\"lid\" value=\"0\">
		<input type=\"hidden\" name=\"date\" value=\"$time\">
		<input type=\"submit\" class=\"button\" value=\""._CLA_VALIDATE."\">
	    </form>";
	CloseTable();
	copyright();
include(XOOPS_ROOT_PATH."/footer.php");
    }
	}
}
	
function AddAnnoncesOk($lid, $cat, $titre, $type, $description, $tel, $price, $typeprix, $date, $email, $submitter, $usid, $town, $country, $valid, $photo, $photo_size, $photo_name, $_FILES) {
    global $db, $bcoosConfig, $photomax, $destination, $myts;
	
include(XOOPS_ROOT_PATH."/modules/MyAnnonces/cache/config.php");
include(XOOPS_ROOT_PATH."/modules/MyAnnonces/include/functions.php");
	$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/upload.class.php");
	$upload = new Upload();
        $upload->setAllowedMimeTypes(".gif|.jpeg|.jpg|.png");
	$upload->setUploadPath(XOOPS_ROOT_PATH."/modules/MyAnnonces/images_ann");
	$upload->setDestinationFileName('photo');
	$upload->setFileName($lid);
        $result = $upload->doUpload();
		
	if ($result == false) {
		$errors = $upload->getUploadErrors();
		$errorstring = "";
		$errorstring .= "<strong>::Errors occured::</strong><br />\n";
		while(list($filename,$values) = each($errors)) {
			$errorstring .= "File: " . $filename . "<br />";
			$count = count($values);
			for($i=0; $i<$count; $i++) { $errorstring .= "==>" . $values[$i] . "<br />"; }
		}
		redirect_header("addannonces.php?cid=$cat", 3, $errorstring);
		exit();
			
	}
}
		
    $db->query("INSERT INTO ".$db->prefix("ann_annonces")." values ('', '$cat', '$titre', '$type', '$description', '$tel', '$price', '$typeprix', '$date', '$email', '$submitter', '$usid',  '$town', '$country',  '$valid', '".$upload->FileName.".".$upload->getFileExtension()."', '0')");
		
			redirect_header("index.php",1,_CLA_ANNADDED);
			exit();
}
	
	
	
switch($op) {	
	
	case "AddAnnoncesOk":
    AddAnnoncesOk($lid, $cid, $titre, $type, $description, $tel, $price, $typeprix, $date, $email, $submitter, $usid, $town, $country, $valid, $photo, $photo_size, $photo_name, $_FILES);
    break;
	
	case "addindex":
    addindex($cid);
    break;
	
	default:
	index($cid);
	break;
}
	
?>