path = $path; else $this->path = "/var/cache/mt-daapd/songs3.db"; $this->dbtype = "sqlite"; $this->sql = ""; $this->pdostatement = false; $this->debug = false; } function setDb(){ if( !$this->db ) $this->db = new PDO( $this->dbtype.":".$this->path ); } function quote( $str ){ $this->setDb(); return $this->db->quote( $str ); } function fetchRow(){ // $pdos = $this->db->query( $this->sql ); return $this->pdostatement->fetch(); } function query( $sql ){ $this->setDb(); if( $this->debug ) echo $sql."\n"; $this->pdostatement = $this->db->query( $sql ); if( $this->debug ){ // if( !$this->pdostatement ) echo "ERROR: Query failed\n"; // print_r( $this->pdostatement->errorInfo() ); } if( !$this->pdostatement ) return false; return true; } function fetchOne( $sql ){ $this->query( $sql ); return $this->fetchRow(); } /** * Set any track IDs in the array to play_count = 1 if they are 0 */ function markTracksPlayed( $aTracks ){ if( sizeof( $aTracks ) == 0 ) return false; $sql = "UPDATE songs SET play_count = 1 WHERE play_count = 0 AND id IN (".join(", ", $aTracks ).")"; return $this->query( $sql ); } function insertPlaylist( $name ){ /* * 0 - Static playlist, updated via the web interface * 1 - Smart playlists (SQL query) * 2 - Static playlist, from a m3u file (can't be edited from the web interface) * 3 - Static playlist, from iTunes XML file (can't be edited from the web interface) */ $sql = "INSERT INTO playlists ( title, type, items, db_timestamp, idx ) VALUES ( '".$name."', 0, 0, 0, 0 )"; $this->query( $sql ); return $this->db->lastInsertId(); } function addTrackToPlaylist( $trackid, $playlistid ){ $sql = "INSERT INTO playlistitems ( songid, playlistid ) VALUES ( ".intval( $trackid ).", ".intval( $playlistid )." ) "; if( $this->debug ) echo $sql."\n"; return $this->query( $sql ); } function getPlaylistIdByName( $name ){ $this->setDb(); $this->query( "SELECT id FROM playlists WHERE title LIKE ".$this->db->quote( $name ) ); $a = $this->fetchRow(); if( !is_array( $a ) || !isset( $a["id"] ) ) return false; else return $a["id"]; } function getPlaylists($type=0){ $aReturn = array(); $sql = "SELECT id, title FROM playlists WHERE type = ".intval($type); $this->query( $sql ); while( $row = $this->fetchRow() ){ $aReturn[$row["id"]] = $row["title"]; } return $aReturn; } function getPlaylistItemsByName( $name ){ $id = $this->getPlaylistIdByName( $name ); if( !$id ) return false; $sql = " SELECT s.id, artist, album, title, song_length, time_added, path FROM playlistitems pi INNER JOIN songs s ON s.id = pi.songid WHERE pi.playlistid = ".intval( $id )." ORDER BY pi.id ASC"; $this->query( $sql ); $aReturn = array(); while( $row = $this->fetchRow() ){ $aReturn[] = $row; } return $aReturn; } function clearPlaylist( $id ){ $sql = "DELETE FROM playlistitems WHERE playlistid = ".intval( $id ); $this->query( $sql ); } function deletePlaylist( $name ){ $playlistid = $this->getPlaylistIdByName( $name ); if( $playlistid === false ) return false; $this->clearPlaylist( $playlistid ); $this->query( "DELETE FROM playlists WHERE id = ".intval( $playlistid ) ); return true; } function savePlaylist( $name, $aTrackIds ){ $playlistid = $this->getPlaylistIdByName( $name ); if( $playlistid === false ) $playlistid = $this->insertPlaylist( $name ); else $this->clearPlaylist( $playlistid ); $sql = "UPDATE playlists SET items = ".intval(sizeof($aTrackIds))." WHERE id = ".intval($playlistid); $this->query($sql); foreach( $aTrackIds as $id ){ $this->addTrackToPlaylist( $id, $playlistid ); } } } ?>