<?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");

/*****************************************************/
/* Fonctions d'extension de la classe PHP EXCEL      */
/*****************************************************/

/* Classe orignale : MaitrePylos Technologies */
/* Modification : Galawa */

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

/**
 * Class EXPORT
 */
class EXPORT extends PHPExcel
{
    public function __construct()
    {
        parent::__construct();
    }

    /* Exportation avec choix par switch */
    /* Format / nom de fichier / type de sortie ($export = TRUE => fichier, FALSE => sauvegarde) */

    /**
     * @param string $format
     * @param string $nomFichier
     * @param bool $export
     */
    public function exportation($format = 'Excel5', $nomFichier = 'Tableur', $export = TRUE)
    {
        $tab_header = [];
        switch ($format) {
            case 'Excel2007' :
                include '../classes/phpexcel/PHPExcel/Writer/Excel2007.php';
                ini_set('memory_limit', '-1');
                $writer = new PHPExcel_Writer_Excel2007($this);
                //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';
                ini_set('memory_limit', '-1');
                $writer = new PHPExcel_Writer_Excel2007($this);
                $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';
                ini_set('memory_limit', '-1');
                $writer = new PHPExcel_Writer_Excel5($this);
                // 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';
                ini_set('memory_limit', '-1');
                $writer = new PHPExcel_Writer_CSV($this);
                $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';
                ini_set('memory_limit', '-1');
                $writer = new PHPExcel_Writer_PDF($this);
                $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';
                ini_set('memory_limit', '-1');
                $writer = new PHPExcel_Writer_HTML($this);
                $writer->setSheetIndex(0); //Une seule feuille possible
                // variable d'export et entêtes
                $ext = 'html';
                $tab_header[] = 'Content-type:text/html';
                break;
        }

        if ($export == TRUE) {
            session_start();
            $_SESSION['generationStatut'] = "fini";


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

?>