extranet/includes/export.php
Michael RICOIS 5e2a861076 Export PDF
2009-03-05 08:31:21 +00:00

137 lines
3.3 KiB
PHP

<?php
// Include XML_Serializer
require_once('XML/Serializer.php');
require_once(realpath(dirname(__FILE__)).'/cache.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 $page = '';
public $rootName = 'page';
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,
);
}
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($page, $siren, $idEntreprise = 0){
$this->page = str_replace('.php','',$page);
$this->siren = $siren;
$this->idEntreprise = $idEntreprise;
$this->serialize();
if ($this->siren==0){
$this->fileName = $this->path.'/'.$this->page.'-'.$this->idEntreprise.'.xml';
}else{
$this->fileName = $this->path.'/'.$this->page.'-'.$this->siren.'.xml';
}
if( !file_put_contents($this->fileName, $this->content))
{
//TODO : Gestion des erreurs
}
}
}
class ExportCSV {
public $siren = '';
public $idEntreprise = '';
public $fileName = '';
public $path = '';
public $page = '';
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($page, $siren, $idEntreprise = 0)
{
$this->page = str_replace('.php','',$page);
$this->siren = $siren;
$this->idEntreprise = $idEntreprise;
if ($this->siren==0){
$this->fileName = $this->path.'/'.$this->page.'-'.$this->idEntreprise.'.csv';
}else{
$this->fileName = $this->path.'/'.$this->page.'-'.$this->siren.'.csv';
}
$newRecords=array();
$this->array_flatten($this->records,$newRecords);
$headerKeys = array_keys($newRecords);
$values = array_values($newRecords);
//Ecriture du fichier
$fp = fopen($this->fileName, 'w');
if ($fp != FALSE){
//Header du fichier csv
fputcsv($fp, $headerKeys, $this->delimiter);
//Contenu du fichier
fputcsv($fp, $values, $this->delimiter);
fclose($fp);
}else{
//Erreur
}
}
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(){}
}
?>