[PLUGINS] +clavettes et dependances
[lhc/web/clavette_www.git] / www / plugins / gis / gis_pipelines.php
1 <?php
2
3 if (!defined('_ECRIRE_INC_VERSION')) return;
4
5 /**
6 * Insertion des css du plugin dans les pages publiques
7 *
8 * @param $flux
9 * @return mixed
10 */
11 function gis_insert_head_css($flux){
12 $flux .="\n".'<link rel="stylesheet" href="'. find_in_path('lib/leaflet/dist/leaflet.css') .'" />';
13 $flux .="\n".'<link rel="stylesheet" href="'. sinon(find_in_path('css/leaflet-plugins.css'),find_in_path('lib/leaflet/plugins/leaflet-plugins.css')) .'" />';
14 $flux .="\n".'<link rel="stylesheet" href="'. sinon(find_in_path('css/leaflet.markercluster.css'),find_in_path('lib/leaflet/plugins/leaflet.markercluster.css')) .'" />';
15 return $flux;
16 }
17
18 /**
19 * Insertion des scripts du plugin dans les pages publiques
20 *
21 * @param $flux
22 * @return mixed
23 */
24 function gis_insert_head($flux){
25
26 // initialisation des valeurs de config
27 $config = @unserialize($GLOBALS['meta']['gis']);
28 if (!isset($config['layers']) || !is_array($config['layers']))
29 $config['layers'] = array('openstreetmap_mapnik');
30
31 include_spip('gis_fonctions');
32 if (!in_array(gis_layer_defaut(),$config['layers']))
33 $config['layers'][] = gis_layer_defaut();
34
35 // insertion des scripts pour google si nécessaire
36 if (count(array_intersect(array('google_roadmap', 'google_satellite', 'google_terrain'), $config['layers'])) > 0) {
37 $flux .="\n".'<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&amp;language='.$GLOBALS['spip_lang'].'"></script>';
38 }
39
40 return $flux;
41 }
42
43 /**
44 * Insertion des scripts et css du plugin dans les pages de l'espace privé
45 *
46 * @param $flux
47 * @return mixed
48 */
49 function gis_header_prive($flux){
50 $flux .= gis_insert_head_css('');
51 $flux .= gis_insert_head('');
52 return $flux;
53 }
54
55 /**
56 * Insertion du bloc GIS dans les pages des objets de l'espace privé
57 *
58 * @param $flux
59 * @return mixed
60 */
61 function gis_afficher_contenu_objet($flux){
62 if ($objet = $flux['args']['type']
63 and include_spip('inc/config')
64 and in_array(table_objet_sql($objet), lire_config('gis/gis_objets', array()))
65 and ($id = intval($flux['args']['id_objet']))
66
67 ){
68 $texte = recuperer_fond(
69 'prive/contenu/gis_objet',
70 array(
71 'table_source'=>'gis',
72 'objet'=>$objet,
73 'id_objet'=>$id
74 )
75 );
76 $flux['data'] .= $texte;
77 }
78
79 return $flux;
80 }
81
82 /**
83 * Si la geolocalisation des documents est activée dans la config,
84 * création/suppression d'un point à partir des métadonnées du document ajouté (jpg, kml et kmz)
85 *
86 * @param $flux
87 * @return mixed
88 */
89 function gis_post_edition($flux){
90 if (is_array($flux) && isset($flux['args']['operation']) && ($flux['args']['operation'] == 'ajouter_document')
91 AND ($document = sql_fetsel("*","spip_documents","id_document=".intval($flux['args']['id_objet'])))
92 AND (in_array(table_objet_sql('document'), lire_config('gis/gis_objets', array())))
93 ) {
94 if(in_array($document['extension'],array('jpg','kml','kmz'))){
95 $config = @unserialize($GLOBALS['meta']['gis']);
96 if(!is_array($config))
97 $config = array();
98 include_spip('inc/documents');
99 $fichier = get_spip_doc($document['fichier']);
100 $id_document = $document['id_document'];
101 }
102 if ($document['extension'] == 'jpg') {
103 // on recupere les coords definies dans les exif du document s'il y en a
104 if (function_exists('exif_read_data') AND $exifs = @exif_read_data($fichier,'GPS')) {
105 if(!function_exists('dms_to_dec'))
106 include_spip('gis_fonctions');
107 spip_log("GIS EXIFS : Récuperation des coordonnees du fichier $fichier","gis");
108
109 $LatDeg = explode("/",$exifs["GPSLatitude"][0]);
110 if(is_numeric($LatDeg[1]) > 0)
111 $intLatDeg = $LatDeg[0]/$LatDeg[1];
112
113 $LatMin = explode("/",$exifs["GPSLatitude"][1]);
114 if(is_numeric($LatMin[1]) > 0)
115 $intLatMin = $LatMin[0]/$LatMin[1];
116
117 $LatSec = explode("/",$exifs["GPSLatitude"][2]);
118 if(is_numeric($LatSec[1]) > 0)
119 $intLatSec = $LatSec[0]/$LatSec[1];
120
121 $LongDeg = explode("/",$exifs["GPSLongitude"][0]);
122 if(is_numeric($LongDeg[1]) > 0)
123 $intLongDeg = $LongDeg[0]/$LongDeg[1];
124
125 $LongMin = explode("/",$exifs["GPSLongitude"][1]);
126 if(is_numeric($LongMin[1]) > 0)
127 $intLongMin = $LongMin[0]/$LongMin[1];
128
129 $LongSec = explode("/",$exifs["GPSLongitude"][2]);
130 if(is_numeric($LongSec[1]) > 0)
131 $intLongSec = $LongSec[0]/$LongSec[1];
132
133 // round to 5 = approximately 1 meter accuracy
134 if(is_numeric($intLatDeg) && is_numeric($intLatMin) && is_numeric($intLatSec))
135 $latitude = round(dms_to_dec($exifs["GPSLatitudeRef"],
136 $intLatDeg,$intLatMin,$intLatSec),5);
137
138 if(is_numeric($intLongDeg) && is_numeric($intLongMin) && is_numeric($intLongSec))
139 $longitude = round(dms_to_dec($exifs["GPSLongitudeRef"],
140 $intLongDeg,$intLongMin,$intLongSec), 5);
141 if($config['geocoder'] == 'on'){
142 include_spip('inc/xml');
143 $url_geocoder = 'http://maps.googleapis.com/maps/api/geocode/xml?latlng='.urlencode($latitude).','.urlencode($longitude).'&sensor=true';
144 $geocoder = spip_xml_load($url_geocoder);
145 spip_xml_match_nodes(',result,',$geocoder,$matches_adress);
146 if(is_array($matches_adress['result'])){
147 foreach($matches_adress['result'] as $component){
148 if(in_array('country',$component['type'])){
149 $pays = $component['address_component'][0]['long_name'][0];
150 $code_pays = $component['address_component'][0]['short_name'][0];
151 }
152 if(in_array('administrative_area_level_1',$component['type'])){
153 $region = $component['address_component'][0]['long_name'][0];
154 }
155 if(in_array('administrative_area_level_2',$component['type'])){
156 $departement = $component['address_component'][0]['long_name'][0];
157 }
158 if(in_array('locality',$component['type'])){
159 $ville = $component['address_component'][0]['long_name'][0];
160 }
161 if(in_array('postal_code',$component['type'])){
162 $code_postal = $component['address_component'][0]['long_name'][0];
163 }
164 if(in_array('route',$component['type'])){
165 $adresse = $component['address_component'][0]['long_name'][0];
166 }
167 }
168 }
169 }
170 }else if(file_exists($fichier)){
171 include_spip("inc/iptc");
172
173 $er = new class_IPTC($fichier);
174 $iptc = $er->fct_lireIPTC();
175 $codesiptc = $er->h_codesIptc;
176 $string_recherche = '';
177
178 if($iptc['city']){
179 $string_recherche .= $iptc['city'].', ';
180 }
181 if($iptc['provinceState']){
182 $string_recherche .= $iptc['provinceState'].', ';
183 }
184 if($iptc['country']){
185 $string_recherche .= $iptc['country'];
186 }
187 if(strlen($string_recherche)){
188 include_spip('inc/xml');
189 $url_geocoder = 'http://maps.googleapis.com/maps/api/geocode/xml?address='.urlencode($string_recherche).'&sensor=true';
190 $geocoder = spip_xml_load($url_geocoder);
191 if(is_array($geocoder)){
192 spip_xml_match_nodes(',location,',$geocoder,$matches);
193 $latitude = $matches['location']['0']['lat']['0'];
194 $longitude = $matches['location']['0']['lng']['0'];
195 if($config['adresse'] == 'on'){
196 spip_xml_match_nodes(',address_component,',$geocoder,$matches_adress);
197 if(is_array($matches_adress['address_component'])){
198 foreach($matches_adress['address_component'] as $component){
199 if(in_array('country',$component['type'])){
200 $pays = $component['long_name'][0];
201 $code_pays = $component['short_name'][0];
202 }
203 if(in_array('administrative_area_level_1',$component['type'])){
204 $region = $component['long_name'][0];
205 }
206 if(in_array('administrative_area_level_2',$component['type'])){
207 $departement = $component['long_name'][0];
208 }
209 if(in_array('locality',$component['type'])){
210 $ville = $component['long_name'][0];
211 }
212 }
213 }
214 }
215 }
216 }
217 }
218 if(is_numeric($latitude) && is_numeric($longitude)){
219 $c = array(
220 'titre' => basename($fichier),
221 'lat'=> $latitude,
222 'lon' => $longitude,
223 'zoom' => $config['zoom'] ? $config['zoom'] :'4',
224 'adresse' => $adresse,
225 'code_postal' => $code_postal,
226 'ville' => $ville,
227 'region' => $region,
228 'departement' => $departement,
229 'pays' => $pays,
230 'code_pays' => $code_pays
231 );
232
233 if (defined('_DIR_PLUGIN_GISGEOM')) {
234 $geojson = '{"type":"Point","coordinates":['.$longitude.','.$latitude.']}';
235 set_request('geojson',$geojson);
236 }
237
238 include_spip('action/editer_gis');
239
240 if($id_gis = sql_getfetsel("G.id_gis","spip_gis AS G LEFT JOIN spip_gis_liens AS T ON T.id_gis=G.id_gis ","T.id_objet=" . intval($id_document) . " AND T.objet='document'")){
241 // Des coordonnées sont déjà définies pour ce document => on les update
242 revisions_gis($id_gis,$c);
243 spip_log("GIS EXIFS : Update des coordonnées depuis EXIFS pour le document $id_document => id_gis = $id_gis","gis");
244 }
245 else{
246 // Aucune coordonnée n'est définie pour ce document => on les crées
247 $id_gis = insert_gis();
248 revisions_gis($id_gis,$c);
249 lier_gis($id_gis, 'document', $id_document);
250 spip_log("GIS EXIFS : Création des coordonnées depuis EXIFS pour le document $id_document => id_gis = $id_gis","gis");
251 }
252 }
253 }elseif(in_array($document['extension'],array('kml','kmz','gpx'))){
254 $recuperer_info = charger_fonction('kml_infos','inc');
255 $infos = $recuperer_info($document['id_document']);
256 if($infos){
257 if(is_numeric($latitude = $infos['latitude']) && is_numeric($longitude = $infos['longitude'])){
258 $c = array(
259 'titre' => $infos['titre'] ? $infos['titre'] : basename($fichier),
260 'descriptif' => $infos['descriptif'],
261 'lat'=> $latitude,
262 'lon' => $longitude,
263 'zoom' => $config['zoom'] ? $config['zoom'] :'4'
264 );
265
266 include_spip('action/editer_gis');
267
268 if($id_gis = sql_getfetsel("G.id_gis","spip_gis AS G LEFT JOIN spip_gis_liens AS T ON T.id_gis=G.id_gis ","T.id_objet=" . intval($id_document) . " AND T.objet='document'")){
269 // Des coordonnées sont déjà définies pour ce document => on les update
270 revisions_gis($id_gis,$c);
271 spip_log("GIS EXIFS : Update des coordonnées depuis EXIFS pour le document $id_document => id_gis = $id_gis","gis");
272 }
273 else{
274 // Aucune coordonnée n'est définie pour ce document => on les crées
275 $id_gis = insert_gis();
276 revisions_gis($id_gis,$c);
277 lier_gis($id_gis, 'document', $id_document);
278 spip_log("GIS EXIFS : Création des coordonnées depuis EXIFS pour le document $id_document => id_gis = $id_gis","gis");
279 }
280 }
281 unset($infos['longitude']);
282 unset($infos['latitude']);
283 if(count($infos) > 0){
284 include_spip('action/editer_document');
285 document_modifier($id_document, $infos);
286 }
287 }
288 }
289 }
290 if (is_array($flux) && isset($flux['args']['operation']) && ($flux['args']['operation'] == 'supprimer_document')
291 AND ($id_document = intval($flux['args']['id_objet'])
292 AND ($id_gis = sql_getfetsel("G.id_gis","spip_gis AS G LEFT JOIN spip_gis_liens AS T ON T.id_gis=G.id_gis ","T.id_objet=" . intval($id_document) . " AND T.objet='document'")))
293 ) {
294 include_spip('action/editer_gis');
295 supprimer_gis($id_gis);
296 spip_log("GIS EXIFS : Suppression des coordonnées pour le document $id_document => id_gis = $id_gis","gis");
297 }
298
299 return $flux;
300 }
301
302
303 /**
304 * Optimiser la base de données en supprimant les liens orphelins
305 *
306 * @param array $flux
307 * @return array
308 */
309 function gis_optimiser_base_disparus($flux){
310
311 include_spip('action/editer_liens');
312 // optimiser les liens morts :
313 // entre gis vers des objets effaces
314 // depuis des gis effaces
315 $flux['data'] += objet_optimiser_liens(array('gis'=>'*'),'*');
316
317 return $flux;
318 }
319
320 function gis_saisies_autonomes($flux){
321 $flux[] = 'carte';
322 return $flux;
323 }
324
325 /**
326 * Insertion dans le pipeline xmlrpc_methodes (xmlrpc)
327 * Ajout de méthodes xml-rpc spécifiques à GIS
328 *
329 * @param array $flux : un array des methodes déjà présentes, fonctionnant sous la forme :
330 * -* clé = nom de la méthode;
331 * -* valeur = le nom de la fonction à appeler;
332 * @return array $flux : l'array complété avec nos nouvelles méthodes
333 */
334 function gis_xmlrpc_methodes($flux){
335 $flux['spip.liste_gis'] = 'spip_liste_gis';
336 $flux['spip.lire_gis'] = 'spip_lire_gis';
337 return $flux;
338 }
339
340 /**
341 * Insertion dans le pipeline xmlrpc_server_class (xmlrpc)
342 * Ajout de fonctions spécifiques utilisés par le serveur xml-rpc
343 */
344 function gis_xmlrpc_server_class($flux){
345 include_spip('inc/gis_xmlrpc');
346 return $flux;
347 }
348
349 /**
350 * Insertion dans le traitement du formulaire de configuration
351 *
352 * Purger le répertoire js si on a une carte google dans les layers pour recalculer le js statique
353 * Peut être à améliorer
354 *
355 * @param array $flux
356 * Le contexte du pipeline
357 * @return array $flux
358 */
359 function gis_formulaire_traiter($flux){
360 if($flux['args']['form'] == 'configurer_gis'){
361 if (count(array_intersect(array('google_roadmap', 'google_satellite', 'google_terrain'), _request('layers'))) > 0){
362 include_spip('inc/invalideur');
363 purger_repertoire(_DIR_VAR.'cache-js');
364 suivre_invalideur(1);
365 }
366 }
367 return $flux;
368 }
369 ?>