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
|
|
|
7 |
*
|
|
|
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 {
|
|
|
15 |
|
|
|
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
|
|
|
18 |
|
1530 |
jpm |
19 |
private $format = null;
|
|
|
20 |
private $service = null;
|
|
|
21 |
private $squelette = null;
|
|
|
22 |
private $squelette_dossier = null;
|
|
|
23 |
|
|
|
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 = '';
|
|
|
31 |
|
|
|
32 |
// Pré traitement des paramêtres
|
|
|
33 |
$pour_bdd = false;
|
|
|
34 |
$this->parametres = $this->traiterParametresUrl(array('service', 'format', 'projet'), $params, $pour_bdd);
|
|
|
35 |
|
|
|
36 |
// Chargement du bon type de service demandé
|
|
|
37 |
if (isset($this->parametres['service'])) {
|
|
|
38 |
$this->service = strtolower($this->parametres['service']);
|
|
|
39 |
|
|
|
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 |
}
|
|
|
59 |
|
|
|
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 |
}
|
|
|
66 |
|
|
|
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 |
}
|
|
|
72 |
|
|
|
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 |
}
|
|
|
85 |
|
|
|
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 |
}
|
|
|
92 |
|
|
|
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 |
}
|
|
|
107 |
|
|
|
108 |
private function getFormatageJson() {
|
|
|
109 |
$json = false;
|
|
|
110 |
switch ($this->service) {
|
|
|
111 |
default:
|
|
|
112 |
$json = false;
|
|
|
113 |
}
|
|
|
114 |
return $json;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
private function getServiceStructureExpertise() {
|
|
|
119 |
$donnees = array();
|
|
|
120 |
$projet_id = $this->parametres['projet'];
|
|
|
121 |
|
|
|
122 |
// Construction de la requête
|
1537 |
jpm |
123 |
$requete = 'SELECT '.(($this->distinct) ? 'DISTINCT' : '').' '.
|
1530 |
jpm |
124 |
' cs_id_structure, '.
|
|
|
125 |
' SUM(csap_bota_travail_hebdo_tps) AS personnel_heure_nbre, '.
|
|
|
126 |
' cs_nom, csc_truk_stockage_parametre '.
|
|
|
127 |
'FROM coel_structure '.
|
|
|
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) '.
|
|
|
130 |
( is_null($projet_id) ? '' : "WHERE cs_ce_projet = $projet_id ").
|
|
|
131 |
'GROUP BY cs_id_structure '.
|
|
|
132 |
'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby : 'cs_nom ASC').' '.
|
|
|
133 |
((!is_null($this->start) && !is_null($this->limit)) ? "LIMIT $this->start,$this->limit " : '');
|
|
|
134 |
$message_echec = "La requête a retourné aucun résultat.";
|
|
|
135 |
$structures = $this->executerRequete($requete, $message_echec);
|
|
|
136 |
|
|
|
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 |
( is_null($projet_id) ? '' : "WHERE cs_ce_projet = $projet_id ").
|
|
|
144 |
'GROUP BY cs_id_structure ';
|
|
|
145 |
$message_echec = "La requête comptant le nombre de collection a retourné aucun résultat.";
|
|
|
146 |
$collections_nbre = $this->executerRequete($requete, $message_echec);
|
|
|
147 |
|
|
|
148 |
// Récupération des métadonnées nécessaires
|
|
|
149 |
$requete = 'SELECT cmlv_id_valeur, cmlv_nom, cmlv_abreviation '.
|
|
|
150 |
'FROM coel_meta_liste_valeur '.
|
|
|
151 |
'WHERE cmlv_ce_parent = '.self::META_LISTE_STOCK_PARAMS.' ';
|
|
|
152 |
$message_echec = "La requête de recherche des métadonnées a retourné aucun résultat.";
|
|
|
153 |
$metadonnees = $this->executerRequete($requete, $message_echec);
|
|
|
154 |
|
|
|
155 |
// Traitement des données
|
|
|
156 |
if ($structures !== false) {
|
|
|
157 |
// Traitement des métadonnées
|
|
|
158 |
$parametres = array(0 => array('nom' => 'Non renseigné', 'abr' => 'NR'));
|
|
|
159 |
foreach ($metadonnees as $meta) {
|
|
|
160 |
$parametres[$meta['cmlv_id_valeur']] = array('nom' => $meta['cmlv_nom'], 'abr' => $meta['cmlv_abreviation']);
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
// Traitement du nombre de collection par structure
|
|
|
164 |
$collections_par_structure = array();
|
|
|
165 |
foreach ($collections_nbre as $collection) {
|
|
|
166 |
$nbre = (empty($collection['collection_nbre'])) ? 0 : $collection['collection_nbre'];
|
|
|
167 |
$collections_par_structure[$collection['cs_id_structure']] = $nbre;
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
// Traitement final des données concernant les structures
|
|
|
171 |
foreach ($structures as $structure) {
|
|
|
172 |
$id_structure = $structure['cs_id_structure'];
|
|
|
173 |
$nom = addcslashes($structure['cs_nom'], '"');
|
|
|
174 |
$stockage_params = implode(' ; ', $export_stockage_parametres);
|
1531 |
jpm |
175 |
$stockage_params_nbre = count($export_stockage_parametres);
|
1530 |
jpm |
176 |
$collection_nbre = $collections_par_structure[$structure['cs_id_structure']];
|
|
|
177 |
$collection_nbre = empty($collection_nbre) ? 0 : $collection_nbre;
|
|
|
178 |
$personnel_heure_nbre = empty($structure['personnel_heure_nbre']) ? 0 : $structure['personnel_heure_nbre'];
|
|
|
179 |
|
|
|
180 |
$donnees['structures'][$id_structure] = array(
|
|
|
181 |
'nom' => $nom,
|
|
|
182 |
'stockage_params' => $stockage_params,
|
1531 |
jpm |
183 |
'stockage_params_nbre' => $stockage_params_nbre,
|
1530 |
jpm |
184 |
'collection_nbre' => $collection_nbre,
|
|
|
185 |
'personnel_heure_nbre' => $personnel_heure_nbre);
|
|
|
186 |
}
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
// Création du contenu
|
|
|
190 |
$contenu = $this->executerService($donnees);
|
|
|
191 |
return $contenu;
|
|
|
192 |
}
|
|
|
193 |
|
1531 |
jpm |
194 |
private function getServiceCollectionExpertise() {
|
|
|
195 |
$projet_id = $this->parametres['projet'];
|
1537 |
jpm |
196 |
|
1531 |
jpm |
197 |
// Construction de la requête
|
1537 |
jpm |
198 |
$requete = 'SELECT '.(($this->distinct) ? 'DISTINCT' : '').' '.
|
1531 |
jpm |
199 |
' cc_id_collection, '.
|
|
|
200 |
' cc_nom, cs_nom, cc_ce_specimen_type, cc_description, cc_truk_periode_constitution, '.
|
|
|
201 |
' cc_truk_couverture_lieu, ccb_truk_etiquette_renseignement, ccb_truk_nature, '.
|
|
|
202 |
' ccb_truk_unite_base, ccb_ce_etat_general, ccb_truk_etiquette_fixation_support, '.
|
|
|
203 |
' ccb_truk_etiquette_fixation_specimen, ccb_truk_degradation_presentation, '.
|
|
|
204 |
' ccb_ce_classement_etat, ccb_ce_inventaire '.
|
|
|
205 |
'FROM coel_collection '.
|
|
|
206 |
' LEFT JOIN coel_collection_botanique ON (cc_id_collection = ccb_id_collection) '.
|
|
|
207 |
' LEFT JOIN coel_structure ON (cs_id_structure = cc_ce_structure) '.
|
|
|
208 |
( is_null($projet_id) ? '' : "WHERE cc_ce_projet = $projet_id ").
|
|
|
209 |
'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby : 'cc_nom ASC').' '.
|
|
|
210 |
((!is_null($this->start) && !is_null($this->limit)) ? "LIMIT $this->start,$this->limit " : '');
|
|
|
211 |
$message_echec = "La requête a retourné aucun résultat.";
|
1537 |
jpm |
212 |
$collections = $this->executerRequete($requete, $message_echec);
|
1531 |
jpm |
213 |
|
|
|
214 |
// Récupération des métadonnées nécessaires
|
|
|
215 |
$requete = 'SELECT cmlv_id_valeur, cmlv_ce_parent, cmlv_nom, cmlv_abreviation '.
|
|
|
216 |
'FROM coel_meta_liste_valeur '.
|
|
|
217 |
'WHERE cmlv_ce_parent IN ('.self::META_LISTES_EXPERTISE_COLLECTION.') ';
|
|
|
218 |
$message_echec = "La requête de recherche des métadonnées a retourné aucun résultat.";
|
|
|
219 |
$metadonnees = $this->executerRequete($requete, $message_echec);
|
|
|
220 |
|
|
|
221 |
// Traitement des données
|
1537 |
jpm |
222 |
$donnees = array();
|
|
|
223 |
if ($collections !== false) {
|
|
|
224 |
// Instanciation des objets nécessaires aux traitements
|
|
|
225 |
$ontologie = new Ontologie($metadonnees);
|
|
|
226 |
$utilTruck = new UtilTruck($ontologie);
|
1531 |
jpm |
227 |
|
|
|
228 |
// Traitement final des données concernant les structures
|
1537 |
jpm |
229 |
foreach ($collections as $collection) {
|
|
|
230 |
$id_collection = $collection['cc_id_collection'];
|
|
|
231 |
$nom = self::nettoyerGuillemets($collection['cc_nom']);
|
|
|
232 |
$nom_structure = self::nettoyerGuillemets($collection['cs_nom']);
|
|
|
233 |
$specimen_type = $utilTruck->construireTxtListeOntologie($collection['cc_ce_specimen_type']);
|
|
|
234 |
$description = self::nettoyerGuillemets($collection['cc_description']);
|
|
|
235 |
$periode_constitution = $utilTruck->construireTxtListeOntologie($collection['cc_truk_periode_constitution']);
|
|
|
236 |
$couverture_lieu = $utilTruck->construireTxtListeOntologie($collection['cc_truk_couverture_lieu']);
|
|
|
237 |
$etiquette_renseignement = $utilTruck->construireTxtListeOntologie($collection['ccb_truk_etiquette_renseignement']);
|
|
|
238 |
$nature = $utilTruck->construireTxtListeOntologie($collection['ccb_truk_nature']);
|
1540 |
jpm |
239 |
$parts_total = UtilTruck::extraireNbrePart($collection['ccb_truk_unite_base']);
|
1531 |
jpm |
240 |
|
1537 |
jpm |
241 |
$donnees['collections'][$id_collection] = array(
|
|
|
242 |
'nom' => $nom,
|
|
|
243 |
'nom_structure' => $nom_structure,
|
|
|
244 |
'specimen_type' => $specimen_type,
|
|
|
245 |
'description' => $description,
|
|
|
246 |
'periode_constitution' => $periode_constitution,
|
|
|
247 |
'couverture_lieu' => $couverture_lieu,
|
|
|
248 |
'etiquette_renseignement' => $etiquette_renseignement,
|
|
|
249 |
'nature' => $nature,
|
|
|
250 |
'parts_total' => $parts_total
|
|
|
251 |
);
|
1531 |
jpm |
252 |
}
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
// Création du contenu
|
|
|
256 |
$contenu = $this->executerService($donnees);
|
|
|
257 |
return $contenu;
|
|
|
258 |
}
|
|
|
259 |
|
1530 |
jpm |
260 |
private function executerRequete($requete, $message_echec) {
|
|
|
261 |
try {
|
|
|
262 |
$infos = $this->bdd->query($requete)->fetchAll(PDO::FETCH_ASSOC);
|
|
|
263 |
if ($infos === false) {
|
|
|
264 |
$this->messages[] = $message_echec;
|
|
|
265 |
}
|
|
|
266 |
} catch (PDOException $e) {
|
1537 |
jpm |
267 |
$this->messages[] = sprintf($this->getTxt('sql_erreur_requete'), $e->getFile(), $e->getLine(), $e->getMessage(), $requete);
|
1530 |
jpm |
268 |
}
|
|
|
269 |
return $infos;
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
private function executerService($donnees) {
|
|
|
273 |
// Création du contenu à partir d'un template PHP
|
|
|
274 |
$contenu = Coel::traiterSquelettePhp($this->squelette, $donnees);
|
|
|
275 |
return $contenu;
|
|
|
276 |
}
|
1537 |
jpm |
277 |
|
|
|
278 |
private static function nettoyerGuillemets($txt) {
|
|
|
279 |
$txt = preg_replace('/"/', '""', $txt);
|
|
|
280 |
return $txt;
|
|
|
281 |
}
|
1530 |
jpm |
282 |
}
|