Subversion Repositories eFlore/Projets.eflore-projets

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
314 delphine 1
<?php
315 delphine 2
class GeneralFormateur implements Formateur {
314 delphine 3
	public $dest_map = array();
4
	public $img = array();
315 delphine 5
	public function __construct($info) {
314 delphine 6
		$this->info = $info;
7
	}
8
	public function img() {
320 aurelien 9
		$qualite = 9;
795 raphael 10
		imagepng($this->img['general'], Config::get('cache_stockageChemin').$this->dest_map['general'], $qualite);
314 delphine 11
	}
12
	//+----------------------------------------------------------------------------------------------------------------+
13
	// Méthodes d'accès aux objets du Framework
14
	/**
15
	* Méthode de connection à la base de données sur demande.
16
	* Tous les services web n'ont pas besoin de s'y connecter.
17
	*/
18
	protected function getBdd() {
694 isa 19
		if (! isset($this->Bdd)) {
20
			$this->Bdd = new Bdd();
21
		}
22
		return $this->Bdd;
314 delphine 23
	}
24
 
25
	public function definirCouleurs() {
26
		// green
27
		$couleur['green'] = imagecolorallocate($this->img['general'], 0, 255, 0);
28
		// red
29
		$couleur['red'] = imagecolorallocate($this->img['general'], 255, 0, 0);
30
		// blue
31
		$couleur['blue'] = imagecolorallocate($this->img['general'], 0, 0, 255);
32
		// purple
33
		$couleur['purple'] = imagecolorallocate($this->img['general'], 255, 0, 255);
34
		// black
35
		$couleur['black'] = imagecolorallocate($this->img['general'], 0, 0, 0);
36
		return $couleur;
37
	}
38
 
39
	public function initialiserImage() {
40
		// Nom du fichier image en sortie
41
		$this->cheminCartesBase = Config::get('Cartes.chemin');
42
		$this->dest_map['general'] = 'nt'.$this->info['nt'].'_'.$this->info['src_map'];
320 aurelien 43
		$this->img['general'] = imagecreatefrompng($this->cheminCartesBase.$this->info['src_map']);
314 delphine 44
	}
45
 
362 delphine 46
	public function testerParametresProjets() {
47
		$test = true;
48
		return $test;
49
	}
314 delphine 50
 
51
	public function chargerDonnees() {
52
		// Recherche nom correspondant au numero nomenclatural en cours (pour etablir le lien avec les donnees moissonnes).
53
 
320 aurelien 54
		// Récuperation donnée inventaire
314 delphine 55
		/*
56
		* Les donnees issues du carnet en ligne sont recuperes directement, sans passer par le mecanisme de moissonage car :
57
		*   - pour l'instant le service de moissonage n'est pas automatise et donc il y a un decalage dans l'affichage des donnees transmises
58
		*   - la table contenant les donnees moissonees ne reprend pas tous les champs necessaires a l'exploitation par eflore (notamment pas de code localite, pas d'identifiant libre)
59
		*
60
		* A terme, utilser vraiment le moissonage, y compris pour les donnees issues du CEL, en utilisant ABCD et en modifiant le programme d'harvesting.
61
		*/
362 delphine 62
		if ($this->info['nt'] == 0) {
63
			$inventoriesCel = array();
64
		} else {
542 delphine 65
			$queryCel = "SELECT 'cel' as collection_code,  zone_geo as location, REPLACE(ce_zone_geo,'INSEE-C:','') as id_location, date_observation,  ".
694 isa 66
				"	longitude as y_utm, latitude as x_utm, geodatum as sector, courriel_utilisateur as identifiant FROM tb_cel.cel_obs ".
67
				"WHERE nt = '".$this->info['nt']."' ".
68
				"	AND transmission = 1";
362 delphine 69
			$inventoriesCel = $this->getBdd()->recupererTous($queryCel);
70
		}
314 delphine 71
 
72
		// Recuperation des donnees de moissonage
73
		// Collection code = 'flore' (Cenlr)
362 delphine 74
		if ($this->info['nom'] == '') {
75
			$inventoriesCenlr = array();
76
		} else {
77
			$queryCenlr = "SELECT collection_code, locality as location, county as id_location, ".
694 isa 78
				"	STR_TO_DATE(concat(year, '/',month, '/',day), '%Y/%m/%d') as date_observation, latitude as y_utm , ".
79
		    	"	longitude as x_utm , max_altitude as sector,  collector_name as identifiant ".
80
				"FROM tb_hit_indexation.raw_occurrence_record ".
81
				"WHERE scientific_name = ".$this->getBdd()->proteger($this->info['nom']).
82
				"	AND collection_code = 'flore' ".
83
				"	AND (deleted IS NULL OR deleted = '0000-00-00 00:00:00')";
362 delphine 84
			$inventoriesCenlr = $this->getBdd()->recupererTous($queryCenlr);
85
		}
314 delphine 86
 
87
		// Collection code = 'FLORE - VAR' (Cbnmed)
88
		/*$queryCbnmed = "SELECT collection_code, locality as location, county as id_location, ".
89
							"	STR_TO_DATE(concat(ifnull(year,0),'/', ifnull(month,0), '/',ifnull(day,0)), '%Y/%m/%d') as date_observation, ".
90
							"	latitude as y_utm , longitude as x_utm , max_altitude as sector, collector_name as identifiant ".
91
							"FROM tb_hit_indexation.raw_occurrence_record ".
92
							"WHERE scientific_name = ".$this->getBdd()->proteger($this->info['nom']).
93
							"	AND collection_code='FLORE - VAR' ".
94
							"	AND  (deleted IS NULL OR deleted = '0000-00-00 00:00:00')";
95
		$inventoriesCbnmed = $this->getBdd()->recupererTous($queryCbnmed);*/
96
 
97
		// Collection code = 'sophy'
362 delphine 98
		if ($this->info['nom_ss_auteur'] == '') {
99
			$inventoriesSophy = array();
100
		} else {
101
			$querySophy = "SELECT 'sophy' as collection_code, lieu_station_nom as location, lieu_commune_code_insee as id_location, ".
694 isa 102
				"	observation_date as date_observation, lieu_station_utm_est as x_utm , ".
103
		    	"	lieu_station_utm_nord as y_utm , lieu_station_utm_zone as sector,  observateur_nom_complet as identifiant ".
104
				"FROM sophy_tapir ".
105
				"WHERE nom_scientifique_complet = ".$this->getBdd()->proteger($this->info['nom_ss_auteur']);
362 delphine 106
			$inventoriesSophy = $this->getBdd()->recupererTous($querySophy);
107
		}
314 delphine 108
 
109
		return array_merge($inventoriesCel, $inventoriesCenlr, $inventoriesSophy); //$inventoriesCbnmed,
110
	}
111
 
112
 
113
	public function dessinerPoint($text, $merge, $couleurs) {
114
		$usemap['general'] = '';
115
 
116
		foreach ($text as $coord => $origines ) {
117
			foreach ($origines as $origine => $maptext ) {
118
				$maptext = preg_replace("/\"/", "\'", $maptext);
119
 
120
				list($x,$y) = explode('|', $coord);
121
 
122
				$tpl_area = '<area shape="%s" alt="" class="tooltip" coords="%s" title="%s"/>';
123
				$rayon = 2;
124
				$type = 'circle';
125
				$coords = "$x,$y,5";
126
				$on_mouseover = "this.ttBgColor='#99C242';this.ttFontColor='#000000';this.T_OFFSETX=-200;this.T_OFFSETY=-50;this.T_STICKY=1;return escape('$maptext')";
127
 
128
				if (isset($merge[$coord]) && $merge[$coord]) {
129
					imagefilledrectangle($this->img['general'], ($x - $rayon), ($y - $rayon), ($x + $rayon), ($y + $rayon), $couleurs['black']);
130
				} elseif (isset ($text[$coord]['cel']) && ($text[$coord]['cel'])) {
131
					imagefilledrectangle($this->img['general'], ($x - $rayon), ($y - $rayon), ($x + $rayon), ($y + $rayon), $couleurs['green']);
132
				} elseif (isset ($text[$coord]['flore']) && ($text[$coord]['flore'])) {
133
					imagefilledrectangle($this->img['general'], ($x - $rayon), ($y - $rayon), ($x + $rayon), ($y + $rayon), $couleurs['red']);
134
				}/* elseif (isset ($text[$coord]['FLORE - VAR']) && ($text[$coord]['FLORE - VAR'])) {
135
					imagefilledrectangle($this->img['general'], ($x - $rayon), ($y - $rayon), ($x + $rayon), ($y + $rayon), $couleurs['blue']);
136
				}*/ elseif (isset ($text[$coord]['sophy']) && ($text[$coord]['sophy'])) {
137
					imagefilledrectangle($this->img['general'], ($x - $rayon), ($y - $rayon), ($x + $rayon), ($y + $rayon), $couleurs['purple']);
138
				}
139
				$usemap['general'] = $usemap['general'].sprintf($tpl_area, $type, $coords, $maptext);
140
			}
141
		}
142
		return $usemap;
143
	}
144
 
145
	public function formaterCartes($usemap) {
315 delphine 146
		$retour = Config::get('Cartes.cel_dst').$this->dest_map['general'];
147
		if ($this->info['retour'] == self::MIME_MAP) {
148
			$retour =  "<img src=\"".$retour."\" style=\"border:none; ".
149
				"cursor:crosshair\" alt=\"\" usemap=\"#themap\" /><br />\n".
150
				"<map name=\"themap\" id=\"themap\">".utf8_encode($usemap['general'])."</map>";
314 delphine 151
		}
152
		return $retour;
153
	}
154
 
155
	//+----------------------------------------------------------------------------------------------------------------+
156
	// sous fonction de calculer répartition
157
	public function chercherVille($inventory, $tab_code_insee = array()) {
158
		$location_protege = $this->getBdd()->proteger($inventory['location']);
159
		$id_location_protege = '"'.$inventory['id_location'].'"';
160
		$utm = array();
161
		if ($inventory['collection_code'] == 'FLORE - VAR') {
162
			if (isset($tab_code_insee[$location_protege])) {
163
				$utm = array(0 => $tab_code_insee[$location_protege]);
164
			}
165
		} elseif ($inventory['collection_code'] == 'sophy') {
166
			if (strpos($inventory['id_location'], '20') === 0) {
167
				$a = str_replace('20', '2A', $inventory['id_location']);
168
				if (isset($tab_code_insee[$a])) {
169
					$utm = array(0 => $tab_code_insee[$a]);
170
				} else {
171
					$b = str_replace('20', '2B', $inventory['id_location']);
172
					if (isset($tab_code_insee[$b])) {
173
						$utm = array(0 => $tab_code_insee[$b]);
174
					}
175
				}
176
			} elseif (isset($tab_code_insee[$inventory['id_location']])) {
177
				$utm = array(0 => $tab_code_insee[$inventory['id_location']]);
178
			} elseif (strpos($inventory['id_location'], '000') === 2) {
179
				$code = $this->getBdd()->proteger(str_replace('000', '', $inventory['id_location']));
180
				$requete = "SELECT chef_lieu ".
181
							"FROM tb_eflore.insee_d_v2011 ".
182
							"WHERE dep = $code ";
183
				$resultat = $this->getBdd()->recupererTous($requete);
184
				if ($resultat != array() && isset($tab_code_insee[$resultat[0]['chef_lieu']])) {
185
					$utm = $tab_code_insee[$resultat[0]['chef_lieu']];
186
				}
187
			}
188
		} else {
189
			if ($inventory['id_location'] != 'null') {
190
				if (isset($tab_code_insee[$id_location_protege])) {
191
					$utm = array(0 => $tab_code_insee[$id_location_protege]);
192
				} else {
542 delphine 193
					$requete = "SELECT nom as name, code as insee_code, utm_x as x_utm, utm_y as y_utm, utm_secteur as sector ".
194
										"FROM tb_cel.cel_zones_geo ".
195
										"WHERE nom LIKE $location_protege ".
314 delphine 196
										"	AND code = $id_location_protege ";
197
					$resultat = $this->getBdd()->recupererTous($requete);
542 delphine 198
					if ($resultat != false) {
199
						$utm = $resultat;
200
					}
314 delphine 201
				}
202
			} else {
542 delphine 203
				$requete = "SELECT nom as name, code as insee_code, utm_x as x_utm, utm_y as y_utm, utm_secteur as sector".
204
					" FROM tb_cel.cel_zones_geo WHERE nom LIKE $location_protege ";
314 delphine 205
				$utm = $this->getBdd()->recupererTous($requete);
206
			}
207
		}
208
		return $utm;
209
	}
210
 
211
	public function formerCommentaire($utm, $inventory) {
212
		$comment = '';
213
 
214
		if ($inventory['date_observation'] != '0000-00-00 00:00:00') {
215
			$comment .= $this->formerDate($inventory);
216
		}
217
 
218
		if ($inventory['collection_code'] == 'cel') {
219
			list($identifiant) = explode("@", $inventory['identifiant']);
220
			$comment .= " par ".$identifiant."@...";
221
		} else {
222
			$comment .= " par ".utf8_decode($inventory['identifiant']);
223
		}
224
		return $comment;
225
	}
226
 
227
	public function formerDate($inventory) {
787 raphael 228
		$date = NULL;
314 delphine 229
		if ($inventory['collection_code'] == 'sophy') {
230
			$date = ', en '.$inventory['date_observation'];
787 raphael 231
		}
232
		elseif($inventory['date_observation']) {
314 delphine 233
			list($year, $month, $day) = explode ('-',$inventory['date_observation']);
234
			list($day) = explode (' ',$day);
235
			if ($month == '00') {
236
				$date = ', en '.$year;
237
			} else {
238
				$date = ', le '.$day.'/'.$month.'/'.$year;
239
			}
240
		}
241
		return $date;
242
	}
243
	/*
244
	 * Stockage commentaire associe a un point :
245
	 *
246
	 * Param :
247
	 * @text : texte cumule
248
	 * @merge : indicateur de commentaire fusionne
249
	 * @name : commune associee
250
	 * @comment : commentaire
251
	 * @origine : origine de la donnee
252
	 *
253
	 *
254
	 * TODO : rendre cette fonction independante des valeurs d'origine passee en parametre
255
	 */
256
	public function stockerCommentaire($text, $merge, $name, $comment, $origine, $x, $y) {
257
		if ($origine == 'cel') {
258
			$prefix = 'CEL : ';
259
		} elseif ($origine == 'flore') {
260
			$prefix = 'CEN LR : ';
261
		} /*elseif ($origine == 'FLORE - VAR') {
262
			$prefix = 'CBN MED : ';
263
		}*/ elseif ($origine == 'sophy') {
264
			$prefix = 'SOPHY : ';
265
		}
266
 
267
		// Cumul toute origine :
268
		if (isset($text[$x.'|'.$y]['tout']) && ($text[$x.'|'.$y])) {
269
			$text[$x.'|'.$y]['tout'] = $text[$x.'|'.$y]['tout'].'<br>'.$prefix.$name.$comment;
270
		} else {
271
			// Nouveau commentaire
272
			$text[$x.'|'.$y]['tout']=$prefix.$name.$comment;
273
		}
274
 
275
		// Deja present  pour cette origine ? on ajoute
276
		if (isset ($text[$x.'|'.$y][$origine]) && ($text[$x.'|'.$y][$origine])) {
277
			$text[$x.'|'.$y][$origine] = $text[$x.'|'.$y][$origine].'<br>'.$name.$comment;
278
		} else { // Nouveau commentaire
279
			$text[$x.'|'.$y][$origine] = $name.$comment;
280
		}
281
 
282
 
283
		// Detection superposition de donnee
284
		if ($origine == 'cel') {
285
			if ((isset ($text[$x.'|'.$y]['sophy']) && ($text[$x.'|'.$y]['sophy'])) || (isset ($text[$x.'|'.$y]['flore']) && ($text[$x.'|'.$y]['flore']))
286
			|| (isset ($text[$x.'|'.$y]['FLORE - VAR']) && ($text[$x.'|'.$y]['FLORE - VAR']))) {
287
				$merge[$x.'|'.$y]=true;
288
			}
289
		} elseif ($origine == 'flore') {
290
			if ((isset ($text[$x.'|'.$y]['sophy']) && ($text[$x.'|'.$y]['sophy'])) ||(isset ($text[$x.'|'.$y]['cel']) && ($text[$x.'|'.$y]['cel'])) ||
291
			(isset ($text[$x.'|'.$y]['FLORE - VAR']) && ($text[$x.'|'.$y]['FLORE - VAR']))) {
292
				$merge[$x.'|'.$y]=true;
293
			}
294
		} elseif ($origine == 'FLORE - VAR') {
295
			if ((isset ($text[$x.'|'.$y]['sophy']) && ($text[$x.'|'.$y]['sophy'])) || (isset ($text[$x.'|'.$y]['cel']) && ($text[$x.'|'.$y]['cel'])) ||
296
			(isset ($text[$x.'|'.$y]['flore']) && ($text[$x.'|'.$y]['flore']))) {
297
				$merge[$x.'|'.$y]=true;
298
			}
299
		} elseif ($origine == 'sophy') {
300
			if ((isset ($text[$x.'|'.$y]['cel']) && ($text[$x.'|'.$y]['cel'])) || (isset ($text[$x.'|'.$y]['flore']) && ($text[$x.'|'.$y]['flore'])||
301
				(isset ($text[$x.'|'.$y]['FLORE - VAR']) && ($text[$x.'|'.$y]['FLORE - VAR'])))) {
302
				$merge[$x.'|'.$y]=true;
303
			}
304
		}
305
 
306
		return array($text, $merge);
307
	}
308
}
309
?>