<?php 
  include_once "./audiostreamerlib.php";

  //  AudioStreamer, www.audiostreamer.org
  //  Copyright (C) <2013>  <Lieven Rottiers>
  //
  //  This program is free software: you can redistribute it and/or modify
  //  it under the terms of the GNU General Public License as published by
  //  the Free Software Foundation, either version 3 of the License, or
  //  (at your option) any later version.
  //
  //  This program is distributed in the hope that it will be useful,
  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  //  GNU General Public License for more details.
  //
  //  You should have received a copy of the GNU General Public License
  //  along with this program.  If not, see <http://www.gnu.org/licenses/>.

  $output = '';
  $covers = '';
  $artist_dir = '';
  $id = $_GET["id"];
  
  try {
    $dbh = new PDO("sqlite:".$sdb);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);     

    //possible to add/play all
    $sql = "select id, filename, parent_id, link from music where id=".$id;
    foreach ($dbh->query($sql) as $row) {
      $artist_dir = utf8_encode($row['filename']);
      $output = $output.'<ul id="directory"><div class="add" onClick="addDirectories('.$row['id'].')"></div>'
                       .'<div class="play" onClick="clearPlaylist();addDirectories('.$row['id'].')"></div>'
                       .$artist_dir;
      //if link field is not empty then show the link
      if (!empty($row['link'])) {
        $output = $output.'<div class="license_link">'.utf8_encode($row['link']).'</div><br />';
      }      
      else {
        $output = $output.'<br /><br />';
      }  
                       
      //$covers = $covers.'<img class="cover-album" src="./getCover.php?id='.$id.'&resize=1">';
      $covers = $covers.'<span href="./getCover.php?id='.$id.'&resize=0">'.
                        '<img class="cover-album" src="./getCover.php?id='.$id.'&resize=1" onClick="showImage(this)" title="zoom"></span>';
    }

    //show master to go back one level
    $sql = "select id, filename from music where id=".$row['parent_id'];
    foreach ($dbh->query($sql) as $row) {
       $output = '<div class="masterDirectory"><a href="#" onClick="getDirectory('.$row['id'].')"><div class="masterDirectoryImage"></div>'.utf8_encode($row['filename']).'</a><br /><br /></div>'.$output;
    }

    $sql = "select id, filename, is_dir, song, album, artist, length from music where parent_id=".$id." and ifnull(extension,'dir') in ('dir','mp3','wma','wav','ogg') order by path collate nocase";
    foreach ($dbh->query($sql) as $row) {
      if ($row['is_dir'] == 1) {
        $output = $output.'<li><div class="add" onClick="addDirectories('.$row['id'].')"></div>'
                         .'<div class="play" onClick="clearPlaylist();addDirectories('.$row['id'].')"></div>'
                         .'<a href="#" onClick="getDirectory('.$row['id'].')" title="'.utf8_encode($row['filename']).'">'.utf8_encode($row['filename']).'</a></li>';
        $covers = $covers.'<a href="#" onClick="getDirectory('.$row['id'].')" title="'.$artist_dir.' - '.utf8_encode($row['filename']).'"><img class="cover-album" src="./getCover.php?id='.$row['id'].'&resize=1"></a>';
      }
      else {
        if (!empty($row['song'])) {
          $playlist = $row['artist']." - ".$row['album']." - ".$row['song']." (".$row['length'].")";
          $song = $row['artist']." - ".$row['song']." (".$row['length'].")";
        }
        else {
          $playlist = utf8_encode($row['filename']);
          $song = utf8_encode($row['filename']);        
        }
        
        $output = $output.'<li><div class="add" onClick="addSong(\'./streammp3.php?id='.$row['id'].'\',\''.str_replace("'", "\'", $playlist).'\',\''.$row['id'].'\')"></div>'
                         .'<div class="play" onClick="clearPlaylist();addSong(\'./streammp3.php?id='.$row['id'].'\',\''.str_replace("'", "\'", $playlist).'\',\''.$row['id'].'\')"></div>'
                         .'<div class="song-detail-link" onClick="getSongDetails('.$row['id'].')" title="'.$song.'"></div>'
                         .$song.'</li>';
      }
    }   
    $output = '<div class="top-spacer"></div>'.$output. '</ul>';
    $output = $output.$covers;
    
    //close connection
    $dbh = null;    
  }
  catch(PDOException $e)
  {
    $output = $output.'<br/>'.$e->getMessage();
  }  

  echo $output;
  //echo utf8_encode($output);
  ?> 
