<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.2
 *
 * Copyright (C) 2006-2015  Sylvain FATOME
 * This program is free software; you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program
 * (in the directory docs);
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 * Contact : galawa@free.fr
 *
 * Web : http://smi.no-ip.org
 ****************************************************************************************/

// Vérification des tentatives de hack
$pos = strpos($_SERVER['PHP_SELF'], "inc.php");
if ($pos === FALSE)
    $ok_inc = TRUE;
else
    $ok_inc = FALSE;
if ($ok_inc == FALSE) header("Location: ../index.php");

include_once("../inc/fic_age.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/fic_plan.inc.php");
include_once("../inc/fic_rdv.inc.php");
include_once("../inc/fic_tac.inc.php");
include_once("../inc/fic_tec.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_time.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/lst_int.inc.php");
include_once("../inc/lst_tri.inc.php");

include_once("../classes/ezSQL/shared/ez_sql_core.php");
include_once("../classes/ezSQL/mysqli/ez_sql_mysqli.php");
include_once("../inc/func_sql.inc.php");
Archivelog(@$_SESSION["code_log"]);
// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);

$mode = "Excel5";
$fi = "";
$cat = "";
$urg = "";
$dateddef1 = "";
$dateddef2 = "";
$dateddefp1 = "";
$dateddefp2 = "";
if (isset($_GET['fi']) && ($_GET['fi'] <> ""))
    $fi = intval($_GET['fi']);
if (isset($_GET['cat']) && ($_GET['cat'] <> ""))
    $cat = $_GET['cat'];
if (isset($_GET['urg']) && ($_GET['urg'] <> ""))
    $urg = intval($_GET['urg']);
if (isset($_GET['dateddef1']) && ($_GET['dateddef1'] <> "")) {
    if (chkdate($_GET['dateddef1']) == TRUE)
        $dateddef1 = $_GET['dateddef1'];
}
if (isset($_GET['dateddef2']) && ($_GET['dateddef2'] <> "")) {
    if (chkdate($_GET['dateddef2']) == TRUE)
        $dateddef2 = $_GET['dateddef2'];
}
if (isset($_GET['dateddefp1']) && ($_GET['dateddefp1'] <> "")) {
    if (chkdate($_GET['dateddefp1']) == TRUE)
        $dateddefp1 = $_GET['dateddefp1'];
}
if (isset($_GET['dateddefp2']) && ($_GET['dateddefp2'] <> "")) {
    if (chkdate($_GET['dateddefp2']) == TRUE)
        $dateddefp2 = $_GET['dateddefp2'];
}
$tab_cli = [];
if (@$_SESSION["niv_log"] == 1)
    $tab_cli = give_tab_rcli($_SESSION["code_log"], TRUE);

$tab_int = [];
if ($_SESSION["niv_log"] == 3)
    $tab_int = give_tab_int_ok_tec(@$_SESSION["code_log"], @$_SESSION["code_age"], TRUE);

$db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

/*** On détermine d'abord le nombre d'enregistrements concernés ***/

// Création de la commande MySQL initiale
$rechnum = cree_strrechlstintfi(TRUE);
$rech = cree_strrechlstintfi(FALSE,TRUE);
// Modification de la chaine de recherche en fonction des filtres par colonne
$rechnum = cree_strrechlst_filtre_col($rechnum, TRUE);
$rech = cree_strrechlst_filtre_col($rech);
// Rajout des tris
switch (@$_SESSION["niv_log"]) {
    case 1 :
        if (count($tab_cli) <= 1) {
            $rech .= crestr_triord($intcli_titre_col, "int_code", FALSE, $limit);
        } else
            $rech .= crestr_triord($int_titre_col, "int_code", FALSE, $limit);
        break;
    default :
        $rech .= crestr_triord($int_titre_col, "int_code", FALSE, $limit);
        break;
}
// Exécution de la requête
$numrows = $db->get_var($rechnum);
switch (@$_SESSION["niv_log"]) {
    case 3 :
        if (count($tab_int) == 0)
            $numrows = 0;
        break;
    default :
        break;
}

set_time_limit(0);

include_once("../classes/phpexcel/PHPExcel.php");

/*
@session_start();   
$_SESSION['generationStatut'] = "Creation";
session_write_close();
*/

$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheSettings = ['memoryCacheSize' => '8MB'];
PHPExcel_Settings::setCacheStorageMethod($cacheMethod,$cacheSettings);
$classeur = new PHPExcel();
$classeur->setActiveSheetIndex(0);
$classeur->getDefaultStyle()->getFont()->setName('Calibri');
$classeur->getDefaultStyle()->getFont()->setSize(10);
$feuille = $classeur->getActiveSheet();
if (@$_SESSION["niv_log"] >= 5)
    $feuille->setTitle(utf8_encode('LISTE DES INTERVENTIONS'));
else
    $feuille->setTitle(utf8_encode('VOS INTERVENTIONS'));
/*
@session_start();
$_SESSION['generationStatut'] = "Generation";   
session_write_close();
*/

// Inclusion module génération du classeur
include_once("../inc/intrexp_lst.inc.php");

/*
session_start();
$_SESSION['generationStatut'] = "Exportation";
session_write_close();
*/

$db->disconnect();

// Création du Writer
$tab_headers = [];
$nomFichier = _("Liste_Interventions");
switch ($mode) {
    case 'Excel2007' :
        include '../classes/phpexcel/PHPExcel/Writer/Excel2007.php';
        $writer = new PHPExcel_Writer_Excel2007($classeur);
        //supprime le pré-calcul
        $writer->setPreCalculateFormulas(false);
        // variable d'export et entêtes
        $ext = 'xlsx';
        $tab_header[] = 'Content-type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
        $tab_header[] = 'Content-Disposition:inline;filename=' . $nomFichier . '.' . $ext;
        $tab_header[] = 'Cache-Control: no-cache, must-revalidate';
        $tab_header[] = 'Expires: Sat, 26 Jul 1997 05:00:00 GMT';
        break;

    case 'Excel2003' :
        include '../classes/phpexcel/PHPExcel/Writer/Excel2007.php';
        $writer = new PHPExcel_Writer_Excel2007($classeur);
        $writer->setOffice2003Compatibility(true);
        //supprime le pré-calcul
        $writer->setPreCalculateFormulas(false);
        // variable d'export et entêtes
        $ext = 'xlsx';
        $tab_header[] = 'Content-type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
        $tab_header[] = 'Content-Disposition:inline;filename=' . $nomFichier . '.' . $ext;
        $tab_header[] = 'Cache-Control: no-cache, must-revalidate';
        $tab_header[] = 'Expires: Sat, 26 Jul 1997 05:00:00 GMT';
        break;

    case 'Excel5' :
        include '../classes/phpexcel/PHPExcel/Writer/Excel5.php';
        $writer = new PHPExcel_Writer_Excel5($classeur);
        // variable d'export et entêtes
        $ext = 'xls';
        $tab_header[] = 'Content-type:application/vnd.ms-excel';
        $tab_header[] = 'Content-Disposition:inline;filename=' . $nomFichier . '.' . $ext;
        $tab_header[] = "Cache-Control: no-cache, must-revalidate";
        $tab_header[] = "Expires: Sat, 26 Jul 1997 05:00:00 GMT";
        break;

    case 'CSV' :
        include '../classes/phpexcel/PHPExcel/Writer/CSV.php';
        $writer = new PHPExcel_Writer_CSV($classeur);
        $writer->setDelimiter(","); //l'Opérateur de séparation est la virgule
        $writer->setSheetIndex(0); //Une seule feuille possible
        // variable d'export et entêtes
        $ext = 'csv';
        $tab_header[] = 'Content-type:text/csv';
        $tab_header[] = 'Content-Disposition: attachment; filename=' . $nomFichier . '.' . $ext;
        break;

    case 'PDF' :
        include '../classes/phpexcel/PHPExcel/Writer/PDF.php';
        $writer = new PHPExcel_Writer_PDF($classeur);
        $writer->setSheetIndex(0); //Une seule feuille possible
        // variable d'export et entêtes
        $ext = 'pdf';
        $tab_header[] = 'Content-type:application/pdf';
        $tab_header[] = 'Content-Disposition: attachment; filename=' . $nomFichier . '.' . $ext;
        $tab_header[] = 'Cache-Control: max-age=0';
        break;

    case 'HTML' :
        include '../classes/phpexcel/PHPExcel/Writer/HTML.php';
        $writer = new PHPExcel_Writer_HTML($classeur);
        $writer->setSheetIndex(0); //Une seule feuille possible
        // variable d'export et entêtes
        $ext = 'html';
        $tab_header[] = 'Content-type:text/html';
        break;
}

/*
session_start();
$_SESSION['generationStatut'] = "Fini";
*/

// Exportation
ob_clean();
if (count($tab_header) > 0) {
    foreach ($tab_header as $value) {
        header($value);
    }
}
$writer->save('php://output');

$classeur->exportation('Excel5', _("Liste_des_interventions"), true);

?>