- 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:
Ismo Vuorinen
2013-11-19 15:34:42 +02:00
parent 787abf7062
commit b0258437cd
8 changed files with 158 additions and 62 deletions

View File

@@ -31,6 +31,19 @@ grunt server # Start development server
grunt # Build minified and optimized version for release to /dist -folder 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 ## ## Credits ##
- Build by [Juho Teperi](https://github.com/Deraen) in 2013 while working for [Ekokumppanit Oy](http://www.ekokumppanit.fi) - Build by [Juho Teperi](https://github.com/Deraen) in 2013 while working for [Ekokumppanit Oy](http://www.ekokumppanit.fi)

View File

@@ -4,13 +4,20 @@
* *
* Configure these and open the project in your browser, * Configure these and open the project in your browser,
* install script takes care of the rest. * 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( $config = array(
'server' => 'localhost', 'server' => 'localhost',
'db' => 'database', 'db' => 'database',
'user' => 'username', 'user' => 'username',
'password' => 'password', 'password' => 'password',
'table' => 'airports' 'table' => 'airports',
'create_table' => true
); );
// ---- // ----

View File

@@ -1,28 +1,75 @@
<?php <?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) { // If we can see lentolaskuri.sql and config.php setting
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { // $config['create_table'] is true, we try to create table for airports.
$id = $mysqli->real_escape_string($data[0]); if (is_readable('lentolaskuri.sql') && $config['create_table']) {
$name = $mysqli->real_escape_string($data[1]); $create_table = file_get_contents('lentolaskuri.sql');
$city = $mysqli->real_escape_string($data[2]); if (! empty($create_table)) {
$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]);
$query = "REPLACE INTO airports (id, name, city, country, iata, icao, lat, `long`) VALUES ('$id', '$name', '$city', '$country', '$iata', '$icao', '$lat', '$long')"; // Test if user has changed database table name and change accordingly
if (!$mysqli->query($query)) { if ($config['table'] !== 'airports') {
printf("Error: %s\n", $mysqli->sqlstate); $create_table = str_replace(
exit(); "CREATE TABLE IF NOT EXISTS `airports`",
} "CREATE TABLE IF NOT EXISTS `".$config['db']."`.`".$config['table']."`",
} $create_table
fclose($handle); );
}
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(); $mysqli->close();

12
app/api/lentolaskuri.sql Normal file
View 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;

View File

@@ -1,56 +1,70 @@
<?php <?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: *"); header("Access-Control-Allow-Origin: *");
$search = $mysqli->real_escape_string($_GET['s']); $get_s = (empty($_GET['s'])) ? null : $_GET['s'];
$id = $mysqli->real_escape_string($_GET['i']); $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)) { if (empty($search) && empty($id)) {
exit(); exit();
} }
$query = ""; $query = "";
if (!empty($search)) { if (!empty($search)) {
$query = " $query = "
SELECT *, SELECT *,
CASE CASE
WHEN iata LIKE '$search%' THEN 100 WHEN iata LIKE '$search%' THEN 100
WHEN name LIKE '$search%' THEN 75 WHEN name LIKE '$search%' THEN 75
WHEN name LIKE '%$search%' THEN 74 WHEN name LIKE '%$search%' THEN 74
WHEN city LIKE '$search%' THEN 70 WHEN city LIKE '$search%' THEN 70
WHEN country LIKE '$search%' THEN 65 WHEN country LIKE '$search%' THEN 65
WHEN city LIKE '%$search%' THEN 60 WHEN city LIKE '%$search%' THEN 60
WHEN country LIKE '%$search%' THEN 55 WHEN country LIKE '%$search%' THEN 55
ELSE 0 END ELSE 0 END
AS score AS
FROM airports score
WHERE FROM
name LIKE '$search%' OR " . $config['table'] . "
name LIKE '%$search%' OR WHERE
country LIKE '$search%' OR name LIKE '$search%' OR
country LIKE '%$search%' OR name LIKE '%$search%' OR
city LIKE '$search%' OR country LIKE '$search%' OR
city LIKE '%$search%' OR country LIKE '%$search%' OR
iata LIKE '$search%' city LIKE '$search%' OR
ORDER BY score DESC city LIKE '%$search%' OR
LIMIT 10; iata LIKE '$search%'
"; ORDER BY
score DESC
LIMIT 10;";
} else if (!empty($id)) { } else if (!empty($id)) {
$query ="SELECT * FROM airports WHERE id LIKE '$id';"; $query = "SELECT * FROM " . $config['table'] . " WHERE id LIKE '$id';";
} }
$results = array(); $results = array();
if ($result = $mysqli->query($query)) { if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) { while ($row = $result->fetch_assoc()) {
$results[] = $row; $results[] = $row;
} }
print(json_encode($results)); print(json_encode($results));
} else { } else {
printf("Error: %s\n", $mysqli->sqlstate); printf("Error: %s\n", $mysqli->sqlstate);
} }

View File

@@ -1,8 +1,10 @@
'use strict'; 'use strict';
var searchlocation = window.location;
define(function () { define(function () {
return { return {
api: 'http://work.lentolaskuri.fi/api', api: searchlocation + 'api',
R: 6371, R: 6371,
radiativeForceFactor: function (dist) { radiativeForceFactor: function (dist) {
return (dist >= 500) ? 2.0 : 1.0; return (dist >= 500) ? 2.0 : 1.0;

View File

@@ -5,7 +5,7 @@ define([
'config' 'config'
], function ($, _, config) { ], function ($, _, config) {
var airportById = function (element, callback) { 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]); callback(data[0]);
}); });
}; };

View File

@@ -48,9 +48,10 @@ require.config({
require([ require([
'jquery', 'jquery',
'app', 'app',
'config',
'bootstrap', 'bootstrap',
'libs/handlebar_helpers' 'libs/handlebar_helpers'
], function ($, App) { ], function ($, App, config) {
$('html:first').removeClass('no-js').addClass('js'); // JavaScript detection $('html:first').removeClass('no-js').addClass('js'); // JavaScript detection
var app = new App(); var app = new App();
}); });