Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2 Station search via device positioning services #79

Merged
merged 10 commits into from Aug 4, 2020
18 changes: 18 additions & 0 deletions app/Http/Controllers/FrontendTransportController.php
Expand Up @@ -42,6 +42,24 @@ public function TrainStationboard(Request $request) {
);
}

public function StationByCoordinates(Request $request)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend renaming it "getStationByCoordinates", even if other method names are in this format. See my general answer to your pull request for further information.

{
$validatedInput = $request->validate([
'latitude' => 'required|numeric|min:-180|max:180',
'longitude' => 'required|numeric|min:-180|max:180'
]);

$NearestStation = TransportBackend::StationByCoordinates($validatedInput['latitude'], $validatedInput['longitude']);
if ($NearestStation === null) {
return redirect()->back()->with('error', __('controller.transport.no-station-found'));
}

return redirect(route('trains.stationboard', [
'station' => $NearestStation['station']['name'],
'provider' => 'train'
]));
}

public function TrainTrip(Request $request) {
$TrainTripResponse = TransportBackend::TrainTrip(
$request->tripID,
Expand Down
9 changes: 9 additions & 0 deletions app/Http/Controllers/TransportController.php
Expand Up @@ -96,6 +96,15 @@ public static function FastTripAccess($departure, $lineName, $number, $when)
return null;
}

public static function StationByCoordinates($latitude, $longitude)
jeyemwey marked this conversation as resolved.
Show resolved Hide resolved
{
$client = new Client(['base_uri' => config('trwl.db_rest')]);
$response = $client->request('GET', "stops/nearby?latitude=$latitude&longitude=$longitude&results=1");
jeyemwey marked this conversation as resolved.
Show resolved Hide resolved
$json = json_decode($response->getBody()->getContents());

return self::TrainStationboard($json[0]->name, null, null);
}

private static function getTrainDepartures($ibnr, $when='now', $trainType=null)
{
$client = new Client(['base_uri' => config('trwl.db_rest')]);
Expand Down
1 change: 1 addition & 0 deletions bootstrap/cache/services.php
Expand Up @@ -182,6 +182,7 @@
'Laravel\\Socialite\\Contracts\\Factory' => 'Laravel\\Socialite\\SocialiteServiceProvider',
'command.tinker' => 'Laravel\\Tinker\\TinkerServiceProvider',
'NunoMaduro\\Collision\\Contracts\\Provider' => 'NunoMaduro\\Collision\\Adapters\\Laravel\\CollisionServiceProvider',
'Revolution\\Mastodon\\MastodonClient' => 'Revolution\\Mastodon\\Providers\\MastodonServiceProvider',
'Revolution\\Mastodon\\Contracts\\Factory' => 'Revolution\\Mastodon\\Providers\\MastodonServiceProvider',
),
'when' =>
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -3,6 +3,7 @@
"private": true,
"scripts": {
"dev": "npm run development",
"start": "npm run development && php artisan serve",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run development -- --watch",
"watch-poll": "npm run watch -- --watch-poll",
Expand Down Expand Up @@ -37,4 +38,4 @@
"mdbootstrap": "^4.8.9",
"momentjs": "^2.0.0"
}
}
}
4 changes: 2 additions & 2 deletions public/js/admin.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions public/js/app.js

Large diffs are not rendered by default.

47 changes: 35 additions & 12 deletions resources/js/components/stationboard.js
@@ -1,4 +1,4 @@
import {longStackSupport} from "q";
import { longStackSupport } from "q";

let delays = document.getElementsByClassName("traindelay");
for (let i = 0; i < delays.length; i++) {
Expand All @@ -17,7 +17,7 @@ for (let i = 0; i < delays.length; i++) {

var touchmoved;
$(document)
.on("click touchstart", ".trainrow", function() {
.on("click touchstart", ".trainrow", function () {
var lineName = $(this).data("linename");
var tripID = $(this).data("tripid");
var start = $(this).data("start");
Expand All @@ -32,15 +32,15 @@ $(document)
start;
}
})
.on("touchmove", function(e) {
.on("touchmove", function (e) {
touchmoved = true;
})
.on("touchstart", function() {
.on("touchstart", function () {
touchmoved = false;
});

$(document)
.on("click touchend", ".train-destinationrow", function() {
.on("click touchend", ".train-destinationrow", function () {
var tripID = $(this)
.parent()
.parent()
Expand All @@ -56,33 +56,33 @@ $(document)
.parent()
.data("linename");
if (touchmoved != true) {
$("#checkinModal").modal("show", function(event) {
$("#checkinModal").modal("show", function (event) {
var modal = $(this);
modal
.find(".modal-title")
.html(
linename +
' <i class="fas fa-arrow-alt-circle-right"></i> ' +
stopname
' <i class="fas fa-arrow-alt-circle-right"></i> ' +
stopname
);
modal.find("#input-tripID").val(tripID);
modal.find("#input-destination").val(destination);
modal.find("#input-start").val(start);
});
}
})
.on("touchmove", function(e) {
.on("touchmove", function (e) {
touchmoved = true;
})
.on("touchstart", function() {
.on("touchstart", function () {
touchmoved = false;
});

$("#checkinModal").on("show.bs.modal", function(event) {
$("#checkinModal").on("show.bs.modal", function (event) {
$(event.relatedTarget);
});

$("#checkinButton").click(function(e) {
$("#checkinButton").click(function (e) {
e.preventDefault();
$("#checkinForm").submit();
});
Expand All @@ -94,3 +94,26 @@ if (document.getElementById("history-button")) {
);
});
}

if (document.getElementById("gps-button")) {
document.getElementById("gps-button").addEventListener("click", () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(searchStationByPosition, handlePositioningError);
} else {
["d-none", "animated", "fadeIn"].forEach(classname =>
document.getElementById("gps-disabled-error").classList.toggle(classname)
);
}
});
}

function searchStationByPosition(position) {
let newLocation = `${window.location.protocol}//${window.location.host}/trains/nearby?latitude=${position.coords.latitude}&longitude=${position.coords.longitude}`;
window.location.href = newLocation;
}

function handlePositioningError(error) {
["d-none", "animated", "fadeIn"].forEach(classname =>
document.getElementById("gps-disabled-error").classList.toggle(classname)
);
}
4 changes: 3 additions & 1 deletion resources/lang/de/stationboard.php
Expand Up @@ -23,5 +23,7 @@
"submit-search" => "Suchen",
"where-are-you" => "Wo bist Du?",
"last-stations" => "Letzte Stationen",
"next-stop" => "Nächster Halt"
"next-stop" => "Nächster Halt",
"search-by-location" => "Standortbasierte Suche",
"position-unavailable" => "Positionsbestimmung nicht möglich!"
];
4 changes: 3 additions & 1 deletion resources/lang/en/stationboard.php
Expand Up @@ -23,5 +23,7 @@
"submit-search" => "Search",
"where-are-you" => "Where are you?",
"last-stations" => "Last stations",
"next-stop" => "Next stop"
"next-stop" => "Next stop",
"search-by-location" => "Search by location",
"position-unavailable" => "Locating not possible!"
];
12 changes: 12 additions & 0 deletions resources/views/includes/station-autocomplete.blade.php
Expand Up @@ -4,6 +4,12 @@
<div class="card">
<div class="card-header">{{__('stationboard.where-are-you')}}</div>
<div class="card-body">
<div id="gps-disabled-error" class="alert my-3 alert-danger d-none" role="alert">
{{__('stationboard.position-unavailable')}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<form action="{{ route('trains.stationboard') }}" method="get" id="autocomplete-form">
<input type="hidden" id="autocomplete-provider" name="provider" value="train">

Expand All @@ -17,6 +23,12 @@
</span>
</div>
@endif

<div class="input-group-append" id="gps-button" title="{{__('stationboard.search-by-location')}}">
<span class="input-group-text" id="basic-addon2">
<i class="fa fa-map-marker-alt"></i>
</span>
</div>

</div>
<div class="list-group d-none" id="last-stations">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/layouts/app.blade.php
Expand Up @@ -169,7 +169,7 @@ class="fas fa-tools"></i> {{__('menu.admin')}}</a>
<span class="footer-nav-link">/ <a href="{{ route('blog.all') }}">{{ __('menu.blog') }}</a></span>
</p>
<p class="mb-0">{!! __('menu.developed') !!}</p>
<p class="mb-0">&copy; 2019 Tr&auml;welling</p>
<p class="mb-0">&copy; 2020 Tr&auml;welling</p>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be better to change the year to {{date('Y')}} to prevent these changes.
Even if this is not the primary part of the PR.

<p class="mb-0 text-muted small">commit: {{ get_current_git_commit() }}</p>
</div>
</footer>
Expand Down
6 changes: 3 additions & 3 deletions resources/views/stationboard.blade.php
Expand Up @@ -8,9 +8,9 @@
<div id="timepicker-wrapper">
<div class="row justify-content-center">
<div class="btn-group" role="group">
<a href="{{ url()->current() . '?' . http_build_query(['provider' => $request->provider, 'station' => $station['name'], 'when' => strtotime('-15 Minutes', $when), 'travelType' => $request->travelType]) }}" alt="{{__('stationboard.minus-15')}}" class="btn btn-light btn-rounded"><i class="fas fa-arrow-circle-left"></i></a>
<a href="{{ route('trains.stationboard', ['provider' => $request->provider, 'station' => $station['name'], 'when' => strtotime('-15 Minutes', $when), 'travelType' => $request->travelType]) }}" alt="{{__('stationboard.minus-15')}}" class="btn btn-light btn-rounded"><i class="fas fa-arrow-circle-left"></i></a>
<a href="#" id="timepicker-reveal" alt="{{__('stationboard.dt-picker')}}" class="btn btn-light btn-rounded c-datepicker-btn"><i class="fas fa-clock"></i></a>
<a href="{{ url()->current() . '?' . http_build_query(['provider' => $request->provider, 'station' => $station['name'], 'when' => strtotime('+15 Minutes', $when), 'travelType' => $request->travelType]) }}" alt="{{__('stationboard.plus-15')}}" class="btn btn-light btn-rounded"><i class="fas fa-arrow-circle-right"></i></a>
<a href="{{ route('trains.stationboard', ['provider' => $request->provider, 'station' => $station['name'], 'when' => strtotime('+15 Minutes', $when), 'travelType' => $request->travelType]) }}" alt="{{__('stationboard.plus-15')}}" class="btn btn-light btn-rounded"><i class="fas fa-arrow-circle-right"></i></a>
</div>
</div>
<div class="row justify-content-center">
Expand All @@ -21,7 +21,7 @@
<a href="#" class="input-group-text btn-primary text-white" id="timepicker-button">{{__('stationboard.set-time')}}</a>
</div>
<script>
window.changeTimeLink = "{{ url()->current() . '?' . http_build_query(['provider' => $request->provider, 'station' => $station['name'], 'travelType' => $request->travelType, 'when' => 'REPLACEME' ]) }}";
window.changeTimeLink = "{{ route('trains.stationboard', ['provider' => $request->provider, 'station' => $station['name'], 'when' => 'REPLACEME', 'travelType' => $request->travelType]) }}";
</script>
</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions routes/web.php
Expand Up @@ -256,6 +256,11 @@
'as' => 'trains.stationboard',
]);

Route::get('/trains/nearby', [
'uses' => 'FrontendTransportController@StationByCoordinates',
'as' => 'trains.nearby',
]);

Comment on lines +259 to +263
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add an API request for this? Then we need to adjust the swagger-api.yml too. Maybe @HerrLevin can help? This is non-blocking from my side, and can be done later.

Route::get('/trains/trip', [
'uses' => 'FrontendTransportController@TrainTrip',
'as' => 'trains.trip'
Expand Down