belongsToMany('App\Playlist'); } public static function whereVerse($passage) { $book = explode(' ',$passage)[0]; $chapter = (int)explode(' ', $passage)[1]; return Song::where('verse', 'LIKE', "$book $chapter:%")->orWhere('verse',"$book $chapter")->orWhere('verse', 'LIKE', "%; $book $chapter:%"); } 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 && $this->id) { file_put_contents("public/text/{$this->id}.txt", $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); } }