Subversion Repositories eFlore/Projets.eflore-projets

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
460 delphine 1
<?php
2
class TaxonDao extends Bdd {
3
	// tableau de la forme $taxons[num][flore][id]
4
	// $taxons[num][flore][nom]
5
	protected $taxons = array();
6
	private $id = 0;
7
	private $flores = array('syntri', 'ciff', 'codefr94', 'bdnff', 'bryo', 'floeur', 'algues', 'characees', 'lichen', 'fournier');
8
	private $table_flores = array('bdnff', 'fournier', 'ciff', 'syntri', 'codefr94',  'bryo', 'floeur');
9
 
10
	public function __construct() {
11
		Bdd::__construct();
12
		$this->id = $this->chargerId() + 1;
13
		$this->taxons = $this->chargerTaxon();
14
	}
15
 
16
	// recherche si un taxon existe sinon le crée et renvoie son id
17
	public function getId($num_taxon, $flore = 'ind', $nom = null, $flore_supp = null, $num_supp = null, $remarques = '') {
18
 
19
		$idTaxon = null; $nom_supp = $nom;
20
		if ($nom == null && $remarques != '') {
21
			$nom = "rem:".$flore_supp.$num_supp.'/'.$remarques;
22
		}
23
 
24
		if (isset($this->taxons['ind'][$nom]['id'])) {
25
			$idTaxon = $this->taxons['ind'][$nom]['id'];
26
		} elseif (isset($this->taxons[$flore][$num_taxon]['id']) && $nom == null) {
27
			$idTaxon = $this->taxons[$flore][$num_taxon]['id'];
28
		} else {
29
			$idTaxon = $this->ajouterTaxon($num_taxon, $flore, $nom_supp, $flore_supp, $num_supp, $remarques);
30
		}
31
		return $idTaxon;
32
	}
33
 
34
	// renvoie un nom d'apres son id
35
	public function getNom($id) {
36
		foreach ($this->taxons['ind'] as $nom=>$param) {
37
			if ($param['id'] === $id) {
38
				if (preg_match('/rem:[a-z]+\d+\/(.*)/', $nom, $match)) {
39
					$nom = $match[1];
40
				}
41
				return $nom;
42
			}
43
		}
44
		return '';
45
	}
46
 
47
	// Ajoute taxon avec pour clé le numéro taxon, le nom ou la remarque
48
	public function ajouterTaxon($num_taxon, $flore, $nom, $flore_supp, $num_supp, $remarques) {
49
		if ($nom != null) {
50
			$cle = 'ind';
51
			$num = $nom;
52
		} elseif ($remarques != '') {
53
			$cle = 'ind';
54
			$num = "rem:".$flore_supp.$num_supp.'/'.$remarques;
55
		} else {
56
			$cle = $flore;
57
			$num = $num_taxon;
58
		}
59
		$this->taxons[$cle][$num]['id'] = $this->id;
60
		foreach ($this->flores as $nom_flore) {
61
			if ($nom_flore == $flore) {
62
				$this->taxons[$cle][$num][$nom_flore] = $num_taxon;
63
			} elseif ($nom_flore == $flore_supp) {
64
				$this->taxons[$cle][$num][$nom_flore] = $num_supp;
65
			} else {
66
				$this->taxons[$cle][$num][$nom_flore] = 'NULL';
67
			}
68
		}
69
		$this->taxons[$cle][$num]['nom'] = $this->ajouterNomTaxon($num_taxon, $flore, $nom, $flore_supp, $num_supp);
70
		$this->taxons[$cle][$num]['remarques'] = $remarques;
71
		$this->id++;
72
		return $this->taxons[$cle][$num]['id'];
73
	}
74
 
75
	public function ajouterNomTaxon($num_taxon, $flore, $nom_supp, $flore_supp, $num_supp) {
76
		$nom = '';
77
		$nomCherche = false;
78
		foreach ($this->table_flores as $nom_flore) {
79
			if ($nomCherche == false) {
80
				if ($nom_flore == $flore) {
81
					$nom = $this->rechercherNomTaxon($nom_flore, $num_taxon);
82
					if ($nom != false) {
83
						$nomCherche = true;
84
					}
85
				} elseif ($nom_flore == $flore_supp) {
86
					$nom = $this->rechercherNomTaxon($nom_flore, $num_supp);
87
					if ($nom != false) {
88
						$nomCherche = true;
89
					}
90
				}
91
			}
92
		}
93
		if ($nom == false && $nom_supp != null) {
94
			$nom = $nom_supp;
95
		} elseif ($nom == '' && $nom_supp != null) {
96
			$nom = $nom_supp;
97
		}
98
		return $nom;
99
	}
100
 
101
	public function rechercherNomTaxon($nom_flore, $num_taxon) {
102
		$requete2 = null;
103
		switch ($nom_flore) {
104
			case 'bdnff' :
105
				$requete = "SELECT sb_nom_complet AS nom FROM sophy_bdnff
106
					WHERE sb_num_tax = {$num_taxon} AND sb_id_num_bdnff = sb_num_nom_retenu; ";
107
				break;
108
			case 'fournier' :
109
				$requete = "SELECT sb_nom_complet AS nom FROM sophy_bdnff, sophy_fournier_bdnff
110
					WHERE sfb_id_num_fournier = {$num_taxon} AND sb_num_tax = sfb_id_num_bdnff AND sb_id_num_bdnff = sb_num_nom_retenu;";
111
				$requete2 = "SELECT sf_nom_fournier AS nom FROM sophy_fournier WHERE sf_id_num_fournier = {$num_taxon};";
112
				break;
113
			case 'ciff' :
114
				$requete = "SELECT sb_nom_complet AS nom
115
					FROM sophy_bdnff, sophy_ciff_bdnff
116
					WHERE scb_id_num_ciff = {$num_taxon} AND scb_id_num_bdnff = sb_id_num_bdnff;";
117
				$requete2 = "SELECT sci_nom_ciff AS nom FROM sophy_ciff WHERE sci_id_num_ciff = {$num_taxon};";
118
				break;
119
			case 'syntri' :
120
				$requete = "SELECT sb_nom_complet AS nom
121
					FROM sophy_bdnff, sophy_syntri_fournier, sophy_fournier_bdnff
122
					WHERE ssf_id_num_syntri = {$num_taxon} AND ssf_id_num_fournier = sfb_id_num_fournier
123
					AND sb_num_tax = sfb_id_num_bdnff AND sb_id_num_bdnff = sb_num_nom_retenu;";
124
				$requete2 = "SELECT ssyn_nom_supp AS nom FROM sophy_syntri WHERE ssyn_id_num_supp = {$num_taxon};";
125
				break;
126
			case 'codefr94' :
127
				$requete = "SELECT sc_nom_codefr AS nom FROM sophy_codefr94 WHERE sc_id_num_codefr = {$num_taxon};";
128
				break;
129
			case 'bryo' :
130
				$requete = "SELECT sbr_nom_bryo AS nom FROM sophy_bryophyte WHERE sbr_id_num_bryo = {$num_taxon};";
131
				break;
132
			case 'floeur' :
133
				$requete = "SELECT sfe_nom_floeur AS nom FROM sophy_flora_europea WHERE sfe_id_num_floeur = {$num_taxon};";
134
				break;
135
		}
136
		$resultat_requete = $this->recuperer($requete);
137
		if ($resultat_requete['nom'] == false && $requete2 != null) {
138
			$resultat_requete = $this->recuperer($requete2);
139
		}
140
		return $resultat_requete['nom'];
141
	}
142
	// recherche le dernier id de la base
143
	public function chargerId() {
144
		$id = 0;
145
		$requete_select_id = "SELECT MAX(st_id_taxon) AS idMax FROM sophy_taxon;";
146
		$resultat_requete_id = $this->recuperer($requete_select_id);
147
		if ($resultat_requete_id['idMax'] != false) {
148
			$id = $resultat_requete_id['idMax'];
149
		}
150
		return $id;
151
	}
152
 
153
	// Regarde si il y a des taxons dans la base, retourne le tableau de valeur et vide la base
154
	public function chargerTaxon() {
155
		$resultat = null;
156
		$retour = null;
157
		$requete_select = "SELECT * FROM sophy_taxon;";
158
		$resultat = $this->recupererTous($requete_select);
159
		if ($resultat != false) {
160
			foreach ($resultat as $result) {
161
				if ($result['st_nom_supp'] != null) {
162
					$retour['ind'][$result['st_nom_supp']]['id'] = $result['st_id_taxon'];
163
					foreach ($this->flores as $nom_flore) {
164
						$retour['ind'][$result['st_nom_supp']][$nom_flore] = $result["st_ce_num_".$nom_flore];
165
					}
166
					$retour['ind'][$result['st_nom_supp']]['remarques'] = $result['st_nom_supp'];
167
				} elseif ($result['st_remarques'] != null) {
168
					$nom = $result['st_remarques'];
169
					$retour['ind'][$nom]['id'] = $result['st_id_taxon'];
170
					foreach ($this->flores as $nom_flore) {
171
						$retour['ind'][$result['st_nom_supp']][$nom_flore] = $result["st_ce_num_".$nom_flore];
172
					}
173
					$retour['ind'][$nom]['remarques'] = $result['st_nom_supp'];
174
				} elseif ($result['st_ce_num_syntri'] != null) {
175
					$retour['syntri'][$result['st_ce_num_syntri']]['id'] = $result['st_id_taxon'];
176
					$retour['syntri'][$result['st_ce_num_syntri']]['num_supp'] = $result['st_num_supp'];
177
				} elseif ($result['st_ce_num_floeur'] != null) {
178
					$retour['floeur'][$result['st_ce_num_floeur']]['id'] = $result['st_id_taxon'];
179
				} elseif ($result['st_ce_num_bdnff'] != null) {
180
					$retour['bdnff'][$result['st_ce_num_bdnff']]['id'] = $result['st_id_taxon'];
181
				} elseif ($result['st_ce_num_codefr94'] != null) {
182
					$retour['codefr94'][$result['st_ce_num_codefr94']]['id'] = $result['st_id_taxon'];
183
				} elseif ($result['st_ce_num_bryo'] != null) {
184
					$retour['bryo'][$result['st_ce_num_bryo']]['id'] = $result['st_id_taxon'];
185
				} elseif ($result['st_ce_num_ciff'] != null) {
186
					$retour['ciff'][$result['st_ce_num_ciff']]['id'] = $result['st_id_taxon'];
187
				}  elseif ($result['st_ce_num_fournier'] != null) {
188
					$retour['fournier'][$result['st_ce_num_fournier']]['id'] = $result['st_id_taxon'];
189
				}
190
			}
191
		}
192
		$requete = "TRUNCATE TABLE `sophy_taxon`; ";
193
		$res = $this->requeter($requete);
194
		return $retour;
195
	}
196
// +-------------------------------------------------------------------------------------------------------------------+
197
// Requête sur table sophy_taxon et toutes les tables flore de sophy
198
	public function integrerTaxons() {
199
		foreach ($this->taxons as $flore=>$taxons) {
200
			switch ($flore) {
201
				case 'ind' :
202
					// insertion par 1000 pour éviter que la requête soit trop lourde
203
					$i = 0; $j = 1000;
204
					$requete = "INSERT INTO `sophy_taxon` (`st_id_taxon`, `st_nom`, `st_nom_supp`,";
205
					foreach ($this->flores as $nom_flore) {
206
						$requete .= "st_ce_num_{$nom_flore}, ";
207
					}
208
					$requete .= " st_remarques) VALUES ";
209
					foreach ($taxons as $taxon=>$valeur) {
210
						if (!isset($valeur['remarques'])){
211
							$valeur['remarques'] = 'NULL';
212
							$taxon = $this->proteger($taxon);
213
						} elseif (substr_compare($taxon, 'rem:', 0, 4) == 0){
214
							$valeur['remarques'] = $this->proteger($taxon);
215
							$taxon = 'NULL';
216
						} else {
217
							$taxon = $this->proteger($taxon);
218
							$valeur['remarques'] = $this->proteger($valeur['remarques']);
219
						}
220
 
221
						if ($i < $j) {
222
							$requete .= " (".$valeur['id'].", ".$this->proteger($valeur['nom']).", ".$taxon.", ";
223
							foreach ($this->flores as $nom_flore) {
224
								$requete .= $valeur[$nom_flore].", ";
225
							}
226
							$requete .= " ".$valeur['remarques']."),";
227
						} elseif ($i == $j) {$j += 1000;
228
							$requete = substr($requete,0,-1).";";
229
							$resultat = $this->requeter($requete);
230
							if ($resultat == false) {
231
								echo $flore.' : '.$i;
232
							}
233
							$requete = "INSERT INTO `sophy_taxon` (`st_id_taxon`, `st_nom`, `st_nom_supp`,";
234
							foreach ($this->flores as $nom_flore) {
235
								$requete .= "st_ce_num_{$nom_flore}, ";
236
							}
237
							$requete .= " st_remarques) VALUES (".$valeur['id'].", ".$this->proteger($valeur['nom']).
238
								", ".$taxon.", ";
239
							foreach ($this->flores as $nom_flore) {
240
								$requete .= $valeur[$nom_flore].", ";
241
							}
242
							$requete .= " ".$valeur['remarques']."),";
243
						}
244
						$i++;
245
					}
246
					$requete = substr($requete,0,-1).";";
247
					break;
248
				case 'syntri' :
249
					$i = 0;
250
					$requete = "INSERT INTO `sophy_taxon` (`st_id_taxon`, ";
251
					foreach ($this->flores as $nom_flore) {
252
						$requete .= "st_ce_num_{$nom_flore}, ";
253
					}
254
					$requete .= "`st_nom`) VALUES ";
255
					foreach ($taxons as $taxon=>$valeur) {
256
						$requete .= " (".$valeur['id'].", ";
257
						foreach ($this->flores as $nom_flore) {
258
								$requete .= $valeur[$nom_flore].", ";
259
						}
260
						$requete .= $this->proteger($valeur['nom'])."), ";
261
					}
262
					$requete = substr($requete,0,-2).";";
263
					break;
264
				default:
265
					$requete = "INSERT INTO `sophy_taxon` (`st_id_taxon`, `st_nom`, st_ce_num_{$flore}) VALUES ";
266
					foreach ($taxons as $numTaxon=>$valeur) {
267
						$requete .= " (".$valeur['id'].", ".$this->proteger($valeur['nom']).", ".$numTaxon."),";
268
					}
269
					$requete = substr($requete,0,-1).";";
270
					break;
271
			}
272
			$resultat = $this->requeter($requete);
273
			if ($resultat == false) {
274
				echo " - flore : ".$flore;
275
			}
276
		}
277
	}
278
}
279
?>