<?php
##############################################################################
# ajout de Cadeaux
##############################################################################

include("../config.php3");
include($BIB."bib_generic.php3");
include($ROOT_DIR."VIRALGAMES_Adm/Bib/bib_synchro.php");

$error = "";
//TAILLE MAX IMAGE
$MAX_WIDTH = 140;
$MAX_HEIGHT = 140;

$NOW = date("Ymd-His");

?>
<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Gestion des cadeaux</div>
<br>
<?php
switch($submit)
{
	case "Valider":
		
		if ( strcmp($_FILES['Image']['type'], "image/jpeg")== 0 )
		{
			$EXT = ".jpg";
		}
		else if ( strcmp($_FILES['Image']['type'], "image/pjpeg")== 0 )
		{
			$EXT = ".jpg";
		}
		
		else if ( strcmp($_FILES['Image']['type'], "image/gif")== 0 )
		{
			$EXT = ".gif";
		}
		else if ( strcmp($_FILES['Image']['type'], "image/png")== 0 )
		{
			$EXT = ".png";
		}
		else
		{
			$error = "Veuillez déposer une image valide au format jpg, gif ou png" ;
		}
		
		$Final_Image_Dir = $GAME_DIR."Images/cadeaux/Gift_".$NOW.$EXT;
		$Final_Image_Url = "Images/cadeaux/Gift_".$NOW.$EXT;
		
		//si besoin on redimensionne l'image
		$dimensions_img = getimagesize($_FILES['Image']['tmp_name']); 
		if ( $dimensions_img[0] > $MAX_WIDTH || $dimensions_img[1] > $MAX_HEIGHT )
		{
		
			if($dimensions_img[0] > $dimensions_img[1])
			{
				$new_width  = $MAX_WIDTH; // largeur a definir
				$new_height = ($new_width *  $dimensions_img[1]) /  $dimensions_img[0] ; // hauteur proportionnelle
			}
			else
			{
				$new_height = $MAX_HEIGHT; // largeur a definir
				$new_width  = ($new_height *  $dimensions_img[0]) /  $dimensions_img[1] ; // hauteur proportionnelle
			}
			
			switch($EXT)
			{
				case ".jpg" :
					$picture =ImageCreateFromJPEG($_FILES['Image']['tmp_name']);
					$newpict =imagecreatetruecolor($new_width ,$new_height);
					imagecopyresampled($newpict,$picture,0,0,0,0,$new_width,$new_height,$dimensions_img[0],$dimensions_img[1]);
					imagejpeg($newpict,$Final_Image_Dir,100);			
				break;
				
				case ".gif" :
					$picture =ImageCreateFromGIF($_FILES['Image']['tmp_name']);
					$newpict =imagecreatetruecolor($new_width ,$new_height);
					imagecopyresampled($newpict,$picture,0,0,0,0,$new_width,$new_height,$dimensions_img[0],$dimensions_img[1]);
					imagegif($newpict,$Final_Image_Dir);			
				break;
				
				case ".png" :
					$picture =ImageCreateFromPNG($_FILES['Image']['tmp_name']);
					$newpict =imagecreatetruecolor($new_width ,$new_height);
					imagecopyresampled($newpict,$picture,0,0,0,0,$new_width,$new_height,$dimensions_img[0],$dimensions_img[1]);
					imagepng($newpict,$Final_Image_Dir,0);			
				break;	
			}		
		}
		else
		{
			//copie de l'image
			copy( $_FILES[Image][tmp_name] , $Final_Image_Dir );
			# on change le propriétaire du dossier, il devient web pour pouvoir être répliqué sans pbs
			cmd($CHOWN_CMD." -R web:web ".$GAME_DIR."Images/cadeaux/");
			//on remet le droit d'ecriture sur le fichier
			cmd($CHMOD_CMD." 777 ".$GAME_DIR."Images/cadeaux/");
			# le nouveau dossier généré doit être répliqué
			shareFileToAll($GAME_DIR."Images/cadeaux/", "");
		}
		
		$Add_query_update = "";
		$Add_query_insert1 = "";
		$Add_query_insert2 = "";
		if($_FILES[Image][tmp_name])
		{
			$Add_query_update = ", Image = '".$Final_Image_Url."'";
			$Add_query_insert1 = ",Image";
			$Add_query_insert2 = ",'".$Final_Image_Url."'";
		}
		
		# formatage de toutes les chaines de char
		$Name = AddSlashes($Name);
		
		if ($Id==0)
		{
			# on insere une nouvelle réponse
			main_db_query("INSERT INTO Cadeaux (Name, Ordre, Dispo, Details, Details2, ShowTime, EndTime, Price".$Add_query_insert1.") VALUES ('".$Name."','".$Ordre."','".$Dispo."','".trim($Details)."','".$Details2."','".$ShowTime."','".$EndTime."', '".$Price."'".$Add_query_insert2.")");
		}
		else
		{
			# on update si c'est une modif
			main_db_query("UPDATE Cadeaux SET Name='".$Name."', Ordre='".$Ordre."', Dispo='".$Dispo."', Details='".trim($Details)."', Details2='".$Details2."', ShowTime='".$ShowTime."',EndTime='".$EndTime."', Price='".$Price."'".$Add_query_update." WHERE Id='".$Id."'");
		}
		$Id=0;

		foreach($HTTP_POST_VARS as $key => $val)
    	$$key = stripslashes($val);

		break;
		
	case "Supprimer":
		main_db_query("DELETE FROM Cadeaux WHERE Id='".$Id."'");
		$Id=0;
		$Name = "";
		$Ordre = "";
		$Dispo = "";
		$Details = "";
		$Details2 = "";
		$ShowTime = "";
		$EndTime = "";
		$Image = "";
		$Price = "";		
		break;
	
	case "Infos":
		$info = db_fetch_array(main_db_query("SELECT * FROM Cadeaux WHERE Id='".$Id."'"));
		
		$Name = $info[Name];
		$Ordre = $info[Ordre];
		$Dispo = $info[Dispo];
		$Details = $info[Details];
		$Details2 = $info[Details2];
		$ShowTime = $info[ShowTime];
		$EndTime = $info[EndTime];
		$Image = $info[Image];
		$Price = $info[Price];
		
		
		break;
}



?>


<form method="post" action="<?php echo $PHP_SELF ?>" enctype="multipart/form-data">
<table>
	<tr>
		<td>
			Id :
		</td>
		<td>
			<input type="text" size=3 name=Id value="<?php echo $Id ?>"> (0 pour ajouter un container)
		</td>
	</tr>
	<tr>
		<td>
			Name :
		</td>
		<td>
			<input type="text" size=30 name=Name value="<?php echo $Name ?>">
		</td>
	</tr>
	<tr>
		<td>
			Ordre :
		</td>
		<td>
			<input type="text" size=30 name=Ordre value="<?php echo $Ordre ?>">
		</td>
	</tr>
	<tr>
		<td>
			Dispo :
		</td>
		<td>
			<input type="text" size="10" name="Dispo" value="<?php echo $Dispo ?>">
		</td>
	</tr>
	--
	<tr>
		<td>
			Titre :
		</td>
		<td>
			<input type="text" size=65 name="Details" value="<?php echo $Details ?>">
		</td>
	</tr>
	<tr>
		<td>
			Détails plus (titres) :
		</td>
		<td>
			<input type="text" size=65 name="Details2" value="<?php echo $Details2 ?>">
		</td>
	</tr>
	
	<tr>
		<td>
			Date d'affichage :
		</td>
		<td>
			<input type="text" name="ShowTime" value="<?php echo $ShowTime ?>">
		</td>
	</tr>
	<tr>
		<td>
			Date limite :
		</td>
		<td>
			<input type="text" name="EndTime" value="<?php echo $EndTime ?>"> AAAA-MM-JJ HH:MM:SS
		</td>
	</tr>
	<tr>
		<td>
			Image :
		</td>
		<td>
			<input type="file" name="Image" size=50 size=25 > 
		</td>
	</tr>
	<tr>
		<td>
			Prix :
		</td>
		<td>
			<input type="text" size=40 name=Price value="<?php echo $Price ?>">
		</td>
	</tr>
</table>
<br>
    <input type=submit name=submit value="Infos">&nbsp;&nbsp;&nbsp;
    <input type=submit name=submit value="Valider">&nbsp;&nbsp;&nbsp;
	<input type=submit name=submit value="Supprimer"><br>
	</form>
<a class=under href="<?php echo $PHP_SELF ?>">Rafraichir la page</a>

<div class=universBold>Liste des Cadeaux</div>
<table border=1>
	<tr class=trTitle>
		<td>Id</td>
		<td>Name</td>
		<td>Ordre</td>
		<td>Dispo</td>	
		<!--
		<td>Details</td>	
		<td>Details2</td>	
		-->
		<td>ShowTime</td>
		<td>EndTime</td>
		<td>Image</td>
		<td>Prix</td>
	</tr>
	
<?php
$rows=main_db_query("SELECT * FROM Cadeaux ORDER BY ShowTime ASC, EndTime ASC, Id ASC");
while($row=db_fetch_array($rows))
{
	if (time_diff_now($row[ShowTime]) > 0 and time_diff_now($row[EndTime]) < 0)
		echo "<tr bgcolor=yellow>";
	elseif(time_diff_now($row[EndTime]) > 0)
		echo "<tr bgcolor=E0E0E0>";
	else
		echo "<tr>";

	echo "<td><a href='?submit=Infos&Id=".$row[Id]."'>".$row[Id]."</a></td>";
	echo "<td>$row[Name]</td>";
	echo "<td>$row[Ordre]</td>";
	echo "<td>$row[Dispo]</td>";
	//echo "<td>$row[Details]</td>";
	//echo "<td>$row[Details2]</td>";
	echo "<td>$row[ShowTime]</td>";
	echo "<td>$row[EndTime]</td>";
	echo "<td>$row[Image]</td>";
	echo "<td>$row[Price]</td>";
	echo "</tr>";
}
    ?>
