<?php
include('include/entete.php');
include('include/entete_adm.php');
include('config/config.inc.php');
include('config/lib.inc.php');
include('config/lib_personal_PhPExcel.php');

function downloadFile($file) {
  $ar_ext = explode('.', $file);
  $ext = strtolower(end($ar_ext));
  $extensions = array(
    'bmp' => 'image/bmp',
    'csv' => 'text/csv',
    'doc' => 'application/msword',
    'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    'exe' => 'application/octet-stream',
    'gif' => 'image/gif',
    'htm' => 'text/html',
    'html' => 'text/html',
    'ico' => 'image/vnd.microsoft.icon',
    'jpeg' => 'image/jpg',
    'jpe' => 'image/jpg',
    'jpg' => 'image/jpg',
    'pdf' => 'application/pdf',
    'png' => 'image/png',
    'ppt' => 'application/vnd.ms-powerpoint',
    'psd' => 'image/psd',
    'swf' => 'application/x-shockwave-flash',
    'tif' => 'image/tiff',
    'tiff' => 'image/tiff',
    'xhtml' => 'application/xhtml+xml',
    'xml' => 'application/xml',
    'xls' => 'application/vnd.ms-excel',
    'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    'zip' => 'application/zip'
  );
  $ctype = isset($extensions[$ext]) ? $extensions[$ext] : 'application/force-download';

  if (file_exists($file) && is_readable($file)) {
    // required for IE, otherwise Content-disposition is ignored
    if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');


    header('Pragma: public'); // required
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Cache-Control: private',false);    // required for certain browsers
    header('Content-Type: '. $ctype);
    header('Content-Disposition: attachment; filename='. $file .';' );
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '. filesize($file));
    readfile($file);
  }
  else {
    header('HTTP/1.0 404 Not Found');
    echo "<h1>Error 404: File Not Found: <br /><em>$file</em></h1>";
  }
}
$maCleDeCryptage = getCleDeCryptage();	

$pathFic = 'export/';
$filename=date("Ymd")."_reservations.xls";
$finalData=array();
$cpt=1;

//libelles
$finalData[0]['A']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Etablissement");
$finalData[0]['B']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Adresse 1");
$finalData[0]['C']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Adresse 2");
$finalData[0]['D']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Code postal");
$finalData[0]['E']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Ville");
$finalData[0]['F']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Classes");
$finalData[0]['G']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Elèves");
$finalData[0]['H']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Date réservation");
$finalData[0]['I']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Créneaux");
$finalData[0]['J']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Nom CFA/LP");
$finalData[0]['K']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Localité CFA/LP");


	$query="SELECT *
			FROM `agenda`
			INNER JOIN eta 
			ON eta.id_eta = agenda.numEta
			WHERE `statut` = 'reserve' AND (
			`numEta` <> 399
			AND  `numEta` <> 400
			AND  `numEta` <> 402
			AND  `numEta` <> 403)";
	$db = connect_base();
	$queryRequest = $db->query($query);
	$queryRequest->execute();
	
		
	
	if($queryRequest->rowCount()>0){
		$data=$queryRequest->fetchAll();
		
		$coLy_tmp = array();
		$coLy_fullDetail = array();
		
		
		foreach($data as $key => $value){
			$coLy_tmp[] = array(
				"id_college_lycee" 	=> $value['numCoLy'],
				"jour_reserve"		=> mysqlToDateFr($value['jour']),
				"creneau"			=> getCreneauPlus($value['creneau']),
				"nom_cfa_lp"		=> decrypter($maCleDeCryptage, $value['nom_eta']),
				"localite_cfa_lp" 	=> decrypter($maCleDeCryptage, $value['cp']). " " . decrypter($maCleDeCryptage, $value['ville'])
			);
		}

		
		foreach($coLy_tmp as $key => $value){
			
			$db = connect_base();
			$query = $db->prepare("SELECT * FROM `collegelycee` WHERE `idCoLy` =:idCoLy");
			$query->bindParam( ':idCoLy', $value['id_college_lycee'], PDO::PARAM_INT );
			$query->execute();
			
			$result[] = $query->fetchAll();	
			$result[$key][0]['chxJour'] = $value['jour_reserve'];
			$result[$key][0]['chxCreneau'] = $value['creneau'];
			$result[$key][0]['nom_cfa_lp'] = $value['nom_cfa_lp'];
			$result[$key][0]['localite_cfa_lp'] = $value['localite_cfa_lp'];
			
		}
		
		foreach($result as $k => $v){
			$coLy=decrypter($maCleDeCryptage,$v[0]['etaCoLy']);
			$add1 = decrypter($maCleDeCryptage,$v[0]['adresse1CoLy']);
			$add2 = decrypter($maCleDeCryptage,$v[0]['adresse2CoLy']);
			$cp = decrypter($maCleDeCryptage,$v[0]['cpCoLy']);
			$ville = decrypter($maCleDeCryptage,$v[0]['villeCoLy']);
			$classes = $v[0]['nbClasses'];
			$eleves = $v[0]['nbEleves'];
			$date = $v[0]['chxJour'];
			$creneaux = $v[0]['chxCreneau'];
			$nomCfaLp = $v[0]['nom_cfa_lp'];
			$localiteCfaLp = $v[0]['localite_cfa_lp'];
			
	
	
	

	$finalData[$cpt]['A']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$coLy);
	$finalData[$cpt]['B']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$add1);
	$finalData[$cpt]['C']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$add2);
	$finalData[$cpt]['D']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$cp);
	$finalData[$cpt]['E']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$ville);
	$finalData[$cpt]['F']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$classes);
	$finalData[$cpt]['G']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$eleves);
	$finalData[$cpt]['H']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$date);
	$finalData[$cpt]['I']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$creneaux);
	$finalData[$cpt]['J']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$nomCfaLp);
	$finalData[$cpt]['K']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$localiteCfaLp);
	$cpt++;
}
	}

$attributsFichier = array();
$attributsFichier[0] = "Export Deciday";
$attributsFichier[1] = "Export Deciday";
$attributsFichier[2] = "Export Deciday";
$attributsFichier[3] = "Export Deciday";
$attributsFichier[4] = "Export Deciday";
$attributsFichier[5] = "Export Deciday";
$attributsFichier[6] = "Export Deciday";
$attributsFichier[7] = "Export Deciday";




	writingArrayWithLetterToExcel5WithType($pathFic.$filename, 0, "DECIDAY", $finalData, $attributsFichier, PHPExcel_Cell_DataType::TYPE_STRING);


	downloadFile($pathFic.$filename);