Backend for songs.zachdecook.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Http\Controllers\SongController;
use TechWilk\BibleVerseParser\BiblePassageParser;

class Song extends Model
{
    public $fillable = ['number', 'title', 'author', 'key', 'text'];
    public function playlists()
    {
        return $this->belongsToMany('App\Playlist');
    }
    public static function whereVerse($passage)
    {
    	$parser = new BiblePassageParser();
      // Can throw an exception.
      $refs = $parser->parse($passage);
    	$bc = explode(':', $refs[0])[0];
		return Song::where('verse', 'LIKE', "$bc:%")->orWhere('verse',"$bc")->orWhere('verse','like',$refs[0].";%")->orWhere('verse', 'LIKE', "%; $bc:%");
    }
    public function getPassagesAttribute() {
        $parser = new BiblePassageParser();
        $refs = $parser->parse($this->verse);
        return $refs;
    }
    public static function passageSort($a, $b) {
        try {
            return $a->passages[0]->from()->integerNotation() <=> $b->passages[0]->from()->integerNotation();
        } catch (\Exception $e) {
            // I don't care.
        }
        return 0;
    }
    public function textTranspose($key)
    {
        $sc = new SongController();
        $transp = $key;
        if ($key && $this->plain_key){
            $try = $sc->keydiff($this->plain_key, $key);
            if ($try !== null){
                $transp = $try;
            }
        }
        return $sc->transposeBlock($this->text, $transp);
    }

    public function setKeyAttribute($value)
    {
        if (!$value) return $this->attributes['key'] = null;
        if ($value[0]>="A"&&$value[0]<="G"){
            $this->attributes['key'] = $value;
        } else {
            throw new \Exception("Invalid key: $value");
        }
    }
    
    public function getNameAttribute()
    {
        return $this->title
            . ( $this->author ? " ($this->author)" : "" )
            . ($this->plain_key ? " ($this->plain_key)" : "")
            . ($this->verse ? " ($this->verse)" : "");
    }
    public function getPlainKeyAttribute()
    {
        // TODO: Validate that this is plain.
        return trim($this->key, "m");
    }
    public function getTextAttribute($text)
    {
        return $text ?: file_get_contents("public/text/{$this->id}.txt");
    }
    public function setTextAttribute($text)
    {
        // Watch out, this saves immediately!
        if ($text && $this->id) {
            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;
        $txt = str_replace(['(',')',"\n", "\r"]," ",$txt);
        $words = explode(' ', $txt);
        $wordsT = array_map(['self','chordTransform'], $words);
        $wordsTKey = array_flip($wordsT);
        $allChords = json_decode(file_get_contents("public/js/chordsdata/chords.json"), TRUE);
        $chords = array_intersect_key($allChords, $wordsTKey);
        // TODO: Replace keys from $chords with values from $words.
        return $chords;
    }
    public function getAudioAttribute()
    {
        if (file_exists("public/music/{$this->id}.mp3")) {
            return "/public/music/{$this->id}.mp3";
        } elseif (file_exists("public/music/{$this->id}.m4a")) {
            return "/public/music/{$this->id}.m4a";
        }
    }
    public static function chordTransform($chord)
    {
        $repl = [
            'sus' => 's',
            's4' => 's',
            's' => 'sus',
            '7sus' => 'sus7',
            'mj7' => 'maj7',
            '/A' => '/a',
            '/B' => '/b',
            '/C' => '/c',
            '/D' => '/d',
            '/E' => '/e',
            '/F' => '/f',
            '/G' => '/g',
            '♯' => '#',
            '♭' => 'b',
            'Db' => 'C#',
            'Eb' => 'D#',
            'Gb' => 'F#',
            'Ab' => 'G#',
            'Bb' => 'A#',
        ];
        return str_replace(array_keys($repl), array_values($repl), $chord);
    }
}