about summary refs log tree commit diff
path: root/laravel/app
diff options
context:
space:
mode:
Diffstat (limited to 'laravel/app')
-rw-r--r--laravel/app/Http/Controllers/SongController.php6
-rw-r--r--laravel/app/Song.php12
2 files changed, 16 insertions, 2 deletions
diff --git a/laravel/app/Http/Controllers/SongController.php b/laravel/app/Http/Controllers/SongController.php
index 1a0ec18..2ad3f75 100644
--- a/laravel/app/Http/Controllers/SongController.php
+++ b/laravel/app/Http/Controllers/SongController.php
@@ -263,7 +263,11 @@ class SongController extends Controller
             return redirect("/s/{$song->id}/edit")->withErrors(['key' => $e->getMessage()])->withInput();
         }
         $song->text   = $_POST['text'];//request->text strips whitespace.
-        $song->verse  = $request->verse;
+        try {
+            $song->verse  = $request->verse;
+        } catch (\Exception $e) {
+            return redirect("/s/{$song->id}/edit")->withErrors(['verse' => $e->getMessage()])->withInput();
+        }
         $song->save();
         return redirect()->route( 'song.show', [
             'song' => $song,
diff --git a/laravel/app/Song.php b/laravel/app/Song.php
index a6cb0c0..0f7248a 100644
--- a/laravel/app/Song.php
+++ b/laravel/app/Song.php
@@ -4,6 +4,7 @@ namespace App;
 
 use Illuminate\Database\Eloquent\Model;
 use App\Http\Controllers\SongController;
+use TechWilk\BibleVerseParser\BiblePassageParser;
 
 class Song extends Model
 {
@@ -34,7 +35,7 @@ class Song extends Model
 
     public function setKeyAttribute($value)
     {
-        if (!$value) return;
+        if (!$value) return $this->attributes['key'] = null;
         if ($value[0]>="A"&&$value[0]<="G"){
             $this->attributes['key'] = $value;
         } else {
@@ -65,6 +66,15 @@ class Song extends Model
             file_put_contents("public/text/{$this->id}.txt", $text);
         }
     }
+
+    public function setVerseAttribute($text)
+    {
+        if (!$text) return $this->attributes['verse'] = null;
+        $parser = new BiblePassageParser();
+        // Can throw an exception.
+        return $this->attributes['verse'] = implode("; ", $parser->parse($text));
+    }
+
     public function getChordsAttribute($txt = null)
     {
         $txt = $txt ?? $this->text;