<?php

// defines if this script requires to be logged in
define( "PRIVATE", false );

# Call common libraries
require_once('./lib/inc-common.php');
require_once(FS_PATH."/etc/locale/".LANG_PACK."/inc-explore.php");

# Grab pear timing code
include_once (FS_PATH."/lib/timer/timer.php");

# Do initialization
$timer = new PHP_timer();
$timer->start();
$button1 = '';
$button2 = '';
$ge_html = '';
$al_html = '';
$ar_html = '';
$artist = '';
$parents = '';

# Navigation SQL
$ge_id = $_REQUEST['ge_id'];
$genres = array ();

while ($ge_id != 0) {
  $sql = "select parent_id from netjuke_genres where id = ".$ge_id;
  $ge_rs = $dbconn->Execute ($sql);
  if (! $ge_rs->RecordCount()) {
    $ge_id = 0;
    break;
  }
  $ge_id = $ge_rs->fields[0];
  $ge_rs->Close ();
  $sql = "select name from netjuke_genres where id = ".$ge_id;
  $ge_rs = $dbconn->Execute ($sql);
  if (! $ge_rs->RecordCount()) {
    $ge_id = 0;
    break;
  }
  $genres[$ge_id] = format_for_display($ge_rs->fields[0]);
  $ge_rs->Close ();
}

foreach ($genres as $id => $genre) {
  $parents .= "<a class = \"header\" HREF=\"".$_SERVER['PHP_SELF']."?do=list.artists&ge_id=".$id."\" title=\""
           . EXPLORE_GENRE_HELP . "\">" . $genre . "</a>"
           . "<b>&nbsp;&raquo;</b>\n";
}

// Build the common HTML
$timer->addmarker('Start Genre');
#echo '<pre>';

# SUB-GENRE TABLE
// Get a list of sub-genres and put them into a table
$sql = "select distinct ge.id, ge.name, count(tr.id), upper(ge.name) AS val"
     . " from netjuke_genres ge, netjuke_tracks tr"
     . " WHERE tr.ge_id = ge.id AND ge.parent_id = ".$_REQUEST['ge_id']
     . " and ge.id = tr.ge_id group by ge.id order by val asc";
#echo $sql."\n";
$ge_rs = $dbconn->Execute ($sql);
$ge_cnt = $ge_rs->RecordCount ();

$ge_html = '';

$cnt = $row_cnt = 0;

if ($ge_rs !== false) {

    while (!$ge_rs->EOF) {

      if ($cnt % 3 == 0) {
        $ge_html .= "<TR>\n";
        $row_cnt++;
      }

      $ge_html .= "<td class='line".(($row_cnt%2)+1)."' width='33%'>";

      $ge_html .= buildButton("./play.php?do=play_all&random=0&type=ge&subs=1&id=".$ge_rs->fields[0], EXPLORE_PLAY_GE_HELP, 'play')
                .  " ".buildButton("./play.php?do=play_all&random=1&type=ge&subs=1&id=".$ge_rs->fields[0], EXPLORE_RANDPLAY_GE_HELP, 'randplay')
                .  " ".(($NETJUKE_SESSION_VARS['gr_id'] <= 2) ? buildButton("./admin/md-edit.php?do=edit&type=ge&id=".$ge_rs->fields[0], EXPLORE_EDIT_GE_HELP, 'edit', 'NetjukeRemote') : "")
                .  ($NETJUKE_SESSION_VARS['explore'] == 't' ? " <A HREF='explore.php?do=list.artists&ge_id=".$ge_rs->fields[0]."'" : " <A HREF='search.php?do=list.tracks&col=ge_id=".$ge_rs->fields[0]."&sort=ge'")
                .  "   title=\"".EXPLORE_GE_HELP."\">"
                .  format_for_display($ge_rs->fields[1])."</A>"
                .  " (".$ge_rs->fields[2].")";

      $ge_html .= "</td>\n";

      if (($cnt + 1) % 3 == 0) $ge_html .= "</TR>\n";

      $ge_rs->MoveNext();

      $cnt++;

    }

    $ge_rs->Close();

}

// Fix the tables
if ( ($cnt + 2) % 3 == 0 ) {

  $ge_html .= "<td class='line".(($row_cnt%2)+1)."' nowrap>&nbsp;</td>\n";
  $ge_html .= "<td class='line".(($row_cnt%2)+1)."' nowrap>&nbsp;</td>\n";
  $ge_html .= "</tr>";

} elseif ( ($cnt + 1) % 3 == 0 ) {

  $ge_html .= "<td class='line".(($row_cnt%2)+1)."' nowrap>&nbsp;</td>\n";
  $ge_html .= "</tr>\n";

}

$row_cnt = $row_cnt;
if (abs($row_cnt) < 5) $row_cnt = 5;

// ARTISTS TABLE
$timer->addmarker('Start Artists');

// Get a list of artists in this genre and present them in a table
$sql = "select distinct ar.id, ar.name, ge.name, count(tr.id), upper(ar.name) AS val"
      . " from netjuke_artists ar, netjuke_tracks tr, netjuke_genres ge"
      . " WHERE tr.ar_id = ar.id AND tr.ge_id = ".$_REQUEST['ge_id']
      . " and ge.id = tr.ge_id group by ar.id order by val";
#$dbconn->debug = 1;
$ar_rs = $dbconn->Execute ($sql);
$ar_cnt = $ar_rs->RecordCount ();
$genre = $ar_rs->fields[2];
$genre_link = "<a class=\"header\" href=\"".$_SERVER['PHP_SELF']."?do=list.artists&ge_id="
            . $_REQUEST['ge_id']."\" title=\""
            . EXPLORE_GENRE_HELP . "\">" . $genre . "</a>";
$ar_html = '';

$cnt = $row_cnt = 0;

if ($ar_rs !== false) {

    while (!$ar_rs->EOF) {

      if ($cnt % 3 == 0) {
        $ar_html .= "<TR>\n";
        $row_cnt++;
      }

      $ar_html .= "<td class='line".(($row_cnt%2)+1)."' width='33%'>";

      $ar_html .= buildButton("./play.php?do=play_all&random=0&type=ar&id=".$ar_rs->fields[0], EXPLORE_PLAY_AR_HELP, 'play')
                .  " ".buildButton("./play.php?do=play_all&random=1&type=ar&id=".$ar_rs->fields[0], EXPLORE_RANDPLAY_AR_HELP, 'randplay')
                .  " ".(($NETJUKE_SESSION_VARS['gr_id'] <= 2) ? buildButton("./admin/md-edit.php?do=edit&type=ar&id=".$ar_rs->fields[0], EXPLORE_EDIT_AR_HELP, 'edit', 'NetjukeRemote') : "")
                .  ($NETJUKE_SESSION_VARS['explore'] == 't' ? " <A HREF='explore.php?do=list.albums&ge_id=".$_REQUEST['ge_id']."&ar_id=".$ar_rs->fields[0]."'" : " <A HREF='search.php?do=list.tracks&col=ar_id&val=".$ar_rs->fields[0]."&sort=ar'")
                .  "   title=\"".EXPLORE_AR_HELP."\">"
                .  format_for_display($ar_rs->fields[1])."</A>"
                .  " (".$ar_rs->fields[3].")";;

      $ar_html .= "</td>\n";

      if (($cnt + 1) % 3 == 0) $ar_html .= "</TR>\n";

      $ar_rs->MoveNext();

      $cnt++;

    }

    $ar_rs->Close();

}

// Fix the tables
$class = "line".(($row_cnt%2)+1);
if ( ($cnt + 2) % 3 == 0 ) {

  $ar_html .= "<td class='$class' nowrap>&nbsp;</td>\n";
  $ar_html .= "<td class='$class' nowrap>&nbsp;</td>\n";
  $ar_html .= "</tr>";

} elseif ( ($cnt + 1) % 3 == 0 ) {

  $ar_html .= "<td class='$class' nowrap>&nbsp;</td>\n";
  $ar_html .= "</tr>\n";

}

// ALBUMS TABLE
$timer->addmarker('Start Albums');

// Get a list of albums in this genre, by this artist, and present them in a table

if (isset ($_REQUEST['ar_id'])) {
  $clause = " WHERE tr.al_id = al.id AND ar.id = ".$_REQUEST['ar_id']." AND tr.ge_id = ".$_REQUEST['ge_id']
          . " AND tr.ar_id = ".$_REQUEST['ar_id']." and ar.id = tr.ar_id and al.id = tr.al_id and ge.id = tr.ge_id";
} else {
  $clause = " WHERE tr.al_id = al.id AND tr.ge_id = ".$_REQUEST['ge_id']
          . " and ar.id = tr.ar_id and al.id = tr.al_id and ge.id = tr.ge_id";
}

$sql = "select distinct al.id, al.name, count(tr.id), upper(al.name) AS val"
      . " from netjuke_artists ar, netjuke_albums al, netjuke_tracks tr, netjuke_genres ge "
      . $clause." group by al.id order by val";
$al_rs = $dbconn->Execute ($sql);
#echo $sql."\n";

$al_cnt = $al_rs->RecordCount ();

$al_html = '';

$cnt = $row_cnt = 0;

if (isset ($_REQUEST['ar_id'])) {
	$ar_rs = $dbconn->Execute ("select name from netjuke_artists where id = ".$_REQUEST['ar_id']);
  	$artist = "<a class = 'header' href = "
          . WEB_PATH."/search.php?do=list.tracks&col=ar_id&sort=ar&val=".$_REQUEST['ar_id']
          . ">".format_for_display($ar_rs->fields[0])."</a>";
	$ar_rs->Close ();
}

if ($al_rs !== false) {

    while (!$al_rs->EOF) {

      if ($cnt % 3 == 0) {
        $al_html .= "<TR>\n";
        $row_cnt++;
      }

      $al_html .= "<td class='line".(($row_cnt%2)+1)."' width='33%'>";

      $al_html .= buildButton("./play.php?do=play_all&random=0&type=al&id=".$al_rs->fields[0], EXPLORE_PLAY_AL_HELP, 'play')
                .  " ".buildButton("./play.php?do=play_all&random=1&type=al&id=".$al_rs->fields[0], EXPLORE_RANDPLAY_AL_HELP, 'randplay')
                .  " ".(($NETJUKE_SESSION_VARS['gr_id'] <= 2) ? buildButton("./admin/md-edit.php?do=edit&type=al&id=".$al_rs->fields[0], EXPLORE_EDIT_AL_HELP, 'edit', 'NetjukeRemote') : "")
                .  ($NETJUKE_SESSION_VARS['explore'] == 't' ? " <A HREF='search.php?do=list.tracks&col=al_id&val=".$al_rs->fields[0]."'" : " <A HREF='search.php?do=list.tracks&col=al_id&val=".$al_rs->fields[0]."&sort=al'")
                .  "   title=\"".EXPLORE_AL_HELP."\">"
                .  format_for_display($al_rs->fields[1])."</A>"
                .  " (".$al_rs->fields[2].")";

      $al_html .= "</td>\n";

      if (($cnt + 1) % 3 == 0) $al_html .= "</TR>\n";

      $al_rs->MoveNext();

      $cnt++;

    }

    $al_rs->Close();

}

// Fix the tables
$class = "line".(($row_cnt%2)+1);
if ( ($cnt + 2) % 3 == 0 ) {

  $al_html .= "<td class='$class' nowrap>&nbsp;</td>\n";
  $al_html .= "<td class='$class' nowrap>&nbsp;</td>\n";
  $al_html .= "</tr>";

} elseif ( ($cnt + 1) % 3 == 0 ) {

  $al_html .= "<td class='$class' nowrap>&nbsp;</td>\n";
  $al_html .= "</tr>\n";

}

#echo '</pre>';

// PROCESS THE REQUEST

if ( $_REQUEST['do'] == 'list.artists' ) {
########################################
# LIST ARTISTS (For explore view)
########################################

    $icons1 = buildButton("./play.php?do=play_all&random=0&type=ge&subs=1&id=".$_REQUEST['ge_id'], EXPLORE_PLAY_GE_HELP, 'play', '', true)
              .  " ".buildButton("./play.php?do=play_all&random=1&type=ge&subs=1&id=".$_REQUEST['ge_id'], EXPLORE_RANDPLAY_GE_HELP, 'randplay', '', true)
              .  " ".(($NETJUKE_SESSION_VARS['gr_id'] <= 2) ? buildButton("./admin/md-edit.php?do=edit&type=ge&id=".$_REQUEST['ge_id'], EXPLORE_EDIT_GE_HELP, 'edit', 'NetjukeRemote', true) : "");
    $icons2 = $icons1;

    // Show genres in first table (if there are any)
    if ($ge_html != '') {

      $header1 = constant ('EXPLORE_SUBGE_HEADER')." [".$parents.$genre_link."] (".$ge_cnt.")";
      $html1 = $ge_html;
      $button1 = "<input type=button value='". COMMON_SUMHEAD_ALL_LINK ."'  onClick=\"self.location.href = '".WEB_PATH."/search.php?do=list.tracks&col=ge_id&val=".$_REQUEST['ge_id']."&sort=ar';\" class='btn_header' title=\"".EXPLORE_LISTALBUMS_HEADER."\">";
      $xml_button1 = buildButton (WEB_PATH."/xml.php?do=show.subs&type=ge&id=".$_REQUEST['ge_id'], COMMON_XML_HELP, 'xml');

      // ... and artists in the second
      $header2 = constant ('EXPLORE_ARTISTS_HEADER')." [".$parents.$genre_link."] (".$ar_cnt.")";
      $html2 = $ar_html;
      $button2 = "<input type=button value='". COMMON_SUMHEAD_ALL_LINK ."'  onClick=\"self.location.href = '".WEB_PATH."/search.php?do=list.tracks&col=ge_id&val=".$_REQUEST['ge_id']."&sort=ge';\" class='btn_header' title=\"".EXPLORE_LISTALBUMS_HEADER."\">";
      $xml_button2 = buildButton (WEB_PATH."/xml.php?do=show.artists&type=ge&id=".$_REQUEST['ge_id'], COMMON_XML_HELP, 'xml');

    } else {

      // Fall back to displaying artists and albums
      $header1 = "<a href=\"".WEB_PATH."/alphabet.php?do=alpha.artists\" target=\"NetjukeRemote\">".constant ('EXPLORE_ARTISTS_HEADER')."</a> [".$parents.$genre_link."] (".$ar_cnt.")";
      $html1 = $ar_html;
      $button1 = "<input type=button value='". COMMON_SUMHEAD_ALL_LINK ."'  onClick=\"self.location.href = '".WEB_PATH."/search.php?do=list.tracks&col=ge_id&val=".$_REQUEST['ge_id']."&sort=ar';\" class='btn_header' title=\"".EXPLORE_LISTARTISTS_HEADER."\">";
      $xml_button1 = buildButton (WEB_PATH."/xml.php?do=show.artists&type=ge&id=".$_REQUEST['ge_id'], COMMON_XML_HELP, 'xml');

      // ... and albums in the second
      $header2 = "<a href=\"".WEB_PATH."/alphabet.php?do=alpha.albums\" target=\"NetjukeRemote\">".constant ('EXPLORE_ALBUMS_HEADER')."</a> "." [".$parents.$genre_link."] (".$al_cnt.")";
      $html2 = $al_html;
      $button2 = "<input type=button value='". COMMON_SUMHEAD_ALL_LINK ."'  onClick=\"self.location.href = '".WEB_PATH."/search.php?do=list.tracks&col=ge_id&val=".$_REQUEST['ge_id']."&sort=al';\" class='btn_header' title=\"".EXPLORE_LISTALBUMS_HEADER."\">";
      $xml_button2 = buildButton (WEB_PATH."/xml.php?do=show.albums&type=ge&id=".$_REQUEST['ge_id'], COMMON_XML_HELP, 'xml');

    }



} elseif ( $_REQUEST['do'] == 'list.albums' ) {
########################################
# LIST ALBUMS (For explore view)
########################################

  // List all the albums by this artist, in this genre
  // Show artists in first table
  $header1 = "<a href=\"".WEB_PATH."/alphabet.php?do=alpha.artists\" target=\"NetjukeRemote\">".constant ('EXPLORE_ARTISTS_HEADER')."</a> [".$parents.$genre_link."] (".$ar_cnt.")";
  $html1 = $ar_html;
  $button1 = "<input type=button value='". COMMON_SUMHEAD_ALL_LINK ."'  onClick=\"self.location.href = '".WEB_PATH."/search.php?do=list.tracks&col=ge_id&val=".$_REQUEST['ge_id']."&sort=ge';\" class='btn_header' title=\"".EXPLORE_LISTALBUMS_HEADER."\">";
  $icons1 = buildButton("./play.php?do=play_all&random=0&type=ge&subs=1&id=".$_REQUEST['ge_id'], EXPLORE_PLAY_GE_HELP, 'play', '', true)
          .  " ".buildButton("./play.php?do=play_all&random=1&type=ge&subs=1&id=".$_REQUEST['ge_id'], EXPLORE_RANDPLAY_GE_HELP, 'randplay', '', true)
          .  " ".(($NETJUKE_SESSION_VARS['gr_id'] <= 2) ? buildButton("./admin/md-edit.php?do=edit&type=ge&id=".$_REQUEST['ge_id'], EXPLORE_EDIT_GE_HELP, 'edit', 'NetjukeRemote', true) : "");
  $xml_button1 = buildButton (WEB_PATH."/xml.php?do=show.artists&type=ge&id=".$_REQUEST['ge_id'], COMMON_XML_HELP, 'xml');

  // Show albums by this artist in second table
  $header2 = "<a href=\"".WEB_PATH."/alphabet.php?do=alpha.albums\" target=\"NetjukeRemote\">".constant ('EXPLORE_ALBUMSBY_HEADER')."</a> ".$artist." [".$parents.$genre_link."] (".$ar_cnt.")";
  $html2 = $al_html;
  $button2 = "<input type=button value='". COMMON_SUMHEAD_ALL_LINK ."'  onClick=\"self.location.href = '".WEB_PATH."/search.php?do=list.tracks&col=ar_id&val=".$_REQUEST['ar_id']."&sort=ar';\" class='btn_header' title=\"".EXPLORE_LISTALBUMS_HEADER."\">";
  $icons2 = buildButton("./play.php?do=play_all&random=0&type=ar&id=".$_REQUEST['ar_id'], EXPLORE_PLAY_AR_HELP, 'play', '', true)
          .  " ".buildButton("./play.php?do=play_all&random=1&type=ar&id=".$_REQUEST['ar_id'], EXPLORE_RANDPLAY_AR_HELP, 'randplay', '', true)
          .  " ".(($NETJUKE_SESSION_VARS['gr_id'] <= 2) ? buildButton("./admin/md-edit.php?do=edit&type=ar&id=".$_REQUEST['ar_id'], EXPLORE_EDIT_AR_HELP, 'edit', 'NetjukeRemote', true) : "");
  $xml_button2 = buildButton (WEB_PATH."/xml.php?do=show.albums&type=ge&id=".$_REQUEST['ge_id'], COMMON_XML_HELP, 'xml');

}

# HTML header
$section = "explore";
include (INTERFACE_HEADER);

SummaryHeader();

?>

<table width='100%' border=0 cellspacing=0 cellpadding=0>
  <tr>
    <td width="50%" align=left valign=top>


    <table width='97%' border=0 cellspacing=1 cellpadding=3 class='border'>
      <tr>
        <td class='header' colspan=3 nowrap>
        <table border=0 width=100% cellspacing=0 cellpadding=0>
          <tr>
            <td align=left class="header" nowrap>
              <B><?php echo  $header1; ?></B>
            </td>
            <td align=right>
              <?php echo $icons1; ?>
              <?php echo $button1; ?>
            </td>
          </tr>
        </table>
      </tr>
      <?php echo $html1; ?>
    </table>
    <?php

      if (ENABLE_XML == 't') {
        echo "<br><center>".$xml_button1."</center>";
      }

    ?>


    </td>
    <td width="50%" align=right valign=top>

    <table width='97%' border=0 cellspacing=1 cellpadding=3 class='border'>
      <tr>
        <td class='header' colspan=3 nowrap>
        <table border=0 width=100% cellspacing=0 cellpadding=0>
          <tr>
            <td align=left class="header" nowrap>
              <B><?php echo  $header2; ?></B>
            </td>
            <td align=right>
              <?php echo $icons2; ?>
              <?php echo $button2; ?>
            </td>
          </tr>
        </table>
      </tr>
      <?php echo $html2; ?>
    </table>
    <?php

      if (ENABLE_XML == 't') {
        echo "<br><center>".$xml_button2."</center>";
      }

    ?>

    </td>
  </tr>
</table>

<?php
include (INTERFACE_FOOTER);

$timer->stop();
//$timer->debug();
#$timer->showtime();
?>
