<?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_pro.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","Siret");
$finalData[0]['C']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Nom");
$finalData[0]['D']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Prénom");
$finalData[0]['E']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Adresse 1");
$finalData[0]['F']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Adresse 2");
$finalData[0]['G']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Code postal");
$finalData[0]['H']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Ville");
$finalData[0]['I']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Mobile");
$finalData[0]['J']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Fixe");
$finalData[0]['K']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Email");
$finalData[0]['L']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Déciday!");
$finalData[0]['M']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Stage 3ème");
$finalData[0]['N']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Rdv étudiant");
$finalData[0]['O']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Date d'inscription");
$finalData[0]['P']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Statut");
$finalData[0]['Q']=iconv("UTF-8", "ISO-8859-1//TRANSLIT","Commentaire");

$query="select * from inscriptions_pro";
$db = connect_base();
$queryRequest = $db->prepare( $query );
$queryRequest->execute();
$data=$queryRequest->fetchAll();

foreach($data as $cle=>$val){
  $eta = decrypter($maCleDeCryptage,$val['etablissement_pro']);
  $siret = decrypter($maCleDeCryptage,$val['siret_pro']);
  $nom = decrypter($maCleDeCryptage,$val['nom_pro']);
  $prenom = decrypter($maCleDeCryptage,$val['prenom_pro']);
  $adr1 = decrypter($maCleDeCryptage,$val['adr1_pro']);
  $adr2 = decrypter($maCleDeCryptage,$val['adr2_pro']);
  $cp = decrypter($maCleDeCryptage,$val['cp_pro']);
  $ville = decrypter($maCleDeCryptage,$val['ville_pro']);
  $mobile = decrypter($maCleDeCryptage,$val['mobile_pro']);
  $fixe = decrypter($maCleDeCryptage,$val['fixe_pro']);
  $email = decrypter($maCleDeCryptage,$val['email_pro']);
  if($val['faireEquipe'] == 1) $deciday = "oui"; else $deciday = "non";
  if($val['recevoirStage'] == 1) $stage = "oui"; else $stage = "non";
  if($val['recevoirJeunes']== 1) $rdv = "oui"; else $rdv = "non";
  $date = mysqlToDateFr($val['date_insert']);
  if($val['statut'] == 1) $statut = "validé";
  else if ($val['statut'] == 2) $statut = "refusé";
  else $statut = "en attente";
  $commentaire = decrypter($maCleDeCryptage,$val['comment_pro']);

	

	$finalData[$cpt]['A']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$eta);
	$finalData[$cpt]['B']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$siret);
	$finalData[$cpt]['C']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$nom);
	$finalData[$cpt]['D']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$prenom);
	$finalData[$cpt]['E']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$adr1);
  $finalData[$cpt]['F']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$adr2);
  $finalData[$cpt]['G']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$cp);
  $finalData[$cpt]['H']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$ville);
  $finalData[$cpt]['I']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$mobile);
  $finalData[$cpt]['J']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$fixe);
  $finalData[$cpt]['K']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$email);
  $finalData[$cpt]['L']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$deciday);
  $finalData[$cpt]['M']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$stage);
  $finalData[$cpt]['N']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$rdv);
  $finalData[$cpt]['O']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$date);
  $finalData[$cpt]['P']=iconv("UTF-8", "ISO-8859-1//TRANSLIT",$statut);
  $finalData[$cpt]['Q']=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);