about summary refs log tree commit diff
path: root/laravel/app/Http/Controllers/SongController.php
diff options
context:
space:
mode:
authorZach DeCook <zachdecook@gmail.com>2019-01-17 14:29:52 -0500
committerZach DeCook <zachdecook@gmail.com>2019-01-17 14:29:52 -0500
commitc7df9d769a2dfbc61aa06b8b094a20d829f44bb8 (patch)
tree9c9d9f27cf4ce618b74b6b3ccad76e5ab68dc8fe /laravel/app/Http/Controllers/SongController.php
parentb4958b4bf1fecbb2bace7dc9664b326841c8067e (diff)
downloadprosongsa-c7df9d769a2dfbc61aa06b8b094a20d829f44bb8.tar.gz
* Suggestions: Offer to transpose into the key of the current song
Diffstat (limited to 'laravel/app/Http/Controllers/SongController.php')
-rw-r--r--laravel/app/Http/Controllers/SongController.php29
1 files changed, 24 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()
     {