#!/usr/bin/perl
use CGI qw(:standard);
use DBI;

require "setup.pl";

my $dbh = DBI->connect($config{'DB_STRING'},$config{'DB_USER'},$config{'DB_PASS'});

$CGI::DISABLE_UPLOADS = 1;
$CGI::POST_MAX        = 102_400; # 100 KB

my $q = new CGI;
print $q->header( "text/html" );


my $s_artist = $q->param( 's_artist' );
my $req =  $q->param( 'req' );
my $chanid =  $q->param( 'chan' );

my $searchtxt = $q->param( 'searchtxt' );
my $search = $q->param( 'search' );

print "<HTML><HEAD>";
print "<TITLE>SPOGBIPER - MAKE A REQUEST</TITLE>";
print "<LINK HREF=\"/request.css\" rel=\"stylesheet\" type=\"text/css\">";
print "</HEAD><BODY><CENTER><P>";


print "<IMG SRC=/spogbiper.gif>";
print "<P>";

print "<IMG SRC=/request.gif><BR></CENTER>";

if ($req ne "")
{
 $qry = "SELECT artist,title FROM tunes WHERE ID = $req";
 $out = $dbh->prepare($qry);
 $out->execute;

 if (my($rart,$rtit)=$out->fetchrow_array)
 {
  # stop assholes from requesting entire albums...

  $qry = "SELECT tunes.ID,tunes.title from schedule,tunes 
          WHERE (tunes.ID = schedule.tuneid) AND (tunes.artist = ?)";
  $out = $dbh->prepare($qry);
  $out->execute($rart);


  if (my($did,$dtitle)=$out->fetchrow_array)
  {
   print "<H1><B><FONT COLOR=#FFFF77>Sorry: \"$dtitle\" by $rart is already queued, and we only queue one song per artist.<br>Feel free to request \"$rtit\" again after it has played.</FONT></B></H5><P>";
  }
  else
  {
   print "<H5><B>Your request for \"$rtit\" by $rart has been added.</B></H5><P>";
 
   $qry = "INSERT INTO schedule (added,TuneID,request,seq,channelid,showid) VALUES (0,$req,1,0,$chanid,-1)";
   $out = $dbh->prepare($qry);
   $out->execute;

   $qry = "UPDATE channeltunes SET requests = requests + 1 WHERE tuneid = $req";   
   $out = $dbh->prepare($qry);   
   $out->execute;
  }
 }
}


if (($s_artist eq "") && ($search ne "Search"))
{
 print "<div id='search'>";
 print "<H1>Search artists, album names and titles:</H1>";
 print "<FORM><CENTER><INPUT id='searchtext' TYPE=TEXT SIZE=34 NAME=searchtxt>&nbsp;";
 print "<INPUT TYPE=HIDDEN NAME=chan VALUE=$chanid>";
 print "<INPUT TYPE=SUBMIT Name=search VALUE=Search id='search'></FORM>";
 print "</div>";
}
elsif ($s_artist ne "")
{
 print "<div id='back'><A HREF=/cgi-bin/request.pl?chan=$chanid><IMG SRC=/back.png></A></div>";
}
elsif ($search eq "Search")
{
 print "<div id='back'><A HREF=/cgi-bin/request.pl?chan=$chanid><IMG SRC=/back.png></A></div>";
}


if (($search ne "Search") && ($s_artist eq ""))
{
 # artist list

 print "<div id='alist'>";

 $qry = "SELECT DISTINCT artist FROM tunes,channeltunes WHERE (tunes.ID = channeltunes.tuneid) AND (channeltunes.channelid = $chanid) ORDER BY artist,album,title";
 $out = $dbh->prepare($qry);
 $out->execute;

 $tot = 0;

 while (my($artist)=$out->fetchrow_array)
 {
  $tot++;
  $artist =~ s/\|/'/g;
  @artists[$tot] = $artist;
 }
 
 $div = int($tot / 3);
 $mod = ($tot % 3);

 $span1 = $div;
 if ($mod > 0) {$span1++;}

 print "<div id='alist1'>";
 for ($x = 1;$x < ($span1);$x++)
 {
  $a = @artists[$x];
  print "<h1><A HREF=\"/cgi-bin/request.pl?chan=$chanid&s_artist=$a\">$a</A></h1>";
 }
 print "</div>";

 print "<div id='alist2'>";
 for ($x = $span1;$x < ($span1*2);$x++)
 {
  $a = @artists[$x];
  print "<h1><A HREF=\"/cgi-bin/request.pl?chan=$chanid&s_artist=$a\">$a</A></h1>";
 }
 print "</div>";

 print "<div id='alist3'>";
 for ($x = ($span1*2);$x < ($span1*3+1);$x++)
 {
  $a = @artists[$x];
  print "<h1><A HREF=\"/cgi-bin/request.pl?chan=$chanid&s_artist=$a\">$a</A></h1>";
 }
 print "</div>";



 print "</div>";
}
elsif ($search ne "Search")
{
 # album + song list by artist 

 print "<div id='albumlist'>";

 $qry = "SELECT DISTINCT artist,album FROM tunes WHERE artist=? ORDER BY album";


 $out = $dbh->prepare($qry);
 $out->execute($s_artist);

 while (my($artist,$album)=$out->fetchrow_array)
 {

  print "<div class='album'>";

  $qry2 = "SELECT tunes.ID,tunes.title,tunes.time,tunes.added,channeltunes.lastplayed,
		  channeltunes.totalplays,channeltunes.requests,channeltunes.cancels,
		  channeltunes.randomplay,channeltunes.disabled,channeltunes.added
           FROM tunes,channeltunes WHERE (tunes.ID = channeltunes.tuneid) AND (channeltunes.channelid = ?)
                and (tunes.artist = ?) and (tunes.album = ?) ORDER BY tunes.title";

  $out2 = $dbh->prepare($qry2); 
  $out2->execute($chanid,$artist,$album);

  $as=0;
  $asr=0;
  $srt=0;
  $tp =0;
  $tr =0;
  $tc =0;  

  while (my($tid,$title,$time,$tadded,$lastplayed,$totalplays,$requests,$cancels,$random,$disabled,$cadded)=$out2->fetchrow_array)
  {
   $as++;
   $sr = displaytune($tid,$title,$time,$tadded,$lastplayed,$totalplays,$requests,$cancels,$random,$disabled,$cadded,$artist,$album,"");
   if ($sr > 0) { $asr++; $srt=$srt+$sr; }
   $tp = $tp + $totalplays;
   $tr = $tr + $requests;
   $tc = $tc + $cancels;
  }
  print "<div class='albumdetails'>"; 

  print "<P>";

  if (-e "/var/www/www.spogbiper.com/coverart/s_$artist - $album.jpg")
  {
   $fn = "/coverart/s_$artist - $album.jpg";
  }
  else
  {
   $fn = "/coverart/s_unknown.jpg";
  }

  print "<IMG SRC=\"$fn\">";

  print "<h2>$artist<br>$album</h2>";

  if ($as > 1)
  {

   print "<h6>Album rating: ";
   if ($asr) { print $srt / $asr; } else { print "Unrated"; }
   print "<BR>";
   print "Total plays: $tp<BR>";
   print "Total requests: $tr<BR>";
   print "Total cancels: $tc</h6>";
  }

  print "</div>";

  print "</div>";

 }
 print "</div>";
}
else
{
 #SEARCH RESULTS
 if ($searchtxt eq "") { $searchtxt = "fancypants"; }

 my $st = lc($searchtxt);
 
 $st =~ s/\s+/\%/g;

 $st = "%" . $st . "%";

 $qry2 = "SELECT tunes.artist,tunes.album,tunes.ID,tunes.title,tunes.time,tunes.added,channeltunes.lastplayed,
                  channeltunes.totalplays,channeltunes.requests,channeltunes.cancels,
                  channeltunes.randomplay,channeltunes.disabled,channeltunes.added
           FROM tunes,channeltunes WHERE ((tunes.ID = channeltunes.tuneid) AND (channeltunes.channelid = ?)) 
         AND ((LCASE(artist) LIKE ?) 
         OR (LCASE(album) LIKE ?) or (LCASE(title) LIKE ?)) ORDER BY artist,album,title";

 $out2 = $dbh->prepare($qry2);
 $out2->execute($chanid,$st,$st,$st); 

 print "<div id='albumlist'>";

 $oldart = "";
 $oldalb = "";

 while (my($artist,$album,$tid,$title,$time,$tadded,$lastplayed,$totalplays,$requests,$cancels,$random,$disabled,$cadded)=$out2->fetchrow_array)
 {
  if (($oldart ne $artist) or ($oldalb ne $album)) { $changed = 1; } else { $changed = 0; }

  if ($changed)
  {
   if ($oldart ne "") {  print "</div>"; }
   print "<div class='album'>";
  }

  $oldart = $artist;
  $oldalb = $album;

  displaytune($tid,$title,$time,$tadded,$lastplayed,$totalplays,$requests,$cancels,$random,$disabled,$cadded,$artist,$album,$searchtxt);
  
  if ($changed)
  {
   print "<div class='albumdetails'>";

   print "<P>";                            

   if (-e "/var/www/www.spogbiper.com/coverart/s_$artist - $album.jpg")
   {
    $fn = "/coverart/s_$artist - $album.jpg";
   }
   else
   {
    $fn = "/coverart/s_unknown.jpg";
   }

   print "<IMG SRC=\"$fn\">";

   $a = "<h2>$artist<br>$album</h2>";

   if ($searchtxt) { $a =~ s/($searchtxt)/\<FONT COLOR=\#FFFF22\>\<B\>$1\<\/B\>\<\/FONT\>/ig; }

   print "$a";


   if ($as > 1)
   {

    print "<h6>Album rating: ";
    if ($asr) { print $srt / $asr; } else { print "Unrated"; }
    print "<BR>";
    print "Total plays: $tp<BR>";
    print "Total requests: $tr<BR>";
    print "Total cancels: $tc</h6>";
   }

   print "</div>";

  }
 }

 print "</div>";

}

print "</BODY></HTML>";








sub displaytune
{
 my ($tid,$title,$time,$tadded,$lastplayed,$totalplays,$requests,$cancels,$random,$disabled,$cadded,$artist,$album,$search) = @_;


 if ($search ne "Search") 
 { 
  print "<div class='tune'>"; 
 } 
 else 
 { 
  print "<div class='tunerel'>"; 
 }

 print "<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0><TR>";
 print "<TD WIDTH=400 COLSPAN=4>";

 if ($search eq "")
 {
  $a ="<h3><A HREF=\"/cgi-bin/request.pl?chan=$chanid\&s_artist=$artist\&req=$tid\">
          <B>$title</B></A></h3>";
 }
 else
 {
  $b = "<h3><A HREF=\"/cgi-bin/request.pl?chan=$chanid\&searchtxt=$search\&req=$tid&search=Search\"><B>";
  $a = "$title</B></A></h3>"; 
  $a =~ s/($searchtxt)/\<FONT COLOR=\#FFFF22\>\<B\>$1\<\/B\>\<\/FONT\>/ig; 
  $a = $b . $a;
 }


 print "$a</td></TR><TR><TD WIDTH=100>";

 #calculate rating

 $qry3 = "SELECT * FROM tuneratings WHERE tuneid = $tid";
 $out3 = $dbh->prepare($qry3);
 $out3->execute;

 $votes = 0;
 $totrating = 0;

 while (my($voteid,$tuneid,$voteip,$rating,$vtime)=$out3->fetchrow_array)
 {
  if (($rating < 6) && ($rating > 0))
  {
   $totrating = $totrating + $rating;
   $votes++;
  }
 }

 if ($votes)
 {
  $ora = $totrating / $votes;
  $rating = int((int(($totrating / $votes)*10)*2)/10);
  print "<IMG SRC=/$rating.gif>";
 }
 else
 {
  print "<h4>Unrated</H4>";
 }

 print "</td>";

 print "<TD WIDTH=100><H4>Votes: $votes</TD>";
 print "<TD WIDTH=100><H4>Requests: $requests</TD>";
 print "<TD WIDTH=100><h4>Cancels: $cancels</TD></TR>";
 print "<TD WIDTH=100 VALIGN=TOP>";
 print "<TABLE><TR><TD>";
 if ($random) { print "<IMG ALIGN=BOTTOM SRC=/check.png>"; } 
   else { print "<IMG VALIGN=BOTTOM SRC=/no.png>"; } 

 print "</TD><TD><h4>Random play</TD></TR></TABLE>";
# show display
 
# $showqry = "SELECT * FROM 


 print "</H4></TD>";
 print "<TD WIDTH=100 VALIGN=TOP><H4>Total plays: $totalplays</TD>";
 print "<TD WIDTH=100 VALIGN=TOP><H4>Last Played:<BR>$lastplayed</TD>";
 print "<TD WIDTH=100 VALIGN=TOP><H4>Added:<BR>$cadded</TD>";
 print "</TR></TABLE>";

# Cancels: $cancels";
#   print " Total plays: $totalplays Last Played: $lastplayed Added: $cadded";


 print "</div>";


 return($ora);
}
