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
<?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 getChordsAttribute($txt = null)
    {
        $txt = $txt ?? $this->text;
        $txt = str_replace(['(',')',"\n", "\r"]," ",$txt);
        $words = array_flip(explode(' ', $txt));
        $allChords = json_decode(file_get_contents("public/js/chordsdata/chords.json"), TRUE);
        $chords = array_intersect_key($allChords, $words);
        return $chords;
    }
}