1530 |
jpm |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Service exportant des informations concernant COEL au format CSV.
|
|
|
5 |
* Encodage en entrée : utf8
|
|
|
6 |
* Encodage en sortie : utf8
|
1586 |
jpm |
7 |
*
|
1530 |
jpm |
8 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
9 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
10 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
11 |
* @version $Id$
|
|
|
12 |
* @copyright 2010
|
|
|
13 |
*/
|
|
|
14 |
class CoelExport extends Coel {
|
1586 |
jpm |
15 |
|
1530 |
jpm |
16 |
const META_LISTE_STOCK_PARAMS = 1020;// Liste des paramètres de contrôle environnemental d'un local
|
1537 |
jpm |
17 |
const META_LISTES_EXPERTISE_COLLECTION = '1040,1041,1043,1045,1046,1049,1050,1053,1057,1081';// Liste des métadonnées pour les informations des des collections
|
1586 |
jpm |
18 |
|
1530 |
jpm |
19 |
private $format = null;
|
|
|
20 |
private $service = null;
|
|
|
21 |
private $squelette = null;
|
|
|
22 |
private $squelette_dossier = null;
|
1586 |
jpm |
23 |
|
1530 |
jpm |
24 |
/**
|
|
|
25 |
* Méthode appelée avec une requête de type GET.
|
|
|
26 |
*/
|
|
|
27 |
public function getElement($params = array()) {
|
|
|
28 |
// Initialisation des variables
|
|
|
29 |
$info = array();
|
|
|
30 |
$contenu = '';
|
1586 |
jpm |
31 |
|
1530 |
jpm |
32 |
// Pré traitement des paramêtres
|
|
|
33 |
$pour_bdd = false;
|
1765 |
aurelien |
34 |
$this->parametres = $this->traiterParametresUrl(array('service', 'format'), $params, $pour_bdd);
|
1586 |
jpm |
35 |
|
1530 |
jpm |
36 |
// Chargement du bon type de service demandé
|
|
|
37 |
if (isset($this->parametres['service'])) {
|
|
|
38 |
$this->service = strtolower($this->parametres['service']);
|
1586 |
jpm |
39 |
|
1530 |
jpm |
40 |
$methode = $this->getNomMethodeService();
|
|
|
41 |
if (method_exists($this, $methode)) {
|
|
|
42 |
if (isset($this->parametres['format']) && preg_match('/^(?:csv|txt)$/i', $this->parametres['format'])) {
|
|
|
43 |
// Mise en minuscule de l'indication du format
|
|
|
44 |
$this->format = strtolower($this->parametres['format']);
|
|
|
45 |
// Définition du fichier squelette demandé
|
|
|
46 |
$this->definirSquelette();
|
|
|
47 |
} else {
|
|
|
48 |
$this->format = '';
|
|
|
49 |
$this->messages[] = "Le service COEL Export nécessite d'indiquer en second paramètre le format : csv, txt.";
|
|
|
50 |
}
|
|
|
51 |
// Récupération du contenu à renvoyer
|
|
|
52 |
$contenu = $this->$methode();
|
|
|
53 |
} else {
|
|
|
54 |
$this->messages[] = "Le type d'information demandé '$this->service' n'est pas disponible.";
|
|
|
55 |
}
|
|
|
56 |
} else {
|
|
|
57 |
$this->messages[] = "Le service COEL Syndication nécessite d'indiquer en premier paramètre le type d'information demandé.";
|
|
|
58 |
}
|
1586 |
jpm |
59 |
|
1530 |
jpm |
60 |
// Envoie sur la sortie standard
|
|
|
61 |
$encodage = 'utf-8';
|
|
|
62 |
$mime = $this->getTypeMime();
|
|
|
63 |
$formatage_json = $this->getFormatageJson();
|
|
|
64 |
$this->envoyer($contenu, $mime, $encodage, $formatage_json);
|
|
|
65 |
}
|
1586 |
jpm |
66 |
|
1530 |
jpm |
67 |
private function definirSquelette() {
|
|
|
68 |
$this->squelette_dossier = dirname(__FILE__).DIRECTORY_SEPARATOR.'squelettes'.DIRECTORY_SEPARATOR;
|
|
|
69 |
$ext = $this->getExtensionFichierSquelette();
|
|
|
70 |
$this->squelette = $this->squelette_dossier.'export_'.$this->service.'.tpl.'.$ext;
|
|
|
71 |
}
|
1586 |
jpm |
72 |
|
1530 |
jpm |
73 |
private function getExtensionFichierSquelette() {
|
|
|
74 |
$ext = '';
|
|
|
75 |
switch ($this->format) {
|
|
|
76 |
case 'csv' :
|
|
|
77 |
case 'txt' :
|
|
|
78 |
$ext = 'csv';
|
|
|
79 |
break;
|
|
|
80 |
default:
|
|
|
81 |
$ext = 'csv';
|
|
|
82 |
}
|
|
|
83 |
return $ext;
|
|
|
84 |
}
|
1586 |
jpm |
85 |
|
1530 |
jpm |
86 |
private function getNomMethodeService() {
|
|
|
87 |
$methode = '';
|
|
|
88 |
$service_formate = str_replace(' ', '', ucwords(implode(' ', explode('_', $this->service))));
|
|
|
89 |
$methode = 'getService'.$service_formate;
|
|
|
90 |
return $methode;
|
|
|
91 |
}
|
1586 |
jpm |
92 |
|
1530 |
jpm |
93 |
private function getTypeMime() {
|
|
|
94 |
$mime = '';
|
|
|
95 |
switch ($this->format) {
|
|
|
96 |
case 'csv' :
|
|
|
97 |
$mime = 'text/csv';
|
|
|
98 |
break;
|
|
|
99 |
case 'txt' :
|
|
|
100 |
$mime = 'text/plain';
|
|
|
101 |
break;
|
|
|
102 |
default:
|
|
|
103 |
$mime = 'text/html';
|
|
|
104 |
}
|
|
|
105 |
return $mime;
|
|
|
106 |
}
|
1586 |
jpm |
107 |
|
1530 |
jpm |
108 |
private function getFormatageJson() {
|
|
|
109 |
$json = false;
|
|
|
110 |
switch ($this->service) {
|
|
|
111 |
default:
|
|
|
112 |
$json = false;
|
|
|
113 |
}
|
|
|
114 |
return $json;
|
|
|
115 |
}
|
1586 |
jpm |
116 |
|
|
|
117 |
|
1530 |
jpm |
118 |
private function getServiceStructureExpertise() {
|
|
|
119 |
$donnees = array();
|
1586 |
jpm |
120 |
|
1530 |
jpm |
121 |
// Construction de la requête
|
1537 |
jpm |
122 |
$requete = 'SELECT '.(($this->distinct) ? 'DISTINCT' : '').' '.
|
1836 |
mathias |
123 |
' cs_id_structure, cs_nom, cs_description, cs_adresse_01, cs_code_postal, cs_ville, cs_courriel, '.
|
|
|
124 |
' cs_truk_telephone, cs_truk_url,'.
|
1530 |
jpm |
125 |
' SUM(csap_bota_travail_hebdo_tps) AS personnel_heure_nbre, '.
|
1836 |
mathias |
126 |
' csc_truk_stockage_parametre, cmlv_nom, csv_mark_visite_avec_motif '.
|
1586 |
jpm |
127 |
'FROM coel_structure '.
|
1530 |
jpm |
128 |
' LEFT JOIN coel_structure_a_personne ON (cs_id_structure = csap_id_structure) '.
|
|
|
129 |
' LEFT JOIN coel_structure_conservation ON (cs_id_structure = csc_id_structure) '.
|
1572 |
jpm |
130 |
' LEFT JOIN coel_structure_valorisation ON (cs_id_structure = csv_id_structure) '.
|
1836 |
mathias |
131 |
' LEFT JOIN coel_meta_liste_valeur on `cmlv_id_valeur` =`cs_ce_truk_type_public`'.
|
1530 |
jpm |
132 |
'GROUP BY cs_id_structure '.
|
1543 |
jpm |
133 |
'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby : 'cs_nom ASC').' ';
|
1836 |
mathias |
134 |
$message_echec = "La requête n'a retourné aucun résultat.";
|
1530 |
jpm |
135 |
$structures = $this->executerRequete($requete, $message_echec);
|
1586 |
jpm |
136 |
|
1530 |
jpm |
137 |
// Construction de la requête
|
1537 |
jpm |
138 |
$requete = 'SELECT '.(($this->distinct) ? 'DISTINCT' : '').' '.
|
1530 |
jpm |
139 |
' cs_id_structure, '.
|
|
|
140 |
' COUNT(cc_id_collection) AS collection_nbre '.
|
|
|
141 |
'FROM coel_structure '.
|
|
|
142 |
' LEFT JOIN coel_collection ON (cs_id_structure = cc_ce_structure) '.
|
|
|
143 |
'GROUP BY cs_id_structure ';
|
1836 |
mathias |
144 |
$message_echec = "La requête comptant le nombre de collections n'a retourné aucun résultat.";
|
1530 |
jpm |
145 |
$collections_nbre = $this->executerRequete($requete, $message_echec);
|
1586 |
jpm |
146 |
|
1530 |
jpm |
147 |
// Récupération des métadonnées nécessaires
|
|
|
148 |
$requete = 'SELECT cmlv_id_valeur, cmlv_nom, cmlv_abreviation '.
|
|
|
149 |
'FROM coel_meta_liste_valeur '.
|
|
|
150 |
'WHERE cmlv_ce_parent = '.self::META_LISTE_STOCK_PARAMS.' ';
|
|
|
151 |
$message_echec = "La requête de recherche des métadonnées a retourné aucun résultat.";
|
|
|
152 |
$metadonnees = $this->executerRequete($requete, $message_echec);
|
1586 |
jpm |
153 |
|
1530 |
jpm |
154 |
// Traitement des données
|
|
|
155 |
if ($structures !== false) {
|
|
|
156 |
// Traitement du nombre de collection par structure
|
|
|
157 |
$collections_par_structure = array();
|
|
|
158 |
foreach ($collections_nbre as $collection) {
|
|
|
159 |
$nbre = (empty($collection['collection_nbre'])) ? 0 : $collection['collection_nbre'];
|
|
|
160 |
$collections_par_structure[$collection['cs_id_structure']] = $nbre;
|
|
|
161 |
}
|
1541 |
jpm |
162 |
|
|
|
163 |
// Instanciation des objets nécessaires aux traitements
|
|
|
164 |
$ontologie = new Ontologie($metadonnees);
|
|
|
165 |
$utilTruck = new UtilTruck($ontologie);
|
1586 |
jpm |
166 |
|
|
|
167 |
// Traitement final des données concernant les structures
|
1530 |
jpm |
168 |
foreach ($structures as $structure) {
|
|
|
169 |
$id_structure = $structure['cs_id_structure'];
|
1541 |
jpm |
170 |
$nom = $structure['cs_nom'];
|
1836 |
mathias |
171 |
$description = $structure['cs_description'];
|
|
|
172 |
$adresse_01 = $structure['cs_adresse_01'];
|
|
|
173 |
$code_postal = $structure['cs_code_postal'];
|
|
|
174 |
$ville = $structure['cs_ville'];
|
|
|
175 |
$telephone = $utilTruck->construireTxtListeOntologie($structure['cs_truk_telephone']);
|
|
|
176 |
$courriel = $structure['cs_courriel'];
|
|
|
177 |
$type = $structure['cmlv_nom'];
|
|
|
178 |
$url = $utilTruck->construireTxtListeOntologie($structure['cs_truk_url']);
|
1541 |
jpm |
179 |
$stockage_params = $utilTruck->construireTxtListeOntologie($structure['csc_truk_stockage_parametre']);
|
|
|
180 |
$stockage_params_nbre = $utilTruck->getNbreValeur($structure['csc_truk_stockage_parametre']);
|
|
|
181 |
$stockage_params_nbre = empty($stockage_params_nbre) ? 0 : $stockage_params_nbre;
|
1530 |
jpm |
182 |
$collection_nbre = $collections_par_structure[$structure['cs_id_structure']];
|
|
|
183 |
$collection_nbre = empty($collection_nbre) ? 0 : $collection_nbre;
|
1838 |
mathias |
184 |
// désactivé à la demande de Louise - 01/09/2014
|
|
|
185 |
//$personnel_heure_nbre = empty($structure['personnel_heure_nbre']) ? 0 : $structure['personnel_heure_nbre'];
|
|
|
186 |
//$visite_avec_motif_science = $structure['csv_mark_visite_avec_motif'];
|
1586 |
jpm |
187 |
|
1541 |
jpm |
188 |
$structure_affichage = array(
|
1586 |
jpm |
189 |
'nom' => $nom,
|
1836 |
mathias |
190 |
'description' => $description,
|
|
|
191 |
'adresse' => $adresse_01,
|
|
|
192 |
'code_postal' => $code_postal,
|
|
|
193 |
'ville' => $ville,
|
|
|
194 |
'telephone' => $telephone,
|
|
|
195 |
'courriel' => $courriel,
|
|
|
196 |
'url' => $url,
|
|
|
197 |
'type' => $type,
|
1530 |
jpm |
198 |
'stockage_params' => $stockage_params,
|
1531 |
jpm |
199 |
'stockage_params_nbre' => $stockage_params_nbre,
|
1530 |
jpm |
200 |
'collection_nbre' => $collection_nbre,
|
1838 |
mathias |
201 |
//'personnel_heure_nbre' => $personnel_heure_nbre,
|
|
|
202 |
//'visite_avec_motif_science' => $visite_avec_motif_science
|
|
|
203 |
);
|
1541 |
jpm |
204 |
$structure_affichage = $this->nettoyerTableau($structure_affichage);
|
1586 |
jpm |
205 |
|
1541 |
jpm |
206 |
$donnees['structures'][$id_structure] = $structure_affichage;
|
1530 |
jpm |
207 |
}
|
|
|
208 |
}
|
1586 |
jpm |
209 |
|
1530 |
jpm |
210 |
// Création du contenu
|
|
|
211 |
$contenu = $this->executerService($donnees);
|
|
|
212 |
return $contenu;
|
|
|
213 |
}
|
1586 |
jpm |
214 |
|
1531 |
jpm |
215 |
private function getServiceCollectionExpertise() {
|
1537 |
jpm |
216 |
|
1531 |
jpm |
217 |
// Construction de la requête
|
1537 |
jpm |
218 |
$requete = 'SELECT '.(($this->distinct) ? 'DISTINCT' : '').' '.
|
1531 |
jpm |
219 |
' cc_id_collection, '.
|
1830 |
mathias |
220 |
' cc_ce_structure, '.
|
1586 |
jpm |
221 |
' cc_nom, cc_cote, cs_nom, cc_ce_specimen_type, cc_description, cc_truk_periode_constitution, '.
|
1531 |
jpm |
222 |
' cc_truk_couverture_lieu, ccb_truk_etiquette_renseignement, ccb_truk_nature, '.
|
1583 |
jpm |
223 |
' ccb_truk_unite_base, ccb_ce_etat_general, ccb_specimen_fixation_pourcent, '.
|
|
|
224 |
' ccb_truk_specimen_fixation_methode, ccb_truk_etiquette_fixation_support, '.
|
1531 |
jpm |
225 |
' ccb_truk_etiquette_fixation_specimen, ccb_truk_degradation_presentation, '.
|
|
|
226 |
' ccb_ce_classement_etat, ccb_ce_inventaire '.
|
1586 |
jpm |
227 |
'FROM coel_collection '.
|
1531 |
jpm |
228 |
' LEFT JOIN coel_collection_botanique ON (cc_id_collection = ccb_id_collection) '.
|
|
|
229 |
' LEFT JOIN coel_structure ON (cs_id_structure = cc_ce_structure) '.
|
1543 |
jpm |
230 |
'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby : 'cc_nom ASC').' ';
|
1531 |
jpm |
231 |
$message_echec = "La requête a retourné aucun résultat.";
|
1537 |
jpm |
232 |
$collections = $this->executerRequete($requete, $message_echec);
|
1586 |
jpm |
233 |
|
1531 |
jpm |
234 |
// Récupération des métadonnées nécessaires
|
|
|
235 |
$requete = 'SELECT cmlv_id_valeur, cmlv_ce_parent, cmlv_nom, cmlv_abreviation '.
|
|
|
236 |
'FROM coel_meta_liste_valeur '.
|
|
|
237 |
'WHERE cmlv_ce_parent IN ('.self::META_LISTES_EXPERTISE_COLLECTION.') ';
|
|
|
238 |
$message_echec = "La requête de recherche des métadonnées a retourné aucun résultat.";
|
|
|
239 |
$metadonnees = $this->executerRequete($requete, $message_echec);
|
1586 |
jpm |
240 |
|
1531 |
jpm |
241 |
// Traitement des données
|
1537 |
jpm |
242 |
$donnees = array();
|
|
|
243 |
if ($collections !== false) {
|
|
|
244 |
// Instanciation des objets nécessaires aux traitements
|
|
|
245 |
$ontologie = new Ontologie($metadonnees);
|
|
|
246 |
$utilTruck = new UtilTruck($ontologie);
|
1586 |
jpm |
247 |
|
|
|
248 |
// Traitement final des données concernant les structures
|
1537 |
jpm |
249 |
foreach ($collections as $collection) {
|
|
|
250 |
$id_collection = $collection['cc_id_collection'];
|
1541 |
jpm |
251 |
$nom = $collection['cc_nom'];
|
1586 |
jpm |
252 |
$cote = $collection['cc_cote'];
|
1830 |
mathias |
253 |
$id_structure = $collection['cc_ce_structure'];
|
1541 |
jpm |
254 |
$nom_structure = $collection['cs_nom'];
|
1537 |
jpm |
255 |
$specimen_type = $utilTruck->construireTxtListeOntologie($collection['cc_ce_specimen_type']);
|
1541 |
jpm |
256 |
$description = $collection['cc_description'];
|
1537 |
jpm |
257 |
$periode_constitution = $utilTruck->construireTxtListeOntologie($collection['cc_truk_periode_constitution']);
|
|
|
258 |
$couverture_lieu = $utilTruck->construireTxtListeOntologie($collection['cc_truk_couverture_lieu']);
|
1583 |
jpm |
259 |
$specimen_fixation_pourcent = $collection['ccb_specimen_fixation_pourcent'];
|
|
|
260 |
$specimen_fixation_methode = $utilTruck->construireTxtListeOntologie($collection['ccb_truk_specimen_fixation_methode']);
|
1537 |
jpm |
261 |
$etiquette_renseignement = $utilTruck->construireTxtListeOntologie($collection['ccb_truk_etiquette_renseignement']);
|
|
|
262 |
$nature = $utilTruck->construireTxtListeOntologie($collection['ccb_truk_nature']);
|
1540 |
jpm |
263 |
$parts_total = UtilTruck::extraireNbrePart($collection['ccb_truk_unite_base']);
|
1541 |
jpm |
264 |
$etat_general = $utilTruck->construireTxtListeOntologie($collection['ccb_ce_etat_general']);
|
|
|
265 |
$etiquette_fixation_support = $utilTruck->construireTxtListeOntologie($collection['ccb_truk_etiquette_fixation_support']);
|
|
|
266 |
$etiquette_fixation_specimen = $utilTruck->construireTxtListeOntologie($collection['ccb_truk_etiquette_fixation_specimen']);
|
|
|
267 |
$degradation_presentation = $utilTruck->construireTxtListeOntologie($collection['ccb_truk_degradation_presentation'], false, true, true);
|
|
|
268 |
$classement_etat = $utilTruck->construireTxtListeOntologie($collection['ccb_ce_classement_etat']);
|
|
|
269 |
$inventaire = $utilTruck->construireTxtListeOntologie($collection['ccb_ce_inventaire']);
|
1586 |
jpm |
270 |
|
1541 |
jpm |
271 |
$collection_affichage = array(
|
|
|
272 |
'id' => $id_collection,
|
1586 |
jpm |
273 |
'cote' => $cote,
|
1537 |
jpm |
274 |
'nom' => $nom,
|
1586 |
jpm |
275 |
'nom_structure' => $nom_structure,
|
1830 |
mathias |
276 |
'id_structure' => $id_structure,
|
1537 |
jpm |
277 |
'specimen_type' => $specimen_type,
|
|
|
278 |
'description' => $description,
|
|
|
279 |
'periode_constitution' => $periode_constitution,
|
|
|
280 |
'couverture_lieu' => $couverture_lieu,
|
|
|
281 |
'etiquette_renseignement' => $etiquette_renseignement,
|
|
|
282 |
'nature' => $nature,
|
1541 |
jpm |
283 |
'parts_total' => $parts_total,
|
|
|
284 |
'etat_general' => $etat_general,
|
|
|
285 |
'etiquette_fixation_support' => $etiquette_fixation_support,
|
|
|
286 |
'etiquette_fixation_specimen' => $etiquette_fixation_specimen,
|
1583 |
jpm |
287 |
'specimen_fixation_pourcent' => $specimen_fixation_pourcent,
|
|
|
288 |
'specimen_fixation_methode' => $specimen_fixation_methode,
|
1541 |
jpm |
289 |
'degradation_presentation' => $degradation_presentation,
|
|
|
290 |
'classement_etat' => $classement_etat,
|
|
|
291 |
'inventaire' => $inventaire);
|
|
|
292 |
$collection_affichage = $this->nettoyerTableau($collection_affichage);
|
1586 |
jpm |
293 |
|
1541 |
jpm |
294 |
$donnees['collections'][] = $collection_affichage;
|
1531 |
jpm |
295 |
}
|
1541 |
jpm |
296 |
$donnees['champs'] = array_keys(current($donnees['collections']));
|
1531 |
jpm |
297 |
}
|
1586 |
jpm |
298 |
|
1531 |
jpm |
299 |
// Création du contenu
|
|
|
300 |
$contenu = $this->executerService($donnees);
|
|
|
301 |
return $contenu;
|
|
|
302 |
}
|
1586 |
jpm |
303 |
|
1530 |
jpm |
304 |
private function executerRequete($requete, $message_echec) {
|
|
|
305 |
try {
|
|
|
306 |
$infos = $this->bdd->query($requete)->fetchAll(PDO::FETCH_ASSOC);
|
|
|
307 |
if ($infos === false) {
|
|
|
308 |
$this->messages[] = $message_echec;
|
|
|
309 |
}
|
|
|
310 |
} catch (PDOException $e) {
|
1537 |
jpm |
311 |
$this->messages[] = sprintf($this->getTxt('sql_erreur_requete'), $e->getFile(), $e->getLine(), $e->getMessage(), $requete);
|
1530 |
jpm |
312 |
}
|
|
|
313 |
return $infos;
|
|
|
314 |
}
|
1586 |
jpm |
315 |
|
1530 |
jpm |
316 |
private function executerService($donnees) {
|
|
|
317 |
// Création du contenu à partir d'un template PHP
|
|
|
318 |
$contenu = Coel::traiterSquelettePhp($this->squelette, $donnees);
|
|
|
319 |
return $contenu;
|
|
|
320 |
}
|
1586 |
jpm |
321 |
|
1541 |
jpm |
322 |
private function nettoyerTableau(Array $tableau) {
|
|
|
323 |
foreach ($tableau as $cle => $valeur) {
|
|
|
324 |
$tableau[$cle] = self::nettoyerGuillemets($valeur);
|
|
|
325 |
}
|
|
|
326 |
return $tableau;
|
|
|
327 |
}
|
1586 |
jpm |
328 |
|
1537 |
jpm |
329 |
private static function nettoyerGuillemets($txt) {
|
|
|
330 |
$txt = preg_replace('/"/', '""', $txt);
|
|
|
331 |
return $txt;
|
|
|
332 |
}
|
1586 |
jpm |
333 |
}
|