<?php

include "gdweb-settings.php";

###########################################################################
### headers / footers
function print_header($current)
{
  print "<!doctype html public \"-//W3C//DTD HTML 3.2 Final//EN\">
  <html>
  <head>
  <title>GiantDisc Web Interface: $current</title>
  <link href=\"gdweb.css\" rel=\"stylesheet\" type=\"text/css\">
  </head>
  <body>";

  print "<h2>GiantDisc Web Interface</h2>";
  print "<div class='$current'>";
  print "<table border='0' cellspacing='0' cellpadding='1'><tr>";
  if (strcmp($current, "search")==0){
    print "<td class=\"menuactive\"><a class=\"menuactive\" href=\"index.php\">Search</a></td>";
  }
  else{
    print "<td class=\"menuinactive\"><a class=\"menuinactive\" href=\"index.php\">Search</a></td>";
  }
  print "<td>&nbsp;</td>";
  if (strcmp($current, "browse")==0){
    print "<td class=\"menuactive\"><a class=\"menuactive\" href=\"browse.php\">Browse</a></td>";
  }
  else{
    print "<td class=\"menuinactive\"><a class=\"menuinactive\" href=\"browse.php\">Browse</a></td>";
  }
/*
  print "<td>&nbsp;</td>";
  if (strcmp($current, "playlist")==0){
    print "<td class=\"menuactive\"><a class=\"menuactive\" href=\"playlist.php\">Playlist</a></td>";
  }
  else{
    print "<td class=\"menuinactive\"><a class=\"menuinactive\" href=\"playlist.php\">Playlist</a></td>";
  }
*/
  print "</tr></table>";
}


###########################################################################
function print_footer($current)
{
  if (!preg_match("/Settings/i", $current)){
    print "<p><small><a href=\"settings.php\">Settings</a></small>";
  }
  print "</div>";
  print "</body></html>";
}


###########################################################################
### functions
function print_genre_options($selected)
{
  $recset = mysql_db_query ("GiantDisc", "SELECT * FROM genre ORDER BY id");
  print "<option value=\"\">&nbsp;</option>\n";
  while($row = mysql_fetch_array($recset)) {
    print "<option value=\"".$row["id"]."\"";
    if ($row["id"] == $selected) {print(" selected");};
    print ">";
	for($i=1; $i<strlen($row["id"]); $i++){print ("&nbsp;&nbsp;&nbsp;");};
	print $row["genre"]."</option>\n";
  }
}


function print_genre_string($genre)
{
  $recset = mysql_db_query ("GiantDisc", "SELECT * FROM genre WHERE id='$genre'");
  if($row = mysql_fetch_array($recset)) {
	print $row["genre"];
  }
}


function print_lang_options($selected)
{
  $recset = mysql_db_query ("GiantDisc", #"SELECT * FROM language ORDER BY id");
           "SELECT language.id, language.language, COUNT(*) AS freq "
          ."FROM language,tracks WHERE tracks.lang=language.id "
          ."GROUP BY language.id ORDER BY freq DESC ");
  print "<option value=\"\">&nbsp;</option>\n";
  while($row = mysql_fetch_array($recset)) {
    print "<option value=\"".$row["id"]."\"";
    if ($row["id"] == $selected) {print(" selected");};
    print ">";
	print $row["language"]."</option>\n";
  }
}

function print_type_options($selected)
{
  $recset = mysql_db_query ("GiantDisc", "SELECT * FROM musictype ORDER BY id");
  print "<option value=\"\">&nbsp;</option>\n";
  while($row = mysql_fetch_array($recset)) {
    print "<option value=\"".$row["id"]."\"";
    if ($row["id"] == $selected) {print(" selected");};
    print ">";
	print $row["musictype"]."</option>\n";
  }
}

function print_source_options($selected)
{
  $recset = mysql_db_query ("GiantDisc", "SELECT * FROM source ORDER BY id");
  print "<option value=\"\">&nbsp;</option>\n";
  while($row = mysql_fetch_array($recset)) {
    print "<option value=\"".$row["id"]."\"";
    if ($row["id"] == $selected) {print(" selected");};
    print ">";
	print $row["source"]."</option>\n";
  }
}

function print_rating_options($selected)
{
  $recset = array("-", "0", "+", "++");
  print "<option value=\"\">&nbsp;</option>\n";
  while(list($key, $value) = each($recset)) {
    print "<option value=\"".$key."\"";
    if (strlen($selected)>0 && $key == $selected) {print(" selected");};
    print ">";
	print $value."</option>\n";
  }
}

function print_rating_string($rating)
{
  $recset = array("-", "0", "+", "++");
  while(list($key, $value) = each($recset)) {
    if ($key == $rating) {print($value);};
  }
}

function language_string($language)
{
  $recset = mysql_db_query ("GiantDisc", "SELECT * FROM language WHERE id='$language'");
  if($row = mysql_fetch_array($recset)) {
    return $row["language"];
  }
}

function print_condition_options($selected)
{
  $recset = array("ok", "noisy", "damaged");
  print "<option value=\"\">&nbsp;</option>\n";
  while(list($key, $value) = each($recset)) {
    print "<option value=\"".$key."\"";
    if ($key == $selected) {print(" selected");};
    print ">";
	print $value."</option>\n";
  }
}

function seconds2time($seconds)
{
  return floor($seconds/60).":".sprintf("%02d",($seconds%60));
}

function full_mp3_fname($fname)
# returns the full path+filename for the mp3 file 'fname'
{
  if (strlen($fname)>=8){
    #exec("/bin/ls /home/music/??/".$fname, $fullname);
    exec("/bin/ls musichome/??/".$fname, $fullname);
    return $fullname[0];
  }
  else{
    return "";
  }
}



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

function show_trackrow($row, 
                       $showartist, $showtitle, $showgenre, $showlang, $showyear, $showrating,
			           $showbpm, $showsource, $showcreated, $showmodified, $showlength,
					   $showedit, $showdownload, $playtp, $showtoplaylist)
{
	if (strlen($row["year"])>2){ $yearstr = "'".substr($row["year"], 2, 2);}
	else                       { $yearstr = $row["year"];}

    if (strcmp($playtp, "dl")==0){$mp3path = full_mp3_fname($row["mp3file"]);}
    
    if ($showartist){
      print "<td class=\"trklst\">&nbsp;".$row["artist"]." </td>";
	}
    if ($showtitle){
	  print "<td class=\"trklst\">&nbsp;".$row["title"]." </td>";
	}
    if ($showgenre){
	  print "<td class=\"trklst\">&nbsp;";
      if (strlen($row["genre1"])>0){
	    print_genre_string($row["genre1"]);
	  }
      if (strlen($row["genre1"])>0 && strlen($row["genre2"])>0){print "<br>";}
      if (strlen($row["genre2"])>0){
	    print "&nbsp;";
	    print_genre_string($row["genre2"]);
	  }
	  print "</td>";
	}
    if ($showrating){
	  print "<td class=\"trklst\">&nbsp;";
      print_rating_string($row["rating"]);
	  print "</td>";
	}
    if ($showlang){
	  print "<td class=\"trklst\">&nbsp;".language_string($row["lang"])." </td>";
	}
    if ($showyear){
	  print "<td class=\"trklst\">&nbsp;".$yearstr." </td>";
	}
    if ($showlength){
	  print "<td class=\"trklst\">&nbsp;".seconds2time($row["length"])." </td>";
	}
    if ($showedit){
	  print "<td class=\"trklst\">&nbsp;<a href=\"gdedittrack.php?id="
		   		.$row["id"]."\" target=\"_blank\">"
				."<img border=\"0\" src=\"img/edit-c.gif\" alt=\"Edit\"></a></td>";
	}
    if ($showdownload){
      # Download mp3
	  # types: playtp = dl, rd, df, dl or dr
      if (strcmp($playtp, "dl")==0){
	    print "<td class=\"trklst\">&nbsp;<a href=\"".$mp3path."\">"
		     ."<img border=\"0\" src=\"img/dl.gif\" alt=\"Download\"></a></td>";
	  }
      else{ # all other playtypes handled by "gdtrid2mp3.php"
	    print "<td class=\"trklst\">&nbsp;<a href=\"gdtrid2mp3.php?playtp=$playtp"
				."&id=".$row["id"]
				."&mp3f=".$row["mp3file"]
				."\">"."<img border=\"0\" src=\"img/dl.gif\" alt=\"Download\"></a></td>";
	  }
	}
}


###########################################################################
### cookie functions

$expire_sec = 1000000;  # Cookie expires in ... seconds


function set_get_cookie($updatecookie, $cookiename, $defaultval, $cookievars)
{
  global $expire_sec;
  
  if (isset($updatecookie) and strlen($updatecookie)>0){
    setcookie ($cookiename, $updatecookie,time()+$expire_sec, "/~music/");
    return $updatecookie;
  }
  else{
    if (strlen ($cookievars[$cookiename]) >0 ){
      return $cookievars[$cookiename];  
    }
    else{
      setcookie ($cookiename, $defaultval,time()+$expire_sec, "/~music/");
      return $defaultval;  
    }
  }
}


###########################################################################
function set_get_cbx_cookie($newsettings, $cookiename, $cookieval, $cookievars)
# set get checkbox cookie
{
  global $expire_sec;
  
  if (isset($newsettings)){
    setcookie ($cookiename, $cookieval, time()+$expire_sec, "/~music/");
  }
  else{
    if (isset($cookievars[$cookiename]) and $cookievars[$cookiename]){
      $cookieval = 1;
	}
	else{
      $cookieval = 0;
	}
  }
  return $cookieval;
}


###########################################################################
### "direct linking" functions

function dl_make_filelist()
{
#  exec("/bin/ls /home/music/??/*0.mp3 > tmp/filelist.txt");

#  $fp = fopen ("tmp/file2.txt", "w");
#  fwrite ($fp, "This is my string\n");
#  fclose($fp);
}

?>
