<?php

include "gdweb.php";
include "gdwebdef.php";

$playtp = set_get_cookie($setplaytp, "playtp", "dl", $HTTP_COOKIE_VARS);

print_header ("browse");

$dbh = mysql_connect();	

#phpinfo();

#######################################################################################
### Constants
$trunclen = 30; # characters


### Functions
function write_alphabet($prefix, $highlight)
{
  $cnt = ord("a");
  while ($cnt <= ord("z")){
    if ($cnt == ord($highlight)){print "<b>";};
    print "<a class=\"brslst\" href=\"".$prefix.chr($cnt)."\">".chr($cnt)."..</a> ";
    if ($cnt == ord($highlight)){print "</b>";};
    $cnt++;
  }
}


function write_genres($prefix, $selgenre, $shgenre)
# displays a part of the genre tree. 
# '$selgenre' specifies, which genre is selected (will be highlighted). 
# '$shgenre' specifies the deepest genre node in the hierarchy that should be opened/printed
{
  $whereclause = " id LIKE '_' "; # always show the first level 
  $i = strlen($shgenre);
  while ($i >= 1){
    $whereclause .= " OR id LIKE '".substr($shgenre, 0, $i)."_' ";
    $i--;
  }
  
  $qrystr = "SELECT * FROM genre WHERE $whereclause ORDER BY id";
  $recset = mysql_db_query ("GiantDisc", $qrystr);
  print "\n<div class='genrebox'>";
  while($row = mysql_fetch_array($recset)) {
    print str_repeat("&nbsp;", (strlen($row["id"])-1)*4);  # indents
    $pos = strpos($shgenre, $row["id"]);  # is current id a prefix of $shgenre?
    if ($pos!==false and $pos==0){# are we in the open branch? 
      $imgdir = "tri-d.png";
      $opengen = substr ($row["id"], 0, strlen($row["id"])-1); # chop off last char
    }else{
      $imgdir = "tri-r.png";
      $opengen = $row["id"];
    }
    # print triangle
    print "<a href='$prefix&shgn=$opengen'><img src='img/$imgdir'></a>&nbsp;"; # ignore $l1: show no results when tree is changed
    # print genre text
    print "<a ".($row["id"]==$selgenre ? "class='selected'":"")
               ." href='".$prefix.$row["id"]."&shgn=$shgenre'>".$row["genre"]."</a><br>";
  }
  print "</div>";
}



function write_artistlink($prefix, $artist)
{
  global $trunclen;
  if (strlen($artist)>$trunclen){
    $art = substr($artist, 0, $trunclen);
    $appendix="..";
  }
  else{
    $art = $artist;
    $appendix="";
  }
  print "<a class=\"brslst\" href=\"".$prefix.urlencode($art)."\">"
        .htmlentities($art).$appendix."</a>&nbsp; ";
}



function write_albumlink($prefix, $albumrecord, $showedit)
{
  global $trunclen;
  #print $albumrecord["artist"]." - ".$albumrecord["title"]." [".$albumrecord["cddbid"]."], ";
  print "<tr>";  #<tr class="lnodd"   ...even>
  print "<td class=\"trklst\">";
  if (strlen($albumrecord["coverimg"])>1){
    $imgpath = full_mp3_fname($albumrecord["coverimg"]);
#	print "<a href=\"$imgpath\">";
	print "<a href=\"gdshowalbum.php?cddbid=".$albumrecord["cddbid"]."\">";
    print "<img width=\"40\" src=\"".$imgpath."\" height=\"40\" border=\"0\">";
	print "</a>";
  }
  print "</td>";
  print "<td class=\"trklst\">".$albumrecord["artist"]."</td>";
  print "<td class=\"trklst\"> - </td>";
  print "<td><a class=\"brslst\" href=\"".$prefix.$albumrecord["cddbid"]."\">"
        .$albumrecord["title"]."</a></td> ";


  if ($showedit){
    print "<td class=\"trklst\">&nbsp;<a href=\"gdeditalbum.php?cddbid="
		   		.$albumrecord["cddbid"]."\" target=\"_blank\">"
				."<img border=\"0\" src=\"img/edit-c.gif\" alt=\"Edit\"></a></td>";
  }

  #Album-Playlist-Link (by Merlot)
  print "<td class=\"trklst\">&nbsp;<a href=\"gdtrid2mp3.php?playtp=pr"
        ."&prAlbum=1" #don't use a new value for $playtp, it would interfere with cookie-settings
        ."&albid=".$albumrecord["cddbid"]
        ."&id=1"
        ."\">"."<img border=\"0\" src=\"img/dl.gif\" alt=\"Download\"></a></td>"; 
  print "</tr>";
}

#######################################################################################

?>

<p>

<?php 

### Show first level:
print "<table class='browsemenu'>";
print "<tr>";
print "<td><small><b><a href=\"browse.php?l0=tr-ar\">Tracks by Artist</a></b></small></td>";
print "<td><small><b><a href=\"browse.php?l0=tr-ti\">Tracks by Title</a></b></small></td>";
print "<td><small><b><a href=\"browse.php?l0=tr-gn\">Tracks by Genre</a></b></small></td>";
print "</small>";
print "<tr>";
print "<td><small><b><a href=\"browse.php?l0=al-ar\">Albums by Artist</a></b></small></td>";
print "<td><small><b><a href=\"browse.php?l0=al-ti\">Albums by Title</a></b></small></td>";
print "<td><small><b><a href=\"browse.php?l0=al-gn\">Albums by Genre</a></b></small></td>";
print "</small>";
print "</table>";


$qrystr = ""; # clear query string

if (isset($l0)){

  ### Tracks by Artist
  if (strcmp($l0, "tr-ar")==0){
    print "<h4>Tracks by Artist</h4>";
    write_alphabet("browse.php?l0=tr-ar&l1=", $l1);
    if (isset($l1)){ # level 1 specified?
      $tempquery = "SELECT DISTINCT artist FROM tracks WHERE artist LIKE '$l1%' ORDER BY artist";
      $recset = mysql_db_query ("GiantDisc", $tempquery);
      if (mysql_affected_rows()>0){print("<p><small>".mysql_affected_rows()." artists</small><br>");}
      while($row = mysql_fetch_array($recset)) {
	    write_artistlink("browse.php?l0=tr-ar&l1=$l1&ar=", $row["artist"]);
      }

	  if (isset($ar)){ # artist / level 2 specified?
        $qrystr = "SELECT * FROM tracks WHERE artist LIKE '$ar%' ORDER BY title ";
      }
	}
  }

  
  ### Tracks by Title
  if (strcmp($l0, "tr-ti")==0){
    print "<h4>Tracks by Title</h4>";
    write_alphabet("browse.php?l0=tr-ti&l1=", "");
	if (isset($l1)){ # level 1 specified?
      $qrystr = "SELECT * FROM tracks WHERE title LIKE '$l1%' ORDER BY title ";
	}
  }

  
  ### Tracks by Genre
  if (strcmp($l0, "tr-gn")==0){
    print "<h4>Tracks by Genre</h4>";
    write_genres("browse.php?l0=tr-gn&l1=", $l1, $shgn);
	if (isset($l1) and strlen($l1)>0 ){ # level 1 specified?
      $qrystr = "SELECT * FROM tracks WHERE genre1 LIKE '$l1%' or genre2 LIKE '$l1%' ORDER BY artist ";
	}
  }


  ### Albums by Artist
  if (strcmp($l0, "al-ar")==0){
    print "<h4>Albums by Artist</h4>";
    write_alphabet("browse.php?l0=al-ar&l1=", $l1);
    if (isset($l1)){ # level 1 specified?
      $tempquery = "SELECT DISTINCT artist FROM album WHERE artist LIKE '$l1%' ORDER BY artist";
      $recset = mysql_db_query ("GiantDisc", $tempquery);
      if (mysql_affected_rows()>0){print("<p><small>".mysql_affected_rows()." artists</small><br>");}
      while($row = mysql_fetch_array($recset)) {
	    write_artistlink("browse.php?l0=al-ar&l1=$l1&ar=", $row["artist"]);
      }

	  if (isset($ar)){ # artist / level 2 specified?
        $tempquery = "SELECT * FROM album WHERE artist LIKE '$ar%' ORDER BY title";
        $recset = mysql_db_query ("GiantDisc", $tempquery);
        if (mysql_affected_rows()>0){print("<p><small>".mysql_affected_rows()." albums</small><br>");}
        print "<table borders=\"0\">";
        while($row = mysql_fetch_array($recset)) {
	      write_albumlink("browse.php?l0=al-ar&l1=$l1&ar=$ar&albid=", $row, $showedit);
        }
        print "</table>";
      }

	  if (isset($albid)){ # albumid / level 3 specified?
        $qrystr = "SELECT * FROM tracks WHERE sourceid = '$albid' ORDER BY tracknb ";
      }
	}
  }


  ### Albums by Title
  if (strcmp($l0, "al-ti")==0){
    print "<h4>Albums by Title</h4>";
    write_alphabet("browse.php?l0=al-ti&l1=", $l1);
    if (isset($l1)){ # level 1 (initial char) specified?
      $tempquery = "SELECT * FROM album WHERE title LIKE '$l1%' ORDER BY title";
      $recset = mysql_db_query ("GiantDisc", $tempquery);
      if (mysql_affected_rows()>0){print("<p><small>".mysql_affected_rows()." albums</small><br>");}
      print "<table borders=\"0\">";
      while($row = mysql_fetch_array($recset)) {
      write_albumlink("browse.php?l0=al-ti&l1=$l1&albid=", $row, $showedit);
      }
      print "</table>";
    }

    if (isset($albid)){ # albumid / level 2 specified?
      $qrystr = "SELECT * FROM tracks WHERE sourceid = '$albid' ORDER BY tracknb ";
    }
  }

  
  ### Albums by Genre
  if (strcmp($l0, "al-gn")==0){
    print "<h4>Albums by Genre</h4>";
    write_genres("browse.php?l0=al-gn&l1=", $l1, $shgn);
	if (isset($l1) and strlen($l1)>0 ){ # level 1 specified?
      $tempquery = "SELECT * FROM album WHERE genre LIKE '$l1%' ORDER BY artist";
      $recset = mysql_db_query ("GiantDisc", $tempquery);
      #if (mysql_affected_rows()>0){print("<p><small>".mysql_affected_rows()." albums</small><br>");}
      print("<p><small>".mysql_affected_rows()." albums</small><br>");
      print "<table borders=\"0\">";
      while($row = mysql_fetch_array($recset)) {
      write_albumlink("browse.php?l0=al-gn&l1=$l1&shgn=$shgn&albid=", $row, $showedit);
      }
      print "</table>";
    }

    if (isset($albid)){ # albumid / level 2 specified?
      $qrystr = "SELECT * FROM tracks WHERE sourceid = '$albid' ORDER BY tracknb ";
    }
  }


}



if(strlen($qrystr)>0){ # do query and show tracks
  if (strlen($rating)==0){$rating=0;}
  ### query tracks
  $recset = mysql_db_query ("GiantDisc", $qrystr);
  
  ### display the tracks
  if (mysql_affected_rows()>0){print "<hr noshade>";}else{print "<p>&nbsp;</p>";}
  print("<p><small><b>".mysql_affected_rows()." machting tracks</b></small></p>");
  print("<table border=\"0\" cellspacing=\"0\">\n");
  $evenln = 1;
  while($row = mysql_fetch_array($recset)) {
    if ($evenln){
      print "<tr class=\"lneven\">";
	}
	else{
      print "<tr class=\"lnodd\">";
	}
    show_trackrow($row,
	          $showartist, $showtitle, $showgenre, $showlang, $showyear, $showrating,
			  $showbpm, $showsource, $showcreated, $showmodified, $showlength,
			  $showedit, $showdownload, $playtp, $showtoplaylist);
    $evenln = 1-$evenln; #toggle $evenln
	
	print "</tr>\n";
  }
  print("</table>\n");
  mysql_free_result($recset);
}
?>

<hr noshade>

<p>

<?php 
print_footer ("search");
?>
