Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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