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
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Http\Controllers\SongController;

class Song extends Model
{
	public $fillable = ['number', 'title', 'author', 'key', 'text'];
    public function playlists()
    {
        return $this->belongsToMany('App\Playlist');
    }
    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 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 && file_put_contents("public/text/{$this->id}.txt", $text)) {
            $this->attributes['text'] = null;
        }
    }
    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";
        }
    }
    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);
    }
}