Backend for songs.zachdecook.com
* Suggestions: Offer to transpose into the key of the current song
Zach DeCook 2019-01-17
parent b4958b4 · commit c7df9d7
-rw-r--r--laravel/app/Http/Controllers/SongController.php29
-rw-r--r--laravel/app/Song.php5
-rw-r--r--laravel/resources/views/song.blade.php5
3 files changed, 34 insertions, 5 deletions
diff --git a/laravel/app/Http/Controllers/SongController.php b/laravel/app/Http/Controllers/SongController.php
index 087c358..77b2aae 100644
--- a/laravel/app/Http/Controllers/SongController.php
+++ b/laravel/app/Http/Controllers/SongController.php
@@ -14,11 +14,17 @@ class SongController extends Controller
$song = Song::where('number', $song )->first();
return redirect()->route('song.show', ['song' => $song ] );
}
- public function show( Song $song )
+ public function show( Song $song, Request $request )
{
$lines = explode( "\n", $song['text'] );
$newText = '';
- $transp = $_GET['transp'] ?? 0;
+ $transp = $request->transp ?? 0;
+ if ($request->key && $song->key){
+ $try = $this->keydiff($song->key, $request->key);
+ if ($try !== null){
+ $transp = $try;
+ }
+ }
foreach( $lines as $line ){
$line = trim(htmlspecialchars( $line ), "\n\t\r");
if ( $this->chordline( $line ) ) {
@@ -46,7 +52,7 @@ class SongController extends Controller
];
$playlist = NULL;
- if ( isset($_GET['playlist']) && $playlist = Playlist::find($_GET['playlist'] ) ){
+ if ( isset($request->playlist) && $playlist = Playlist::find($request->playlist) ){
$params['playlist'] = $playlist;
// TODO: Allow playlists to be re-ordered
$params['back'] = $playlist->songs()->where('song_id', '<', $song->id)->orderBy('id', 'desc')->first();
@@ -64,13 +70,13 @@ class SongController extends Controller
}
return view('song', $params );
}
- public function suggested($song, $from)
+ public function suggested($song, $from, Request $request)
{
$sug = Suggestion::firstOrNew(['song' => $song, 'from' => $from]);
$sug->clicks++;
$sug->save();
//Suggestion::make(['song' => $song, 'from' => $from]);
- return redirect(route('song.show', $song));
+ return redirect(route('song.show', ['song' => $song, 'key' => $request->key]));
}
/**
@@ -184,6 +190,19 @@ class SongController extends Controller
return $ochords[($chords[$fromkey] + $integer + 24)%12];
} return false;
}
+ private function keydiff($fromkey, $tokey){
+ $chords = array_flip(array( "C","C#","D","D#","E","F","F#","G","G#","A","A#","B" ));
+ $chords["Db"] = $chords["C#"];
+ $chords["Eb"] = $chords["D#"];
+ $chords["Gb"] = $chords["F#"];
+ $chords["Ab"] = $chords["G#"];
+ $chords["Bb"] = $chords["A#"];
+ // TODO: Handle minor keys (or other different modes)
+ if (isset($chords[$fromkey]) && isset($chords[$tokey])){
+ return $chords[$tokey] - $chords[$fromkey];
+ }
+ return null;
+ }
public function post()
{
diff --git a/laravel/app/Song.php b/laravel/app/Song.php
index ceaae21..58e2317 100644
--- a/laravel/app/Song.php
+++ b/laravel/app/Song.php
@@ -15,4 +15,9 @@ class Song extends Model
{
return $this->title . ( $this->author ? " ($this->author)" : "" );
}
+ public function getPlainKeyAttribute()
+ {
+ // TODO: Validate that this is plain.
+ return trim($this->key, "m");
+ }
}
diff --git a/laravel/resources/views/song.blade.php b/laravel/resources/views/song.blade.php
index 6b4b661..6dfe409 100644
--- a/laravel/resources/views/song.blade.php
+++ b/laravel/resources/views/song.blade.php
@@ -65,6 +65,11 @@
@foreach ($suggestions as $sug)
<div class='controlArea'>
<a class='but' href='{{route('song.suggested', ['song' => $sug, 'from' => $song])}}'>{{$sug->name}}</a>
+ @if ($song->plain_key)
+ <a class='but' href='{{route('song.suggested', ['song' => $sug, 'from' => $song, 'key' => $song->plain_key])}}'>
+ transposed to {{$song->plain_key}}
+ </a>
+ @endif
</div>
@endforeach