Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 26 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 26 Rev 31
1
<?php
1
<?php
2
 
2
 
3
 
3
 
4
/**
4
/**
5
 * Classe qui va effectuer toutes les operations liees aux referentiels ou rechercher
5
 * Classe qui va effectuer toutes les operations liees aux referentiels ou rechercher
6
 * des informations sur les taxons dans les tables des referentiels dans la base de donnees
6
 * des informations sur les taxons dans les tables des referentiels dans la base de donnees
7
 * 
7
 * 
8
 * Operations a utiliser :
8
 * Operations a utiliser :
9
 *   - recupererListeReferentielsDisponibles (statique) : recherche dans les fichiers de configuration
9
 *   - recupererListeReferentielsDisponibles (statique) : recherche dans les fichiers de configuration
10
 *     la liste des referentiels disponibles et les renvoie dans un tableau
10
 *     la liste des referentiels disponibles et les renvoie dans un tableau
11
 *
11
 *
12
 *   - chargerTaxon : recherche dans la table possedant le meme nom que le nom du referentiel en attribut
12
 *   - chargerTaxon : recherche dans la table possedant le meme nom que le nom du referentiel en attribut
13
 *     les informations generales sur un taxon (numero nomenclatural retenu, numero taxonomique,
13
 *     les informations generales sur un taxon (numero nomenclatural retenu, numero taxonomique,
14
 *     nom scientifique, rang) a partir de son numero taxonomique
14
 *     nom scientifique, rang) a partir de son numero taxonomique
15
 *     On stockera dans un attribut un tableau associatif contenant ces informations + le nom du referentiel,
15
 *     On stockera dans un attribut un tableau associatif contenant ces informations + le nom du referentiel,
16
 *     ou la valeur nulle si aucun taxon n'a ete trouve
16
 *     ou la valeur nulle si aucun taxon n'a ete trouve
17
 *
17
 *
18
 *   - recupererSousTaxons : a partir du numero nomenclatural d'un taxon, la fonction va recuperer
18
 *   - recupererSousTaxons : a partir du numero nomenclatural d'un taxon, la fonction va recuperer
19
 *     tous les taxons de rang inferieur de maniere recursive jusqu'en ne plus en trouver de nouveaux
19
 *     tous les taxons de rang inferieur de maniere recursive jusqu'en ne plus en trouver de nouveaux
20
 *     la recherche s'effectuera seulement au niveau des rangs famille, genre, espece et sous-espece
20
 *     la recherche s'effectuera seulement au niveau des rangs famille, genre, espece et sous-espece
21
 *     pour limiter le nombre d'informations qui sera a la fin renvoye
21
 *     pour limiter le nombre d'informations qui sera a la fin renvoye
22
 *
22
 *
23
 *   - obtenirNumeroNomenclatural : recherche le numero nomenclatural d'un taxon a partir de son nom scientifique
23
 *   - obtenirNumeroNomenclatural : recherche le numero nomenclatural d'un taxon a partir de son nom scientifique
24
 *
24
 *
25
 * @package framework-0.3
25
 * @package framework-0.3
26
 * @author Alexandre GALIBERT <alexandre.galibert@tela-botanica.org>
26
 * @author Alexandre GALIBERT <alexandre.galibert@tela-botanica.org>
27
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
27
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
28
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
28
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
29
 * @version $Id$
29
 * @version $Id$
30
 * @copyright 2013 Tela Botanica (accueil@tela-botanica.org)
30
 * @copyright 2013 Tela Botanica (accueil@tela-botanica.org)
31
 * 
31
 * 
32
 */
32
 */
33
 
-
 
34
define("RANG_FAMILLE", 180);
-
 
35
 
33
 
36
 
34
 
37
 
35
 
38
class Referentiel {
36
class Referentiel {
39
	
37
	
40
	private $nomComplet;
38
	private $nomComplet;
41
	private $nomCourt;
39
	private $nomCourt;
42
	
-
 
43
	private $taxon;
40
	private $taxon;
44
	private $bdd = null;
41
	private $bdd = null;
45
	
-
 
46
	private $nomsChampsBdtfx = array('nn' => 'num_nom_retenu', 'nt' => 'num_taxonomique', 'ns' => 'nom_sci');
42
	private $nomsChampsBdtfx = array('nn' => 'num_nom_retenu', 'nt' => 'num_taxonomique', 'ns' => 'nom_sci');
47
	private $nomsChampsBdtxa = array('nn' => 'num_nom_retenu', 'nt' => 'num_tax', 'ns' => 'nom_sci');
43
	private $nomsChampsBdtxa = array('nn' => 'num_nom_retenu', 'nt' => 'num_tax', 'ns' => 'nom_sci');
48
	private $champs;
44
	private $champs;
49
	
45
	
50
	
-
 
51
	
46
	
52
	public function __construct($nomReferentiel, $taxon = null) {
47
	public function __construct($nomReferentiel, $taxon = null) {
53
		$this->nomComplet = $nomReferentiel;
48
		$this->nomComplet = $nomReferentiel;
54
		$this->nomCourt = current(explode('_', $nomReferentiel));
49
		$this->nomCourt = current(explode('_', $nomReferentiel));
55
		$proprieteChamps = 'nomsChamps' . ucfirst($this->nomCourt);
50
		$proprieteChamps = 'nomsChamps' . ucfirst($this->nomCourt);
56
		foreach ($this->$proprieteChamps as $nomCourt => $nomChamp) {
51
		foreach ($this->$proprieteChamps as $nomCourt => $nomChamp) {
57
			$this->champs[$nomCourt] = $nomChamp;
52
			$this->champs[$nomCourt] = $nomChamp;
58
		}
53
		}
59
		$this->taxon = $taxon;
54
		$this->taxon = $taxon;
60
	}
55
	}
61
	
-
 
62
	
56
		
63
	
-
 
64
	public function renvoyerNomCompletReferentiel() {
57
	public function renvoyerNomCompletReferentiel() {
65
		return $this->nomComplet;
58
		return $this->nomComplet;
66
	}
59
	}
67
	
60
	
68
	public function renvoyerTaxon() {
61
	public function renvoyerTaxon() {
69
		return $this->taxon;
62
		return $this->taxon;
70
	}
63
	}
71
	
64
	
72
	public function chargerTaxon($numTaxon) {
65
	public function chargerTaxon($typeNumero, $numTaxon) {
73
		$taxon = null;
66
		$taxon = null;
74
		// verifier que le numero de taxon soit bien une chaine de caracteres composee uniquement
67
		// verifier que le numero de taxon soit bien une chaine de caracteres composee uniquement
75
		// que de nombres entiers avant de construire la requete SQL a executer
68
		// que de nombres entiers avant de construire la requete SQL a executer
76
		if (preg_match('/^\d+$/', $numTaxon) == 1) {
69
		if (preg_match('/^\d+$/', $numTaxon) == 1) {
-
 
70
			$champWhere = $typeNumero == 'nt' ? $this->champs['nt']: 'num_nom';
77
			$requete = 
71
			$requete = 
78
				"SELECT ".$this->champs['nn']." AS nn, ".$this->champs['nt']." AS nt, ".
72
				"SELECT ".$this->champs['nn']." AS nn, ".$this->champs['nt']." AS nt, ".
79
				$this->champs['ns']." AS nom, rang FROM ".$this->nomComplet." WHERE ".
73
				$this->champs['ns']." AS nom, rang FROM ".$this->nomComplet." WHERE ".
80
				$this->champs['nt']."={$numTaxon} AND num_nom=".$this->champs['nn']." ".
74
				$champWhere."={$numTaxon} AND num_nom=".$this->champs['nn']." ".
81
				"ORDER BY rang, If(num_nom=".$this->champs['nn'].", 0, 1) LIMIT 0, 1";
75
				"ORDER BY rang, If(num_nom=".$this->champs['nn'].", 0, 1) LIMIT 0, 1";
82
			$taxon = $this->getBdd()->recuperer($requete);
76
			$taxon = $this->getBdd()->recuperer($requete);
83
			if ($taxon == false) {
77
			if ($taxon == false) {
84
				$taxon = null;
78
				$taxon = null;
85
			} else {
79
			} else {
86
				$taxon['referentiel'] = $this->nomComplet;
80
				$taxon['referentiel'] = $this->nomComplet;
87
			}
81
			}
88
		}
82
		}
89
		$this->taxon = $taxon;
83
		$this->taxon = $taxon;
90
	}
84
	}
-
 
85
	
-
 
86
	private function getBdd() {
-
 
87
		if (is_null($this->bdd)) {
-
 
88
			$this->bdd = new Bdd();
-
 
89
		}
-
 
90
		$nomBdd = Config::get('bdd_eflore');
-
 
91
		if (is_null($nomBdd)) {
-
 
92
			$nomBdd = Config::get('bdd_nom');
-
 
93
		}
-
 
94
		$this->bdd->requeter("USE ".$nomBdd);
-
 
95
		return $this->bdd;
91
	
96
	}
92
	
97
	
93
	public function recupererSousTaxons($listeNn = null) {
98
	public function recupererTaxonsSousEspeces($listeNn = null) {
94
		$sousTaxons = array();
99
		$sousTaxons = array();
95
		if (!is_null($this->taxon) && $this->taxon['rang'] >= RANG_FAMILLE) {
100
		if (!is_null($this->taxon) && $this->taxon['rang'] == Config::get('rang.espece')) {
96
			if (is_null($listeNn)) {
101
			if (is_null($listeNn)) {
97
				$listeNn = $this->taxon['nn'];
102
				$listeNn = $this->taxon['nn'];
98
			}
103
			}
99
			$requete =
104
			$requete =
100
				"SELECT ".$this->champs['nn']." AS nn, ".$this->champs['nt']." AS nt, ".
105
				"SELECT ".$this->champs['nn']." AS nn, ".$this->champs['nt']." AS nt, ".
101
				$this->champs['ns']." AS nom, rang FROM ".$this->nomComplet." WHERE ".
106
				$this->champs['ns']." AS nom, rang FROM ".$this->nomComplet." WHERE ".
102
				"num_tax_sup IN ({$listeNn}) AND num_nom=".$this->champs['nn'];
107
				"num_tax_sup IN ({$listeNn}) AND num_nom=".$this->champs['nn'];
103
			$resultats = $this->getBdd()->recupererTous($requete);
108
			$resultats = $this->getBdd()->recupererTous($requete);
104
			
109
			
105
			$nouvelleListeNn = '';
110
			$nouvelleListeNn = '';
106
			foreach ($resultats as $sousTaxon) {
111
			foreach ($resultats as $sousTaxon) {
107
				$sousTaxons[] = $sousTaxon;
112
				$sousTaxons[] = $sousTaxon;
108
				$nouvelleListeNn .= (strlen($nouvelleListeNn) == 0 ? '' : ',') . $sousTaxon['nn'];
113
				$nouvelleListeNn .= (strlen($nouvelleListeNn) == 0 ? '' : ',') . $sousTaxon['nn'];
109
			}
114
			}
110
			// recherche de sous taxons au niveau inferieur (parcours recursif de la methode)
115
			// recherche de sous taxons au niveau inferieur (parcours recursif de la methode)
111
			if (count($resultats) > 0) {
116
			if (count($resultats) > 0) {
112
				$sousTaxons = array_merge($sousTaxons, $this->recupererSousTaxons($nouvelleListeNn));
117
				$sousTaxons = array_merge($sousTaxons, $this->recupererTaxonsSousEspeces($nouvelleListeNn));
113
			}
118
			}
114
		}
119
		}
115
		
-
 
116
		return $sousTaxons;
120
		return $sousTaxons;
117
	}
121
	}
118
	
-
 
119
	
122
	
120
	private function getBdd() {
123
	public function recupererTaxonsFamilles() {
121
		if (is_null($this->bdd)) {
124
		$sousTaxons = array();
122
			$this->bdd = new Bdd();
125
		if (!is_null($this->taxon) && $this->taxon['rang'] == Config::get('rang.genre')) {
-
 
126
			$requete =
123
		}
127
				"SELECT ".$this->champs['nn']." AS nn, ".$this->champs['nt']." AS nt, ".
124
		$nomBdd = Config::get('bdd_eflore');
128
				$this->champs['ns']." AS nom, rang FROM ".$this->nomComplet." WHERE ".
125
		if (is_null($nomBdd)) {
129
				"num_tax_sup=".$this->taxon['nn']." AND num_nom=".$this->champs['nn'];
126
			$nomBdd = Config::get('bdd_nom');
130
			$sousTaxons = $this->getBdd()->recupererTous($requete);
127
		}
-
 
128
		$this->bdd->requeter("USE ".$nomBdd);
131
		}
129
		return $this->bdd;
132
		return $sousTaxons;
130
	}
-
 
131
	
-
 
132
	
133
	}
133
	
134
	
134
	public static function recupererListeReferentielsDisponibles() {
135
	public static function recupererListeReferentielsDisponibles() {
135
		$tableau = array();
136
		$tableau = array();
136
		$tableauPartiel = explode(',', Config::get('referentiels_dispo'));
137
		$tableauPartiel = explode(',', Config::get('referentiels_dispo'));
137
		$tableauPartiel = array_map('trim', $tableauPartiel);
138
		$tableauPartiel = array_map('trim', $tableauPartiel);
138
		foreach ($tableauPartiel as $champ) {
139
		foreach ($tableauPartiel as $champ) {
139
			if (strpos($champ, '=') === false) {
140
			if (strpos($champ, '=') === false) {
140
				$tableau[] = $champ;
141
				$tableau[] = $champ;
141
			} else {
142
			} else {
142
				list($cle, $val) = explode('=', $champ);
143
				list($cle, $val) = explode('=', $champ);
143
				$clePropre = trim($cle);
144
				$clePropre = trim($cle);
144
				$valeurPropre = trim($val);
145
				$valeurPropre = trim($val);
145
				$tableau[$clePropre] = $valeurPropre;
146
				$tableau[$clePropre] = $valeurPropre;
146
			}
147
			}
147
		}
148
		}
148
		return $tableau;
149
		return $tableau;
149
	}
150
	}
150
	
151
	
151
	public function obtenirNumeroNomenclatural($nomScientifique) {
152
	public function obtenirNumeroNomenclatural($nomScientifique) {
152
		$aProteger = $this->getBdd()->proteger(trim($nomScientifique) . '%');
153
		$aProteger = $this->getBdd()->proteger(trim($nomScientifique) . '%');
153
		$requete = "SELECT ".$this->champs['nn']." AS nn FROM ".$this->nomComplet." ".
154
		$requete = "SELECT ".$this->champs['nn']." AS nn FROM ".$this->nomComplet." ".
154
			"WHERE ".$this->champs['ns']." LIKE ".$aProteger;
155
			"WHERE ".$this->champs['ns']." LIKE ".$aProteger;
155
		$taxon = $this->getBdd()->recuperer($requete);
156
		$taxon = $this->getBdd()->recuperer($requete);
156
		if ($taxon == false) {
157
		if ($taxon == false) {
157
			$taxon = null;
158
			$taxon = null;
158
		} else {
159
		} else {
159
			$taxon['referentiel'] = $this->nomComplet;
160
			$taxon['referentiel'] = $this->nomComplet;
160
		}
161
		}
161
		return $taxon;
162
		return $taxon;
162
	}
163
	}
163
	
164
	
164
}
165
}
165
?>
166
?>