Implemented sets.
[songbook.git] / web / songbook.php
1 <?php
2 $baseDir = getcwd();
3 $songDir = "/opt/songbook/Songs";
4 $debug   = $_REQUEST["debug"];
5
6 function debug ($msg) {
7   global $debug;
8
9   if (isset ($debug)) {
10     echo "<font color=red>DEBUG:</font> $msg<br>";
11   } // if
12 } // debug
13
14 function getSongs () {
15   global $songDir;
16
17   return glob("$songDir/*.pro");
18 } // getSongs
19
20 function getSets () {
21   global $songDir;
22
23   return glob("$songDir/*.lst");
24 } // getSets
25
26 function songsDropdown () {
27   $songs = getSongs();
28
29   print "<form method=\"get\" action=\"webchord.cgi\" name=\"song\">";
30   print "Songs:&nbsp;&nbsp;";
31   print "<select name=\"chordpro\">";
32
33   sort ($songs);
34   foreach ($songs as $song) {
35     $title = basename ($song, ".pro");
36     $artist = getArtist ($song);
37
38     print "<option value=\"$title.pro\">$title</option>";
39
40     if ($artist != "") {
41       $title .= "&nbsp;($artist)";
42     } // if
43   } // foreach
44
45   print "</select>";
46   print "&nbsp;<input type=\"submit\" value=\"Go\">";
47   print "</form>";
48 } // songsDropdown
49
50 function artistsDropdown () {
51   $songs = getSongs();
52   $artists = getArtists ($songs);
53
54   print "<form method=\"get\" action=\"displayartist.php\" name=\"artist\">";
55   print "Artists:&nbsp;&nbsp;";
56   print "<select name=\"artist\">";
57
58   sort ($artists);
59   foreach ($artists as $artist) {
60     print "<option>$artist</option>";
61   } // foreach
62
63   print "</select>";
64   print "&nbsp;<input type=\"submit\" value=\"Go\">";
65   print "</form>";
66 } // artistsDropdown
67
68 function setsDropdown () {
69   $sets = getSets();
70
71   print "<form method=\"get\" action=\"displayset.php\" name=\"set\">";
72   print "Sets:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
73   print "<select name=\"set\">";
74
75   sort ($sets);
76   foreach ($sets as $set) {
77     print "Processing set<br>";
78     $title = basename ($set, ".lst");
79
80     print "<option value=\"$title.lst\">$title</option>";
81   } // foreach
82
83   print "</select>";
84   print "&nbsp;<input type=\"submit\" value=\"Go\">";
85   print "</form>";
86 } // setsDropdown
87
88 function getArtist ($song) {
89   $lyrics = file_get_contents ($song);
90
91   if (preg_match ("/\{(st|subtitle):(.*)\}/", $lyrics, $matches)) {
92     return trim ($matches[2]);
93   } else {
94     return "";
95   } // if
96 } // getArtist
97
98 function getArtists ($songs) {
99   $artists = array();
100
101   foreach ($songs as $song) {
102     $artist = getArtist ($song);
103
104     if ($artist != '') {
105       $artists[$artist] = 1;
106     } // if
107   } // foreach
108
109   return array_keys ($artists);
110 } // getArtists
111
112 function formatTable ($songs) {
113   echo "<ol>";
114
115   foreach ($songs as $song) {
116     $artist = getArtist ($song);
117
118     $title = basename ($song, ".pro");
119
120     echo "<li><a href=\"webchord.cgi?chordpro=$song\">$title</a>";
121
122     if ($artist != "") {
123     echo "&nbsp;($artist)";
124     } // if
125   } // foreach
126
127   echo "</ol>";
128 } // formatTable