<?php
// Include XML_Serializer
require_once('XML/Serializer.php');
/**
 * Export - Gère les différents format d'exportation
 * @package export
 * @author Michael RICOIS
 * @copyright Scores&Decisions
 */
class ExportXML {
	public $fileName = '';
	public $path = '';
	public $rootName = 'root';
	public $defaultTagName = 'element';
	public $records = array();
	public $encoding = 'UTF-8';
	public $serializer_options = array();
	public $content = '';

	function __construct() {
		$this->path = realpath(dirname(__FILE__).'/../cache/');
	}

	function setXMLOptions(){
		// An array of serializer options
		$this->serializer_options = array (
		   	'addDecl' => TRUE,
		   	'encoding' => $this->encoding,
		   	'indent' => '  ',
		   	'rootName' => $this->rootName,
			'defaultTagName' => $this->defaultTagName,
		);
	}

	function serialize(){
		$this->setXMLOptions();
		// Instantiate the serializer with the options
                $err_rep = error_reporting();
                error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
		$Serializer = new XML_Serializer($this->serializer_options);
		// Serialize the data structure
		$status = $Serializer->serialize($this->records);
		// Check whether serialization worked
		if (PEAR::isError($status)) {
		   //$status->getMessage()
		}
		$this->content = $Serializer->getSerializedData();
                error_reporting($err_rep);
	}

	function writeXML($fileName){
		$fileName = str_replace('.php','',$fileName);
		$this->fileName = $this->path .'/'. $fileName . '.xml';
		$this->serialize();
		if( !file_put_contents($this->fileName, $this->content))
		{
			//TODO : Gestion des erreurs
		}
	}
}

class ExportCSV {

	public $fileName = '';
	public $path = '';
	public $encoding = 'UTF-8';
	public $delimiter = ';';
	public $records = array();

	function __construct() {
		$this->path = realpath(dirname(__FILE__).'/../cache/');
	}

	/**
	 * Ecrit un fichier csv à partir d'un tableau.
	 * @param string $fileName
	 * @param array $records
	 * @param string $delimiter
	 * @return void
	 */
	function writeCSV($fileName)
	{
		$fileName = str_replace('.php','',$fileName);
		$this->fileName = $this->path .'/'. $fileName . '.csv';

		//Ouverture du fichier
		$fp = fopen($this->fileName, 'w');
		//On attribue le header
		$newRecords=array();
		$this->array_flatten($this->records[0],$newRecords);
		$headerKeys = array_keys($newRecords);
		if ($fp != FALSE){
			//Header du fichier csv
			fputcsv($fp, $headerKeys, $this->delimiter);
		}
		foreach( $this->records as $record ){
			$newRecords=array();
			$this->array_flatten($record,$newRecords);
			$values = array_values($newRecords);
			if ($fp != FALSE){
				//Contenu du fichier
				fputcsv($fp, $values, $this->delimiter);
			}else{
				//Erreur
			}
		}
		fclose($fp);
	}

	function array_flatten($array, &$newArray = Array(),
                               $prefix = '', $delimiter = '|') {
            foreach ($array as $key => $child) {
                if (is_array($child)) {
                    $newPrefix = $prefix.$key.$delimiter;
                    $newArray = $this->array_flatten($child, $newArray,
                                                     $newPrefix, $delimiter);
                } else {
                    $newArray[$prefix.$key] = $child;
                }
            }
            return $newArray;
	}

}

class tabExport
{
    public $tab = array();
    public $notdisplay = array();
    public $title = array();
    public $type = array();

    public $convert = array();

    function __construct() {}

    function convertTable() {
        $this->convert = $this->rArray($this->tab);
        return $this->convert;
    }

    function rArray($array) {
        foreach ($array as $key => $child) {
            if (is_array($child)) {
                if (!in_array($key, $this->notdisplay, TRUE)) {
                    $newArray[$this->renameKey($key)] = $this->rArray($child);
                }
            } else {
                if (!in_array($key, $this->notdisplay, TRUE)) {
                    $newArray[$this->renameKey($key)] =
                        $this->convertValue($key, $child);
                }
            }
        }
        return $newArray;
    }

    function renameKey($key){
        $newKey = $key;
        if (array_key_exists($key, $this->title)) {
            $newKey = $this->title[$key];
        }
        return $newKey;
    }

    function convertValue($key, $value){
        if (array_key_exists($key, $this->type)) {
            switch ($this->type[$key]) {
            case 'date':
                //Test des différents formats de value
                break;
            }
        }
        return $value;
    }
}

class ExportPDF
{
    function __construct(){}

}
?>