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
|
1531 |
jpm |
17 |
const META_LISTES_EXPERTISE_COLLECTION = '';// Liste des métadonnées pour les informations des des collections
|
1530 |
jpm |
18 |
private $format = null;
|
|
|
19 |
private $service = null;
|
|
|
20 |
private $squelette = null;
|
|
|
21 |
private $squelette_dossier = null;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Méthode appelée avec une requête de type GET.
|
|
|
25 |
*/
|
|
|
26 |
public function getElement($params = array()) {
|
|
|
27 |
// Initialisation des variables
|
|
|
28 |
$info = array();
|
|
|
29 |
$contenu = '';
|
|
|
30 |
|
|
|
31 |
// Pré traitement des paramêtres
|
|
|
32 |
$pour_bdd = false;
|
|
|
33 |
$this->parametres = $this->traiterParametresUrl(array('service', 'format', 'projet'), $params, $pour_bdd);
|
|
|
34 |
|
|
|
35 |
// Chargement du bon type de service demandé
|
|
|
36 |
if (isset($this->parametres['service'])) {
|
|
|
37 |
$this->service = strtolower($this->parametres['service']);
|
|
|
38 |
|
|
|
39 |
$methode = $this->getNomMethodeService();
|
|
|
40 |
if (method_exists($this, $methode)) {
|
|
|
41 |
if (isset($this->parametres['format']) && preg_match('/^(?:csv|txt)$/i', $this->parametres['format'])) {
|
|
|
42 |
// Mise en minuscule de l'indication du format
|
|
|
43 |
$this->format = strtolower($this->parametres['format']);
|
|
|
44 |
// Définition du fichier squelette demandé
|
|
|
45 |
$this->definirSquelette();
|
|
|
46 |
} else {
|
|
|
47 |
$this->format = '';
|
|
|
48 |
$this->messages[] = "Le service COEL Export nécessite d'indiquer en second paramètre le format : csv, txt.";
|
|
|
49 |
}
|
|
|
50 |
// Récupération du contenu à renvoyer
|
|
|
51 |
$contenu = $this->$methode();
|
|
|
52 |
} else {
|
|
|
53 |
$this->messages[] = "Le type d'information demandé '$this->service' n'est pas disponible.";
|
|
|
54 |
}
|
|
|
55 |
} else {
|
|
|
56 |
$this->messages[] = "Le service COEL Syndication nécessite d'indiquer en premier paramètre le type d'information demandé.";
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
// Envoie sur la sortie standard
|
|
|
60 |
$encodage = 'utf-8';
|
|
|
61 |
$mime = $this->getTypeMime();
|
|
|
62 |
$formatage_json = $this->getFormatageJson();
|
|
|
63 |
$this->envoyer($contenu, $mime, $encodage, $formatage_json);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
private function definirSquelette() {
|
|
|
67 |
$this->squelette_dossier = dirname(__FILE__).DIRECTORY_SEPARATOR.'squelettes'.DIRECTORY_SEPARATOR;
|
|
|
68 |
$ext = $this->getExtensionFichierSquelette();
|
|
|
69 |
$this->squelette = $this->squelette_dossier.'export_'.$this->service.'.tpl.'.$ext;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
private function getExtensionFichierSquelette() {
|
|
|
73 |
$ext = '';
|
|
|
74 |
switch ($this->format) {
|
|
|
75 |
case 'csv' :
|
|
|
76 |
case 'txt' :
|
|
|
77 |
$ext = 'csv';
|
|
|
78 |
break;
|
|
|
79 |
default:
|
|
|
80 |
$ext = 'csv';
|
|
|
81 |
}
|
|
|
82 |
return $ext;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
private function getNomMethodeService() {
|
|
|
86 |
$methode = '';
|
|
|
87 |
$service_formate = str_replace(' ', '', ucwords(implode(' ', explode('_', $this->service))));
|
|
|
88 |
$methode = 'getService'.$service_formate;
|
|
|
89 |
return $methode;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
private function getTypeMime() {
|
|
|
93 |
$mime = '';
|
|
|
94 |
switch ($this->format) {
|
|
|
95 |
case 'csv' :
|
|
|
96 |
$mime = 'text/csv';
|
|
|
97 |
break;
|
|
|
98 |
case 'txt' :
|
|
|
99 |
$mime = 'text/plain';
|
|
|
100 |
break;
|
|
|
101 |
default:
|
|
|
102 |
$mime = 'text/html';
|
|
|
103 |
}
|
|
|
104 |
return $mime;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
private function getFormatageJson() {
|
|
|
108 |
$json = false;
|
|
|
109 |
switch ($this->service) {
|
|
|
110 |
default:
|
|
|
111 |
$json = false;
|
|
|
112 |
}
|
|
|
113 |
return $json;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
private function getServiceStructureExpertise() {
|
|
|
118 |
$donnees = array();
|
|
|
119 |
$projet_id = $this->parametres['projet'];
|
|
|
120 |
|
|
|
121 |
// Construction de la requête
|
|
|
122 |
$requete = 'SELECT '.(($this->distinct) ? 'SELECT DISTINCT' : '').' '.
|
|
|
123 |
' cs_id_structure, '.
|
|
|
124 |
' SUM(csap_bota_travail_hebdo_tps) AS personnel_heure_nbre, '.
|
|
|
125 |
' cs_nom, csc_truk_stockage_parametre '.
|
|
|
126 |
'FROM coel_structure '.
|
|
|
127 |
' LEFT JOIN coel_structure_a_personne ON (cs_id_structure = csap_id_structure) '.
|
|
|
128 |
' LEFT JOIN coel_structure_conservation ON (cs_id_structure = csc_id_structure) '.
|
|
|
129 |
( is_null($projet_id) ? '' : "WHERE cs_ce_projet = $projet_id ").
|
|
|
130 |
'GROUP BY cs_id_structure '.
|
|
|
131 |
'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby : 'cs_nom ASC').' '.
|
|
|
132 |
((!is_null($this->start) && !is_null($this->limit)) ? "LIMIT $this->start,$this->limit " : '');
|
|
|
133 |
$message_echec = "La requête a retourné aucun résultat.";
|
|
|
134 |
$structures = $this->executerRequete($requete, $message_echec);
|
|
|
135 |
|
|
|
136 |
// Construction de la requête
|
|
|
137 |
$requete = 'SELECT '.(($this->distinct) ? 'SELECT DISTINCT' : '').' '.
|
|
|
138 |
' cs_id_structure, '.
|
|
|
139 |
' COUNT(cc_id_collection) AS collection_nbre '.
|
|
|
140 |
'FROM coel_structure '.
|
|
|
141 |
' LEFT JOIN coel_collection ON (cs_id_structure = cc_ce_structure) '.
|
|
|
142 |
( is_null($projet_id) ? '' : "WHERE cs_ce_projet = $projet_id ").
|
|
|
143 |
'GROUP BY cs_id_structure ';
|
|
|
144 |
$message_echec = "La requête comptant le nombre de collection a retourné aucun résultat.";
|
|
|
145 |
$collections_nbre = $this->executerRequete($requete, $message_echec);
|
|
|
146 |
|
|
|
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);
|
|
|
153 |
|
|
|
154 |
// Traitement des données
|
|
|
155 |
if ($structures !== false) {
|
|
|
156 |
// Traitement des métadonnées
|
|
|
157 |
$parametres = array(0 => array('nom' => 'Non renseigné', 'abr' => 'NR'));
|
|
|
158 |
foreach ($metadonnees as $meta) {
|
|
|
159 |
$parametres[$meta['cmlv_id_valeur']] = array('nom' => $meta['cmlv_nom'], 'abr' => $meta['cmlv_abreviation']);
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
// Traitement du nombre de collection par structure
|
|
|
163 |
$collections_par_structure = array();
|
|
|
164 |
foreach ($collections_nbre as $collection) {
|
|
|
165 |
$nbre = (empty($collection['collection_nbre'])) ? 0 : $collection['collection_nbre'];
|
|
|
166 |
$collections_par_structure[$collection['cs_id_structure']] = $nbre;
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
// Traitement final des données concernant les structures
|
|
|
170 |
foreach ($structures as $structure) {
|
1531 |
jpm |
171 |
// Traitement des paramêtres de stockage
|
1530 |
jpm |
172 |
$truk_stockage_parametres = explode(';;', trim($structure['csc_truk_stockage_parametre'], ' ;'));
|
|
|
173 |
$export_stockage_parametres = array();
|
|
|
174 |
foreach ($truk_stockage_parametres as $param_code) {
|
|
|
175 |
$param_code = preg_replace('/^AUTRE##/', '', trim($param_code));
|
|
|
176 |
$param_txt = preg_match('/^[0-9]+$/', $param_code) ? $parametres[$param_code]['nom'] : $param_code;
|
|
|
177 |
$export_stockage_parametres[] = addcslashes($param_txt, '"');
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
$id_structure = $structure['cs_id_structure'];
|
|
|
181 |
$nom = addcslashes($structure['cs_nom'], '"');
|
|
|
182 |
$stockage_params = implode(' ; ', $export_stockage_parametres);
|
1531 |
jpm |
183 |
$stockage_params_nbre = count($export_stockage_parametres);
|
1530 |
jpm |
184 |
$collection_nbre = $collections_par_structure[$structure['cs_id_structure']];
|
|
|
185 |
$collection_nbre = empty($collection_nbre) ? 0 : $collection_nbre;
|
|
|
186 |
$personnel_heure_nbre = empty($structure['personnel_heure_nbre']) ? 0 : $structure['personnel_heure_nbre'];
|
|
|
187 |
|
|
|
188 |
$donnees['structures'][$id_structure] = array(
|
|
|
189 |
'nom' => $nom,
|
|
|
190 |
'stockage_params' => $stockage_params,
|
1531 |
jpm |
191 |
'stockage_params_nbre' => $stockage_params_nbre,
|
1530 |
jpm |
192 |
'collection_nbre' => $collection_nbre,
|
|
|
193 |
'personnel_heure_nbre' => $personnel_heure_nbre);
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
// Création du contenu
|
|
|
198 |
$contenu = $this->executerService($donnees);
|
|
|
199 |
return $contenu;
|
|
|
200 |
}
|
|
|
201 |
|
1531 |
jpm |
202 |
private function getServiceCollectionExpertise() {
|
|
|
203 |
$donnees = array();
|
|
|
204 |
$projet_id = $this->parametres['projet'];
|
|
|
205 |
|
|
|
206 |
// Construction de la requête
|
|
|
207 |
$requete = 'SELECT '.(($this->distinct) ? 'SELECT DISTINCT' : '').' '.
|
|
|
208 |
' cc_id_collection, '.
|
|
|
209 |
' cc_nom, cs_nom, cc_ce_specimen_type, cc_description, cc_truk_periode_constitution, '.
|
|
|
210 |
' cc_truk_couverture_lieu, ccb_truk_etiquette_renseignement, ccb_truk_nature, '.
|
|
|
211 |
' ccb_truk_unite_base, ccb_ce_etat_general, ccb_truk_etiquette_fixation_support, '.
|
|
|
212 |
' ccb_truk_etiquette_fixation_specimen, ccb_truk_degradation_presentation, '.
|
|
|
213 |
' ccb_ce_classement_etat, ccb_ce_inventaire '.
|
|
|
214 |
'FROM coel_collection '.
|
|
|
215 |
' LEFT JOIN coel_collection_botanique ON (cc_id_collection = ccb_id_collection) '.
|
|
|
216 |
' LEFT JOIN coel_structure ON (cs_id_structure = cc_ce_structure) '.
|
|
|
217 |
( is_null($projet_id) ? '' : "WHERE cc_ce_projet = $projet_id ").
|
|
|
218 |
'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby : 'cc_nom ASC').' '.
|
|
|
219 |
((!is_null($this->start) && !is_null($this->limit)) ? "LIMIT $this->start,$this->limit " : '');
|
|
|
220 |
$message_echec = "La requête a retourné aucun résultat.";
|
|
|
221 |
$structures = $this->executerRequete($requete, $message_echec);
|
|
|
222 |
|
|
|
223 |
// Récupération des métadonnées nécessaires
|
|
|
224 |
$requete = 'SELECT cmlv_id_valeur, cmlv_ce_parent, cmlv_nom, cmlv_abreviation '.
|
|
|
225 |
'FROM coel_meta_liste_valeur '.
|
|
|
226 |
'WHERE cmlv_ce_parent IN ('.self::META_LISTES_EXPERTISE_COLLECTION.') ';
|
|
|
227 |
$message_echec = "La requête de recherche des métadonnées a retourné aucun résultat.";
|
|
|
228 |
$metadonnees = $this->executerRequete($requete, $message_echec);
|
|
|
229 |
|
|
|
230 |
// Traitement des données
|
|
|
231 |
if ($structures !== false) {
|
|
|
232 |
// Traitement des métadonnées
|
|
|
233 |
$parametres = array(0 => array('nom' => 'Non renseigné', 'abr' => 'NR'));
|
|
|
234 |
foreach ($metadonnees as $meta) {
|
|
|
235 |
$parametres[$meta['cmlv_id_valeur']] = array('nom' => $meta['cmlv_nom'], 'abr' => $meta['cmlv_abreviation']);
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
// Traitement du nombre de collection par structure
|
|
|
239 |
$collections_par_structure = array();
|
|
|
240 |
foreach ($collections_nbre as $collection) {
|
|
|
241 |
$nbre = (empty($collection['collection_nbre'])) ? 0 : $collection['collection_nbre'];
|
|
|
242 |
$collections_par_structure[$collection['cs_id_structure']] = $nbre;
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
// Traitement final des données concernant les structures
|
|
|
246 |
foreach ($structures as $structure) {
|
|
|
247 |
$truk_stockage_parametres = explode(';;', trim($structure['csc_truk_stockage_parametre'], ' ;'));
|
|
|
248 |
$export_stockage_parametres = array();
|
|
|
249 |
foreach ($truk_stockage_parametres as $param_code) {
|
|
|
250 |
$param_code = preg_replace('/^AUTRE##/', '', trim($param_code));
|
|
|
251 |
$param_txt = preg_match('/^[0-9]+$/', $param_code) ? $parametres[$param_code]['nom'] : $param_code;
|
|
|
252 |
$export_stockage_parametres[] = addcslashes($param_txt, '"');
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
$id_structure = $structure['cs_id_structure'];
|
|
|
256 |
$nom = addcslashes($structure['cs_nom'], '"');
|
|
|
257 |
$stockage_params = implode(' ; ', $export_stockage_parametres);
|
|
|
258 |
$collection_nbre = $collections_par_structure[$structure['cs_id_structure']];
|
|
|
259 |
$collection_nbre = empty($collection_nbre) ? 0 : $collection_nbre;
|
|
|
260 |
$personnel_heure_nbre = empty($structure['personnel_heure_nbre']) ? 0 : $structure['personnel_heure_nbre'];
|
|
|
261 |
|
|
|
262 |
$donnees['structures'][$id_structure] = array(
|
|
|
263 |
'nom' => $nom,
|
|
|
264 |
'stockage_params' => $stockage_params,
|
|
|
265 |
'collection_nbre' => $collection_nbre,
|
|
|
266 |
'personnel_heure_nbre' => $personnel_heure_nbre);
|
|
|
267 |
}
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
// Création du contenu
|
|
|
271 |
$contenu = $this->executerService($donnees);
|
|
|
272 |
return $contenu;
|
|
|
273 |
}
|
|
|
274 |
|
1530 |
jpm |
275 |
private function executerRequete($requete, $message_echec) {
|
|
|
276 |
try {
|
|
|
277 |
$infos = $this->bdd->query($requete)->fetchAll(PDO::FETCH_ASSOC);
|
|
|
278 |
if ($infos === false) {
|
|
|
279 |
$this->messages[] = $message_echec;
|
|
|
280 |
}
|
|
|
281 |
} catch (PDOException $e) {
|
|
|
282 |
$this->messages[] = sprintf($this->getTxt('sql_erreur'), $e->getFile(), $e->getLine(), $e->getMessage());
|
|
|
283 |
}
|
|
|
284 |
return $infos;
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
private function executerService($donnees) {
|
|
|
288 |
// Création du contenu à partir d'un template PHP
|
|
|
289 |
$contenu = Coel::traiterSquelettePhp($this->squelette, $donnees);
|
|
|
290 |
return $contenu;
|
|
|
291 |
}
|
|
|
292 |
}
|