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

//libelles
$finalData[0]['A']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","CFA/LP");
$finalData[0]['B']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Code postal");
$finalData[0]['C']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Ville");
$finalData[0]['D']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","E-mail référent");
$finalData[0]['E']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Etablissement");
$finalData[0]['F']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Nom Prénom");
$finalData[0]['G']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Fonction");
$finalData[0]['H']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Téléphone");
$finalData[0]['I']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","E-mail");
$finalData[0]['J']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Adresse 1");
$finalData[0]['K']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Adresse 2");
$finalData[0]['L']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Code postal");
$finalData[0]['M']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Ville");
$finalData[0]['N']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Classes");
$finalData[0]['O']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Elèves");
$finalData[0]['P']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Créneaux");
$finalData[0]['Q']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Statut");
$finalData[0]['R']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Date");
$finalData[0]['S']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Commentaire");
$query="SELECT *
		FROM `sollicitations`
		INNER JOIN `eta` ON id_eta = chxNumEta
		ORDER BY dateCreate ASC";
$db = connect_base();
$queryRequest = $db->prepare( $query );
$queryRequest->execute();
$data=$queryRequest->fetchAll();
foreach($data as $cle=>$val){
	$lp=$lpCP=$lpVille=$lpEmail=$coLy=$add1=$add2=$cp=$ville=$nom_CoLy=$fonction_CoLy=$tel_CoLy=$email_CoLy="";
	$lp=decrypter($maCleDeCryptage,$val['nom_eta']);
	$lpCP=decrypter($maCleDeCryptage,$val['cp']);
	$lpVille=decrypter($maCleDeCryptage,$val['ville']);
	$lpEmail=decrypter($maCleDeCryptage,$val['email_ref']);
	$coLy=decrypter($maCleDeCryptage,$val['etaCoLy']);
	$nom_CoLy=decrypter($maCleDeCryptage,$val['nomCoLy'])." ".decrypter($maCleDeCryptage,$val['prenomCoLy']);
	$fonction_CoLy=decrypter($maCleDeCryptage,$val['fonctionCoLy']);
	$tel_CoLy=decrypter($maCleDeCryptage,$val['telCoLy']);
	$email_CoLy=decrypter($maCleDeCryptage,$val['emlCoLy']);
	$add1 = decrypter($maCleDeCryptage,$val['adresse1CoLy']);
	$add2 = decrypter($maCleDeCryptage,$val['adresse2CoLy']);
	$cp = decrypter($maCleDeCryptage,$val['cpCoLy']);
	$ville = decrypter($maCleDeCryptage,$val['villeCoLy']);
	$classes = $val['nbClasses'];
	$eleves = $val['nbEleves'];
	$creneaux = MysqlToDateFr($val['chxJour'])." ".$val['chxCreneau'];
	if($val['traitement'] <> 1) {
		if(intval($val['idCapitaine']) > 0 ){
			$statut = "En attente";
		} else {
			$statut = "Refusée";
		}
	} else {
		$statut = "Traitée";
	}
	$date=MysqlToDateFr($val['dateCreate']);
	$commentaire=decrypter($maCleDeCryptage,$val['commentCoLy']);
	
	
	

	$finalData[$cpt]['A']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$lp);
	$finalData[$cpt]['B']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$lpCP);
	$finalData[$cpt]['C']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$lpVille);
	$finalData[$cpt]['D']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$lpEmail);
	$finalData[$cpt]['E']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$coLy);
	$finalData[$cpt]['F']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$nom_CoLy);
	$finalData[$cpt]['G']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$fonction_CoLy);
	$finalData[$cpt]['H']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$tel_CoLy);
	$finalData[$cpt]['I']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$email_CoLy);
	$finalData[$cpt]['J']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$add1);
	$finalData[$cpt]['K']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$add2);
	$finalData[$cpt]['L']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$cp);
	$finalData[$cpt]['M']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$ville);
	$finalData[$cpt]['N']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$classes);
	$finalData[$cpt]['O']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$eleves);
	$finalData[$cpt]['P']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$creneaux);
	$finalData[$cpt]['Q']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$statut);
	$finalData[$cpt]['R']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$date);
	$finalData[$cpt]['S']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$commentaire);
	$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);