Subversion Repositories eFlore/Applications.cel

Rev

Rev 483 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
417 aurelien 1
<?php
2
 
3
// In : utf8
4
// Out : utf8
5
 
483 david 6
 
7
// TODO : traiter image multilignes
540 david 8
// TODO : doublons
483 david 9
 
480 david 10
/*
417 aurelien 11
 
480 david 12
Octobre 2010 David Delon.
13
Import d'observations dans le carnet en ligne à partir d'un fichier excel chargé par l'utilisateur
14
et liaison d'images déjà chargee aux observations ainsi crées.
417 aurelien 15
 
480 david 16
Nom des colonnes imposé, mais présence de toutes les colonnes non obligatoires, ordre non imposé
17
Aucune valeur dans les colonnes n'est obligatoire
18
Pour une ligne donnée, si tous les champs vides on ne fait rien, ou si seul le champ image est présent.
19
Si la seule différence entre deux lignes est la valeur de la colonne image, on considère que c'est la même observation à laquelle on associe plusieurs images.
20
Si au moins deux lignes (ou plus) sont complètement identiques on prend en compte une seule ligne (les doublons sont éliminés).
445 david 21
 
480 david 22
*/
445 david 23
 
480 david 24
 
25
// Nom des colonnes
26
 
445 david 27
define('COMMUNE','commune'); // soit un nom de commune, soit un code INSEE (5 chiffres), ou "nom de commune (numero departement)"
28
define('LIEUDIT','lieu-dit'); // Texte libre
29
define('STATION','station'); // Texte libre
30
define('MILIEU','milieu'); // Texte libre
31
define('LATITUDE','latitude'); // En decimal systeme WGS84
32
define('LONGITUDE','longitude'); // En decimal systeme WGS84
33
define('NOTES','notes'); // Texte libre
34
define('DATEOBS','date'); // date au format jj/mm/aaaa
35
define('ESPECE','espece'); // texte libre, nom latin, ou code nomenclatural (format BDNFFnn999999)
36
define('IMAGE','image'); //  nom des fichiers images préalablement uploadés sur le CEL séparés par des "/"
540 david 37
define('DEPARTEMENT','departement'); //  Texte libre
445 david 38
 
540 david 39
 
480 david 40
// Resultat de l'analyse d'une ligne
445 david 41
define('LIGNE_VIDE',1); //
42
define('LIGNE_NORMALE',2); //
43
define('LIGNE_IMAGE_SEULEMENT',3); //
44
 
540 david 45
// Parser de Nom
46
include('NameParser.php');
445 david 47
 
417 aurelien 48
Class InventoryImportExcel extends DBAccessor  {
49
 
445 david 50
// Element constituant une observation
417 aurelien 51
 
540 david 52
	var $format_observation=array(COMMUNE ,LIEUDIT ,STATION , DEPARTEMENT, MILIEU ,LATITUDE ,LONGITUDE ,NOTES ,DATEOBS ,ESPECE ,IMAGE );
480 david 53
 
54
// Fichier configuration
417 aurelien 55
	var $config;
480 david 56
 
57
// Encapsulation classe lecture fichier excel
417 aurelien 58
	var $extendExcelReader;
59
 
540 david 60
// Dernier numero d'ordre utilise
61
	var $dernier_ordre=1;
62
 
480 david 63
/**
64
 Constructeur
65
**/
417 aurelien 66
	function InventoryImportExcel($config) {
67
 
68
		$this->config=$config;
69
		// Pas d'heritage multiple en php :(
70
 
71
		$this->extendExcelReader = new ExcelReader();
72
		$this->extendExcelReader->initExcelReader();
540 david 73
 
74
/* Recherche dernier numero d'ordre utilise : pas de mise a jour concurente a priori */
75
 
76
 
417 aurelien 77
	}
78
 
480 david 79
/**
80
 Sur post
81
**/
445 david 82
	function createElement($pairs) {
417 aurelien 83
 
84
 
480 david 85
		$pairs['utilisateur']=$_POST['identifiant'];
417 aurelien 86
 
540 david 87
        session_start();
88
        $this->controleUtilisateur($pairs['utilisateur']);
480 david 89
 
417 aurelien 90
 
540 david 91
        foreach($_FILES as $file) { // C'est le plus simple
92
            $infos_fichier = $file ;
93
        }
417 aurelien 94
 
480 david 95
 
96
// Chargement tableau en memoire
445 david 97
 
98
		$data = new Spreadsheet_Excel_Reader($infos_fichier['tmp_name'], false); // false : pour menager la memoire.
480 david 99
		$arr = array();
445 david 100
 
101
		$rowcount=$data->rowcount(0);
102
		$colcount=$data->colcount(0);
103
 
104
		if ($rowcount<=1) { // TODO : retour erreur
105
			print "Tableau vide";
106
			exit;
107
		}
108
 
540 david 109
// Chargement tableau
110
        for($row=1;$row<=$rowcount;$row++)
111
            for($col=1;$col<=$colcount;$col++)
112
                $arr[$col][$row] = $data->val($row,$col,0); // Attention, inversion voulue
445 david 113
 
540 david 114
 
417 aurelien 115
 
540 david 116
// 1 : Traitement intitules
417 aurelien 117
 
445 david 118
		$line = array();
417 aurelien 119
 
445 david 120
/* Les colonnes ne sont pas forcemment dans l'ordre  : on les extrait pour traitement futur  */
121
 
540 david 122
        for($col=1;$col<=$colcount;$col++) {
123
            $colonne=strtolower($arr[$col][1]);
124
            $colonne=trim($colonne);
125
            $colonne=cp1252_to_utf8($colonne);
126
            $colonne=remove_accent($colonne);
127
            switch ($colonne) {  // On ne garde que les colonnes que l'on souhaite traiter
128
                case COMMUNE:
129
                case LIEUDIT:
130
                case STATION:
131
                case MILIEU:
132
                case DEPARTEMENT:
133
                case LATITUDE:
134
                case LONGITUDE:
135
                case NOTES:
136
                case DATEOBS:
137
                case ESPECE:
138
                case IMAGE:
139
                    $selection=array_values($arr[$col]);
140
                    array_shift($selection); // On ne garde pas la premiere ligne, qui contient les intitules
141
                    $line[$colonne]=$selection;
142
                    break;
445 david 143
 
540 david 144
            }
145
        }
445 david 146
 
147
 
540 david 148
//	print_r($line[COMMUNE]);
149
//		print_r($line[LIEUDIT]);
150
//		print_r($line[STATION]);
151
//		print_r($line[MILIEU]);
152
//		print_r($line[DEPARTEMENT]);
153
//		print_r($line[LATITUDE]);
154
//		print_r($line[LONGITUDE]);
155
//		print_r($line[NOTES]);
156
//		print_r($line[DATEOBS]);
157
//		print_r($line[ESPECE]);
158
//		print_r($line[IMAGE]);
159
 
160
 
445 david 161
// 1 : Traitement lignes
162
 
483 david 163
		$cpt_obs=0;
540 david 164
 
165
 
166
        /* Recherche dernier numero d'ordre utilise : pas de mise a jour concurente a priori */
167
 
168
 
169
        $DB=$this->connectDB($this->config,'database_cel');
170
        $query="SELECT MAX(ordre) AS ordre FROM cel_inventory WHERE identifiant='".$DB->escapeSimple($pairs['utilisateur'])."' ";
171
 
172
        $res =& $DB->query($query);
173
        if (DB::isError($res)) {
174
            die($res->getMessage());
175
        }
176
 
177
        while ($row =& $res->fetchrow(DB_FETCHMODE_ASSOC)) {
178
            $this->dernier_ordre=$row['ordre']; // 1  par defaut
179
        }
180
 
480 david 181
		for ($i=0;$i<=$rowcount-1;$i++) {
540 david 182
			// On saute les eventuelles lignes vides du debut et les lignes contenant des information sur image uniquement
480 david 183
			while ((in_array($retour_analyse=$this->analyserLigne($line,$i),array(LIGNE_IMAGE_SEULEMENT, LIGNE_VIDE))) && ($i<=$rowcount)) {
184
				if  ($retour_analyse==LIGNE_IMAGE_SEULEMENT) {
185
	//				print "image non rattachee a une observation";
186
				}
187
				else {
188
	//				print "vide";
189
				}
445 david 190
				$i++;
191
			}
480 david 192
			while (($this->analyserLigne($line,$i)==LIGNE_NORMALE) && ($i<=$rowcount)) {
540 david 193
				$ordre=$this->traiterLigne($line,$i,$pairs['utilisateur'],$DB);
483 david 194
				if ($ordre>0) {
195
					$cpt_obs++; // Compteur d'observations crees
196
				}
445 david 197
				$i++;
480 david 198
				// On saute les lignes vide ou on traite les lignes suivantes contenant des informations sur image seulement
199
				while ((in_array($retour_analyse=$this->analyserLigne($line,$i),array(LIGNE_IMAGE_SEULEMENT, LIGNE_VIDE))) && ($i<=$rowcount)) {
445 david 200
					if  ($retour_analyse==LIGNE_IMAGE_SEULEMENT) {
480 david 201
						$this->traiterLigneComplement($line,$i,$pairs['utilisateur'],$ordre); // images supplementaires
445 david 202
					}
203
					else {
480 david 204
	//					print "vide";
445 david 205
					}
206
					$i++;
207
				}
208
			}
209
 
210
 
211
		}
540 david 212
		//print $cpt_obs ." nouvelle(s) observation(s) !";
213
		print $cpt_obs ;
445 david 214
 
417 aurelien 215
	}
216
 
445 david 217
function analyserLigne($line,$i) {
218
	$ligne_vide=true;
219
	$ligne_image_seulement=true;
220
	$ligne_normale=true;
221
	foreach ($this->format_observation as $colonne) {
222
		if (isset ($line[$colonne][$i]) && $line[$colonne][$i]!='') {
223
			if ($colonne!=IMAGE) {
224
				$ligne_image_seulement=false;
225
				$ligne_vide=false;
226
				break;
227
			}
228
			$ligne_vide=false;
229
		}
230
	}
231
	if ($ligne_vide) {
232
		return LIGNE_VIDE;
233
	}
234
	else {
235
		if ($ligne_image_seulement) {
236
			return LIGNE_IMAGE_SEULEMENT;
237
		}
238
		else {
239
			return LIGNE_NORMALE;
240
		}
241
	}
242
 
417 aurelien 243
 
445 david 244
}
417 aurelien 245
 
540 david 246
function traiterLigne($line,$i,$utilisateur,$DB) { // Controle donnee et insertion
480 david 247
	$info_image=array();
445 david 248
	foreach ($this->format_observation as $colonne) {
249
		if (isset ($line[$colonne][$i]) && $line[$colonne][$i]!='') {
250
			switch ($colonne) {  // On ne garde que les colonnes que l'on souhaite traiter
251
				case COMMUNE:
252
					$info_commune=$this->traiterCommune($line[COMMUNE][$i]);
253
					break;
254
				case LIEUDIT:
480 david 255
					$info_lieudit=$this->traiterLieudit($line[LIEUDIT][$i]);
445 david 256
					break;
257
				case STATION:
480 david 258
					$info_station=$this->traiterStation($line[STATION][$i]);
445 david 259
					break;
260
				case MILIEU:
480 david 261
					$info_milieu=$this->traiterMilieu($line[MILIEU][$i]);
445 david 262
					break;
540 david 263
                case DEPARTEMENT:
264
					$info_commune['code']=$this->traiterDepartement($line[DEPARTEMENT][$i]);
265
					break;
445 david 266
				case LATITUDE:
480 david 267
					$info_latitude=$this->traiterLatitude($line[LATITUDE][$i]);
445 david 268
					break;
269
				case LONGITUDE:
480 david 270
					$info_longitude=$this->traiterLongitude($line[LONGITUDE][$i]);
445 david 271
					break;
272
				case NOTES:
480 david 273
					$info_notes=$this->traiterNotes($line[NOTES][$i]);
445 david 274
					break;
275
				case DATEOBS:
480 david 276
					$info_dateobs=$this->traiterDateObs($line[DATEOBS][$i]);
445 david 277
					break;
278
				case ESPECE:
480 david 279
					$info_espece=$this->traiterEspece($line[ESPECE][$i]);
540 david 280
                    if (isset($info_espece['en_id_nom']) && $info_espece['en_id_nom']!='') {
281
                        $complement=$this->rechercherInformationsComplementaires($info_espece['en_id_nom']);
282
                        $info_espece['nom_ret']=$complement['Nom_Retenu'];
283
                        $info_espece['num_nom_ret']=$complement['Num_Nom_Retenu'];
284
                        $info_espece['num_taxon']=$complement['Num_Taxon'];
285
                        $info_espece['famille']=$complement['Famille'];
286
                    }
287
                    break;
445 david 288
				case IMAGE:
480 david 289
					$info_image=$this->traiterImage($line[IMAGE][$i],$utilisateur); // Image separee par des / +  utilisateur
445 david 290
					break;
291
			}
292
		}
480 david 293
		else {
294
		 	switch($colonne) {
295
				case COMMUNE:
296
					$info_commune['name']="000null";
297
					$info_commune['code']="000null";
298
					break;
299
				case LIEUDIT:
300
					$info_lieudit="000null";
301
					break;
302
				case STATION:
303
					$info_station="000null";
304
					break;
305
				case MILIEU:
306
					$info_milieu="000null";
307
					break;
540 david 308
				case DEPARTEMENT:
309
					$info_commune['code']="000null";
310
					break;
480 david 311
				case LATITUDE:
312
					$info_latitude="000null";
313
					break;
314
				case LONGITUDE:
315
					$info_longitude="000null";
316
					break;
317
 
318
			}
319
 
320
		}
445 david 321
	}
322
 
540 david 323
                $this->dernier_ordre++;
480 david 324
 
325
                list($jour,$mois,$annee)=split("/",$info_dateobs);
326
                $info_dateobs=$annee."-".$mois."-".$jour." 0:0:0";
327
 
540 david 328
                $query  = "INSERT INTO cel_inventory (identifiant,ordre,nom_sel,num_nom_sel,nom_ret,num_nom_ret,num_taxon,famille,location,id_location,date_observation,lieudit,station, milieu, commentaire, date_creation,date_modification,coord_x,coord_y) " .
329
                    " VALUES('".$DB->escapeSimple($utilisateur)."','".
330
                    $DB->escapeSimple($this->dernier_ordre)."','".
331
                    $DB->escapeSimple($info_espece['nom_sel'])."','".
332
                    $DB->escapeSimple($info_espece['en_id_nom'])."','".
333
                    $DB->escapeSimple($info_espece['nom_ret'])."','".
334
                    $DB->escapeSimple($info_espece['num_nom_ret'])."','".
335
                    $DB->escapeSimple($info_espece['num_taxon'])."','".
336
                    $DB->escapeSimple($info_espece['famille'])."','".
337
                    $DB->escapeSimple($info_commune['name'])."','".
338
                    $DB->escapeSimple($info_commune['code'])."','".
339
                    $DB->escapeSimple($info_dateobs)."','".
340
                    $DB->escapeSimple($info_lieudit)."','".
341
                    $DB->escapeSimple($info_station)."','".
342
                    $DB->escapeSimple($info_milieu)."','".
343
                    $DB->escapeSimple($info_notes)."',".
344
                    "now() , now(),'".
345
                    $DB->escapeSimple($info_latitude)."','".
346
                    $DB->escapeSimple($info_longitude)."')";
480 david 347
//		print "\n";
348
 
349
 
350
		   $res =& $DB->query($query);
351
 
352
                if (PEAR::isError($res)) {
353
                        return false;
354
                }
355
 
356
 
357
	// creation lien image
358
	foreach ($info_image as $pic) {
359
 
360
		$query = 'INSERT INTO cel_obs_images (coi_ce_image, coi_ce_utilisateur, coi_ce_observation) VALUES ("'.$DB->escapeSimple($pic['ci_id_image']).'","'.$DB->escapeSimple($utilisateur).'",    "'.$DB->escapeSimple($ordre).'") ON DUPLICATE KEY UPDATE coi_ce_image = coi_ce_image' ;
361
 
362
//print $query;
363
 
364
		$res =& $DB->query($query);
365
 
366
                if (PEAR::isError($res)) {
367
                        return false;
368
                }
369
 
370
 
371
	}
372
 
373
 
374
 
540 david 375
		return $this->dernier_ordre;
480 david 376
 
377
 
417 aurelien 378
}
379
 
480 david 380
function traiterLigneComplement($line,$i,$utilisateur) {
417 aurelien 381
 
480 david 382
// TODO
383
 
445 david 384
}
385
function traiterCommune($identifiant_commune) {  // Recherche correspondance sur nom, si pas unique, correspondance dep. sinon code insee
386
 
387
 
540 david 388
        $identifiant_commune=trim($identifiant_commune);
445 david 389
 
390
        $identifiant_commune=utf8_encode($identifiant_commune); // FIXME : devrait deja etre en utf8 a ce niveau
391
 
540 david 392
	preg_match('/(.*) \(([0-9][0-9]*)\)/',$identifiant_commune,$elements);
445 david 393
 
394
        $DB=$this->connectDB($this->config,'database_cel'); // FIXME regarder si opportun ici
395
 
540 david 396
	if ($elements[1]) { // commune + departement : montpellier (34)
445 david 397
		$nom_commune=$elements[1];
398
		$code_commune=$elements[2];
399
 
480 david 400
 	        $query="SELECT DISTINCT name, code  FROM locations WHERE name = '".$DB->escapeSimple($nom_commune)."' AND code ='".$DB->escapeSimple($code_commune)."'";
445 david 401
 
402
	}
540 david 403
	else { // Code insee seul
404
        preg_match('/([0-9][0-9]*)|(2A[0-9][0-9]*)|(2B[0-9][0-9]*)/',$identifiant_commune,$elements);
405
        if ($elements[1]) { // code insee  commune
406
            $code_insee_commune=$elements[1];
407
            $query="SELECT DISTINCT name, code  FROM locations WHERE insee_code ='".$DB->escapeSimple($code_insee_commune)."'";
408
        }
409
        else { // Commune seule (le departement sera recupere dans la colonne departement si elle est presente, on prend le risque ici de retourner une mauvaise
410
               // Commune
411
            preg_match('/(.*)/',$identifiant_commune,$elements);
412
            if ($elements[1]) { // commune
413
                $nom_commune=$elements[1];
414
                $query="SELECT DISTINCT name, code  FROM locations WHERE name = '".$DB->escapeSimple($nom_commune)."'";
415
            }
416
        }
445 david 417
	}
418
 
419
 
420
	$res =& $DB->query($query);
421
 
422
        if (DB::isError($res)) {
423
		 die($res->getMessage());
424
	}
425
 
426
	return $res->fetchrow(DB_FETCHMODE_ASSOC);
427
 
428
 
429
 
430
 
431
}
432
 
433
function traiterLieudit($lieudit) { // texte libre
434
 
480 david 435
	//echo "traitement lieudit";
436
 
540 david 437
	return utf8_encode(trim($lieudit));
445 david 438
}
439
 
440
function traiterStation($station) { // texte libre
480 david 441
//	echo "traitement station";
540 david 442
	return utf8_encode(trim($station));
445 david 443
}
444
 
445
function traiterMilieu($milieu) { // texte libre
480 david 446
//	echo "traitement milieu";
540 david 447
	return utf8_encode(trim($milieu));
445 david 448
}
449
 
540 david 450
function traiterDepartement($departement) { // texte libre
451
//	echo "traitement milieu";
452
	return utf8_encode(trim($departement));
453
}
454
 
445 david 455
function traiterLatitude($latitude) {	//  verifier formal decimal + limite france ? TODO
480 david 456
//	echo "traitement latitude";
540 david 457
	return trim($latitude);
445 david 458
}
459
 
460
function traiterLongitude($longitude) { // verifier format decimal + limite france ? TODO
480 david 461
//	echo "traitement longitude";
540 david 462
	return trim($longitude);
445 david 463
}
464
 
465
function traiterNotes($notes) { // texte libre
480 david 466
//	echo "traitement notes";
540 david 467
	return utf8_encode(trim($notes));
445 david 468
}
469
 
470
function traiterDateObs($dateobs) { // verifier jj/mm/aaaa sinon date vide TODO
480 david 471
//	echo "traitement dateobs";
540 david 472
	return trim($dateobs);
445 david 473
}
474
 
540 david 475
function traiterEspece($identifiant_espece) {  // texte libre, nom scientifique , ou code nomenclatural (format BDNFFnn999999) ou code taxonomique (format BDNFFnt999999)
449 david 476
 
480 david 477
//	echo "traitement  espece";
540 david 478
        $identifiant_espece=trim($identifiant_espece);
445 david 479
 
480
        $identifiant_espece=utf8_encode($identifiant_espece); // FIXME : devrait deja etre en utf8 a ce niveau
481
 
540 david 482
	    preg_match('/BDNFFnn([0-9][0-9]*)/',$identifiant_espece,$elements);
483
 
449 david 484
		if ($elements[1]) { // Numero nomenclatural
445 david 485
 
449 david 486
 
540 david 487
            // Recherche du nom associe
488
            $DB=$this->connectDB($this->config); // FIXME : gerer cache de connection
489
            $query = "SELECT DISTINCT en_nom_genre, en_epithete_espece, en_nom_supra_generique, en_epithete_infra_generique,".
490
                "   auteur_bex.enaia_intitule_abrege AS abreviation_auteur_basio_ex ".
491
                " , auteur_b.enaia_intitule_abrege AS abreviation_auteur_basio ".
492
                " , auteur_mex.enaia_intitule_abrege AS abreviation_auteur_modif_ex ".
493
                " , auteur_m.enaia_intitule_abrege AS abreviation_auteur_modif ".
494
                " , en_epithete_espece, en_epithete_infra_specifique, enrg_abreviation_rang, en_id_nom" .
495
                " FROM eflore_nom, eflore_nom_rang, eflore_selection_nom a,  " .
496
                " eflore_naturaliste_intitule_abreviation AS auteur_bex ".
497
                " , eflore_naturaliste_intitule_abreviation AS auteur_b ".
498
                " , eflore_naturaliste_intitule_abreviation AS auteur_mex ".
499
                " , eflore_naturaliste_intitule_abreviation AS auteur_m ".
500
                " WHERE a.esn_id_nom= '".$elements[1]. "'".
501
                " AND a.esn_id_version_projet_taxon = 25 ".
502
                " AND en_ce_rang = enrg_id_rang" .
503
                " AND en_id_nom = a.esn_id_nom" .
504
                " AND en_ce_auteur_basio_ex = auteur_bex.enaia_id_intitule_naturaliste_abrege ".
505
                " AND en_ce_auteur_basio = auteur_b.enaia_id_intitule_naturaliste_abrege  ".
506
                " AND en_ce_auteur_modif_ex = auteur_mex.enaia_id_intitule_naturaliste_abrege ".
507
                " AND en_ce_auteur_modif = auteur_m.enaia_id_intitule_naturaliste_abrege ".
508
                " AND a.esn_id_version_projet_taxon=en_id_version_projet_nom ";
480 david 509
 
540 david 510
            $res =& $DB->query($query);
480 david 511
 
512
 
540 david 513
            if (DB::isError($res)) {
514
                die($res->getMessage());
515
            }
480 david 516
 
540 david 517
            $row =& $res->fetchrow(DB_FETCHMODE_ASSOC);
518
            return array("nom_sel"=>$this->formaterNom($row),"en_id_nom"=>$elements[1]);
519
 
449 david 520
		}
521
 
540 david 522
		else { //  Numero taxonomique ou nom scientifique
523
	        preg_match('/BDNFFnt([0-9][0-9]*)/',$identifiant_espece,$elements);
449 david 524
 
540 david 525
		    if ($elements[1]) { // Numero taxonomique
526
 
527
                $DB=$this->connectDB($this->config);
449 david 528
 
540 david 529
                $query = "SELECT DISTINCT en_nom_genre, en_epithete_espece, en_nom_supra_generique, en_epithete_infra_generique,".
530
                    "   auteur_bex.enaia_intitule_abrege AS abreviation_auteur_basio_ex ".
531
                    " , auteur_b.enaia_intitule_abrege AS abreviation_auteur_basio ".
532
                    " , auteur_mex.enaia_intitule_abrege AS abreviation_auteur_modif_ex ".
533
                    " , auteur_m.enaia_intitule_abrege AS abreviation_auteur_modif ".
534
                    " , en_epithete_espece, en_epithete_infra_specifique, enrg_abreviation_rang, en_id_nom" .
535
                    " FROM eflore_nom, eflore_nom_rang," .
536
                    "     eflore_naturaliste_intitule_abreviation AS auteur_bex ".
537
                    "   , eflore_naturaliste_intitule_abreviation AS auteur_b ".
538
                    "   , eflore_naturaliste_intitule_abreviation AS auteur_mex ".
539
                    "   , eflore_naturaliste_intitule_abreviation AS auteur_m ".
540
                    " , eflore_selection_nom ".
541
                    " WHERE esn_id_taxon = '".$elements[1]. "'".
542
                    " AND esn_id_version_projet_taxon = 25 ".
543
                    " AND esn_ce_statut=3 ".
544
                    " AND en_id_nom = esn_id_nom" .
545
                    " AND en_ce_rang = enrg_id_rang" .
546
                    " AND en_ce_auteur_basio_ex = auteur_bex.enaia_id_intitule_naturaliste_abrege ".
547
                    " AND en_ce_auteur_basio = auteur_b.enaia_id_intitule_naturaliste_abrege  ".
548
                    " AND en_ce_auteur_modif_ex = auteur_mex.enaia_id_intitule_naturaliste_abrege ".
549
                    " AND en_ce_auteur_modif = auteur_m.enaia_id_intitule_naturaliste_abrege ".
550
                    " AND esn_id_version_projet_taxon=en_id_version_projet_nom ";
449 david 551
 
540 david 552
                $res =& $DB->query($query);
449 david 553
 
540 david 554
                if (DB::isError($res)) {
555
                    die($res->getMessage());
556
                }
449 david 557
 
540 david 558
                $row =& $res->fetchrow(DB_FETCHMODE_ASSOC);
559
                return array("nom_sel"=>$this->formaterNom($row),"en_id_nom"=>$row['en_id_nom']);
449 david 560
 
561
 
540 david 562
            }
480 david 563
 
540 david 564
            else { // Nom scientifique
565
                $nameparser=new NameParser();
566
                $nom_latin_decoupe=$nameparser->parse($identifiant_espece);
480 david 567
 
540 david 568
//print_r($nom_latin_decoupe);
569
                    $DB=$this->connectDB($this->config); // FIXME regarder si opportun ici
480 david 570
 
540 david 571
                        // requete sous espece (on privilegie les noms retenu cf tri par esn_ce_statut)
572
                        if (isset($nom_latin_decoupe['infra']) && $nom_latin_decoupe['infra']!="") {
573
                            $query="SELECT DISTINCT en_id_nom, esn_ce_statut" .
574
                                            " FROM eflore_nom, eflore_nom_rang, eflore_selection_nom " .
575
                                            " WHERE en_id_version_projet_nom = '25' AND en_nom_genre = '".$DB->escapeSimple($nom_latin_decoupe['genus'])."' " .
576
                                            " AND enrg_abreviation_rang = '".$DB->escapeSimple($nom_latin_decoupe['infra_type'])."' " .
577
                                            " AND en_epithete_infra_specifique = '".$DB->escapeSimple($nom_latin_decoupe['infra'])."' " .
578
                                            " AND esn_id_nom= en_id_nom ".
579
                                            " AND esn_id_version_projet_taxon=en_id_version_projet_nom " .
580
                                            " AND en_epithete_espece =  '".$DB->escapeSimple($nom_latin_decoupe['species'])."' AND en_ce_rang = enrg_id_rang " .
581
                                            " ORDER BY esn_ce_statut ".
582
                                            " LIMIT 1";
583
                        }
584
                        else { // espece  (on privilegie les noms retenu cf tri par esn_ce_statut)
585
                             $query="SELECT DISTINCT en_id_nom, esn_ce_statut" .
586
                                            " FROM eflore_nom, eflore_nom_rang, eflore_selection_nom " .
587
                                            " WHERE en_id_version_projet_nom = '25' AND en_nom_genre = '".$DB->escapeSimple($nom_latin_decoupe['genus'])."' " .
588
                                            " AND enrg_abreviation_rang = 'sp.' " .
589
                                            " AND esn_id_nom= en_id_nom ".
590
                                            " AND esn_id_version_projet_taxon=en_id_version_projet_nom " .
591
                                            " AND en_epithete_espece =  '".$DB->escapeSimple($nom_latin_decoupe['species'])."' AND en_ce_rang = enrg_id_rang " .
592
                                            " ORDER BY esn_ce_statut ".
593
                                            " LIMIT 1";
594
 
595
                        }
596
                $res =& $DB->query($query);
597
                if (DB::isError($res)) {
598
                     die($res->getMessage());
599
                }
480 david 600
 
540 david 601
                $id_nom=$res->fetchrow(DB_FETCHMODE_ASSOC);
480 david 602
 
540 david 603
                // Recherche du nom associe
604
                $DB=$this->connectDB($this->config); // FIXME : gerer cache de connection
605
                $query = "SELECT DISTINCT en_nom_genre, en_epithete_espece, en_nom_supra_generique, en_epithete_infra_generique,".
606
                    "   auteur_bex.enaia_intitule_abrege AS abreviation_auteur_basio_ex ".
607
                    " , auteur_b.enaia_intitule_abrege AS abreviation_auteur_basio ".
608
                    " , auteur_mex.enaia_intitule_abrege AS abreviation_auteur_modif_ex ".
609
                    " , auteur_m.enaia_intitule_abrege AS abreviation_auteur_modif ".
610
                    " , en_epithete_espece, en_epithete_infra_specifique, enrg_abreviation_rang, en_id_nom" .
611
                    " FROM eflore_nom, eflore_nom_rang, eflore_selection_nom a,  " .
612
                    "         eflore_naturaliste_intitule_abreviation AS auteur_bex ".
613
                    "   , eflore_naturaliste_intitule_abreviation AS auteur_b ".
614
                    "   , eflore_naturaliste_intitule_abreviation AS auteur_mex ".
615
                    "   , eflore_naturaliste_intitule_abreviation AS auteur_m ".
616
                    " WHERE a.esn_id_nom= '".$id_nom['en_id_nom']. "'".
617
                    " AND a.esn_id_version_projet_taxon = 25 ".
618
                    " AND en_ce_rang = enrg_id_rang" .
619
                    " AND en_id_nom = a.esn_id_nom" .
620
                    " AND en_ce_auteur_basio_ex = auteur_bex.enaia_id_intitule_naturaliste_abrege ".
621
                    " AND en_ce_auteur_basio = auteur_b.enaia_id_intitule_naturaliste_abrege  ".
622
                    " AND en_ce_auteur_modif_ex = auteur_mex.enaia_id_intitule_naturaliste_abrege ".
623
                    " AND en_ce_auteur_modif = auteur_m.enaia_id_intitule_naturaliste_abrege ".
624
                    " AND a.esn_id_version_projet_taxon=en_id_version_projet_nom ";
625
                $res =& $DB->query($query);
480 david 626
 
627
 
540 david 628
                if (DB::isError($res)) {
629
                    die($res->getMessage());
630
                }
445 david 631
 
540 david 632
                if ($res->numRows() > 0 ) {
633
                    $row =& $res->fetchrow(DB_FETCHMODE_ASSOC);
634
                    return array("nom_sel"=>$this->formaterNom($row),"en_id_nom"=>$id_nom['en_id_nom']);
635
                }
636
                else {
637
                    return array("nom_sel"=>$identifiant_espece);
638
                }
445 david 639
 
480 david 640
 
540 david 641
                }
642
            }
480 david 643
 
644
 
445 david 645
}
646
 
647
 
648
 
480 david 649
function traiterImage($images,$utilisateur) { // recherche id image de ce nom
650
 
651
	//echo "traitement  image";
652
        $DB=$this->connectDB($this->config,'cel_db');
653
 
654
	$liste_images = explode("/",$images) ;
655
 
656
	$row =array();
657
        foreach($liste_images as $image) {
658
 
659
		$query="SELECT * FROM cel_images WHERE ci_ce_utilisateur='".$DB->escapeSimple($utilisateur)."' AND ci_nom_original='".$DB->escapeSimple($image)."'";
660
 
661
	        $res  =& $DB->query($query);
662
		$row [] =& $res->fetchrow(DB_FETCHMODE_ASSOC);
663
 
664
	        if (DB::isError($res)) {
665
        	    die($res->getMessage());
666
	        }
667
 
668
	}
669
	return $row;
670
 
671
 
445 david 672
}
673
 
540 david 674
       function rechercherInformationsComplementaires($numNom) { // Num taxon, Num retenu ...
445 david 675
 
480 david 676
                $DB=$this->connectDB($this->config);
677
 
678
                $query = "SELECT DISTINCT en_nom_genre, en_epithete_espece, en_nom_supra_generique, en_epithete_infra_generique,".
540 david 679
                    "   auteur_bex.enaia_intitule_abrege AS abreviation_auteur_basio_ex ".
680
                    " , auteur_b.enaia_intitule_abrege AS abreviation_auteur_basio ".
681
                    " , auteur_mex.enaia_intitule_abrege AS abreviation_auteur_modif_ex ".
682
                    " , auteur_m.enaia_intitule_abrege AS abreviation_auteur_modif ".
683
                    " , en_epithete_espece, en_epithete_infra_specifique, enrg_abreviation_rang, b.esn_id_taxon, b.esn_id_nom" .
684
                    " FROM eflore_nom, eflore_nom_rang," .
685
                    "     eflore_naturaliste_intitule_abreviation AS auteur_bex ".
686
                    "   , eflore_naturaliste_intitule_abreviation AS auteur_b ".
687
                    "   , eflore_naturaliste_intitule_abreviation AS auteur_mex ".
688
                    "   , eflore_naturaliste_intitule_abreviation AS auteur_m ".
689
                    " ,eflore_selection_nom a, eflore_selection_nom b".
690
                    " WHERE a.esn_id_nom= ".$numNom.
691
                    " AND a.esn_id_version_projet_taxon = 25 ".
692
                    " AND a.esn_id_taxon=b.esn_id_taxon ".
693
                    " AND b.esn_ce_statut=3 ".
694
                    " AND a.esn_id_version_projet_taxon=b.esn_id_version_projet_taxon" .
695
                    " AND en_ce_rang = enrg_id_rang" .
696
                    " AND en_id_nom = b.esn_id_nom" .
697
                    " AND en_ce_auteur_basio_ex = auteur_bex.enaia_id_intitule_naturaliste_abrege ".
698
                    " AND en_ce_auteur_basio = auteur_b.enaia_id_intitule_naturaliste_abrege  ".
699
                    " AND en_ce_auteur_modif_ex = auteur_mex.enaia_id_intitule_naturaliste_abrege ".
700
                    " AND en_ce_auteur_modif = auteur_m.enaia_id_intitule_naturaliste_abrege ".
701
                    " AND a.esn_id_version_projet_taxon=en_id_version_projet_nom ";
480 david 702
 
703
 
540 david 704
                $res =& $DB->query($query);
480 david 705
 
706
 
707
 
540 david 708
                if (DB::isError($res)) {
709
                    die($res->getMessage());
710
                }
480 david 711
 
540 david 712
                // Nom retenu, Num Nomenclatural nom retenu, Num Taxon,
480 david 713
 
714
                $value=array('Nom_Retenu'=>"",'Num_Nom_Retenu'=>"0",'Num_Taxon'=>"0",'Famille'=>"");
715
 
716
                while ($row =& $res->fetchrow(DB_FETCHMODE_ASSOC)) {
540 david 717
                    $fam=$this->rechercherFamille($row['esn_id_taxon'],$DB);
718
 
719
                    // Recherche Famille
720
                    while (($fam['en_ce_rang']!='fin') && ($fam['en_ce_rang'] !=120)) {
480 david 721
                        $fam=$this->rechercherFamille($fam['etr_id_taxon_2'],$DB);
540 david 722
                    }
723
                    if ($fam['en_ce_rang']==120) {
480 david 724
                        $famille=$fam['en_nom_supra_generique'];
540 david 725
                    }
726
                    else {
480 david 727
                        $famille="Famille inconnue";
540 david 728
                    }
729
 
730
                    $value=array('Nom_Retenu'=>$this->formaterNom($row),'Num_Nom_Retenu'=>$row['esn_id_nom'],'Num_Taxon'=>$row['esn_id_taxon'],'Famille'=>$famille);
480 david 731
                }
732
 
540 david 733
                return $value;
480 david 734
 
735
 
736
 
737
        }
738
 
449 david 739
function formaterNom($rawnom) {
480 david 740
 
741
 
449 david 742
                // Constitution du nom:
743
                $nom = '';
445 david 744
 
449 david 745
                if ($rawnom['en_nom_supra_generique'] != '') {
746
                    $nom .= $rawnom['en_nom_supra_generique'];
747
                } else if ($rawnom['en_epithete_infra_generique'] != '') {
748
                    $nom .= $rawnom['en_epithete_infra_generique'];
749
                } else {
750
                        if ($rawnom['en_nom_genre'] != '') {
751
                            $nom .=  $rawnom['en_nom_genre'];
752
                        }
753
                        if ($rawnom['en_epithete_espece']!= '') {
754
                            $nom .= ' '.$rawnom['en_epithete_espece'];
755
                        }
756
                        if ($rawnom['en_epithete_infra_specifique'] != '') {
757
                                if (!empty($rawnom['enrg_abreviation_rang'])) {
758
                                        $nom .= ' '.$rawnom['enrg_abreviation_rang'].'';
759
                                }
760
                                $nom .= ' '.$rawnom['en_epithete_infra_specifique'];
761
                        }
762
 
763
                }
480 david 764
 
765
                return $nom .$this->retournerAuteur($rawnom) ;
766
 
767
 }
768
 
769
 
770
function rechercherFamille($taxon,&$DB) {
771
 
540 david 772
    $row=array();
480 david 773
 
540 david 774
    $query="SELECT DISTINCT en_ce_rang, etr_id_taxon_2, en_id_nom, en_nom_supra_generique ".
480 david 775
        " FROM eflore_taxon_relation, eflore_selection_nom, eflore_nom ".
776
        " WHERE etr_id_taxon_1 = ".$taxon.
777
        " AND etr_id_version_projet_taxon_1 = 25 ".
778
        " AND etr_id_categorie_taxon = 3 ".
779
        " AND etr_id_valeur_taxon = 3 ".
780
        " AND esn_id_taxon =  etr_id_taxon_2 ".
781
        " AND esn_ce_statut = 3 ".
782
        " AND esn_id_version_projet_taxon = etr_id_version_projet_taxon_1 ".
783
        " AND en_id_nom = esn_id_nom ".
784
        " AND esn_id_version_projet_taxon=en_id_version_projet_nom  ";
540 david 785
    $res =& $DB->query($query);
480 david 786
 
540 david 787
    if (DB::isError($res)) {
788
        die($res->getMessage());
789
    }
480 david 790
 
540 david 791
    if ($row =& $res->fetchrow(DB_FETCHMODE_ASSOC)) {
792
        return $row;
480 david 793
    }
794
    else {
795
        $row['en_ce_rang']='fin';
796
        return $row;
797
    }
798
 
445 david 799
}
800
 
480 david 801
 
449 david 802
function retournerAuteur($rawnom) {
445 david 803
 
540 david 804
    $auteurs = '';
805
    $auteur_basio = '';
806
    $auteur_modif = '';
807
    if (!empty($rawnom['abreviation_auteur_basio_ex']) && $rawnom['abreviation_auteur_basio_ex']!='-' )  {
808
        $auteur_basio .= $rawnom['abreviation_auteur_basio_ex'];
809
        if (!empty($rawnom['abreviation_auteur_basio']) && $rawnom['abreviation_auteur_basio']!='-') {
810
            $auteur_basio .= ' ex '.$rawnom['abreviation_auteur_basio'];
811
        }
812
    } else if (!empty($rawnom['abreviation_auteur_basio']) && $rawnom['abreviation_auteur_basio']!='-') {
813
        $auteur_basio .= $rawnom['abreviation_auteur_basio'];
814
    }
445 david 815
 
540 david 816
    if (!empty($rawnom['abreviation_auteur_modif_ex']) && $rawnom['abreviation_auteur_modif_ex']!='-') {
817
        $auteur_modif .= $rawnom['abreviation_auteur_modif_ex'];
818
        if (!empty($rawnom['abreviation_auteur_modif']) && $rawnom['abreviation_auteur_modif']!='-') {
819
            $auteur_modif .= ' ex '.$rawnom['abreviation_auteur_modif'];
820
        }
821
    } else if (!empty($rawnom['abreviation_auteur_modif']) && $rawnom['abreviation_auteur_modif']!='-')  {
822
        $auteur_modif .= $rawnom['abreviation_auteur_modif'];
823
    }
449 david 824
 
540 david 825
    if (!empty($auteur_modif)) {
826
        $auteurs = ' ('.$auteur_basio.') '.$auteur_modif;
827
    } elseif (!empty($auteur_basio)) {
828
        $auteurs = ' '.$auteur_basio;
829
    }
830
 
831
    return $auteurs ;
449 david 832
}
833
 
834
 
835
 
480 david 836
 
449 david 837
}
838
 
839
 
540 david 840
function remove_accent($str)
841
{
842
  $a = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î',
843
             'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß',
844
             'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î',
845
             'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā',
846
             'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď',
847
             'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ',
848
             'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', 'ĭ',
849
             'Į', 'į', 'İ', 'ı', 'IJ', 'ij', 'Ĵ', 'ĵ', 'Ķ', 'ķ', 'Ĺ', 'ĺ', 'Ļ', 'ļ', 'Ľ',
850
             'ľ', 'Ŀ', 'ŀ', 'Ł', 'ł', 'Ń', 'ń', 'Ņ', 'ņ', 'Ň', 'ň', 'ʼn', 'Ō', 'ō', 'Ŏ',
851
             'ŏ', 'Ő', 'ő', 'Œ', 'œ', 'Ŕ', 'ŕ', 'Ŗ', 'ŗ', 'Ř', 'ř', 'Ś', 'ś', 'Ŝ', 'ŝ',
852
             'Ş', 'ş', 'Š', 'š', 'Ţ', 'ţ', 'Ť', 'ť', 'Ŧ', 'ŧ', 'Ũ', 'ũ', 'Ū', 'ū', 'Ŭ',
853
             'ŭ', 'Ů', 'ů', 'Ű', 'ű', 'Ų', 'ų', 'Ŵ', 'ŵ', 'Ŷ', 'ŷ', 'Ÿ', 'Ź', 'ź', 'Ż',
854
             'ż', 'Ž', 'ž', 'ſ', 'ƒ', 'Ơ', 'ơ', 'Ư', 'ư', 'Ǎ', 'ǎ', 'Ǐ', 'ǐ', 'Ǒ', 'ǒ',
855
             'Ǔ', 'ǔ', 'Ǖ', 'ǖ', 'Ǘ', 'ǘ', 'Ǚ', 'ǚ', 'Ǜ', 'ǜ', 'Ǻ', 'ǻ', 'Ǽ', 'ǽ', 'Ǿ', 'ǿ');
856
 
857
  $b = array('A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I',
858
             'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's',
859
             'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i',
860
             'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a',
861
             'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd',
862
             'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g',
863
             'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i',
864
             'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l',
865
             'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R',
866
             'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't',
867
             'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y',
868
             'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I',
869
             'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
870
  return str_replace($a, $b, $str);
871
}
449 david 872
 
540 david 873
 
874
function cp1252_to_utf8($str) {
875
$cp1252_map = array ("\xc2\x80" => "\xe2\x82\xac",
876
"\xc2\x82" => "\xe2\x80\x9a",
877
"\xc2\x83" => "\xc6\x92",
878
"\xc2\x84" => "\xe2\x80\x9e",
879
"\xc2\x85" => "\xe2\x80\xa6",
880
"\xc2\x86" => "\xe2\x80\xa0",
881
"\xc2\x87" => "\xe2\x80\xa1",
882
"\xc2\x88" => "\xcb\x86",
883
"\xc2\x89" => "\xe2\x80\xb0",
884
"\xc2\x8a" => "\xc5\xa0",
885
"\xc2\x8b" => "\xe2\x80\xb9",
886
"\xc2\x8c" => "\xc5\x92",
887
"\xc2\x8e" => "\xc5\xbd",
888
"\xc2\x91" => "\xe2\x80\x98",
889
"\xc2\x92" => "\xe2\x80\x99",
890
"\xc2\x93" => "\xe2\x80\x9c",
891
"\xc2\x94" => "\xe2\x80\x9d",
892
"\xc2\x95" => "\xe2\x80\xa2",
893
"\xc2\x96" => "\xe2\x80\x93",
894
"\xc2\x97" => "\xe2\x80\x94",
895
 
896
"\xc2\x98" => "\xcb\x9c",
897
"\xc2\x99" => "\xe2\x84\xa2",
898
"\xc2\x9a" => "\xc5\xa1",
899
"\xc2\x9b" => "\xe2\x80\xba",
900
"\xc2\x9c" => "\xc5\x93",
901
"\xc2\x9e" => "\xc5\xbe",
902
"\xc2\x9f" => "\xc5\xb8"
903
);
904
return strtr ( utf8_encode ( $str ), $cp1252_map );
905
}
906
 
417 aurelien 907
/* +--Fin du code ---------------------------------------------------------------------------------------+
908
* $Log$
909
*
910
*
911
*/
912
 
913
 
914
?>