Subversion Repositories eFlore/Projets.eflore-projets

Rev

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