<?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")."_suivi_cap.xls";
$finalData=array();
$cpt=1;

//libelles
$finalData[0]['A']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Nom");
$finalData[0]['B']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Prénom");
$finalData[0]['C']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Email");
$finalData[0]['D']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Etablissement");
$finalData[0]['E']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Code postal");
$finalData[0]['F']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Ville");

$query="SELECT * FROM `capitaines`
			INNER JOIN eta ON  num_eta = id_eta";
$db = connect_base();
$queryRequest = $db->prepare( $query );
$queryRequest->execute();
$data=$queryRequest->fetchAll();
foreach($data as $cle=>$val){
	$eta=$nom=$prenom=$cp=$ville=$email="";
	$eta=decrypter($maCleDeCryptage,$val['nom_eta']);
	$cp=decrypter($maCleDeCryptage,$val['cp']);
	$ville=decrypter($maCleDeCryptage,$val['ville']);
	$nom=decrypter($maCleDeCryptage,$val['nom_cap']);
	$prenom=decrypter($maCleDeCryptage,$val['prenom_cap']);
	$email=decrypter($maCleDeCryptage,$val['email_cap']);
					

	$finalData[$cpt]['A']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$nom);
	$finalData[$cpt]['B']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$prenom);
	$finalData[$cpt]['C']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$email);
	$finalData[$cpt]['D']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$eta);
	$finalData[$cpt]['E']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$cp);
	$finalData[$cpt]['F']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$ville);
	$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);