mirror of
https://github.com/Ekokumppanit/Lentolaskuri.git
synced 2026-01-26 03:03:58 +00:00
- Added section on configuration to README.md
- Made the application more configurable by the user - Made installation easier - API location is now dynamic based on window.location - Cleaned up some older code that was pointing to dev stuff - Now includes the SQL-table required to run the application - Application now exposes config to console for easier debugging - Search now supports user specified table name - Updated the OpenFlights airport data file source
This commit is contained in:
13
README.md
13
README.md
@@ -31,6 +31,19 @@ grunt server # Start development server
|
||||
grunt # Build minified and optimized version for release to /dist -folder
|
||||
```
|
||||
|
||||
### Configuration ###
|
||||
|
||||
1. Configure database access in ``app/api/config.sample.php`` and save as ``app/api/config.php``. The ``config.php`` file gets ignored in ``.gitignore`` so no worries.
|
||||
2. Build project using ``grunt``, this generates ``dist`` folder
|
||||
3. Push files and folders in ``dist/*`` to your server, for example as **http://example.com/lentolaskuri**
|
||||
4. Set up MySQL database table to your server and import airport data to your database:
|
||||
- Visit **http://example.com/lentolaskuri/api/import.php** to create mysql table automagically (or import ``app/api/lentolaskuri.sql`` by hand, but change table name to one you used in ``config.php``)
|
||||
- Change variable ``$config['create_table']`` to ``false`` in ``app/api/config.php`` to prevent further table creation attempts
|
||||
- ProTip: You can make ``cron`` or similar visit this url weekly to update the latest airport data from openflights database
|
||||
6. Test your lentolaskuri application
|
||||
7. Donate money based on emissions to a good cause
|
||||
|
||||
|
||||
## Credits ##
|
||||
|
||||
- Build by [Juho Teperi](https://github.com/Deraen) in 2013 while working for [Ekokumppanit Oy](http://www.ekokumppanit.fi)
|
||||
|
||||
@@ -4,13 +4,20 @@
|
||||
*
|
||||
* Configure these and open the project in your browser,
|
||||
* install script takes care of the rest.
|
||||
*
|
||||
* @category Lentolaskuri
|
||||
* @package Lentolaskuri
|
||||
* @author Ismo Vuorinen <ismo@ivuorinen.net>
|
||||
* @license http://www.gnu.org/licenses/agpl.html GNU Affero General Public License
|
||||
* @link https://github.com/Ekokumppanit/Lentolaskuri
|
||||
*/
|
||||
$config = array(
|
||||
'server' => 'localhost',
|
||||
'db' => 'database',
|
||||
'user' => 'username',
|
||||
'password' => 'password',
|
||||
'table' => 'airports'
|
||||
'server' => 'localhost',
|
||||
'db' => 'database',
|
||||
'user' => 'username',
|
||||
'password' => 'password',
|
||||
'table' => 'airports',
|
||||
'create_table' => true
|
||||
);
|
||||
|
||||
// ----
|
||||
|
||||
@@ -1,28 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* Import Airport data from OpenFlights csv-file to database
|
||||
*
|
||||
* @category Lentolaskuri
|
||||
* @package Lentolaskuri
|
||||
* @author Ismo Vuorinen <ismo@ivuorinen.net>
|
||||
* @license http://www.gnu.org/licenses/agpl.html GNU Affero General Public License
|
||||
* @link https://github.com/Ekokumppanit/Lentolaskuri
|
||||
*/
|
||||
|
||||
require_once('config.php');
|
||||
require_once 'config.php';
|
||||
|
||||
if (($handle = fopen("http://openflights.svn.sourceforge.net/viewvc/openflights/openflights/data/airports.dat", "r")) !== FALSE) {
|
||||
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
|
||||
$id = $mysqli->real_escape_string($data[0]);
|
||||
$name = $mysqli->real_escape_string($data[1]);
|
||||
$city = $mysqli->real_escape_string($data[2]);
|
||||
$country = $mysqli->real_escape_string($data[3]);
|
||||
$iata = $mysqli->real_escape_string($data[4]);
|
||||
$icao = $mysqli->real_escape_string($data[5]);
|
||||
if (empty($iata)) {
|
||||
continue;
|
||||
}
|
||||
$lat = $mysqli->real_escape_string($data[6]);
|
||||
$long = $mysqli->real_escape_string($data[7]);
|
||||
// If we can see lentolaskuri.sql and config.php setting
|
||||
// $config['create_table'] is true, we try to create table for airports.
|
||||
if (is_readable('lentolaskuri.sql') && $config['create_table']) {
|
||||
$create_table = file_get_contents('lentolaskuri.sql');
|
||||
if (! empty($create_table)) {
|
||||
|
||||
$query = "REPLACE INTO airports (id, name, city, country, iata, icao, lat, `long`) VALUES ('$id', '$name', '$city', '$country', '$iata', '$icao', '$lat', '$long')";
|
||||
if (!$mysqli->query($query)) {
|
||||
printf("Error: %s\n", $mysqli->sqlstate);
|
||||
exit();
|
||||
}
|
||||
}
|
||||
fclose($handle);
|
||||
// Test if user has changed database table name and change accordingly
|
||||
if ($config['table'] !== 'airports') {
|
||||
$create_table = str_replace(
|
||||
"CREATE TABLE IF NOT EXISTS `airports`",
|
||||
"CREATE TABLE IF NOT EXISTS `".$config['db']."`.`".$config['table']."`",
|
||||
$create_table
|
||||
);
|
||||
}
|
||||
|
||||
if (! $mysqli->query($create_table)) {
|
||||
printf("Error: %s\n", $mysqli->sqlstate);
|
||||
exit();
|
||||
}
|
||||
|
||||
echo "Created table: {$config['db']}.{$config['table']} (Please turn off table creation now from config.php: 'create_table' => false )\n";
|
||||
|
||||
} else {
|
||||
die("Couldn't find lentolaskuri.sql");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Try to load the airports.dat file, or bail out
|
||||
if (($handle = fopen("http://sourceforge.net/p/openflights/code/HEAD/tree/openflights/data/airports.dat?format=raw", "r")) !== false) {
|
||||
|
||||
// Similar to fgets() except that fgetcsv() parses the line it reads for
|
||||
// fields in CSV format and returns an array containing the fields read.
|
||||
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
|
||||
$id = $mysqli->real_escape_string($data[0]);
|
||||
$name = $mysqli->real_escape_string($data[1]);
|
||||
$city = $mysqli->real_escape_string($data[2]);
|
||||
$country = $mysqli->real_escape_string($data[3]);
|
||||
$iata = $mysqli->real_escape_string($data[4]);
|
||||
$icao = $mysqli->real_escape_string($data[5]);
|
||||
if (empty($iata)) {
|
||||
continue;
|
||||
}
|
||||
$lat = $mysqli->real_escape_string($data[6]);
|
||||
$long = $mysqli->real_escape_string($data[7]);
|
||||
|
||||
// REPLACE works exactly like INSERT, except that if an old row in the
|
||||
// table has the same value as a new row for a PRIMARY KEY or
|
||||
// a UNIQUE index, the old row is deleted before the new row is inserted.
|
||||
// http://dev.mysql.com/doc/refman/5.0/en/replace.html
|
||||
$query = "REPLACE INTO ".$config['db'].".".$config['table']." (id, name, city, country, iata, icao, lat, `long`) "
|
||||
. "VALUES ('$id', '$name', '$city', '$country', '$iata', '$icao', '$lat', '$long')";
|
||||
|
||||
if (! $mysqli->query($query)) {
|
||||
printf("Error: %s\n", $mysqli->sqlstate);
|
||||
exit();
|
||||
}
|
||||
}
|
||||
fclose($handle);
|
||||
}
|
||||
|
||||
$mysqli->close();
|
||||
|
||||
12
app/api/lentolaskuri.sql
Normal file
12
app/api/lentolaskuri.sql
Normal file
@@ -0,0 +1,12 @@
|
||||
CREATE TABLE IF NOT EXISTS `airports` (
|
||||
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(200) NOT NULL,
|
||||
`city` varchar(200) NOT NULL,
|
||||
`country` varchar(200) NOT NULL,
|
||||
`iata` varchar(4) NOT NULL,
|
||||
`icao` varchar(4) NOT NULL,
|
||||
`lat` float NOT NULL,
|
||||
`long` float NOT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
FULLTEXT KEY `SEARCH` (`name`,`city`,`country`,`iata`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
|
||||
@@ -1,56 +1,70 @@
|
||||
<?php
|
||||
/**
|
||||
* Return Airport data from the database based on search query
|
||||
*
|
||||
* @category Lentolaskuri
|
||||
* @package Lentolaskuri
|
||||
* @author Ismo Vuorinen <ismo@ivuorinen.net>
|
||||
* @license http://www.gnu.org/licenses/agpl.html GNU Affero General Public License
|
||||
* @link https://github.com/Ekokumppanit/Lentolaskuri
|
||||
*/
|
||||
|
||||
require_once('config.php');
|
||||
require_once 'config.php';
|
||||
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
|
||||
$search = $mysqli->real_escape_string($_GET['s']);
|
||||
$id = $mysqli->real_escape_string($_GET['i']);
|
||||
$get_s = (empty($_GET['s'])) ? null : $_GET['s'];
|
||||
$get_i = (empty($_GET['i'])) ? null : $_GET['i'];
|
||||
|
||||
$search = $mysqli->real_escape_string($get_s);
|
||||
$id = $mysqli->real_escape_string($get_i);
|
||||
|
||||
if (empty($search) && empty($id)) {
|
||||
exit();
|
||||
exit();
|
||||
}
|
||||
|
||||
$query = "";
|
||||
|
||||
if (!empty($search)) {
|
||||
$query = "
|
||||
SELECT *,
|
||||
CASE
|
||||
WHEN iata LIKE '$search%' THEN 100
|
||||
WHEN name LIKE '$search%' THEN 75
|
||||
WHEN name LIKE '%$search%' THEN 74
|
||||
WHEN city LIKE '$search%' THEN 70
|
||||
WHEN country LIKE '$search%' THEN 65
|
||||
WHEN city LIKE '%$search%' THEN 60
|
||||
WHEN country LIKE '%$search%' THEN 55
|
||||
$query = "
|
||||
SELECT *,
|
||||
CASE
|
||||
WHEN iata LIKE '$search%' THEN 100
|
||||
WHEN name LIKE '$search%' THEN 75
|
||||
WHEN name LIKE '%$search%' THEN 74
|
||||
WHEN city LIKE '$search%' THEN 70
|
||||
WHEN country LIKE '$search%' THEN 65
|
||||
WHEN city LIKE '%$search%' THEN 60
|
||||
WHEN country LIKE '%$search%' THEN 55
|
||||
ELSE 0 END
|
||||
AS score
|
||||
FROM airports
|
||||
WHERE
|
||||
name LIKE '$search%' OR
|
||||
name LIKE '%$search%' OR
|
||||
country LIKE '$search%' OR
|
||||
country LIKE '%$search%' OR
|
||||
city LIKE '$search%' OR
|
||||
city LIKE '%$search%' OR
|
||||
iata LIKE '$search%'
|
||||
ORDER BY score DESC
|
||||
LIMIT 10;
|
||||
";
|
||||
AS
|
||||
score
|
||||
FROM
|
||||
" . $config['table'] . "
|
||||
WHERE
|
||||
name LIKE '$search%' OR
|
||||
name LIKE '%$search%' OR
|
||||
country LIKE '$search%' OR
|
||||
country LIKE '%$search%' OR
|
||||
city LIKE '$search%' OR
|
||||
city LIKE '%$search%' OR
|
||||
iata LIKE '$search%'
|
||||
ORDER BY
|
||||
score DESC
|
||||
LIMIT 10;";
|
||||
} else if (!empty($id)) {
|
||||
$query ="SELECT * FROM airports WHERE id LIKE '$id';";
|
||||
$query = "SELECT * FROM " . $config['table'] . " WHERE id LIKE '$id';";
|
||||
}
|
||||
|
||||
$results = array();
|
||||
if ($result = $mysqli->query($query)) {
|
||||
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$results[] = $row;
|
||||
}
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$results[] = $row;
|
||||
}
|
||||
|
||||
print(json_encode($results));
|
||||
print(json_encode($results));
|
||||
|
||||
} else {
|
||||
printf("Error: %s\n", $mysqli->sqlstate);
|
||||
printf("Error: %s\n", $mysqli->sqlstate);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
var searchlocation = window.location;
|
||||
|
||||
define(function () {
|
||||
return {
|
||||
api: 'http://work.lentolaskuri.fi/api',
|
||||
api: searchlocation + 'api',
|
||||
R: 6371,
|
||||
radiativeForceFactor: function (dist) {
|
||||
return (dist >= 500) ? 2.0 : 1.0;
|
||||
|
||||
@@ -5,7 +5,7 @@ define([
|
||||
'config'
|
||||
], function ($, _, config) {
|
||||
var airportById = function (element, callback) {
|
||||
$.get('http://localhost:8000/search.php?i=' + element.val(), null, function (data) {
|
||||
$.get( config.api + '/search.php?i=' + element.val(), null, function (data) {
|
||||
callback(data[0]);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -48,9 +48,10 @@ require.config({
|
||||
require([
|
||||
'jquery',
|
||||
'app',
|
||||
'config',
|
||||
'bootstrap',
|
||||
'libs/handlebar_helpers'
|
||||
], function ($, App) {
|
||||
], function ($, App, config) {
|
||||
$('html:first').removeClass('no-js').addClass('js'); // JavaScript detection
|
||||
var app = new App();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user