Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:edwardcrompton/php-lle into main
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardcrompton committed Apr 11, 2024
2 parents b7dd24e + 45b60f3 commit 1752792
Show file tree
Hide file tree
Showing 18 changed files with 291 additions and 65 deletions.
28 changes: 21 additions & 7 deletions app/Http/Controllers/AudioController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

use App\Models\AudioRecord;
use Illuminate\Http\Request;
use App\Services\UrlLocaliser;

class AudioController extends Controller
{
public function uploadAudio(Request $request)
protected $urlLocaliser;

public function __construct(UrlLocaliser $urlLocaliser) {
$this->urlLocaliser = $urlLocaliser;
}

public function upload(Request $request)
{
if ($request->hasFile('audio')) {
$audio = $request->file('audio');
Expand All @@ -24,18 +31,25 @@ public function uploadAudio(Request $request)

$record->save();

return response()->json(['message' => 'Audio uploaded successfully']);
flash('Audio uploaded successfully.')->success();
return $this->urlLocaliser->redirect('index');
}
flash('No audio file was provided.')->error();
return $this->urlLocaliser->redirect('record');
}

return response()->json(['message' => 'No audio file provided'], 400);
public function record() {
$urllocaliser = $this->urlLocaliser;
return view('audio', compact('urllocaliser'));
}

public function listAudio()
public function list()
{
// Retrieve paginated audio records, typically with a specific number of records per page
$audioRecords = AudioRecord::paginate(10);
// Retrieve paginated audio records.
$audioRecords = AudioRecord::orderBy('id', 'DESC')->paginate(10);
$urllocaliser = $this->urlLocaliser;

return view('list', compact('audioRecords'));
return view('list', compact('audioRecords', 'urllocaliser'));
}

}
36 changes: 30 additions & 6 deletions app/Http/Controllers/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,58 @@

namespace App\Http\Controllers;

use App\Models\AudioRecord;
use NominatimLaravel\Content\Nominatim;
use App\Services\UrlLocaliser;

class SearchController extends Controller
{
public function __construct(Nominatim $searchApi) {
protected $searchApi;
protected $urlLocaliser;

public function __construct(Nominatim $searchApi, UrlLocaliser $urlLocaliser) {
$this->searchApi = $searchApi;
$this->urlLocaliser = $urlLocaliser;
}

public function index(string $place) {
public function index(string $locale, string $place = '') {

$search = $this->searchApi->newSearch();
$search->query($place);
$search->query($place)->viewbox(-5.5151, 51.2956, -2.6870, 53.5011);

$results = $this->searchApi->find($search);

$list = [];
foreach ($results as $result) {
$list[] = [
$id = NULL;
$audioPath = NULL;
$location = $result['display_name'];

$audioRecord = AudioRecord::select('id', 'audio_path')
->where('location_address', $result['display_name'])
->first();

if ($audioRecord) {
$id = $audioRecord->id;
$audioPath = $audioRecord->audio_path;
}

$audio = new AudioRecord([
'id' => $id,
'type' => $result['type'],
'name' => $result['name'],
'address' => $result['display_name'],
'location_address' => $location,
'audio_path' => $audioPath,
'osm_place_id' => $result['place_id'],
];
]);

$list[] = $audio;
}

return view('search', [
'place' => $place,
'results' => $list,
'urllocaliser' => $this->urlLocaliser
]);
}
}
2 changes: 2 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Kernel extends HttpKernel
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\Localization::class,
],

'api' => [
Expand Down Expand Up @@ -64,5 +65,6 @@ class Kernel extends HttpKernel
'signed' => \App\Http\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'setlocale' => \App\Http\Middleware\Localization::class,
];
}
24 changes: 24 additions & 0 deletions app/Http/Middleware/Localization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class Localization
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$locale = $request->route('locale');
if (in_array($locale, config('app.available_locales'))) {
app()->setLocale($locale);
}
return $next($request);
}
}
7 changes: 7 additions & 0 deletions app/Models/AudioRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ class AudioRecord extends Model
protected $table = 'audio_records';

protected $fillable = ['audio_path', 'location_address'];

public function getUpdatePath() {
return route(
'record',
['location' => $this->location_address, 'locale' => app()->getLocale()],
);
}
}
13 changes: 13 additions & 0 deletions app/Services/UrlLocaliser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Services;

class UrlLocaliser {
public function route($route) {
return route($route, ['locale' => app()->getLocale()]);
}

public function redirect($route) {
return redirect()->route($route, ['locale' => app()->getLocale()]);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"php": "^8.1",
"guzzlehttp/guzzle": "^7.2",
"jbohme/nominatim-laravel": "^1.0",
"laracasts/flash": "^3.2",
"laravel/framework": "^10.10",
"laravel/sanctum": "^3.2",
"laravel/tinker": "^2.8"
Expand Down
59 changes: 58 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@

'fallback_locale' => 'en',

/*
|--------------------------------------------------------------------------
| Available Locales
|--------------------------------------------------------------------------
|
| All locales that the application works with.
|
*/

'available_locales' => [
'English' => 'en',
'Cymraeg' => 'cy',
],

/*
|--------------------------------------------------------------------------
| Faker Locale
Expand Down Expand Up @@ -184,6 +198,7 @@

'aliases' => Facade::defaultAliases()->merge([
// 'Example' => App\Facades\Example::class,
'UrlLocaliser' => \App\Services\UrlLocaliser::class,
])->toArray(),

];
29 changes: 29 additions & 0 deletions lang/cy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"Search places": "Chwilio lleodd",
"Search": "Chwilio",
"Search results": "Canlyniadau chwilio",
"Location": "Lleoliad",
"Update audio": "Ail-wneud clip sain",
"This list shows the latest audio clips.": "Mae rhestr hynna yn dangos y clipau sain diwethaf.",
"Search for a place name to add a new audio clip.": "Chwilach am enw lle er mwyn creu clip newydd.",
"Audio files will be shown if they already exist.": "",
"Start recording": "Dechrach recordio",
"Stop recording": "Stopach recordio",
"Save": "Archebu",
"Showing": "Yn dangos",
"to": "i",
"of": "o",
"results": "canlyniad",
"pagination.previous": "« Blaenorol",
"pagination.next": "Nesa »",
"Place name": "Enw lle",
"Place": "Lle",
"Search for a place name": "Chwilach am enw lle",
"Audio file": "",
"No file selected": "",
"Audio uploaded successfully.": "",
"No audio file was provided.": "",
"Location address": "",
"Results for :place": "",
"Audio recorder": ""
}
40 changes: 27 additions & 13 deletions resources/views/audio.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@

@section('content')
<h1>{{ app('request')->input('name') }}</h1>
<form method="POST" enctype="multipart/form-data" id="audioClipForm" action="{{ url('/audio/upload') }}">
<form method="POST" enctype="multipart/form-data" id="audioClipForm" action="{{ $urllocaliser->route('upload') }}">
@csrf

<label for="address">Location address</label>
<input type="text" name="location_address" id="address" value="{{ app('request')->input('location') }}"></input>

<label for="audio">Audio file</label>
<input id="audio" name="audio" type="file"></input>

<button id="toggleRecord" type="button">Start Recording</button>
<audio id="audioPlayer" controls></audio>

<input type="submit" value="Save">
<div id="address-container">
<label for="address">{{ __('Location address') }}</label>
<input type="text" name="location_address" id="address" value="{{ app('request')->input('location') }}"></input>
</div>

<div id="audio-container">
<label for="audio">{{ __('Audio file') }}</label>
<input id="audio" name="audio" type="file"></input>
</div>

<div id="controls">
<button id="toggleRecord" type="button">{{ __('Start recording') }}</button>
<audio id="audioPlayer" controls></audio>
</div>

<div id="save">
<input type="submit" value="{{ __('Save') }}">
</div>
</form>
@endsection

Expand All @@ -24,8 +32,14 @@
let audioChunks = [];
let isRecording = false;
var labelStopRecording = {!! json_encode(__('Stop recording')) !!};
var labelStartRecording = {!! json_encode(__('Start recording')) !!};
const toggleRecordButton = document.getElementById('toggleRecord');
const audioPlayer = document.getElementById('audioPlayer');
// If JS is running we don't need to show the file upload field.
document.getElementById('audio-container').style.display = 'none';
toggleRecordButton.addEventListener('click', toggleRecording);
Expand Down Expand Up @@ -63,14 +77,14 @@
mediaRecorder.start();
isRecording = true;
toggleRecordButton.textContent = 'Stop Recording';
toggleRecordButton.textContent = labelStopRecording;
}
function stopRecording() {
if (mediaRecorder && mediaRecorder.state === 'recording') {
mediaRecorder.stop();
isRecording = false;
toggleRecordButton.textContent = 'Start Recording';
toggleRecordButton.textContent = labelStartRecording;
}
}
Expand Down
Loading

0 comments on commit 1752792

Please sign in to comment.