diff --git a/.bzrignore b/.bzrignore index cd1d28e..c17c34d 100644 --- a/.bzrignore +++ b/.bzrignore @@ -1,2 +1,2 @@ load.php -import +*.csv diff --git a/facile.js b/facile.js index cd63eb5..f6eb3a1 100755 --- a/facile.js +++ b/facile.js @@ -1,290 +1,289 @@ var map; var EUROS = 40; var errortooresults = true; var trovalatuazona = true; var nessunfornitore = true; -var ZOOM_OPENING_SEARCH_RESULT = 17; var Impianti = { all: [], exists: function(id) { var i = 0; while(i < this.all.length && this.all[i++] !== id); return Impianti.all[i-1] === id; }, add: function(id) { this.all.push(id); } }; var Overworld = { $el: undefined, $action: undefined, visible: true, running: false, hide: function() { if(Overworld.running) { return; } Overworld.running = true; Overworld.$el.hide("drop", {direction: "up"}, "slow", function() { Overworld.$action.show("drop", {direction: "up"}, "fast", function() { Overworld.running = false; }); }); }, show: function() { if(Overworld.running) { return; } Overworld.running = true; map.closePopup(); Overworld.$el.find("input").val(""); Overworld.$action.hide("drop", {direction: "up"}, "slow", function() { Overworld.$el.show("drop", {direction: "up"}, "fast", function() { Overworld.$el.find("input").focus(); Overworld.running = false; }); }); } }; $(document).ready(function() { Overworld.$action = $("#overworld-buttons"); Overworld.$el = $("#overworld"); Overworld.$action.hide().click(function() { Overworld.show(); }); $("form").submit(function(event) { suggest_nominatim_addresses( $("input[name=search_address]").val() ); event.preventDefault(); }); map = L.map('map'); // create the tile layer with correct attribution var osmUrl='http://{s}.tile.osm.org/{z}/{x}/{y}.png'; var osmAttrib='Map data Facile.it © OpenStreetMap contributors'; var osm = new L.TileLayer(osmUrl, {minZoom: 8, maxZoom: 17, attribution: osmAttrib}); map.addLayer(osm); map.setView([45.49, 9.21], 15); map.on("moveend", function() { getMarkersInBounds(); return true; }); map.on("popupopen", function() { Overworld.hide(); }); fitDocument(); map.locate({setView: true, enableHighAccuracy: true, maxZoom: 16}); map.on("locationfound", function(e) { var radius = e.accuracy / 2; enfatizeLatLng(e.latlng, 2000); }); map.on("locationerror", function(e) { Materialize.toast("Posizione non disponibile. Zomma!", 3000); }); getMarkersInBounds(); }); function getMarkersInBounds(preCallback, postCallback) { var zoom = map.getZoom(); if(zoom < 13) { trovalatuazona && Materialize.toast("Trova la tua zona, zomma!", 3000); trovalatuazona = false; return; } trovalatuazona = true; var bounds = map.getBounds(); var data = { lat_n: bounds.getNorth(), lat_s: bounds.getSouth(), lng_w: bounds.getWest(), lng_e: bounds.getEast() }; $.getJSON("/api/bounds.php", data, function(json) { if(json.error) { errortooresults && Materialize.toast("Vedo molte stazioni. Zomma per scoprirle!", 2000); errortooresults = false; return; } errortooresults = true; if(json.length === 0) { nessunfornitore && Materialize.toast("Nessun fornitore in questa zona", 2000); nessunfornitore = false; return; } nessunfornitore = true; if(json.length > 90) { Materialize.toast("Vedo " + json.length + " risultati! Zooma per scoprirli", 2000); return; } if(preCallback && preCallback(bounds, json) === false ) { return; } var minPriceId = 0; var minPrice = 999.0; for(var i=0; i" + json[i].gestore + ":
"; for(var j=0; j"; } else { txt += ""; } txt += ""; txt += ""; txt += ""; } txt += "
" + litres + " Lper il " + json[i].prezzi[j].descCarburante + ""; if(json[i].prezzi[j].isSelf === "1") { txt += "(self)"; } else { txt += ""; } txt += "
"; txt += "

+ PREFERITI"; txt += "segnala errore

"; L.marker([json[i].latitudine, json[i].longitudine], { bounceOnAdd: true, bounceOnAddOptions: {duration: 500, height: 100}, bounceOnAddCallback: function() {} }).bindPopup(txt).addTo(map).options.idImpianto = json[i].idImpianto; } postCallback && postCallback(bounds, json); }); } function suggest_nominatim_addresses(query) { $.ajax({ url: "https://nominatim.openstreetmap.org/search", jsonp: "nominatimJsonp", dataType: "jsonp", data: { q: query, format: "json", json_callback: "nominatimJsonp" } }); } function nominatimJsonp(json) { var $searchResults = $("#modal-search-addr-results"); var $list = $searchResults.find("ol").empty(); if(json.length > 0) { for(i=0; i") .append( $('') .text(json[i].display_name) .data("latLng", {lat: json[i].lat, lng: json[i].lon}) .data("bounds", json[i].boundingbox) .click(function(event) { $searchResults.closeModal(); var latLng = $(this).data("latLng"); var bounds = $(this).data("bounds"); var adaptedBounds = [ [bounds[0], bounds[3]], [bounds[1], bounds[2]] ]; map.fitBounds( adaptedBounds ); getMarkersInBounds(); Overworld.hide(); event.preventDefault(); }) ) .append( $("
") ) .append( $("") .text(json[i].licence) ) ); } // Result links $searchResults.find("a").click(function() { $searchResults.closeModal(); }); } else { $list.append( $("
  • ").text( "Nessun indirizzo trovato" ) ); } $searchResults.openModal(); } function enfatizeLatLng(latLng, duration, radius) { duration = duration || 2000; radius = radius || 100; var circle = L.circle(latLng, radius).addTo(map); window.setTimeout(function () { map.removeLayer(circle); }, duration); } function fitDocument() { var w = $("body").width(); var h = $("body").height(); $("#map").width( w ).height( h ); } $(window).resize(function() { // jQuery's windowResize is a bit crazy setTimeout(fitDocument, 500); setTimeout(fitDocument, 2000); setTimeout(fitDocument, 4000); }); $(document).on("click", ".add-favorites", function() { Materialize.toast("Aggiunto ai tuoi preferiti. Facile!", 3000); }); $(document).on("click", ".segnala-errore", function() { Materialize.toast("La tua segnalazione è preziosa. Grazie!", 3000); }); diff --git a/import/import.php b/import/import.php new file mode 100755 index 0000000..1afb639 --- /dev/null +++ b/import/import.php @@ -0,0 +1,148 @@ +query("TRUNCATE {$db->getTable('price')}"); +$db->query("TRUNCATE {$db->getTable('station')}"); + +$error_code = null; +$error_message = null; + +$comuni = $db->getResults("SELECT * FROM {$db->getTable('comune')}"); +$provincie = $db->getResults("SELECT * FROM {$db->getTable('provincia')}"); + +function get_comune_ID($comune_uid, $comune_name, $provincia_name) { + global $db, $comuni; + + $n = count($comuni); + $i = 0; + while($i<$n && $comuni[$i++]->comune_uid !== $comune_uid); + $i--; + if($i>=0 && $i<$n && $comuni[$i]->comune_uid === $comune_uid) { + return $comuni[$i]->comune_ID; + } + + $db->insertRow('comune', [ + new DBCol('comune_uid', $comune_uid, 's'), + new DBCol('comune_name', $comune_name, 's') + ] ); + + $comuni[] = $db->getRow( sprintf( + "SELECT * FROM {$db->getTable('comune')} WHERE comune_ID = '%d'", + $db->getLastInsertedID() + ) ); + + $db->insertRow('rel_provincia_comune', [ + new DBCol( + 'provincia_ID', + get_provincia_ID( + generate_slug($provincia_name), + $provincia_name + ), + 'd' + ), + new DBCol('comune_ID', $comuni[$n]->comune_ID, 'd') + ] ); + + return $comuni[$n]->comune_ID; +} + +function get_provincia_ID($provincia_uid, $provincia_name) { + global $db, $provincie; + + $n = count($provincie); + $i = 0; + while($i<$n && $provincie[$i++]->provincia_uid !== $provincia_uid); + $i--; + if($i>=0 && $i<$n && $provincie[$i]->provincia_uid === $provincia_uid) { + return $provincie[$i]->provincia_ID; + } + + $db->insertRow('provincia', [ + new DBCol('provincia_uid', $provincia_uid, 's'), + new DBCol('provincia_name', $provincia_name, 's') + ] ); + + $provincie[] = $db->getRow( sprintf( + "SELECT * FROM {$db->getTable('provincia')} WHERE provincia_ID = '%d'", + $db->getLastInsertedID() + ) ); + + return $provincie[$n]->provincia_ID; +} + +try { + if( ! file_exists(FILENAME_STATIONS) ) { + throw new Exception( _("File delle stazioni non trovato"), 1 ); + } + + if( ! $handle = fopen(FILENAME_STATIONS, 'r') ) { + throw new Exception( _("Impossibile aprire il file delle stazioni"), 2 ); + } + + // Waste first 2 lines + fgetcsv($handle); + fgetcsv($handle); + while( $data = fgetcsv($handle, 512, ';') ) { + $db->insertRow('station', [ + new DBCol('idImpianto', intval($data[0]), 'd'), + new DBCol('gestore', $data[1], 's'), + new DBCol('bandiera', $data[2], 's'), + new DBCol('tipoImpianto', $data[3], 's'), + new DBCol('nomeImpianto', $data[4], 's'), + new DBCol('indirizzo', $data[5], 's'), + new DBCol('latitudine', floatval($data[8]), 'f'), + new DBCol('longitudine', floatval($data[9]), 'f'), + new DBCol( + 'comune_ID', + get_comune_ID( + generate_slug($data[6]), + $data[6], + $data[7] + ), + 'd' + ) + ] ); + } + fclose($handle); + + // Prices + if( ! file_exists(FILENAME_PRICES) ) { + throw new Exception( _("File dei prezzi non trovato"), 3 ); + } + + if( ! $handle = fopen(FILENAME_PRICES, 'r') ) { + throw new Exception( _("Impossibile aprire il file dei prezzi"), 4 ); + } + + // Waste first 2 lines + fgetcsv($handle); + fgetcsv($handle); + + while( $data = fgetcsv($handle, 255, ';') ) { + $db->insertRow('price', [ + new DBCol('idImpianto', intval($data[0]), 'd'), + new DBCol('descCarburante', $data[1], 's'), + new DBCol('prezzo', floatval($data[2]), 'f'), + new DBCol('isSelf', intval($data[3]), 'd'), + new DBCol('dtComu', $data[4], 's') + ] ); + } + + fclose($handle); + +} catch(Exception $e) { + $error_code = $e->getCode(); + $error_message = $e->getMessage(); +} + +echo json_encode( [ + 'error' => $error_code, + 'errorMessage' => $error_message +] ); diff --git a/import/index.php b/import/index.php new file mode 100755 index 0000000..eea59b9 --- /dev/null +++ b/import/index.php @@ -0,0 +1,2 @@ +