<?php
session_start();
include('../connexion.php');
if (!isset($_SESSION['celest']))
{
	header ('Location: index.php');
}
if (isset($_GET['W9mvyDvFWE5']) and isset($_GET['id']) and isset($_GET['retour']))
{
	$chaiz=addslashes($_GET['W9mvyDvFWE5']);
	$id=addslashes($_GET['id']);
	$retour=addslashes($_GET['retour']);
	$active=$retour;
?>
	<?php include('tete.php'); ?>
    <link rel="stylesheet" type="text/css" href="style/style_admin.css" />
</head>
<body>
    <div id="wrapper">
    	<?php include('menu_admin.php'); ?>
        <div id="page-wrapper">
            <div class="row">
                <div class="col-lg-2 col-md-1"></div>
                <div class="col-lg-8 col-md-10 col-sm-11 col-xs-12" id="administration">
                    <div id="titro_admin">Ajout <?php echo $chaiz ?></div>
                    <?php
                    switch($chaiz)
					{
						case 'video':
						?>
						<div align="center">
							<?php
							if (isset($_POST['valider']))
							{
								$nom=addslashes(addslashes($_POST['nom']));
								$date_mise=date('Y-m-d H:i:s');
								if (empty($nom))
								{
									echo '<div class="alert alert-danger">Veuillez renseigner le titre</div>';
								}
								else
								{
									$req='INSERT INTO '.$chaiz.' VALUES (NULL, "'.$nom.'", "'.$id.'", "'.$date_mise.'", "'.$date_mise.'")';
									$reponse=$bdd-> query($req) or die('Erreur SQL !<br />'.$req.'<br />'.mysql_error());
									echo '<div class="alert alert-success"> ajout effectué avec succès</div>';
									?> 
									<script type="text/javascript">
									window.setTimeout("location=('ajouter-fichier.php?id=<?php echo $id ?>&W9mvyDvFWE5=<?php echo $chaiz; ?>&retour=<?php echo $retour; ?>');",2500);
									</script>
									<?php
								}
							}			
							?>
						</div>
						<form role="form" method="post" action="" enctype="multipart/form-data" name="form2">
                        	<h4>Vidéo</h4>
							<input autofocus type="text" class="form-control" name="nom" value="<?php if (isset($_POST['valider'])){echo $_POST['nom'];} ?>">
							<div id="valider">
								<input type="submit" class="btn btn-primary" value="Valider" name="valider">
								&nbsp; <a href="afficher-table.php?W9mvyDvFWE5=<?php echo $retour ?>&QYtgoGbhhd=<?php echo $id ?>#positionimg"><i class="fa fa-reply"></i> Retour</a>
							</div>
						</form>
						<?php
						break;
						
						case 'adresse':
						?>
						<div align="center">
							<?php
							if (isset($_POST['valider']))
							{
								$succursale=(addslashes($_POST['succursale']));
								$commune=(addslashes($_POST['commune']));
								$adresse=(addslashes($_POST['adresse']));
								$mail=(addslashes($_POST['mail']));
								$fax=(addslashes($_POST['fax']));
								$tel=(addslashes($_POST['tel']));
								$longitude=(addslashes($_POST['longitude']));
								$latitude=(addslashes($_POST['latitude']));
								$descrip_adresse=(addslashes($_POST['descrip_adresse']));
								$date_mise=date('Y-m-d H:i:s');

								if (empty($succursale))
								{
									echo '<div class="alert alert-danger">Veuillez renseigner le nom</div>';
								}
								elseif (empty($commune))
								{
									echo '<div class="alert alert-danger">Veuhjkgillez renseigner la commune</div>';
								}
								elseif (empty($adresse))
								{
									echo '<div class="alert alert-danger">Veuillez renseigner la commune</div>';
								}
								elseif (empty($longitude))
								{
									echo '<div class="alert alert-danger">Veuillez renseigner la commune</div>';
								}
								elseif (empty($latitude))
								{
									echo '<div class="alert alert-danger">Veuillez renseigner la commune</div>';
								}
								else
								{
									$req='INSERT INTO adresse VALUES (NULL, "'.$succursale.'", "'.$adresse.'", "'.$longitude.'", "'.$latitude.'", "'.$mail.'", "'.$tel.'", "'.$fax.'", "'.$descrip_adresse.'", "'.$id.'", "'.$commune.'", "'.$date_mise.'")';
									$reponse=$bdd-> query($req) or die('Erreur SQL !<br />'.$req.'<br />'.mysql_error());
									echo '<div class="alert alert-success"> ajout effectué avec succès</div>';
									?> 
									<script type="text/javascript">
									window.setTimeout("location=('ajouter-fichier.php?id=<?php echo $id ?>&W9mvyDvFWE5=<?php echo $chaiz; ?>&retour=<?php echo $retour; ?>');",2500);
									</script>
									<?php
								}
							}			
							?>
						</div>
						<form role="form" method="post" action="" enctype="multipart/form-data" name="form2">
                        	<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                                <h5>Nom</h5>
                                <input type="text" class="form-control" name="succursale" value="<?php if (isset($_POST['valider'])){echo $_POST['succursale'];} ?>">
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                                <h5>Commune</h5>
                                <select name="commune" class="form-control">
                                    <?php
                                    $sql2 = "SELECT * FROM commune ORDER BY lib_commune ASC";
                                    $reponse2=$bdd-> query($sql2) or die('Erreur SQL !<br />'.$sql2.'<br />'.mysql_error());
                                    while ($data2 =$reponse2->fetch())
                                    {
                                        if(isset($_POST['valider']) and $_POST['commune']==$data2['id_commune'])
                                        {
                                            echo $cocher=' selected="selected"';
                                        }
                                        else
                                        {
                                            echo $cocher='';
                                        }
                                        echo '<option'.$cocher.' value="'.$data2[0].'">'.$data2['lib_commune'].'</option>';
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                                <h5>Adresse géographique</h5>
                                <input type="text" class="form-control" name="adresse" value="<?php if (isset($_POST['valider'])){echo $_POST['adresse'];} ?>">
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                                <h5>Email</h5>
                                <input type="text" class="form-control" name="mail" value="<?php if (isset($_POST['valider'])){echo $_POST['mail'];} ?>">
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                                <h5>N°Téléphone</h5>
                                <input type="text" class="form-control" name="tel" value="<?php if (isset($_POST['valider'])){echo $_POST['tel'];} ?>">
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                                <h5>Fax</h5>
                                <input type="text" class="form-control" name="fax" value="<?php if (isset($_POST['valider'])){echo $_POST['fax'];} ?>">
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                                <h5>Longitude</h5>
                                <input type="text" class="form-control" name="longitude" value="<?php if (isset($_POST['valider'])){echo $_POST['longitude'];} ?>">
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                                <h5>Latitude</h5>
                                <input type="text" class="form-control" name="latitude" value="<?php if (isset($_POST['valider'])){echo $_POST['latitude'];} ?>">
                            </div>
                            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                                <h5>Description</h5>
                                <textarea class="form-control" name="descrip_adresse"><?php if (isset($_POST['valider'])){echo $_POST['descrip_adresse'];} ?></textarea>
                            </div>
                            
							<div id="valider" class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
								<input type="submit" class="btn btn-primary" value="Valider" name="valider">
								&nbsp; <a href="afficher-entreprise.php?QYtgoGbhhd=<?php echo $id ?>#positionimg"><i class="fa fa-reply"></i> Retour</a>
							</div>
						</form>
						<?php
						break;
						
						case 'categorie':
						?>
						<div align="center">
							<?php
							$nbr_categorie=1;
							if (isset($_POST['valider']))
							{
								$nbr_cat=0;
								$date_mise=date('Y-m-d H:i:s');
								for ($n=1; $n<=$nbr_categorie; $n++)
								{
									$grande_categorie=(addslashes($_POST['grande_categorie'.$n.'']));
									if(!empty($grande_categorie))
									{
										$nbr_cat2=0;
										for ($m=1; $m<=$nbr_categorie2; $m++)
										{
											$categorie=(addslashes($_POST['categorie'.$m.'_'.$n.'']));
											if(!empty($categorie))
											{
												$nbr_cat2++;
												$sql2= 'SELECT COUNT(*) FROM entreprise_categorie WHERE id_entreprise='.$id.' AND id_categorie='.$categorie.'';
												$reponse2=$bdd-> query($sql2) or die('Erreur SQL !<br />'.$sql2.'<br />'.mysql_error());
												$data2=$reponse2->fetch();
												if($data2[0]==0)
												{
													$req='INSERT INTO entreprise_categorie VALUES ('.$id.', '.$categorie.', "'.$date_mise.'")';
													$reponse=$bdd-> query($req) or die('Erreur SQL !<br />'.$req.'<br />'.mysql_error());
												}
											}
										}
										$sql2= 'SELECT COUNT(*) FROM entreprise_grande WHERE id_entreprise='.$id.' AND id_grande_categorie='.$grande_categorie.'';
										$reponse2=$bdd-> query($sql2) or die('Erreur SQL !<br />'.$sql2.'<br />'.mysql_error());
										$data2=$reponse2->fetch();
										if($data2[0]==0)
										{
											$nbr_cat++;
											$req='INSERT INTO entreprise_grande VALUES ('.$id.', '.$grande_categorie.', "'.$date_mise.'")';
											$reponse=$bdd-> query($req) or die('Erreur SQL !<br />'.$req.'<br />'.mysql_error());
											echo '<div class="alert alert-success">Ajout affectué avec succès</div>';
											?> 
											<script type="text/javascript">
                                            window.setTimeout("location=('ajouter-fichier.php?id=<?php echo $id ?>&W9mvyDvFWE5=<?php echo $chaiz; ?>&retour=<?php echo $retour; ?>');",2500);
                                            </script>
                                            <?php
										}
									}
									if($nbr_cat==0)
									{
										echo '<div class="alert alert-danger">Veuillez choisir la grande catégorie cette grande catégorie existe déjà dans la base de données</div>';
									}
								}
							}			
							?>
						</div>
						<form role="form" method="post" action="" enctype="multipart/form-data" name="form2">
                            <?php
							for($i=1; $i<=$nbr_categorie; $i++)
							{
							?>
								<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12" id="padding_left">
									<script type="text/javascript">$(function(){
										$("#categorie<?php echo $i ?>").chained("#grande_categorie<?php echo $i ?>");
									});
									</script>
																	
									<script type="text/javascript">$(function(){
										$("#sous_categorie<?php echo $i ?>").chained("#categorie<?php echo $i ?>");
									});
									</script>
									<span id="span" style="float:left;">[<?php echo $i; ?>]</span>
									<h5> Grande catégorie</h5>
									<select name="grande_categorie<?php echo $i ?>" id="grande_categorie<?php echo $i ?>" class="form-control">
										<?php
										if(isset($_GET['id_grande']))
										{
											$sql2 = 'SELECT * FROM grande_categorie WHERE id_grande_categorie= '.$id_grande.'';
											$reponse2=$bdd-> query($sql2) or die('Erreur SQL !<br />'.$sql2.'<br />'.mysql_error());
											$data2 =$reponse2->fetch();
											$id_grande_envoye=$data2[0];
											$libelle_grande_envoye=$data2['lib_grande_categorie'];
			
											echo '<option selected="selected" value="'.$id_grande_envoye.'">'.$libelle_grande_envoye.'</option>';
										}
										?>
										<option value="">Sélectionner une grande catégorie</option>
										<?php
										if(!isset($_GET['id_grande']))
										{
											$id_grande_envoye = 0;
										}
										$sql2='SELECT * FROM grande_categorie  WHERE id_grande_categorie<>'.$id_grande_envoye.' ORDER BY id_grande_categorie';
										$reponse2=$bdd-> query($sql2) or die('Erreur SQL !<br />'.$sql2.'<br />'.mysql_error());
										while ($data2 =$reponse2->fetch())
										{
											if(isset($_POST['valider']) and $_POST['grande_categorie'.$i.'']==$data2['id_grande_categorie'])
											{
												echo $cocher=' selected="selected"';
											}
											else
											{
												echo $cocher='';
											}
											echo '<option'.$cocher.' value="'.$data2[0].'">'.$data2['lib_grande_categorie'].'</option>';
										}
										?>
									</select>
									<?php
									for($m=1; $m<=$nbr_categorie2; $m++)
									{
										?>
										<div class="col-lg-6" id="padding">
											<script type="text/javascript">$(function(){
												$("#categorie<?php echo $m ?>_<?php echo $i ?>").chained("#grande_categorie<?php echo $i ?>");
											});
											</script>
											<h5>Catégorie <span id="span" style="font-size:12px;"><?php echo $m; ?></span></h5>
											<?php
											if(!isset($_GET['id_categorie']))
											{ 
												echo'<select name="categorie'.$m.'_'.$i.'" id="categorie'.$m.'_'.$i.'" class="form-control">';
											}
											elseif(isset($_GET['id_categorie']))
											{
												$sql2 = 'SELECT * FROM categorie WHERE id_categorie= '.$id_categorie.'';
												$reponse2=$bdd-> query($sql2) or die('Erreur SQL !<br />'.$sql2.'<br />'.mysql_error());
												$data2 =$reponse2->fetch();
												$id_grande_envoye=$data2[0];
												$libelle_grande_envoye=$data2['lib_categorie'];
					
												echo '<select name="categorie'.$i.'" class="form-control">
												<option selected="selected" value="'.$id_categorie_envoye.'">'.$libelle_categorie_envoye.'</option>';
											}
											?>
											<option value="">Sélectionner une catégorie</option>
											<?php
											if(!isset($_GET['id_categorie']))
											{
												$id_categorie_envoye = 0;
											}
											$sql2='SELECT * FROM categorie  WHERE id_categorie<> '.$id_categorie_envoye.' ORDER BY id_categorie';
											$reponse2=$bdd-> query($sql2) or die('Erreur SQL !<br />'.$sql2.'<br />'.mysql_error());
											while ($data2 =$reponse2->fetch())
											{
												if(isset($_GET['id_categorie']))
												{
													if(isset($_POST['valider']) and $_POST['categorie'.$i.'']==$data2['id_categorie'])
													{
														echo $cocher=' selected="selected"';
													}
													else
													{
														echo $cocher='';
													}
												}
												else
												{
													echo $cocher='';
												}
												echo '<option'.$cocher.' value="'.$data2[0].'" class="'.$data2['id_grande_categorie'].'">'.$data2['lib_categorie'].'</option>';
											}
											?>
											</select>
										</div>
									<?php
									}
									?>
								</div>
							<?php
							}
							?>
							<div id="valider" class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
								<input type="submit" class="btn btn-primary" value="Valider" name="valider">
								&nbsp; <a href="afficher-entreprise.php?QYtgoGbhhd=<?php echo $id ?>#positionimg"><i class="fa fa-reply"></i> Retour</a>
							</div>
						</form>
						<?php
						break;
						
						case 'entreprise_categorie':
						$categorie=addslashes($_GET['categorie']);
						?>
						<div align="center">
							<?php
							if (isset($_POST['valider']))
							{
								$type=addslashes(addslashes($_POST['type']));
								$date_mise=date('Y-m-d H:i:s');
								if (empty($type))
								{
									echo '<div class="alert alert-danger">Veuillez renseigner le type</div>';
								}
								else
								{
									$sql2= 'SELECT COUNT(*) FROM entreprise_categorie WHERE id_entreprise='.$id.' AND id_categorie='.$type.'';
									$reponse2=$bdd-> query($sql2) or die('Erreur SQL !<br />'.$sql2.'<br />'.mysql_error());
									$data2=$reponse2->fetch();
									if($data2[0]==0)
									{
										$req='INSERT INTO '.$chaiz.' VALUES ("'.$id.'", "'.$type.'", "'.$date_mise.'")';
										$reponse=$bdd-> query($req) or die('Erreur SQL !<br />'.$req.'<br />'.mysql_error());
										echo '<div class="alert alert-success"> ajout effectué avec succès</div>';
										?> 
										<script type="text/javascript">
										window.setTimeout("location=('ajouter-fichier.php?id=<?php echo $id ?>&categorie=<?php echo $categorie ?>&W9mvyDvFWE5=<?php echo $chaiz; ?>&retour=<?php echo $retour; ?>');",2500);
										</script>
										<?php
									}
									else
									{
										echo '<div class="alert alert-danger">Déjà ajouté</div>';
									}
								}
							}
							?>
						</div>
						<form role="form" method="post" action="" enctype="multipart/form-data" name="form2">
							<h4>Categorie</h4>
                            <select name="type" class="form-control">
								<?php
                                $sql2 = 'SELECT * FROM categorie WHERE id_grande_categorie="'.$categorie.'" ORDER BY lib_categorie ASC';
                                $reponse2=$bdd-> query($sql2) or die('Erreur SQL !<br />'.$sql2.'<br />'.mysql_error());
                                while ($data2 =$reponse2->fetch())
                                {
                                    if(isset($_POST['valider']) and $_POST['type']==$data2['id_categorie'])
                                    {
                                        echo $cocher=' selected="selected"';
                                    }
                                    else
                                    {
                                        echo $cocher='';
                                    }
                                    echo '<option'.$cocher.' value="'.$data2[0].'">'.$data2['lib_categorie'].'</option>';
                                }
                                ?>
                            </select>
							<div id="valider" class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
								<input type="submit" class="btn btn-primary" value="Valider" name="valider">
								&nbsp; <a href="afficher-entreprise.php?QYtgoGbhhd=<?php echo $id ?>#positionimg"><i class="fa fa-reply"></i> Retour</a>
							</div>
						</form>
						<?php
						break;
						
						case 'image':
						case 'photo':
						?>
						<div align="center">
							<?php
							if (isset($_POST['valider']))
							{
								$name_img=$_FILES['tof']['name'];
								$type_img=$_FILES['tof']['type'];
								$size_img=$_FILES['tof']['size'];
								$temp_img=$_FILES['tof']['tmp_name'];
								$error_img=$_FILES['tof']['error'];
								$extension=strrchr($name_img,'.');
								$date_mise=date('Y-m-d H:i:s');
								
								if ($error_img==0)
								{
									if ($extension=='.jpg' or $extension=='.png' or $extension=='.jpeg' or $extension=='.gif' or $extension=='.bmp' or $extension=='.JPG' or $extension=='.PNG' or $extension=='.JPEG' or $extension=='.GIF' or $extension=='.BMP')
									{
										$size2=getimagesize($temp_img);
										$width=$size2[0];
										$height=$size2[1];
										$lien_img=$date_img.'777ng_koben'.$extension;
										move_uploaded_file($temp_img, "../upload/".$lien_img);
										$req='INSERT INTO '.$chaiz.' VALUES (NULL, "'.$lien_img.'", "'.$date_mise.'", "'.$id.'", 0)';
										$reponse=$bdd-> query($req) or die('Erreur SQL !<br />'.$req.'<br />'.mysql_error());
										echo '<div class="alert alert-success"> Ajout effectué avec succès</div>';
										?>
										<script type="text/javascript">
										window.setTimeout("location=('ajouter-fichier.php?id=<?php echo $id ?>&W9mvyDvFWE5=<?php echo $chaiz; ?>&retour=<?php echo $retour; ?>');",2500);
										</script>
										<?php
									}
									else
									{
										echo '<div class="alert alert-danger">Format de l\'image Incorrect !</div>';
									}
								}
								elseif($error_img==4)
								{
									echo '<div class="alert alert-danger">Pas d\'image sélectionnée !</div>';
								}
								else
								{
									echo '<div class="alert alert-danger">Erreur de Transmission !</div>';
								}
							}			
							?>
						</div>
						<form role="form" method="post" action="" enctype="multipart/form-data" name="form2">
                        	<h4>Image</h4>
							<input type="file" class="form-control" name="tof">
							<div id="valider">
								<input type="submit" class="btn btn-primary" value="Valider" name="valider">
								&nbsp;
                                <?php
                                if($chaiz=='image')
								{
								?>
                                	<a href="afficher-table.php?W9mvyDvFWE5=<?php echo $retour ?>&QYtgoGbhhd=<?php echo $id ?>#positionimg"><i class="fa fa-reply"></i> Retour</a>
                                <?php
								}
								else
								{
									?>
                                	<a href="afficher-entreprise.php?QYtgoGbhhd=<?php echo $id ?>#positionimg"><i class="fa fa-reply"></i> Retour</a>
                                <?php
								}
								?>
							</div>
						</form>
						<?php
						break;
						
						case 'fichier':
						?>
						<div align="center">
							<?php
							if (isset($_POST['valider']))
							{
								$name_img=$_FILES['tof']['name'];
								$type_img=$_FILES['tof']['type'];
								$size_img=$_FILES['tof']['size'];
								$temp_img=$_FILES['tof']['tmp_name'];
								$error_img=$_FILES['tof']['error'];
								$extension=strrchr($name_img,'.');
								$date_mise=date('Y-m-d H:i:s');
								if ($error_img==0)
								{
									if ($extension=='.pdf' or $extension=='.mp3' or $extension=='.epub' or $extension=='.mp4' or $extension=='.amr' or $extension=='.PDF' or $extension=='.MP3' or $extension=='.EPUB' or $extension=='.MP4' or $extension=='.AMR' or $extension=='.doc' or $extension=='.DOC' or $extension=='.docx' or $extension=='.DOCX' or $extension=='.xls' or $extension=='.XLS' or $extension=='.xlsx' or $extension=='.XLSX' or $extension=='.3gp' or $extension=='.3GP')
									{
										include('format.php');
										$size2=getimagesize($temp_img);
										$width=$size2[0];
										$height=$size2[1];
										$lien_img=$date_img.'777ng_koben'.$extension;
										move_uploaded_file($temp_img, "../upload/".$lien_img);
										$req='INSERT INTO fichier VALUES (NULL, "'.$lien_img.'", "'.$extension.'", "'.$format.'", "'.$date_mise.'", "'.$id.'")';
										$reponse=$bdd-> query($req) or die('Erreur SQL !<br />'.$req.'<br />'.mysql_error());
										echo '<div class="alert alert-success"> Ajout effectué avec succès</div>';
										?>
										<script type="text/javascript">
										window.setTimeout("location=('ajouter-fichier.php?id=<?php echo $id ?>&W9mvyDvFWE5=<?php echo $chaiz; ?>&retour=<?php echo $retour; ?>');",2500);
										</script>
										<?php
									}
									else
									{
										echo '<div class="alert alert-danger">Format de l\'image Incorrect !</div>';
									}
								}
								elseif($error_img==4)
								{
									echo '<div class="alert alert-danger">Pas d\'image sélectionnée !</div>';
								}
								else
								{
									echo '<div class="alert alert-danger">Erreur de Transmission !</div>';
								}
							}			
							?>
						</div>
						<form role="form" method="post" action="" enctype="multipart/form-data" name="form2">
                        	<h4>Fichier</h4>
							<input type="file" class="form-control" name="tof">
							<div id="valider">
								<input type="submit" class="btn btn-primary" value="Valider" name="valider">
								&nbsp; <a href="afficher-table.php?W9mvyDvFWE5=<?php echo $retour ?>&QYtgoGbhhd=<?php echo $id ?>#positionimg"><i class="fa fa-reply"></i> Retour</a>
							</div>
						</form>
						<?php
						break;
						
						default: header('administration.php');
					}
					?>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

<?php 
}
else
{
	header('Location: index.php');
}
?>