<?php

// Slim
require 'Slim/Slim.php';
require 'Views/TwigView.php';

// Configuration
TwigView::$twigDirectory = __DIR__ . '/vendor/Twig/lib/Twig/';

// PHP active record
require_once __DIR__ . '/vendor/PHPActiveRecord/ActiveRecord.php';
ActiveRecord\Config::initialize(function($cfg) {
    $cfg->set_model_directory('/var/www/flyingabroad.co.uk/public_html/models');
    $cfg->set_connections(array(
        'development' => 'mysql://flyingabroad:windows22@localhost/flyingabroad;charset=utf8'
    ));
});

// Start Slim
$app = new Slim(array(
   'view' => new TwigView,
   'mode' => 'production'
));

// set some stuff that we use for lots of things
$app->hook('slim.before', function () use ($app) {
    $app->view()->appendData(array('basepath' => 'http://www.flyingabroad.co.uk/', 'appname' => 'Flying Abroad', 'copyrightdate' => date("Y")));
});

// other errors
$app->error(function (\Exception $e) use ($app) {
	$app->render('404.html');
});

// 404
$app->notFound(function () use ($app) {
	$app->render('404.html');
});

// home template
	$app->get('/', function() use ($app) {
	$app->etag('home123456789');
	$app->expires('+1 day');
	$data['title'] = 'Cheap flights and tickets, travel advice, airport parking, who flies where, and more';
	$data['metadescription'] = 'We provide advice and information on flying abroad from the UK, parking at airports or advice on cheap flights from UK airports';
	$data['metakeywords'] =  'cheap flights, who flies where, charter flights, scheduled flights, airport information, flight information, flying abroad, airport parking, airport hotels, fly and stay, travel advice';
	$options = array('limit' => 10, 'order' => 'datestamp desc');
	$flyingnews = Flyingnew::find('all', $options);
	$data['flyingnew'] = $flyingnews;
	$routes = Airport::find_by_sql('SELECT A.City as fromcity, A.Country as fromcountry, A.Region as fromregion, B.City as tocity, B.Country as tocountry, B.Region as toregion, C.AirlineName, R.LastUpdated FROM tblRoutes R LEFT JOIN tblAirports A ON A.AirportCode = R.SourceAirport LEFT JOIN tblAirports B ON R.DestinationAirport = B.AirportCode LEFT JOIN tblAirlines C ON R.AirlineCode = C.AirlineCode WHERE A.City IS NOT NULL AND A.Country IS NOT NULL AND A.Region IS NOT NULL AND B.City IS NOT NULL AND B.Country IS NOT NULL AND B.Region IS NOT NULL AND R.CodeShare <> "Y" AND R.Stops = 0 AND C.AirlineName IS NOT NULL ORDER BY R.ReleaseDate DESC LIMIT 10');
	$data['route'] = $routes;
	$options = array('limit' => 5, 'order' => 'lastupdated desc','conditions' => array("isbankrupt <> '1'"));
	$airlines = Airline::find('all', $options);
	return $app->render('home.html', array('airlines' => $airlines, 'routes' => $routes, 'flyingnews' => $flyingnews, 'urlcheck' => $data));
});

// region page (just redirect from aspx to html)

$app->get('/:region/default.html', function ($region) use ($app) {
	$region = str_replace("-", " ", $region);
	$record = Region::find($region);
	$data['title'] = 'Fancy a holiday, trip, break or flying to one of the many countries in ' . $record->regionname;
	$data['metadescription'] = 'Flying to one of the countries in ' . $record->regionname .' is easy with many airlines offering cheap flights - ideal for holidays and breaks abroad';
	$data['metakeywords'] = 'holidays to ' . $record->regionname . ', flying to ' . $record->regionname . ', short breaks in ' . $record->regionname . ', countries in ' . $record->regionname . ', fly to ' . $record->regionname;
	$date['text'] = $record->title;
	$data['region'] = $record->regionname;
	$data['topdescription'] = $record->topdescription;
	$data['bottomdescription'] = $record->bottomdescription;
	$options = array('order' => 'country asc', 'select' => 'distinct country', 'conditions' => array("region = '$record->regionname'"));
	$countrys = Airport::find('all', $options);
	$data['country'] = $countrys;
	return $app->render('region.html', array('countrys' => $countrys, 'urlcheck' => $data));
})->conditions(array('region' => '(Africa|Antartica|Asia|Caribbean|Europe|North-America|Pacific|South-America)'));

// country page (just redirect from aspx to html)

$app->get('/:region/:country/default.html', function ($region,$country) use ($app) {
	$region = str_replace("-", " ", $region);
	$country = str_replace("-", " ", $country);
	$record = Country::find($country);
	$data['region'] = $region;
	$data['country'] = $record->countryname;
	$data['title'] = 'Find cheap flights to ' . $record->countryname . ' in ' . $region;
	$data['metadescription'] = $record->countryname . ' in ' . $region . ' offers tourists and travellers a wealth of places to visit and cultural experiences.  Traditionally, flying to ' . $record->countryname . ' can be expensive, but with cheap flight tickets available to purchase over the...';
	$data['metakeywords'] = 'travel guide to ' . $record->countryname . ', miniguide to ' . $record->countryname . ', tourist guide, guide to ' . $record->countryname . ', fly to ' . $record->countryname . ', cheap flight tickets to ' . $record->countryname . ', ' . $record->countryname . ' travel advice, cities in ' . $record->countryname . ', travel tips';
	$data['topdescription'] = $record->topdescription;
	$data['bottomdescription'] = $record->bottomdescription;
	$options = array('order' => 'cityname asc', 'conditions' => array("country = '$record->countryname'"));
	$citys = City::find('all', $options);
	$data['city'] = $citys;
	return $app->render('country.html', array('citys' => $citys, 'urlcheck' => $data));
})->conditions(array('region' => '(Africa|Antartica|Asia|Caribbean|Europe|North-America|Pacific|South-America)'));

// Airports-In-.html (redirect from eg europe germany airports-in-germany to airports europe country airports in germany)

$app->get('/Airports/:region/:country.html', function ($region,$country) use ($app) {
	$country = str_replace("Airports-in-", "", $country);
	$country = str_replace("-", " ", $country);
	$data['country'] = $country;
	$data['region'] = $region;
	$data['title'] = 'Airports in ' . $country . ', fly to ' . $country . ' with a cheap flight ticket';
	$data['metadescription'] = 'We provide advice and information on flying abroad from the ' . $country .  ', parking at airports or advice on cheap flights from ' . $country . ' airports';
	$data['metakeywords'] = 'cheap flights, who flies where, charter flights, scheduled flights, airport information, flight information, flying abroad, airport parking, airport hotels, fly and stay, travel advice';
	$options = array('order' => 'airportname asc', 'conditions' => array("country = '$country'"));
	$airports = Airport::find('all', $options);
	return $app->render('airportsin.html', array('airports' => $airports, 'urlcheck' => $data));
});

// city page (just redirect from aspx to html)

$app->get('/:region/:country/:city.html', function ($region,$country,$city) use ($app) {
	$region = str_replace("-", " ", $region);
	$country = str_replace("-", " ", $country);
	$city = str_replace("-", " ", $city);
	$options = array('conditions' => array("cityname = '$city' AND country = '$country'"));
	$record = City::find($city, $options);
	$data['region'] = $region;
	$data['country'] = $country;
	$data['city'] = $record->cityname;
	$data['title'] = 'A Mini guide to ' . $record->cityname . ',' . $country;
	$data['metadescription'] = 'Travelling to ' . $record->cityname  . ' is easy with the right flight - but ensure you read our travel tips for ' . $record->cityname  . ' and we can ensure you get the cheapest flight there so youve got more to spend on your break or holiday';
	$data['metakeywords'] = 'guide to ' . $record->cityname . ', travel guide to ' . $record->cityname .', city guide to ' . $record->cityname . ', cheap flights to ' . $record->cityname . ', fly to ' . $record->cityname . ', who flies to ' . $record->cityname . ', flights to ' . $record->cityname . ', travel tips';
	$data['topdescription'] = $record->topdescription;
	$data['bottomdescription'] = $record->bottomdescription;
	$options = array('order' => 'airportname asc', 'conditions' => array("city = '$record->cityname' AND country = '$country'"));
	$airports = Airport::find('all', $options);
	$data['airport'] = $airports;
	$counter = count($airports);
	switch ($counter) {
	case 1:
		$airlines = Airline::find_by_sql('SELECT DISTINCT(A.AirlineName), A.website, A.affiliatetrackingcode FROM tblAirlines A, tblRoutes R WHERE R.AirlineCode = A.AirlineCode AND R.DestinationAirport IN ("' . $airports[0]->airportcode . '") AND R.CodeShare <> "Y" AND R.Stops = 0 AND A.IsBankrupt = 0 ORDER BY A.AirlineName ASC');
		break;
	case 2:
		$airlines = Airline::find_by_sql('SELECT DISTINCT(A.AirlineName), A.website, A.affiliatetrackingcode FROM tblAirlines A, tblRoutes R WHERE R.AirlineCode = A.AirlineCode AND R.DestinationAirport IN ("' . $airports[0]->airportcode . '", "' . $airports[1]->airportcode . '") AND R.CodeShare <> "Y" AND R.Stops = 0 AND A.IsBankrupt = 0 ORDER BY A.AirlineName ASC');
		break;
	case 3:
		$airlines = Airline::find_by_sql('SELECT DISTINCT(A.AirlineName), A.website, A.affiliatetrackingcode FROM tblAirlines A, tblRoutes R WHERE R.AirlineCode = A.AirlineCode AND R.DestinationAirport IN ("' . $airports[0]->airportcode . '", "' . $airports[1]->airportcode . '", "' . $airports[2]->airportcode . '") AND R.CodeShare <> "Y" AND R.Stops = 0 AND A.IsBankrupt = 0 ORDER BY A.AirlineName ASC');
		break;
	case 4:
		$airlines = Airline::find_by_sql('SELECT DISTINCT(A.AirlineName), A.website, A.affiliatetrackingcode FROM tblAirlines A, tblRoutes R WHERE R.AirlineCode = A.AirlineCode AND R.DestinationAirport IN ("' . $airports[0]->airportcode . '", "' . $airports[1]->airportcode . '", "' . $airports[2]->airportcode . '", "' . $airports[3]->airportcode . '") AND R.CodeShare <> "Y" AND R.Stops = 0 AND A.IsBankrupt = 0 ORDER BY A.AirlineName ASC');
		break;
	case 5:
		$airlines = Airline::find_by_sql('SELECT DISTINCT(A.AirlineName), A.website, A.affiliatetrackingcode FROM tblAirlines A, tblRoutes R WHERE R.AirlineCode = A.AirlineCode AND R.DestinationAirport IN ("' . $airports[0]->airportcode . '", "' . $airports[1]->airportcode . '", "' . $airports[2]->airportcode . '", "' . $airports[3]->airportcode . '", "' . $airports[4]->airportcode . '") AND R.CodeShare <> "Y" AND R.Stops = 0 AND A.IsBankrupt = 0 ORDER BY A.AirlineName ASC');
		break;
	case 6:
		$airlines = Airline::find_by_sql('SELECT DISTINCT(A.AirlineName), A.website, A.affiliatetrackingcode FROM tblAirlines A, tblRoutes R WHERE R.AirlineCode = A.AirlineCode AND R.DestinationAirport IN ("' . $airports[0]->airportcode . '", "' . $airports[1]->airportcode . '", "' . $airports[2]->airportcode . '", "' . $airports[3]->airportcode . '", "' . $airports[4]->airportcode . '", "' . $airports[5]->airportcode . '") AND R.CodeShare <> "Y" AND R.Stops = 0 AND A.IsBankrupt = 0 ORDER BY A.AirlineName ASC');
		break;
	case 7:
		$airlines = Airline::find_by_sql('SELECT DISTINCT(A.AirlineName), A.website, A.affiliatetrackingcode FROM tblAirlines A, tblRoutes R WHERE R.AirlineCode = A.AirlineCode AND R.DestinationAirport IN ("' . $airports[0]->airportcode . '", "' . $airports[1]->airportcode . '", "' . $airports[2]->airportcode . '", "' . $airports[3]->airportcode . '", "' . $airports[4]->airportcode . '", "' . $airports[5]->airportcode . '", "' . $airports[6]->airportcode . '") AND R.CodeShare <> "Y" AND R.Stops = 0 AND A.IsBankrupt = 0 ORDER BY A.AirlineName ASC');
		break;
	default:
		$airlines = "No Airlines fly";
		break;
	}
	$data['airline'] = $airlines;
return $app->render('city.html', array('airlines' => $airlines,'airports' => $airports, 'urlcheck' => $data));
})->conditions(array('region' => '(Africa|Antartica|Asia|Caribbean|Europe|North-America|Pacific|South-America)'));

// airport page (need to use htaccess to redirect to a new URL with city included)

$app->get('/:region/:country/:city/:airport.html', function ($region,$country,$city,$airport) use ($app) {
	$region = str_replace("-", " ", $region);
	$country = str_replace("-", " ", $country);
	$city = str_replace("-", " ", $city);
	$airport = str_replace("-", " ", $airport);
	$record = Airport::find('first', array('conditions' => array('airportname = ?', $airport)));
	$data['region'] = $region;
	$data['country'] = $country;
	$data['city'] = $city;
	$data['airport'] = $record->airportname;
	$data['title'] = 'Travelling to or from ' . $record->airportname . ' in ' . $city . ', ' . $country . '?';
	$data['metadescription'] = $record->airportname . ' has an airport code of ' . $record->airportcode . ' and is located in ' . $record->city . ', ' . $record->country;
	$data['metakeywords'] = $record->airportname . ', ' . $record->airportcode . ', airports in ' . $record->region . ', airports in ' . $city . ', cheap flights from ' . $city . ', fly from ' . $city . ', flights from ' . $city . ', travel advice, travel to ' . $city;
	$data['description'] = $record->description;
	$data['flightarrivalsurl'] = $record->flightarrivalsurl;
	$data['interestingfact'] = $record->interestingfact;
	$data['usefullink1'] = $record->usefullink1;
	$data['usefultext1'] = $record->usefultext1;
	$data['usefullink2'] = $record->usefullink2;
	$data['usefultext2'] = $record->usefultext2;
	$data['usefullink3'] = $record->usefullink3;
	$data['usefultext3'] = $record->usefultext3;
	$data['usefullink4'] = $record->usefullink4;
	$data['usefultext4'] = $record->usefultext4;
	$data['usefullink5'] = $record->usefullink5;
	$data['usefultext5'] = $record->usefultext5;
	$data['flightdeparturesurl'] = $record->flightdeparturesurl;
	$data['address1'] = $record->address1;
	$data['address2'] = $record->address2;
	$data['address3'] = $record->address3;
	$data['postcode'] = $record->postcode;
	$data['country'] = $record->country;
	$data['region'] = $record->region;
	$data['airportcode'] = $record->airportcode;
	// explode out latitude and longitude
	$pieces = explode(",", $record->geolocation);
	$data['latitude'] = $pieces[0];
	$data['longitude'] = $pieces[1];
	$data['telephone'] = $record->telephone;
	$data['fax'] = $record->fax;
	$data['emailaddress'] = $record->emailaddress;
	$data['howtogetthere'] = $record->howtogetthere;
	$data['numberofrunways'] = $record->numberofrunways;
	$data['numberofairlines'] = $record->numberofairlines;
	$data['numberofpassengersperyear'] = $record->numberofpassengersperyear;
	$data['numberofterminals'] = $record->numberofterminals;
	$data['numberofflightsperyear'] = $record->numberofflightsperyear;
	$data['ownedby'] = $record->ownedby;
	$data['numberofdestinations'] = $record->numberofdestinations;
	$airlines = Airline::find_by_sql('SELECT DISTINCT(A.AirlineName) FROM tblAirlines A, tblRoutes R WHERE R.AirlineCode = A.AirlineCode AND R.DestinationAirport = "' . $record->airportcode . '" AND R.CodeShare <> "Y" AND R.Stops = 0 AND A.IsBankrupt = 0 ORDER BY A.AirlineName ASC');
	$data['airline'] = $airlines;
	$flytos = Airline::find_by_sql('SELECT A.Country, A.City, B.AirlineName, A.Region, A.CountryIcon, B.AffiliateTrackingCode FROM tblAirports A, tblRoutes R LEFT JOIN tblAirlines B ON R.AirlineCode = B.AirlineCode WHERE R.DestinationAirport = A.AirportCode AND R.SourceAirport = "' . $record->airportcode . '" AND R.CodeShare <> "Y" AND R.Stops = 0 ORDER BY A.Country, A.City ASC');
	//$data['flyto'] = $flytos;
	return $app->render('airport.html', array('flytos' => $flytos, 'airlines' => $airlines, 'urlcheck' => $data));
})->conditions(array('region' => '(Africa|Antartica|Asia|Caribbean|Europe|North-America|Pacific|South-America)'));

// airline page (need to use htaccess to redirect from asp to html)

$app->get('/Airlines/:airline.html', function ($airline) use ($app) {
	$airline = str_replace("-", " ", $airline);
	$options = array('conditions' => array("airlinename = '$airline'"));
	$record = Airline::find($airline, $options);
	$data['airlinename'] = $record->airlinename;
	$data['title'] = $record->airlinename . ' | Where does ' . $record->airlinename . ' fly to? ';
	$data['metadescription'] = $record->airlinename . ' has a airline code of ' . $record->airlinecode . ' and its home location is ' . $record->country . ', ' . $record->region;
	$data['metakeywords'] = $record->airlinename . ', ' . $record->airlinecode . ', fly with ' . $record->airlinename . ', cheap flights with ' . $record->airlinename;
	$data['country'] = $record->country;
	$data['region'] = $record->region;
	$data['website'] = $record->website;
	$data['affiliatetrackingcode'] = $record->affiliatetrackingcode;
	$data['airlinecode'] = $record->airlinecode;
	$data['affiliatetrackingbanner'] = $record->affiliatetrackingbanner;
	$data['description'] = $record->description;
	$data['reservations'] = $record->telephone;
	$data['logofilename'] = $record->logofilename;
	$data['isbankrupt'] = $record->isbankrupt;
	$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$record->airlinecode%' OR headline LIKE '%$record->airlinename%'"));
	$flyingnews = Flyingnew::find('all', $options);
	$data['flyingnew'] = $flyingnews;
	$routes = Airport::find_by_sql('SELECT A.Region as fromregion, A.City as fromcity, A.Country as fromcountry, A.CountryIcon as fromcountryicon, B.Region as toregion, B.City as tocity, B.Country as tocountry, B.CountryIcon as tocountryicon FROM tblAirports A LEFT JOIN tblRoutes R ON A.AirportCode = R.SourceAirport LEFT JOIN tblAirports B ON R.DestinationAirport = B.AirportCode WHERE B.City IS NOT NULL AND R.CodeShare <> "Y" AND R.Stops = 0 AND R.AirlineCode = "' . $record->airlinecode . '" ORDER BY A.City, A.Country, B.City, B.Country DESC');
	$data['route'] = $routes;
	return $app->render('airline.html', array('routes' => $routes, 'flyingnews' => $flyingnews, 'urlcheck' => $data));
});

// individual airline news pages (just redirect needed from aspx to html)

$app->get('/:airline/News.html', function ($airline) use ($app) {
	$airline = str_replace("-", " ", $airline);
	$options = array('conditions' => array("airlinename = '$airline'"));
	$record = Airline::find($airline, $options);
	$data['airlinename'] = $record->airlinename;
	$data['airlinecode'] = $record->airlinecode;
	$data['title'] = $record->airlinename . ' news stories and archive';
	$data['metadescription'] = 'Current and historical news on ' . $record->airlinename . ' with information on flight offers, new routes, company news and more.';
	$data['metakeywords'] = 'news about ' . $record->airlinename . ', ' . $record->airlinename . ' news, airport news, airports news, travel advice';
	$options = array('order' => 'datestamp desc', 'conditions' => array("keywords like '%$record->airlinecode%' OR headline LIKE '%$record->airlinename%'"));
	$flyingnews = Flyingnew::find('all', $options);
	$data['flyingnew'] = $flyingnews;
	return $app->render('airlinenews.html', array('flyingnews' => $flyingnews, 'urlcheck' => $data));
});

// tag page

$app->get('/tag/:tagname', function ($tagname) use ($app) {
	$options = array('order' => 'datestamp desc', 'conditions' => array("keywords like '%$tagname%'"));
	$data['tagname'] = $tagname;
	$flyingnews = Flyingnew::find('all', $options);
	$data['title'] = 'News stories tagged with #' . $tagname;
	$data['metadescription'] = 'All news stories in Flying Abroad with the tag #' . $tagname;
	$data['metakeywords'] = 'tags,flying,abroad,#' . $tagname;
	return $app->render('singletag.html', array('flyingnews' => $flyingnews, 'urlcheck' => $data));
});

// news archive (no redirect)
	$app->get('/News/:archivedate/', function ($archivedate) use ($app) {
	$options = array('order' => 'datestamp desc', 'conditions' => array("url like '%$archivedate%'"));
	$flyingnews = Flyingnew::find('all', $options);
	$data['flyingnew'] = $flyingnews;
	$data['archivedate'] = $archivedate;
	$data['title'] = 'Travel news for Flyers in ' . str_replace("-", " ", $archivedate)  . ' - ensure you are prepared before going abroad';
	$data['metadescription'] = 'Updated several times a day, details of news affecting travel, airlines, cheap flight sales and advice.';
	$data['metakeywords'] = 'travel news for flyers, airport news, uk airports, passengers, heathrow airport, gatwick airport, travel advice';
	$archives = Flyingnew::find_by_sql('SELECT COUNT(*), YEAR(datestamp) AS year, MONTHNAME(datestamp) AS month FROM tblNews GROUP BY year, month ORDER BY year DESC, MONTH(datestamp) DESC');
	$data['archive'] = $archives;
	return $app->render('archivenews.html', array('archives' => $archives, 'flyingnews' => $flyingnews, 'urlcheck' => $data));
});


// individual news articles (just redirect needed from aspx to html

$app->get('/News/:articledate/:article', function ($articledate,$article) use ($app) {
	$articlelink = "News/" . $articledate . "/" . $article;
	$options = array('conditions' => array("url = '$articlelink'"));
	$record = Flyingnew::find($articlelink, $options);
	$data['datestamp'] = $record->datestamp;
	$data['headline'] = $record->headline;
	$data['description'] = $record->description;
	$data['metakeywords'] = $record->metakeywords;
	$data['metadescription'] = $record->metadescription;
	$data['title'] = $record->titletag;
	$data['keywords'] = $record->keywords;
	$tags = explode(",",$record->keywords);
	$counter = count($tags);
	switch ($counter) {
	case 1:
		$options = array('conditions' => array("airlinecode = '$tags[0]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 2:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 3:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 4:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]' OR airlinecode = '$tags[3]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%' OR keywords LIKE '%$tags[3]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 5:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]' OR airlinecode = '$tags[3]' OR airlinecode = '$tags[4]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%' OR keywords LIKE '%$tags[3]%' OR keywords LIKE '%$tags[4]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 6:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]' OR airlinecode = '$tags[3]' OR airlinecode = '$tags[4]' OR airlinecode = '$tags[5]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%' OR keywords LIKE '%$tags[3]%' OR keywords LIKE '%$tags[4]%' OR keywords LIKE '%$tags[5]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 7:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]' OR airlinecode = '$tags[3]' OR airlinecode = '$tags[4]' OR airlinecode = '$tags[5]' OR airlinecode = '$tags[6]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%' OR keywords LIKE '%$tags[3]%' OR keywords LIKE '%$tags[4]%' OR keywords LIKE '%$tags[5]%' OR keywords LIKE '%$tags[6]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 8:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]' OR airlinecode = '$tags[3]' OR airlinecode = '$tags[4]' OR airlinecode = '$tags[5]' OR airlinecode = '$tags[6]' OR airlinecode = '$tags[7]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%' OR keywords LIKE '%$tags[3]%' OR keywords LIKE '%$tags[4]%' OR keywords LIKE '%$tags[5]%' OR keywords LIKE '%$tags[6]%' OR keywords LIKE '%$tags[7]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 9:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]' OR airlinecode = '$tags[3]' OR airlinecode = '$tags[4]' OR airlinecode = '$tags[5]' OR airlinecode = '$tags[6]' OR airlinecode = '$tags[7]' OR airlinecode = '$tags[8]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%' OR keywords LIKE '%$tags[3]%' OR keywords LIKE '%$tags[4]%' OR keywords LIKE '%$tags[5]%' OR keywords LIKE '%$tags[6]%' OR keywords LIKE '%$tags[7]%' OR keywords LIKE '%$tags[8]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 10:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]' OR airlinecode = '$tags[3]' OR airlinecode = '$tags[4]' OR airlinecode = '$tags[5]' OR airlinecode = '$tags[6]' OR airlinecode = '$tags[7]' OR airlinecode = '$tags[8]' OR airlinecode = '$tags[9]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%' OR keywords LIKE '%$tags[3]%' OR keywords LIKE '%$tags[4]%' OR keywords LIKE '%$tags[5]%' OR keywords LIKE '%$tags[6]%' OR keywords LIKE '%$tags[7]%' OR keywords LIKE '%$tags[8]%' OR keywords LIKE '%$tags[9]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 11:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]' OR airlinecode = '$tags[3]' OR airlinecode = '$tags[4]' OR airlinecode = '$tags[5]' OR airlinecode = '$tags[6]' OR airlinecode = '$tags[7]' OR airlinecode = '$tags[8]' OR airlinecode = '$tags[9]' OR airlinecode = '$tags[10]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%' OR keywords LIKE '%$tags[3]%' OR keywords LIKE '%$tags[4]%' OR keywords LIKE '%$tags[5]%' OR keywords LIKE '%$tags[6]%' OR keywords LIKE '%$tags[7]%' OR keywords LIKE '%$tags[8]%' OR keywords LIKE '%$tags[9]%' OR keywords LIKE '%$tags[10]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 12:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]' OR airlinecode = '$tags[3]' OR airlinecode = '$tags[4]' OR airlinecode = '$tags[5]' OR airlinecode = '$tags[6]' OR airlinecode = '$tags[7]' OR airlinecode = '$tags[8]' OR airlinecode = '$tags[9]' OR airlinecode = '$tags[10]' OR airlinecode = '$tags[11]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%' OR keywords LIKE '%$tags[3]%' OR keywords LIKE '%$tags[4]%' OR keywords LIKE '%$tags[5]%' OR keywords LIKE '%$tags[6]%' OR keywords LIKE '%$tags[7]%' OR keywords LIKE '%$tags[8]%' OR keywords LIKE '%$tags[9]%' OR keywords LIKE '%$tags[10]%' OR keywords LIKE '%$tags[11]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 13:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]' OR airlinecode = '$tags[3]' OR airlinecode = '$tags[4]' OR airlinecode = '$tags[5]' OR airlinecode = '$tags[6]' OR airlinecode = '$tags[7]' OR airlinecode = '$tags[8]' OR airlinecode = '$tags[9]' OR airlinecode = '$tags[10]' OR airlinecode = '$tags[11]' OR airlinecode = '$tags[12]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%' OR keywords LIKE '%$tags[3]%' OR keywords LIKE '%$tags[4]%' OR keywords LIKE '%$tags[5]%' OR keywords LIKE '%$tags[6]%' OR keywords LIKE '%$tags[7]%' OR keywords LIKE '%$tags[8]%' OR keywords LIKE '%$tags[9]%' OR keywords LIKE '%$tags[10]%' OR keywords LIKE '%$tags[11]%' OR keywords LIKE '%$tags[12]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 14:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]' OR airlinecode = '$tags[3]' OR airlinecode = '$tags[4]' OR airlinecode = '$tags[5]' OR airlinecode = '$tags[6]' OR airlinecode = '$tags[7]' OR airlinecode = '$tags[8]' OR airlinecode = '$tags[9]' OR airlinecode = '$tags[10]' OR airlinecode = '$tags[11]' OR airlinecode = '$tags[12]' OR airlinecode = '$tags[13]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%' OR keywords LIKE '%$tags[3]%' OR keywords LIKE '%$tags[4]%' OR keywords LIKE '%$tags[5]%' OR keywords LIKE '%$tags[6]%' OR keywords LIKE '%$tags[7]%' OR keywords LIKE '%$tags[8]%' OR keywords LIKE '%$tags[9]%' OR keywords LIKE '%$tags[10]%' OR keywords LIKE '%$tags[11]%' OR keywords LIKE '%$tags[12]%' OR keywords LIKE '%$tags[13]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 15:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]' OR airlinecode = '$tags[3]' OR airlinecode = '$tags[4]' OR airlinecode = '$tags[5]' OR airlinecode = '$tags[6]' OR airlinecode = '$tags[7]' OR airlinecode = '$tags[8]' OR airlinecode = '$tags[9]' OR airlinecode = '$tags[10]' OR airlinecode = '$tags[11]' OR airlinecode = '$tags[12]' OR airlinecode = '$tags[13]' OR airlinecode = '$tags[14]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%' OR keywords LIKE '%$tags[3]%' OR keywords LIKE '%$tags[4]%' OR keywords LIKE '%$tags[5]%' OR keywords LIKE '%$tags[6]%' OR keywords LIKE '%$tags[7]%' OR keywords LIKE '%$tags[8]%' OR keywords LIKE '%$tags[9]%' OR keywords LIKE '%$tags[10]%' OR keywords LIKE '%$tags[11]%' OR keywords LIKE '%$tags[12]%' OR keywords LIKE '%$tags[13]%' OR keywords LIKE '%$tags[14]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 16:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]' OR airlinecode = '$tags[3]' OR airlinecode = '$tags[4]' OR airlinecode = '$tags[5]' OR airlinecode = '$tags[6]' OR airlinecode = '$tags[7]' OR airlinecode = '$tags[8]' OR airlinecode = '$tags[9]' OR airlinecode = '$tags[10]' OR airlinecode = '$tags[11]' OR airlinecode = '$tags[12]' OR airlinecode = '$tags[13]' OR airlinecode = '$tags[14]' OR airlinecode = '$tags[15]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%' OR keywords LIKE '%$tags[3]%' OR keywords LIKE '%$tags[4]%' OR keywords LIKE '%$tags[5]%' OR keywords LIKE '%$tags[6]%' OR keywords LIKE '%$tags[7]%' OR keywords LIKE '%$tags[8]%' OR keywords LIKE '%$tags[9]%' OR keywords LIKE '%$tags[10]%' OR keywords LIKE '%$tags[11]%' OR keywords LIKE '%$tags[12]%' OR keywords LIKE '%$tags[13]%' OR keywords LIKE '%$tags[14]%' OR keywords LIKE '%$tags[15]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 17:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]' OR airlinecode = '$tags[3]' OR airlinecode = '$tags[4]' OR airlinecode = '$tags[5]' OR airlinecode = '$tags[6]' OR airlinecode = '$tags[7]' OR airlinecode = '$tags[8]' OR airlinecode = '$tags[9]' OR airlinecode = '$tags[10]' OR airlinecode = '$tags[11]' OR airlinecode = '$tags[12]' OR airlinecode = '$tags[13]' OR airlinecode = '$tags[14]' OR airlinecode = '$tags[15]' OR airlinecode = '$tags[16]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%' OR keywords LIKE '%$tags[3]%' OR keywords LIKE '%$tags[4]%' OR keywords LIKE '%$tags[5]%' OR keywords LIKE '%$tags[6]%' OR keywords LIKE '%$tags[7]%' OR keywords LIKE '%$tags[8]%' OR keywords LIKE '%$tags[9]%' OR keywords LIKE '%$tags[10]%' OR keywords LIKE '%$tags[11]%' OR keywords LIKE '%$tags[12]%' OR keywords LIKE '%$tags[13]%' OR keywords LIKE '%$tags[14]%' OR keywords LIKE '%$tags[15]%' OR keywords LIKE '%$tags[16]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 18:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]' OR airlinecode = '$tags[3]' OR airlinecode = '$tags[4]' OR airlinecode = '$tags[5]' OR airlinecode = '$tags[6]' OR airlinecode = '$tags[7]' OR airlinecode = '$tags[8]' OR airlinecode = '$tags[9]' OR airlinecode = '$tags[10]' OR airlinecode = '$tags[11]' OR airlinecode = '$tags[12]' OR airlinecode = '$tags[13]' OR airlinecode = '$tags[14]' OR airlinecode = '$tags[15]' OR airlinecode = '$tags[16]' OR airlinecode = '$tags[17]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%' OR keywords LIKE '%$tags[3]%' OR keywords LIKE '%$tags[4]%' OR keywords LIKE '%$tags[5]%' OR keywords LIKE '%$tags[6]%' OR keywords LIKE '%$tags[7]%' OR keywords LIKE '%$tags[8]%' OR keywords LIKE '%$tags[9]%' OR keywords LIKE '%$tags[10]%' OR keywords LIKE '%$tags[11]%' OR keywords LIKE '%$tags[12]%' OR keywords LIKE '%$tags[13]%' OR keywords LIKE '%$tags[14]%' OR keywords LIKE '%$tags[15]%' OR keywords LIKE '%$tags[16]%' OR keywords LIKE '%$tags[17]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 19:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]' OR airlinecode = '$tags[3]' OR airlinecode = '$tags[4]' OR airlinecode = '$tags[5]' OR airlinecode = '$tags[6]' OR airlinecode = '$tags[7]' OR airlinecode = '$tags[8]' OR airlinecode = '$tags[9]' OR airlinecode = '$tags[10]' OR airlinecode = '$tags[11]' OR airlinecode = '$tags[12]' OR airlinecode = '$tags[13]' OR airlinecode = '$tags[14]' OR airlinecode = '$tags[15]' OR airlinecode = '$tags[16]' OR airlinecode = '$tags[17]' OR airlinecode = '$tags[18]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%' OR keywords LIKE '%$tags[3]%' OR keywords LIKE '%$tags[4]%' OR keywords LIKE '%$tags[5]%' OR keywords LIKE '%$tags[6]%' OR keywords LIKE '%$tags[7]%' OR keywords LIKE '%$tags[8]%' OR keywords LIKE '%$tags[9]%' OR keywords LIKE '%$tags[10]%' OR keywords LIKE '%$tags[11]%' OR keywords LIKE '%$tags[12]%' OR keywords LIKE '%$tags[13]%' OR keywords LIKE '%$tags[14]%' OR keywords LIKE '%$tags[15]%' OR keywords LIKE '%$tags[16]%' OR keywords LIKE '%$tags[17]%' OR keywords LIKE '%$tags[18]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 20:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]' OR airlinecode = '$tags[3]' OR airlinecode = '$tags[4]' OR airlinecode = '$tags[5]' OR airlinecode = '$tags[6]' OR airlinecode = '$tags[7]' OR airlinecode = '$tags[8]' OR airlinecode = '$tags[9]' OR airlinecode = '$tags[10]' OR airlinecode = '$tags[11]' OR airlinecode = '$tags[12]' OR airlinecode = '$tags[13]' OR airlinecode = '$tags[14]' OR airlinecode = '$tags[15]' OR airlinecode = '$tags[16]' OR airlinecode = '$tags[17]' OR airlinecode = '$tags[18]' OR airlinecode = '$tags[19]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%' OR keywords LIKE '%$tags[3]%' OR keywords LIKE '%$tags[4]%' OR keywords LIKE '%$tags[5]%' OR keywords LIKE '%$tags[6]%' OR keywords LIKE '%$tags[7]%' OR keywords LIKE '%$tags[8]%' OR keywords LIKE '%$tags[9]%' OR keywords LIKE '%$tags[10]%' OR keywords LIKE '%$tags[11]%' OR keywords LIKE '%$tags[12]%' OR keywords LIKE '%$tags[13]%' OR keywords LIKE '%$tags[14]%' OR keywords LIKE '%$tags[15]%' OR keywords LIKE '%$tags[16]%' OR keywords LIKE '%$tags[17]%' OR keywords LIKE '%$tags[18]%' OR keywords LIKE '%$tags[19]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	case 21:
		$options = array('conditions' => array("airlinecode = '$tags[0]' OR airlinecode = '$tags[1]' OR airlinecode = '$tags[2]' OR airlinecode = '$tags[3]' OR airlinecode = '$tags[4]' OR airlinecode = '$tags[5]' OR airlinecode = '$tags[6]' OR airlinecode = '$tags[7]' OR airlinecode = '$tags[8]' OR airlinecode = '$tags[9]' OR airlinecode = '$tags[10]' OR airlinecode = '$tags[11]' OR airlinecode = '$tags[12]' OR airlinecode = '$tags[13]' OR airlinecode = '$tags[14]' OR airlinecode = '$tags[15]' OR airlinecode = '$tags[16]' OR airlinecode = '$tags[17]' OR airlinecode = '$tags[18]' OR airlinecode = '$tags[19]' OR airlinecode = '$tags[20]'"));
		$airlines = Airline::find('all', $options);
		$options = array('limit' => 10, 'order' => 'datestamp desc', 'conditions' => array("keywords like '%$tags[0]%' OR keywords LIKE '%$tags[1]%' OR keywords LIKE '%$tags[2]%' OR keywords LIKE '%$tags[3]%' OR keywords LIKE '%$tags[4]%' OR keywords LIKE '%$tags[5]%' OR keywords LIKE '%$tags[6]%' OR keywords LIKE '%$tags[7]%' OR keywords LIKE '%$tags[8]%' OR keywords LIKE '%$tags[9]%' OR keywords LIKE '%$tags[10]%' OR keywords LIKE '%$tags[11]%' OR keywords LIKE '%$tags[12]%' OR keywords LIKE '%$tags[13]%' OR keywords LIKE '%$tags[14]%' OR keywords LIKE '%$tags[15]%' OR keywords LIKE '%$tags[16]%' OR keywords LIKE '%$tags[17]%' OR keywords LIKE '%$tags[18]%' OR keywords LIKE '%$tags[19]%' OR keywords LIKE '%$tags[20]%'"));
		$flyingnews = Flyingnew::find('all', $options);
		break;
	default:
		$airlines = "No Airlines fly";
		break;
	}
	return $app->render('newsarticle.html', array('airlines' => $airlines, 'flyingnews' => $flyingnews, 'tags' => $tags, 'urlcheck' => $data));
});

// all airlines in UK (no redirect)

$app->get('/Airlines/', function () use ($app) {
	//$options = array('order' => 'airlinename asc');
	//$airlines = Airline::find('all', $options);
	$app->etag('airlines123456789');
	$app->expires('+1 day');
	$airlines = Airline::find_by_sql('SELECT DISTINCT(A.AirlineName), A.IsBankrupt, A.Country, A.Region FROM tblAirlines A LEFT JOIN tblRoutes R ON A.AirlineCode = R.AirlineCode LEFT JOIN tblAirports B ON R.DestinationAirport = B.AirportCode WHERE B.Country = "UK" AND R.DestinationAirport <> "" ORDER BY A.AirlineName ASC');
	$data['title'] = 'Airlines which fly to the UK - cheap flights and airlines provider passenger flights around the world';
	$data['metadescription'] = 'A list of all cheap flight airlines flying in and out of the UK';
	$data['metakeywords'] = 'all,airlines,uk,flying';
	$data['airline'] = $airlines;
	return $app->render('allairlines.html', array('airlines' => $airlines, 'urlcheck' => $data));
});

// all airlines (no redirect)

$app->get('/All-Airlines/', function () use ($app) {
	//$options = array('order' => 'airlinename asc');
	//$airlines = Airline::find('all', $options);
	$app->etag('allairlines123456789');
	$app->expires('+1 day');
	$airlines = Airline::find_by_sql('SELECT DISTINCT(A.AirlineName), A.IsBankrupt, A.Country as Country, A.Region FROM tblAirlines A LEFT JOIN tblRoutes R ON A.AirlineCode = R.AirlineCode LEFT JOIN tblAirports B ON R.DestinationAirport = B.AirportCode AND R.DestinationAirport <> "" ORDER BY A.AirlineName ASC');
	$data['title'] = 'Cheap flights and airlines provider passenger flights around the world';
	$data['metadescription'] = 'A list of all airlines globally';
	$data['metakeywords'] = 'all,airlines,global,flying';
	$data['airline'] = $airlines;
	return $app->render('everyairline.html', array('airlines' => $airlines, 'urlcheck' => $data));
});

//all news

$app->get('/News/', function () use ($app) {
	$flyingnews = Flyingnew::find('all', array('limit' => '80', 'order' => 'datestamp desc'));
	$data['flyingnew'] = $flyingnews;
	$archives = Flyingnew::find_by_sql('SELECT COUNT(*), YEAR(datestamp) AS year, MONTHNAME(datestamp) AS month FROM tblNews GROUP BY year, month ORDER BY year DESC, MONTH(datestamp) DESC');
	$data['archive'] = $archives;
	$data['title'] = 'Travel news Flyers - ensure you are prepared before going abroad';
	$data['metadescription'] = 'Updated several times a day, details of news affecting travel, airlines, cheap flight sales and advice.';
	$data['metakeywords'] = 'travel news for flyers, airport news, uk airports, passengers, heathrow airport, gatwick airport, travel advice';
	return $app->render('allnews.html', array('archives' => $archives, 'flyingnews' => $flyingnews, 'urlcheck' => $data));
});

// advance passenger info (redirect from aspx to html

$app->get('/Advice/Advance-Passenger-Information.html', function () use ($app) {
	$data['title'] = 'Advance Passenger Information (API) What is it and why its very important!';
	$data['metadescription'] = 'Governments of several countries now require airlines to collect Advance Passenger Information for passengers PRIOR to them travelling.  The exact information can vary depending...';
	$data['metakeywords'] = 'flying to spain, advance passenger information, api, airport security, passport control, xray machines, security search, passenger searches';
	return $app->render('advance-passenger-information.html', array('urlcheck' => $data));
});

// Healthcare-Whilst-Abroad (redirect from aspx to html)

$app->get('/Advice/Healthcare-Whilst-Abroad.html', function () use ($app) {
	$data['title'] = 'How to ensure you have the right healthcare when travelling abroad';
	$data['metadescription'] = 'What would happen if you were to become ill, or have an accident or injury whilst on holiday?  Many people do not realise it, but many travel insurance schemes will have clauses in their policies..';
	$data['metakeywords'] = 'EHIC, accident on holiday, E111, travel insurance, medical help';
	return $app->render('healthcare.html', array('urlcheck' => $data));
});

// Shopping-At-The-Airport (redirect from aspx to html

$app->get('/Advice/Shopping-At-The-Airport.html', function () use ($app) {
	$data['title'] = 'Shopping At The Airport';
	$data['metadescription'] = 'Hints and tips on shopping at airports before you fly - and how to make sure you are staying within your duty free allowances.  All UK Airports have shopping facilities both before checking in (Landside) and after checking in..';
	$data['metakeywords'] = 'landside shopping, airside shopping, airport shopping, duty free shopping, duty free allowances, customers allowance';
	return $app->render('shopping.html', array('urlcheck' => $data));
});

// Shopping-Abroad (redirect from aspx to html

$app->get('/Advice/Shopping-Abroad.html', function () use ($app) {
	$data['title'] = 'Shopping Abroad - What are you allowed to bring back into the country?';
	$data['metadescription'] = 'Duty free advice and rules on what you can and cannot buy when going and coming back from holiday.  What you are permitted to bring back from abroad without paying any extra tax and VAT will depend on whether you are arriving from ...';
	$data['metakeywords'] = 'duty free, cheap alcogol, import duty, duty free allowances, eu allowances, non-eu allowances, canary island duty free';
	return $app->render('abroad.html', array('urlcheck' => $data));
});

// Airport-Taxes (redirect from aspx to html

$app->get('/Advice/Airport-Taxes.html', function () use ($app) {
	$data['title'] = 'Airport Taxes - What they are, and why they can make your flight expensive';
	$data['metadescription'] = 'Airport taxes are in the main unavoidable and can add tens of pounds to your flight.  When you buy your flight ticket (whether direct with the airline, or part of a package holiday)...';
	$data['metakeywords'] = 'airport taxes, tax for flying, air passenger duty, tax on flights';
	return $app->render('taxes.html', array('urlcheck' => $data));
});

// Airport-Security (redirect from aspx to html

$app->get('/Advice/Airport-Security.html', function () use ($app) {
	$data['title'] = 'Airport Security - all you need to know before flying';
	$data['metadescription'] = 'It.s a fact of life that when you fly anywhere these days, whether it is abroad or within the UK then you will have .....';
	$data['metakeywords'] = 'airport security, passport control, xray machines, security search, passenger searches';
	return $app->render('security.html', array('urlcheck' => $data));
});

// All-About-DVT (redirect from aspx to html

$app->get('/Advice/All-About-DVT.html', function () use ($app) {
	$data['title'] = 'DVT - All About Deep Vein Thrombosis';
	$data['metadescription'] = 'Commonly called \"economy class syndrome\" by the media, DVT a blood clot in a vein, usually in the leg, which may cause swelling - if untreated, it can be a cause of death.';
	$data['metakeywords'] = 'dvt, deep vein thrombosis, deep venous thrombosis';
	return $app->render('dvt.html', array('urlcheck' => $data));
});

// biggest-and-busiest-airports-in-the-world (redirect from aspx to html

$app->get('/biggest-and-busiest-airports-in-the-world.html', function () use ($app) {
	$data['title'] = 'What are the biggest and busiest airports in the world?';
	$data['metadescription'] = 'What is the biggest and/or busiest airport in the world?';
	$data['metakeywords'] = 'largest airport, busiest airport, atlanta airport, most passengers, most cargo, most international passengers';
	return $app->render('busiest.html', array('urlcheck' => $data));
});

// contact (redirect from aspx to html)

	$app->get('/Contact-Us.html', function() use ($app) {
	$data['title'] = 'Contact Us';
	$data['metadescription'] = 'If you want to contact us, then please use these details below.  Note that we will try and get back to you as soon as possible, although it may be up to 48 hours later.';
	$data['metakeywords'] = 'contact form, email form, online enquiry form';
	return $app->render('contact.html', array('urlcheck' => $data));
});

// terms of use (redirect from aspx to html)

	$app->get('/Information/Terms-Of-Use.html', function() use ($app) {
	$data['title'] = 'Terms Of Use';
	$data['metadescription'] = 'FlyingAbroad.co.uk can only be used if you agree to our terms of use...';
	$data['metakeywords'] = 'terms of use, terms and conditions';
	return $app->render('terms.html', array('urlcheck' => $data));
});

// privacy policy (redirect from aspx to html)

	$app->get('/Information/Privacy-Policy.html', function() use ($app) {
	$data['title'] = 'Privacy Policy';
	$data['metadescription'] = 'At FlyingAbroad.co.uk we value your privacy and this is our privacy policy is...';
	$data['metakeywords'] = 'privacy policy';
	return $app->render('privacy.html', array('urlcheck' => $data));
});

// about us (redirect from aspx to html)

	$app->get('/Information/About-Us.html', function() use ($app) {
	$data['title'] = 'About Flying Abroad';
	$data['metadescription'] = 'FlyingAbroad.co.uk was setup to fill the gap in the market that other websites do not fill for important information about flying abroad...';
	$data['metakeywords'] = 'airport websites, airport parking, airport information, cheap flights, save money, best deal';
	return $app->render('about.html', array('urlcheck' => $data));
});

// cheap flight search (redirect from aspx to html)

	$app->get('/Flight-Search.html', function() use ($app) {
	$data['title'] = 'Find cheap flights to Europe';
	$data['metadescription'] = 'Airlines are fighting for your business and shopping around with the major agents will help you to get the cheapest tickets. We can strongly recommend the....';
	$data['metakeywords'] = 'cheap flight tickets, flights to europe, cheap flights, ticket saver, save on flights, flights to holiday, going on holiday';
	return $app->render('search.html', array('urlcheck' => $data));
});

// travel advice (redirect from aspx to html)

	$app->get('/Advice/', function() use ($app) {
	$data['title'] = 'Travel and Airport Advice';
	$data['metadescription'] = 'Impartial and no-nonsense advice on getting to the airport, getting to your destination and when you are out there.';
	$data['metakeywords'] = 'travel advice, e111, health advice, airport advice, advice on travelling';
	return $app->render('advice.html', array('urlcheck' => $data));
});

// Newsletters (redirect from aspx to html)

	$app->get('/Newsletters/', function() use ($app) {
	$data['title'] = 'Newsletters';
	$data['metadescription'] = 'We provide advice and information on flying abroad from the UK, parking at airports or advice on cheap flights from UK airports, all via our newsletter';
	$data['metakeywords'] = 'cheap flights, who flies where, charter flights, scheduled flights, airport information, flight information, flying abroad, airport parking, airport hotels, fly and stay, travel advice,newsletter';
	return $app->render('newsletters.html', array('urlcheck' => $data));
});

// redirects

$app->get('/Site-Improvements.aspx', function () use ($app) {
    $app->redirect($app->urlFor('Site-Improvements.html'), 301);
	//return $app->redirect('/Newsletters', 301);
});

// site improvement (redirect from aspx to html)

	$app->get('/Site-Improvements.html', function() use ($app) {
	$data['title'] = 'Improvements and enhancements to the Flying Abroad website';
	$data['metadescription'] = 'We are committed to improving the breadth and scope of information on our website and making it a very useful resource for occasional and regular travellers alike.';
	$data['metakeywords'] = 'site improvements, google maps, airport maps, site updates, new features';
	return $app->render('improvements.html', array('urlcheck' => $data));
});

// etiquette

	$app->get('/flying-etiquette', function() use ($app) {
	$data['title'] = 'Top 10 airline and flying etiquette tips';
	$data['metadescription'] = 'Flying and airline etiquette tips';
	$data['metakeywords'] = 'airline,etiquette,flying,tips';
	return $app->render('etiquette.html', array('urlcheck' => $data));
});

// twitter social page

	$app->get('/social', function() use ($app) {
	$data['title'] = 'Social networks for airlines and airports including Twitter';
	$data['metadescription'] = 'all the airlines and airports on Twitter and other social networks';
	$data['metakeywords'] = 'twitter,airlines,airports,facebook,social';
	$options = array('order' => 'airportname asc', 'conditions' => array("Twitter != ''"));
	$airports = Airport::find('all', $options);
	$options = array('order' => 'airlinename asc', 'conditions' => array("Twitter != ''"));
	$airlines = Airline::find('all', $options);
	return $app->render('twitter.html', array('urlcheck' => $data, 'airports' => $airports, 'airlines' => $airlines));
});

// points converter

	$app->get('/points-converter', function() use ($app) {
	$data['title'] = 'Credit card and loyalty points conversion calculator';
	$data['metadescription'] = 'Conversion calculator';
	$data['metakeywords'] = 'airline,credit card,hotel,loyalty,converter,calculator';
	return $app->render('points.html', array('urlcheck' => $data));
});

// travel compensation

	$app->get('/travel-compensation', function() use ($app) {
	$data['title'] = 'Travel compensation for delayed or cancelled flights';
	$data['metadescription'] = 'Claim for travel delays and cancellations from EU airlines';
	$data['metakeywords'] = 'EU,travel,delays,cancellation,compensation';
	$options = array('order' => 'airportname asc');
	$airports = Airport::find('all', $options);
	$options = array('order' => 'AirlineName asc', 'conditions' => array('IsBankrupt = 0'));
	$airlines = Airline::find('all', $options);
	return $app->render('travel-compensation.html', array('urlcheck' => $data, 'airports' => $airports, 'airlines' => $airlines));
});

// claim eligibility check

	$app->post('/travel-compensation/cecheck', function() use ($app) {
	$from = $app->request()->post('from');
	$airline = $app->request()->post('airline');
	
	$airline_region = Airline::find('all', array('select' => 'Region', 'conditions' => array('AirlineName = ?', $airline)));
	$airport_region = Airport::find('all', array('select' => 'Region', 'conditions' => array('AirportCode = ?', $from)));
	
	if (strtolower($airline_region[0]->region) == 'europe')
		echo 1;
	else if (strtolower($airport_region[0]->region) == 'europe')
		echo 1;
	else
		echo 0;
	
	return false;
});

// compensation calculate

	$app->post('/travel-compensation/calculate_compensation', function() use ($app) {
	$dc = $app->request()->post('flightstatus');
	$flightlength = $app->request()->post('flightlength');
	$cn = $app->request()->post('cancel-period');
	$de = $app->request()->post('cancel-depart');
	$al = $app->request()->post('cancel-arrive');
	
	echo calculate_compensation($dc, $flightlength, 0, 0, 0, $cn, $de, $al);
	
	return false;
});

// pdf process

	$app->post('/travel-compensation/complete', function() use ($app) {
	require __DIR__ . '/vendor/dompdf/dompdf_config.inc.php';
	$data['title'] = 'PDF Download';
	$data['metadescription'] = 'pdf final download';
	$data['metakeywords'] = 'pdf,download';
	
	// Stops
	$stops = $app->request()->post('stops');
	$airportStops = array();
	$airportStopsText = '';
	
	for ($i=1; $i<=$stops; $i++)
		$airportStops[] = Airport::find('all', array('select' => 'AirportName', 'conditions' => array('AirportCode = ?', $app->request()->post('stop'.$i))));
	
	for ($i=0; $i<count($airportStops); $i++) {
		if (count($airportStops) > 1 && $i+1 == count($airportStops))
			$airportStopsText = substr($airportStopsText, 0, -2) . ' and ' . $airportStops[$i][0]->airportname;
		else
			$airportStopsText .= $airportStops[$i][0]->airportname . ', ';
	}
	
	// Cancel or Delay
	$dc = $app->request()->post('flightstatus');
	
	// Departure date format
	$departure_date = strtotime($app->request()->post('date'));
	$departure_date = date('j F Y', $departure_date);
	
	// Passengers
	$passengercount = $app->request()->post('passengerno');
	$passengers = array();
	
	for ($i=1; $i<=$passengercount; $i++)
		$passengers[] = $app->request()->post('passenger'.$i);
	
	$passengersSentence = '';
	$i = 0;
	if ($passengercount > 1) {
		foreach ($passengers as $p) {
			if (++$i == $passengercount)
				$passengersSentence = substr($passengersSentence, 0, -2) . ' and ' . $p;
			else
				$passengersSentence .= $p . ', ';
		}
	} else
		$passengersSentence = $passengers[0];
	
	// Airport names
	$from = Airport::find('all', array('select' => 'AirportName', 'conditions' => array('AirportCode = ?', $app->request()->post('from'))));
	$to = Airport::find('all', array('select' => 'AirportName', 'conditions' => array('AirportCode = ?', $app->request()->post('to'))));
	
	// Refund
	$refund = $app->request()->post('refund');
	$refund = isset($refund);
	
	// Claims
	$claimnum = $app->request()->post('claimnum');
	$claims = array();
	$claimcategory = array(0,0,0); // [0] => Refreshment, [1] => Accommodation, [2] => Communication
	$claimtotal = 0;
	for ($i=1; $i<=$claimnum; $i++) {
		$claims[] = array(
						'purpose' => $app->request()->post('cpurpose'.$i),
						'receiptnum' => $app->request()->post('creceiptnum'.$i),
						'amount' => $app->request()->post('camount'.$i)
					);
	}
	foreach ($claims as $k=>$c) {
		if (empty($c['purpose']) && empty($c['receiptnum']) && empty($c['amount']))
			unset($claims[$k]); // Unset empty array element
		else {
			switch($c['purpose']) {
				case 1:
					$claims[$k]['purpose'] = 'Meals and refreshments';
					$claimcategory[0] += 1;
					break;
				case 2:
					$claims[$k]['purpose'] = 'Accommodation';
					$claimcategory[1] += 1;
					break;
				case 3:
					$claims[$k]['purpose'] = 'Communications';
					$claimcategory[2] += 1;
					break;
				default:
					$claims[$k]['purpose'] = 'Unspecified';
			}
			$claims[$k]['amount'] = floatval($c['amount']) < 0.01 ? '' : number_format(floatval($c['amount']), 2);
			$claimtotal += floatval($c['amount']);
		}
	}
	$claims = array_values($claims); // Re-index array
	$claimnum = count($claims);
	$claimcat = 0; // Meals and refreshments (+1), Accommodation (+2), Communication (+4)
	for ($i=0; $i<count($claimcategory); $i++) {
		if ($i==0)
			$claimcat += 1;
		elseif ($i==1)
			$claimcat += 2;
		/* Did not implement communication as of yet (not specified in specifications)
		elseif ($i==2)
			$claimcat += 4;
		*/
	}
	switch ($claimcat) {
		case 1:
			$claimcategorytext = 'refreshments';
			break;
		case 2:
			$claimcategorytext = 'hotel accommodation';
			break;
		case 3:
			$claimcategorytext = 'refreshments and hotel accommodation';
			break;
		default:
			$claimcategorytext = 'stuff';
	}
	
	// Flight length sentence
	$flightlength = $app->request()->post('flightlength');
	switch ($flightlength) {
		case 1:
			$flightlengthtext = '0 - 1500 km';
			break;
		case 2:
			$flightlengthtext = '1500 - 3500 km';
			break;
		case 3:
			$flightlengthtext = 'over 3500 km';
			break;
		default:
			$flightlengthtext = '';
	}
	
	// Compensation Calculation
	$flightlength = $app->request()->post('flightlength');
	$compensationperpax = calculate_compensation($dc, $flightlength, $app->request()->post('from'), $app->request()->post('to'), $app->request()->post('delay-hour'), $app->request()->post('cancel-period'), $app->request()->post('cancel-depart'), $app->request()->post('cancel-arrive'));
	$totalcompensation = $compensationperpax * $passengercount;
	
	$data = array(
				'name' => $app->request()->post('claim_name'),
				'address' => str_replace("\n", "<br>", $app->request()->post('claim_address')),
				'email_address' => $app->request()->post('claim_email'),
				'phone_number' => $app->request()->post('claim_phone'),
				'date' => date('j F Y'),
				'airline' => $app->request()->post('airline'),
				'airline_address' => '',
				'booking_no' => $app->request()->post('bookingnum'),
				'flight_number' => $app->request()->post('flightnum'),
				'departure_date' => $departure_date,
				'departure_airport' => $from[0]->airportname,
				'arrival_airport' => $to[0]->airportname,
				'stops' => $stops,
				'stop_airports' => $airportStopsText,
				'scheduled_departure_time' => $app->request()->post('scheduleddeptime'),
				'all_passengers_names' => $passengersSentence,
				'flight_length' => $flightlengthtext,
				'refund' => $refund,
				'flight_cost' => number_format(floatval($app->request()->post('flightcost')), 2),
				'flight_cost_party' => number_format(floatval($app->request()->post('flightcost')) * $passengercount, 2),
				'claim_count' => $claimnum,
				'claim_categories' => $claimcategorytext,
				'claims' => $claims,
				'claim_total' => number_format($claimtotal, 2),
				'compensation_per_pax' => number_format($compensationperpax, 2),
				'total_compensations' => number_format($totalcompensation, 2),
				'compensation_grand_total' => number_format($totalcompensation+$claimtotal, 2)
			);
	
	switch ($dc) {
		case 1:
			$templateFile = 'letter-delayed.html';
			$generatedFilename = 'compensation-claim-for-delayed-flight.pdf';
			$hours_late = $app->request()->post('delay-hour');
			switch ($hours_late) {
				case 1:
					$data['hours_late'] = 'at least 3 hours and up to 4 hours';
					break;
				case 2:
					$data['hours_late'] = 'more than 4 hours';
					break;
				default:
					$data['hours_late'] = '';
			}
			break;
		case 2:
			$templateFile = 'letter-cancelled.html';
			$generatedFilename = 'compensation-claim-for-cancelled-flight.pdf';
			// Cancelled notice text
			$ch = $app->request()->post('cancel-period');
			if (strpos($ch, 'h') === false)
				$chtext = $ch . (($ch==1) ? ' day' : ' days');
			else {
				$ch = substr($ch, 0, -1);
				$chtext = $ch . (($ch==1) ? ' hour' : ' hours');
			}
			$data['cancelled_period'] = $chtext;
			$alternative_flight = $app->request()->post('cancel-arrive');
			switch ($alternative_flight) {
				case -1:
					$alternative_flight = 'earlier than scheduled';
					break;
				case 0:
					$alternative_flight = 'as scheduled';
					break;
				case 1:
					$alternative_flight = '1 hour later than scheduled';
					break;
				case 2:
					$alternative_flight = '2 hours later than scheduled';
					break;
				case 3:
					$alternative_flight = '3 hours later than scheduled';
					break;
				case 4:
					$alternative_flight = '4 hours or more later than scheduled';
					break;
				default:
					$alternative_flight = '';
			}
			$data['alternative_flight'] = $alternative_flight;
			break;
		default:
			$templateFile = 'letter-cancelled.html';
	}
	
	ob_start();
	$app->render($templateFile, $data);
	$pdfhtml = ob_get_contents();
	ob_end_clean();
	
	$dompdf = new DOMPDF();
	$dompdf->load_html($pdfhtml);
	$dompdf->set_paper('a4', 'portrait');
	$dompdf->render();

	$app->response()->headers()->offsetSet('Content-Type', 'application/pdf');
	$dompdf->stream($generatedFilename, array("Attachment" => true));
	
	return false;
});

// Slim Run.
$app->run();

// functions
	
/* Calculate compensation
 * $dc: Delay (1) or cancel (2)
 * $flightlength
 *  1: 0 - 1500 km
 *  2: 1500 - 3500 km
 *  3: over 3500 km
 * $from: Departure airport (for $dc==1)
 * $to: Arrival airport (for $dc==1)
 * $dh: Delayed hours (for $dc==1)
 * $cn: Cancellation notice (for $dc==2)
 * $de: Departed early (for $dc==2)
 * $al: Arrived late (for $dc==2)
 */
function calculate_compensation($dc, $flightlength, $from=0, $to=0, $dh=0, $cn=0, $de=0, $al=0) {
	if (intval($cn) !== 0) {
		if (strpos($cn, 'h') === false) {
			if ($cn < 7)
				$ct = 2; // Compensation tariff (Less than 7 days before departure)
			else
				$ct = 1; // 7 - 14 days before departure
		} else {
			$ct = 2; // Less than 24 hours before departure, hence less than 7 days tariff
		}
	} else
		return 0;
	
	switch ($dc) {
		case 1: // Delay
			if ($dh == 0)
				return 0;
			$isEU = is_eu_flight($from, $to);
			switch ($flightlength) {
				case 1:
					return 250;
					break;
				case 2:
					return 400;
					break;
				case 3:
					if ($isEU)
						return 400;
					else {
						if ($dh == 2) // 4 hours or more delay
							return 600;
						else
							return 300;
					}
					break;
				default:
			}
			break;
		case 2: // Cancel
			switch ($ct) {
				case 1: // 7 - 14 days notice
					switch ($flightlength) {
						case 1:
							if ($al >= 4 || ($de <= -2 && $al >= 3))
								return 250;
							else if ($de <= -2 && $al >= 0)
								return 125;
							break;
						case 2:
							if ($al >= 4 || ($de <= -2 && $al > 3))
								return 400;
							else if (($de <= -2 && $al >= 0) || ($de >= 1 && $al >= 3))
								return 200;
							break;
						case 3:
							if ($al >= 4)
								return 600;
							else if ($de <= -2 && $al > 0)
								return 300;
							break;
						default:
					}
					break;
				case 2: // less than 7 days notice
					switch ($flightlength) {
						case 1:
							if ($al > 2)
								return 250;
							else if ($al > 0 && $de <= -1)
								return 125;
							break;
						case 2:
							if ($al >= 3)
								return 400;
							else if ($al > 0 && $de <= -1)
								return 200;
							break;
						case 3:
							if ($al >= 4)
								return 600;
							else if ($al > 0 && $de <= -1)
								return 300;
							break;
						default:
					}
					break;
				default:
			}
			break;
		default:
	}
	
	return 0;
}

function is_eu_flight($from, $to) {
	$from_region = Airport::find('all', array('select' => 'Region', 'conditions' => array('AirportCode = ?', $from)));
	$to_region = Airport::find('all', array('select' => 'Region', 'conditions' => array('AirportCode = ?', $to)));
	
	if (strtolower($from_region[0]->region) == 'europe' && strtolower($to_region[0]->region) == 'europe')
		return true;
	
	return false;
}

?>
