1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| <?php
$http = eZHTTPTool::instance();
## Récupération des paramètres
$enfants = $http->getVariable('enfants', null);
## Système anti plantage.
gc_enable(); // Enable Garbage Collector
ini_set('max_execution_time', 1200); # On se donne plus de temps !
ini_set('memory_limit', '2048M'); # et de mémoire !
OnTheRazorsEdge::sharp();
$LIMIT = 100; # On va fetch petit lot par petit lot. Ce serait bète de planter pandant le fetch !
$OFFSET = 0; # A la fin de chaque do on incrément $OFFSET de $LIMIT
$filename = "export-recherche.".(ENV != ENV_PREPROD ? ENV.'.' : '').date('Y-m-d_H-i-s').'.csv';
$file_path = eZSys::varDirectory().'/storage/export-pro/'.$filename;
CSV::sendCsvHeaders($filename); # Envoie des entête csv
# Ouverture du flux de sortie.
# Pour économiser de ressources on ne stoque pas le csv sans un string. On l'envoie directement sur la sortie std.
$fp = fopen('php://output', 'w'); # On ouvre directement le flux sur la sortie standard.
if ($fp === false) {
throw new Exception("Impossible d'ouvrir le fichier '$file_path'.");
}
$heads = array('', 'nom', 'prenom', 'naissance',
'responsable_nom', 'responsable_prenom', 'adresse_responsable', 'complement_adresse_responsable',
'code_postal_responsable', 'commune_responsable', 'telephone_responsable', 'debut', 'fin'=>'fin',
);
fputcsv($fp, $heads, ';');
$matrice = CSV::matrice($heads);
do {
$search_result = eZProFunctionCollection::fetchEnfants_a($enfants, $OFFSET, $LIMIT);
foreach ($search_result["result"]['list'] as $i => $node) {
$dm = $node->object()->dataMap();
$node->object()->resetDataMap();
$fields = $matrice;
$fields['nom'] = $dm['nom']->toString();
$fields['prenom'] = $dm['prenom']->toString();
$fields['debut'] = $dm['date_debut_accueil']->toString() ? date('d/m/Y',$dm['date_debut_accueil']->toString()) : '';
$fields['responsable_prenom'] = $dm['prenom_responsable']->toString();
$fields['responsable_nom'] = $dm['nom_responsable']->toString();
$fields['adresse_responsable'] = $dm['adresse_responsable']->toString();
$fields['code_postal_responsable'] = $dm['code_postal_responsable']->toString();
$fields['commune_responsable'] = $dm['commune_responsable']->toString();
$fields['telephone_responsable'] = $dm['telephone_responsable']->toString();
$communeAttContent = $dm['commune']->content();
foreach ($communeAttContent["relation_list"] as $relation) {
$d = ExportMemoryCache::commune_data($relation['contentobject_id']);
$fields['commune'] = $d['commune'];
break;
}
unset($communeAttContent);
fputcsv($fp, $fields, ';');
unset($node);unset($dm); # And more !
if (!OnTheRazorsEdge::run()) {
fputcsv($fp, array("Et plus"), ';');
break (2); # On break le foreach et le do while
}
}
eZContentObject::clearCache(); # Clear in-memory caches.
gc_collect_cycles();
$OFFSET += $LIMIT;
} while ($search_result["result"]['list']);
fclose($fp);
return eZExecution::cleanExit();
class ExportMemoryCache
{
private static $commune_data = array();
public static function commune_data($commune_contentobject_id)
{
if ( ! isset(self::$commune_data[$commune_contentobject_id])) {
$fields = array('commune' => '', 'canton' => '', 'circonscription' => '');
$communeObject = eZContentObject::fetch($commune_contentobject_id);
if ($communeObject->ClassIdentifier == 'commune') {
$cdm = $communeObject->dataMap();
$communeObject->resetDataMap();
$fields['commune'] = $cdm['commune']->toString();
$fields['canton'] = $cdm['canton']->toString();
$fields['circonscription'] = $cdm['circonscription']->toString();
unset($cdm);
}
unset($communeObject);
self::$commune_data[$commune_contentobject_id] = $fields;
}
return self::$commune_data[$commune_contentobject_id];
}
} |
Partager