starlinks/library/Scores/Conso/Export.php
Michael RICOIS bdce1b423e Demarrage
2015-09-29 10:09:23 +00:00

103 lines
2.7 KiB
PHP

<?php
class Scores_Conso_Export
{
//Extraction des logs payants
//Extraction des logs bruts
protected $entete = array(
'login' => "Identifiant",
'referenceParDefaut' => "Service",
'page' => "Code Requete",
'pageLib' => "Libellé Requete",
'params' => "Paramètres",
'siren' => "Siren",
'nic' => "Nic",
'raisonSociale' => "Raison Sociale",
'cp' => "CP",
'ville' => "Ville",
'dateHeure' => "DateHeure",
'nbDoublon' => "Nb Doublon",
);
protected $pageLib = array();
protected $data = array();
public function __construct()
{
$this->setPageLib();
}
public function setData($d)
{
$this->data = $d;
}
/**
* Formattage des données pour le CSV
* @param string $filename
* @param string $addEntete
*/
public function export($filename, $addEntete = true)
{
$enteteCode = array_keys($this->entete);
$enteteLabel = array_values($this->entete);
$logs = array();
foreach ($this->data as $data){
$row = array();
foreach ($enteteCode as $entete) {
// La colonne existe dans les données
if (array_key_exists($entete, $data)) {
$row[] = $data[$entete];
}
// La colonne existe dans les propriétés
else if (property_exists($this, $entete)){
$code = str_replace('Lib', '', $entete);
$row[] = $this->{$entete}[$data[$code]];
} else {
$row[] = '';
}
}
$logs[] = $row;
}
if ($addEntete && count($this->entete) > 0) {
$export = array_merge(array(0 => $enteteLabel), $logs);
} else {
$export = $logs;
}
$this->exportCSV($export, $filename);
}
/**
* Création du fichier CSV
* @param array $list
* @param string $filename
* @param string $delimiter
*/
protected function exportCSV($list, $filename, $delimiter=',')
{
$fp = fopen($filename, 'w');
foreach($list as $fields){
fputcsv($fp, $fields, $delimiter);
}
fclose($fp);
}
/**
* Récupération des libellés de requete
*/
protected function setPageLib()
{
$libM = new Application_Model_Sdv1LogsItem();
$libSql = $libM->select()->from($libM, array('Code', 'Label'));
$libResult = $libM->fetchAll($libSql);
if (count($libResult) > 0) {
foreach ($libResult as $l) {
$this->pageLib[$l->Code] = $l->Label;
}
}
}
}