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