<?php require_once(dirname(__DIR__, 1) . '/config.php');
require_once(BASE_PATH . '/db/idl_connect.php');
echo '<meta charset="utf-8" />';
if(isset($_SESSION['user_droits'])) {
	if($_SESSION['user_droits'][5]<2)
	{
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="../index.php">'.ACCUEIL.'</a></p>';
	}
	else {
		$verif_tabeleves = mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves WHERE categ = '1' ORDER BY 
	replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(
	replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(
	replace(nom,'À','A'),'Á','A'),'Â','A'),'Ã','A'),'Ä','A'),'Å','A'),'Ç','C'),'È','E'),'É','E')
	,'Ê','E'),'Ë','E'),'Ì','I'),'Í','I'),'Î','I'),'Ï','I'),'Ò','O'),'Ó','O'),'Ô','O'),'Õ','O')
	,'Ö','O'),'Ù','U'),'Ú','U'),'Û','U'),'Ü','U'),'Ý','Y')");
		$nb_ele = mysqli_num_rows($verif_tabeleves);
		?><style type="text/css">
		#mda3 { background-color: #fff; }
		#mda3 td { border: 3px #086A87 solid; }
		#mda2 td { border: 0; }
		</style><?php
		echo DATAC_EXP1;
		
		echo '<form method="POST" name="print" action="" class="padd2">
		<table id="install" class="noborder">';
		include('db/proc/database_formexport.php');
		echo '<tr><td>'.DATAEXP3.' </td><td>';
		echo '<div class="box" style="float:right;"><select name="format"><option value="ods">ODS</option><option value="csv">CSV</option><option value="xls">XLS</option></select></div></td></tr>';
		echo '<tr><td colspan="2"><p  class="right"><input class="bouton boutdb" type="submit" value="'.VALID.'" name="go" /></td></tr>
		</table>
		
		</form>';

		if(isset($_POST['go'])&&@$_POST['format']!=""&&@$_POST['division']!="") {
			
			if($_POST['format']=='ods'||$_POST['format']=='xls') {		    
				$bctab = '<table><thead><tr>';

				for($i=1;$i<11;$i++) {
					if(@$_POST['col'.$i]!='0') {
						$bctab .= '<th>';
						if($_POST['col'.$i]=="eleves_id") { $bctab .= IDENT2; }
						else if($_POST['col'.$i]=="nom") { $bctab .= NAME; }
						else if($_POST['col'.$i]=="prenom") { $bctab .= mb_convert_encoding(FORNAME,'utf-16','utf-8'); }
						else if($_POST['col'.$i]=="division") { $bctab .= DIV; }
						else if($_POST['col'.$i]=="naissance") { $bctab .= BDAY; }
						else if($_POST['col'.$i]=="sexe") { $bctab .= SEX; }
						else if($_POST['col'.$i]=="code") { $bctab .= CODE; }
						else if($_POST['col'.$i]=="mail") { $bctab .= MAIL2; }
						$bctab .= '</th>';
					}
					else if(@$_POST['col'.$i]=='0'&&@$_POST['txtcol'.$i]!='') {
						$bctab .= '<th>'.$_POST['txtcol'.$i].'</th>';
					}
				}

				$bctab .= '</tr></thead><tbody>';

				if($_POST['division']=='tout') {
					$eleves = mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves WHERE categ = '1' ORDER BY nom ASC, prenom ASC");
				}
				else {
					$division = mysqli_real_escape_string($bdd,$_POST['division']);
					$eleves = mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves WHERE division = '".$division."' ORDER BY nom ASC, prenom ASC");
				}
				
				$eleves_nb = mysqli_num_rows($eleves);
				if($eleves_nb>0) {
					foreach($eleves as $eleve_data) {
						$bctab .= '<tr>';
						for($i=1;$i<11;$i++) {
							if(@$_POST['col'.$i]!='0') {
								$bctab .= '<td>'.mb_convert_encoding($eleve_data[$_POST['col'.$i]],'utf-16','utf-8').'</td>';
							}
							else if(@$_POST['col'.$i]=='0'&&@$_POST['txtcol'.$i]!='') {
								$bctab .= '<td></td>';
							}
						}
						$bctab .= '</tr>';
					}
				}
				else {
					$groupele = mysqli_query($bdd,"SELECT groupele.groupe_id, groupele.eleves_id, eleves.nom, eleves.prenom, eleves.division, eleves.code, eleves.naissance, eleves.sexe
					FROM ".$prefix."_groupele groupele
					INNER JOIN ".$prefix."_eleves eleves
					ON groupele.eleves_id = eleves.eleves_id
					WHERE groupele.groupe_id = '".$division."' 
					ORDER BY eleves.division ASC, eleves.nom ASC");
					$eleves_nb = mysqli_num_rows($groupele);
					foreach($groupele as $eleve_data) {
						$bctab .= '<tr>';
						for($i=1;$i<11;$i++) {
							if($_POST['col'.$i]!='0') {
								$bctab .= '<td>'.mb_convert_encoding($eleve_data[$_POST['col'.$i]],'utf-16','utf-8').'</td>';
							}
							else if($_POST['txtcol'.$i]!='') {
								$bctab .= '<td></td>';
							}
						}
						$bctab .= '</tr>';
					}
				}
				$bctab .= '</tbody></table>';
				$_SESSION['bctab'] = $bctab;
			}
			else {
				$tabcsv = array();
				for($i=1;$i<11;$i++) {
					if(@$_POST['col'.$i]!='0') {
						if($_POST['col'.$i]=="eleves_id") { array_push($tabcsv,'eleves_id'); }
						else if($_POST['col'.$i]=="nom") { array_push($tabcsv,'nom'); }
						else if($_POST['col'.$i]=="prenom") { array_push($tabcsv,'prenom'); }
						else if($_POST['col'.$i]=="division") { array_push($tabcsv,'division'); }
						else if($_POST['col'.$i]=="naissance") { array_push($tabcsv,'naissance'); }
						else if($_POST['col'.$i]=="sexe") { array_push($tabcsv,'sexe'); }
						else if($_POST['col'.$i]=="code") { array_push($tabcsv,'code'); }
						else if($_POST['col'.$i]=="mail") { array_push($tabcsv,'mail'); }
					}
				}
				$_SESSION['tabcsv'] = str_replace(array('"',"[","]"),"",json_encode($tabcsv));
			}
			$_SESSION['division'] = $_POST['division'];
			$_SESSION['format'] = $_POST['format'];
			echo '<script language="Javascript">
				<!--
				document.location.replace("./db/proc/database_tabexport.php");
				// -->
			</script>';			
		}
	}
}
else {
	include('include/noaccess.php');
	echo '<a href="../index.php">'.ACCUEIL.'</a></p>';
}
?>
