extranet/includes/export.php

128 lines
3.0 KiB
PHP

<?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
$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();
}
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 ExportPDF {
function __construct(){}
}
?>