Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 31 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 31 Rev 34
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
 
33
 
34
 
34
 
35
 
35
 
36
class Referentiel {
36
class Referentiel {
37
	
37
	
38
	private $nomComplet;
38
	private $nomComplet;
39
	private $nomCourt;
39
	private $nomCourt;
40
	private $taxon;
40
	private $taxon;
41
	private $bdd = null;
41
	private $bdd = null;
-
 
42
	
42
	private $nomsChampsBdtfx = array('nn' => 'num_nom_retenu', 'nt' => 'num_taxonomique', 'ns' => 'nom_sci');
43
	private $nomsChampsBdtfx = array('nn' => 'num_nom_retenu', 'nt' => 'num_taxonomique', 'ns' => 'nom_sci');
43
	private $nomsChampsBdtxa = array('nn' => 'num_nom_retenu', 'nt' => 'num_tax', 'ns' => 'nom_sci');
44
	private $nomsChampsBdtxa = array('nn' => 'num_nom_retenu', 'nt' => 'num_tax', 'ns' => 'nom_sci');
44
	private $champs;
45
	private $champs;
45
	
46
	
46
	
47
	
47
	public function __construct($nomReferentiel, $taxon = null) {
48
	public function __construct($nomReferentiel, $taxon = null) {
48
		$this->nomComplet = $nomReferentiel;
49
		$this->nomComplet = $nomReferentiel;
49
		$this->nomCourt = current(explode('_', $nomReferentiel));
50
		$this->nomCourt = current(explode('_', $nomReferentiel));
50
		$proprieteChamps = 'nomsChamps' . ucfirst($this->nomCourt);
51
		$proprieteChamps = 'nomsChamps' . ucfirst($this->nomCourt);
51
		foreach ($this->$proprieteChamps as $nomCourt => $nomChamp) {
52
		foreach ($this->$proprieteChamps as $nomCourt => $nomChamp) {
52
			$this->champs[$nomCourt] = $nomChamp;
53
			$this->champs[$nomCourt] = $nomChamp;
53
		}
54
		}
54
		$this->taxon = $taxon;
55
		$this->taxon = $taxon;
55
	}
56
	}
56
		
57
		
57
	public function renvoyerNomCompletReferentiel() {
58
	public function renvoyerNomCompletReferentiel() {
58
		return $this->nomComplet;
59
		return $this->nomComplet;
59
	}
60
	}
60
	
61
	
61
	public function renvoyerTaxon() {
62
	public function renvoyerTaxon() {
62
		return $this->taxon;
63
		return $this->taxon;
63
	}
64
	}
64
	
65
	
65
	public function chargerTaxon($typeNumero, $numTaxon) {
66
	public function chargerTaxon($typeNumero, $numTaxon) {
66
		$taxon = null;
67
		$taxon = null;
67
		// verifier que le numero de taxon soit bien une chaine de caracteres composee uniquement
68
		// verifier que le numero de taxon soit bien une chaine de caracteres composee uniquement
68
		// que de nombres entiers avant de construire la requete SQL a executer
69
		// que de nombres entiers avant de construire la requete SQL a executer
69
		if (preg_match('/^\d+$/', $numTaxon) == 1) {
70
		if (preg_match('/^\d+$/', $numTaxon) == 1) {
70
			$champWhere = $typeNumero == 'nt' ? $this->champs['nt']: 'num_nom';
71
			$champWhere = $typeNumero == 'nt' ? $this->champs['nt']: 'num_nom';
71
			$requete = 
72
			$requete = 
72
				"SELECT ".$this->champs['nn']." AS nn, ".$this->champs['nt']." AS nt, ".
73
				"SELECT ".$this->champs['nn']." AS nn, ".$this->champs['nt']." AS nt, ".
73
				$this->champs['ns']." AS nom, rang FROM ".$this->nomComplet." WHERE ".
74
				$this->champs['ns']." AS nom, rang FROM ".$this->nomComplet." WHERE ".
74
				$champWhere."={$numTaxon} AND num_nom=".$this->champs['nn']." ".
75
				$champWhere."={$numTaxon} AND num_nom=".$this->champs['nn']." ".
75
				"ORDER BY rang, If(num_nom=".$this->champs['nn'].", 0, 1) LIMIT 0, 1";
76
				"ORDER BY rang, If(num_nom=".$this->champs['nn'].", 0, 1) LIMIT 0, 1";
76
			$taxon = $this->getBdd()->recuperer($requete);
77
			$taxon = $this->getBdd()->recuperer($requete);
77
			if ($taxon == false) {
78
			if ($taxon == false) {
78
				$taxon = null;
79
				$taxon = null;
79
			} else {
80
			} else {
80
				$taxon['referentiel'] = $this->nomComplet;
81
				$taxon['referentiel'] = $this->nomComplet;
81
			}
82
			}
82
		}
83
		}
83
		$this->taxon = $taxon;
84
		$this->taxon = $taxon;
84
	}
85
	}
85
	
86
	
86
	private function getBdd() {
87
	private function getBdd() {
87
		if (is_null($this->bdd)) {
88
		if (is_null($this->bdd)) {
88
			$this->bdd = new Bdd();
89
			$this->bdd = new Bdd();
89
		}
90
		}
90
		$nomBdd = Config::get('bdd_eflore');
-
 
91
		if (is_null($nomBdd)) {
-
 
92
			$nomBdd = Config::get('bdd_nom');
91
		$nomBdd = Config::get('bdd_nom');
93
		}
-
 
94
		$this->bdd->requeter("USE ".$nomBdd);
92
		$this->bdd->requeter("USE ".$nomBdd);
95
		return $this->bdd;
93
		return $this->bdd;
96
	}
94
	}
97
	
95
	
98
	public function recupererTaxonsSousEspeces($listeNn = null) {
96
	public function recupererSynonymesEtSousEspeces() {
99
		$sousTaxons = array();
97
		$sousTaxons = array();
100
		if (!is_null($this->taxon) && $this->taxon['rang'] == Config::get('rang.espece')) {
-
 
101
			if (is_null($listeNn)) {
-
 
102
				$listeNn = $this->taxon['nn'];
-
 
103
			}
98
		if (!is_null($this->taxon) && $this->taxon['rang'] >= Config::get('rang.espece')) {
104
			$requete =
99
			$requete =
105
				"SELECT ".$this->champs['nn']." AS nn, ".$this->champs['nt']." AS nt, ".
100
			"SELECT ".$this->champs['nn']." AS nn, ".$this->champs['nt']." AS nt, ".
106
				$this->champs['ns']." AS nom, rang FROM ".$this->nomComplet." WHERE ".
101
			$this->champs['ns']." AS nom, rang FROM ".$this->nomComplet. " WHERE ".
107
				"num_tax_sup IN ({$listeNn}) AND num_nom=".$this->champs['nn'];
102
			$this->champs['nt']."=".$this->taxon['nt']." OR hierarchie LIKE '%-".$this->champs['nn']."-%'";
108
			$resultats = $this->getBdd()->recupererTous($requete);
-
 
109
			
-
 
110
			$nouvelleListeNn = '';
-
 
111
			foreach ($resultats as $sousTaxon) {
-
 
112
				$sousTaxons[] = $sousTaxon;
-
 
113
				$nouvelleListeNn .= (strlen($nouvelleListeNn) == 0 ? '' : ',') . $sousTaxon['nn'];
-
 
114
			}
-
 
115
			// recherche de sous taxons au niveau inferieur (parcours recursif de la methode)
-
 
116
			if (count($resultats) > 0) {
-
 
117
				$sousTaxons = array_merge($sousTaxons, $this->recupererTaxonsSousEspeces($nouvelleListeNn));
-
 
118
			}
103
			$sousTaxons = $this->getBdd()->recupererTous($requete);
119
		}
104
		}
120
		return $sousTaxons;
105
		return $sousTaxons;
121
	}
106
	}
122
	
107
	
123
	public function recupererTaxonsFamilles() {
108
	public function recupererGenres() {
124
		$sousTaxons = array();
109
		$sousTaxons = array();
125
		if (!is_null($this->taxon) && $this->taxon['rang'] == Config::get('rang.genre')) {
110
		if (!is_null($this->taxon) && $this->taxon['rang'] == Config::get('rang.famille')) {
126
			$requete =
111
			$requete =
127
				"SELECT ".$this->champs['nn']." AS nn, ".$this->champs['nt']." AS nt, ".
112
			"SELECT ".$this->champs['nn']." AS nn, ".$this->champs['nt']." AS nt, ".
128
				$this->champs['ns']." AS nom, rang FROM ".$this->nomComplet." WHERE ".
113
			$this->champs['ns']." AS nom, rang FROM ".$this->nomComplet. " WHERE ".
129
				"num_tax_sup=".$this->taxon['nn']." AND num_nom=".$this->champs['nn'];
114
			"num_tax_sup=".$this->taxon['nn']." AND num_nom=".$this->champs['nn'];
130
			$sousTaxons = $this->getBdd()->recupererTous($requete);
115
			$sousTaxons = $this->getBdd()->recupererTous($requete);
131
		}
116
		}
132
		return $sousTaxons;
117
		return $sousTaxons;
133
	}
118
	}
134
	
119
	
135
	public static function recupererListeReferentielsDisponibles() {
120
	public static function recupererListeReferentielsDisponibles() {
136
		$tableau = array();
121
		$tableau = array();
137
		$tableauPartiel = explode(',', Config::get('referentiels_dispo'));
122
		$tableauPartiel = explode(',', Config::get('referentiels_dispo'));
138
		$tableauPartiel = array_map('trim', $tableauPartiel);
123
		$tableauPartiel = array_map('trim', $tableauPartiel);
139
		foreach ($tableauPartiel as $champ) {
124
		foreach ($tableauPartiel as $champ) {
140
			if (strpos($champ, '=') === false) {
125
			if (strpos($champ, '=') === false) {
141
				$tableau[] = $champ;
126
				$tableau[] = $champ;
142
			} else {
127
			} else {
143
				list($cle, $val) = explode('=', $champ);
128
				list($cle, $val) = explode('=', $champ);
144
				$clePropre = trim($cle);
129
				$clePropre = trim($cle);
145
				$valeurPropre = trim($val);
130
				$valeurPropre = trim($val);
146
				$tableau[$clePropre] = $valeurPropre;
131
				$tableau[$clePropre] = $valeurPropre;
147
			}
132
			}
148
		}
133
		}
149
		return $tableau;
134
		return $tableau;
150
	}
135
	}
151
	
136
	
152
	public function obtenirNumeroNomenclatural($nomScientifique) {
137
	public function obtenirNumeroNomenclatural($nomScientifique, $numeroNomenclatural = null) {
153
		$aProteger = $this->getBdd()->proteger(trim($nomScientifique) . '%');
138
		$aProteger = $this->getBdd()->proteger(trim($nomScientifique) . '%');
154
		$requete = "SELECT ".$this->champs['nn']." AS nn FROM ".$this->nomComplet." ".
139
		$requete = "SELECT ".$this->champs['nn']." AS nn FROM ".$this->nomComplet." ".
155
			"WHERE ".$this->champs['ns']." LIKE ".$aProteger;
140
			"WHERE ".$this->champs['ns']." LIKE ".$aProteger;
-
 
141
		if (!is_null($numeroNomenclatural) || strlen($numeroNomenclatural) > 0) {
-
 
142
			$requete .= " OR ".$this->champs['nn']."={$numeroNomenclatural}";
-
 
143
		}
156
		$taxon = $this->getBdd()->recuperer($requete);
144
		$taxon = $this->getBdd()->recuperer($requete);
157
		if ($taxon == false) {
145
		if ($taxon == false) {
158
			$taxon = null;
146
			$taxon = null;
159
		} else {
147
		} else {
160
			$taxon['referentiel'] = $this->nomComplet;
148
			$taxon['referentiel'] = $this->nomComplet;
161
		}
149
		}
162
		return $taxon;
150
		return $taxon;
163
	}
151
	}
164
	
152
	
165
}
153
}
-
 
154
 
166
?>
155
?>
167
156