diff options
author | Karl 'vollmerk' Vollmer <vollmer@ampache.org> | 2006-01-03 09:21:29 +0000 |
---|---|---|
committer | Karl 'vollmerk' Vollmer <vollmer@ampache.org> | 2006-01-03 09:21:29 +0000 |
commit | dfbe65e1bf0a4d7cff1e2d31a2ade18de6756196 (patch) | |
tree | 1036034cfcb8653f37b2e690f8b83068f98fe509 /lib | |
parent | a2cf31f96cd9ba28b1f5e7090208312a9e5b7ca9 (diff) | |
download | ampache-dfbe65e1bf0a4d7cff1e2d31a2ade18de6756196.tar.gz ampache-dfbe65e1bf0a4d7cff1e2d31a2ade18de6756196.tar.bz2 ampache-dfbe65e1bf0a4d7cff1e2d31a2ade18de6756196.zip |
whole new playlists files new coolness supported but not implmented, playback works but playlist edit functions are still untested...
Diffstat (limited to 'lib')
-rw-r--r-- | lib/class/playlist.class.php | 568 | ||||
-rw-r--r-- | lib/playlist.lib.php | 91 | ||||
-rw-r--r-- | lib/song.php | 16 | ||||
-rw-r--r-- | lib/ui.lib.php | 23 |
4 files changed, 389 insertions, 309 deletions
diff --git a/lib/class/playlist.class.php b/lib/class/playlist.class.php index 279134ee..f9cf0732 100644 --- a/lib/class/playlist.class.php +++ b/lib/class/playlist.class.php @@ -18,393 +18,375 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - -*/ -/*! - @header Playlist Class - This class handles all actual work in regards to playlists. */ +/** + * Playlist Class + * This class handles playlists in ampache. it references the playlist* tables + */ +class Playlist { -class Playlist { - - // Variables from DB + /* Variables from the Datbase */ var $id; var $name; var $user; var $type; - var $time; - var $items; + var $date; - /*! - @function Playlist - @discussion Playlist class - @param $playlist_id The ID of the playlist - */ - function Playlist($playlist_id = 0) { + /* Generated Elements */ + var $items = array(); - /* If we have an id then do something */ - if ($playlist_id) { - // Assign id - $this->id = $playlist_id; - // Get the information from the db - $this->refresh_object(); - } + /** + * Constructor + * This takes a playlist_id as an optional argument and gathers the information + * if not playlist_id is passed returns false (or if it isn't found + */ + function Playlist($playlist_id = 0) { + + if (!$playlist_id) { return false; } - } + $this->id = $playlist_id; + $info = $this->_get_info(); + $this->name = $info['name']; + $this->user = $info['user']; + $this->type = $info['type']; + $this->date = $info['date']; + + } // Playlist + /** + * _get_info + * This is an internal (private) function that gathers the information for this object from the + * playlist_id that was passed in. + */ + function _get_info() { + $sql = "SELECT * FROM playlist WHERE id='" . sql_escape($this->id) . "'"; + $db_results = mysql_query($sql, dbh()); - /*! - @function refresh_object - @discussion Reads playlist information from the db and updates the Playlist object with it - */ - function refresh_object() { + $results = mysql_fetch_assoc($db_results); - $dbh = dbh(); + return $results; - if ($this->id) { - $sql = "SELECT name, user, type, date FROM playlist" . - " WHERE id = '$this->id'"; - $db_results = mysql_query($sql, $dbh); + } // _get_info - if ($r = mysql_fetch_object($db_results)) { - $this->name = $r->name; - $this->user = $r->user; - $this->type = $r->type; - $this->time = $r->date; - $this->items = array(); + /** + * get_items + * This returns an array of playlist songs that are in this playlist. Because the same + * song can be on the same playlist twice they are key'd by the uid from playlist_data + */ + function get_items() { - // Fetch playlist items - $sql = "SELECT song, track FROM playlist_data" . - " WHERE playlist = '$this->id'" . - " ORDER BY track"; - $db_results = mysql_query($sql, $dbh); + $sql = "SELECT * FROM playlist_data WHERE playlist='" . sql_escape($this->id) . "'"; + $db_results = mysql_query($sql, dbh()); - while ($r = mysql_fetch_object($db_results)) { - $this->items[] = array("song_id" => $r->song, "track" => $r->track); - } - } + while ($r = mysql_fetch_assoc($db_results)) { - return TRUE; - } + $key = $r['id']; + $results[$key] = $r; - return FALSE; + } // end while - } + return $results; + } // get_items - /*! - @function create_playlist - @discussion Creates an empty playlist, given a name, user_id, and type. - */ - function create_playlist($name, $user, $type) { + /** + * get_songs + * This returns an array of song_ids accounting for any dyn_song entries this playlist + * may have. This is what should be called when trying to generate a m3u or other playlist + */ + function get_songs() { - $dbh = dbh(); + $sql = "SELECT * FROM playlist_data WHERE playlist='" . sql_escape($this->id) . "'"; + $db_results = mysql_query($sql, dbh()); - if (isset($name) && isset($user) && isset($type) && $this->check_type($type)) { - $name = sql_escape($name); - $user = sql_escape($user); - $type = sql_escape($type); - - $sql = "INSERT INTO playlist (name, user, type)" . - " VALUES ('$name', '$user', '$type')"; - $db_results = mysql_query($sql, $dbh); - - if ($this->id = mysql_insert_id($dbh)) { - $this->refresh_object(); - return true; - } // end if it created correctly - - } // end if this is a valid playlist entry + $results = array(); - if (conf('debug')) { - log_event($GLOBALS['user']->username,'playlist_create',"Failed to Create Playlist of $type Type named $name for $user"); - } + while ($r = mysql_fetch_assoc($db_results)) { - return false; + if ($r['dyn_song']) { + $array = $this->get_dyn_songs($r['dyn_song']); + $results = array_merge($array,$results); + } + else { + $results[] = $r['song']; + } - } // create_playlist + } // end while + return $results; - /*! - @function delete - @discussion Deletes the playlist. - */ - function delete() { + } // get_songs - $dbh = dbh(); + /** + * get_random_songs + * This returns all of the songs in a random order, except those + * pulled from dyn_songs + */ + function get_random_songs() { - if ($this->id) { - $sql = "DELETE FROM playlist_data" . - " WHERE playlist = '$this->id'"; - $db_results = mysql_query($sql, $dbh); + $sql = "SELECT * FROM playlist_data WHERE playlist='" . sql_escape($this->id) . "'" . + " ORDER BY RAND()"; + $db_results = mysql_query($sql, dbh()); - $sql = "DELETE FROM playlist" . - " WHERE id = '$this->id'"; - $db_results = mysql_query($sql, $dbh); + $results = array(); - $sql = "DELETE FROM playlist_permission" . - " WHERE playlist = '$this->id'"; - $db_results = mysql_query($sql, $dbh); + while ($r = mysql_fetch_assoc($db_results)) { + if ($r['dyn_song']) { + $array = $this->get_dyn_songs($r['dyn_song']); + $results = array_merge($array,$results); + } + else { + $results[] = $r['song']; + } + } // end while - return true; - } // if we've got a valid playlist + return $results; - return false; + } // get_random_songs - } // delete + /** + * get_dyn_songs + * This returns an array of song_ids for a single dynamic playlist entry + */ + function get_dyn_songs($dyn_string) { + /* Ok honestly I know this is risky, so we have to be + * 100% sure that the user never got to touch this. This + * Query has to return id which must be a song.id + */ + $db_results = mysql_query($dyn_string, dbh()); - /*! - @function update_track_numbers - @discussion Reads an array of song_ids and track numbers to update - */ - function update_track_numbers($changes) { - - $dbh = dbh(); - - if ($this->id && isset($changes) && is_array($changes)) { - foreach ($changes as $change) { - // Check for valid song_id - $sql = "SELECT count(*) FROM song WHERE id = '" . $change['song_id'] . "'"; - $db_results = mysql_query($sql, $dbh); - $r = mysql_fetch_row($db_results); - if ($r[0] == 1) { - $sql = "UPDATE playlist_data SET" . - " track = '" . $change['track'] . "'" . - " WHERE playlist = '$this->id'". - " AND song = '" . $change['song_id'] . "'"; - $db_results = mysql_query($sql, $dbh); - } - } + $results = array(); - // Refresh the playlist object - $this->refresh_object(); + while ($r = mysql_fetch_assoc($db_results)) { + $results[] = $r['id']; + } // end while - return TRUE; - } + return $results; - return FALSE; - - } - - - /*! - @function add_songs - @discussion Reads an array of song_ids to add to the playlist - @param $song_ids the array of song_ids - @param $is_ordered boolean, if true insert in order submitted, not by track number - */ - function add_songs($song_ids, $is_ordered = false) { - - $dbh = dbh(); - - if ($this->id && isset($song_ids) && is_array($song_ids)) { - $count = 0; - foreach ($song_ids as $song_id) { - if( $is_ordered ) { - $track_num = $count++; - } else { - $track_num = $song->track; - } - $song = new Song($song_id); - if (isset($song->id)) { - $sql = "INSERT INTO playlist_data" . - " (playlist, song, track)" . - " VALUES ('$this->id', '$song->id', '$track_num')"; - $db_results = mysql_query($sql, $dbh); - } - } + } // get_dyn_songs - // Refresh the playlist object - $this->refresh_object(); + /** + * get_song_count + * This simply returns a int of how many song elements exist in this playlist + * For now let's consider a dyn_song a single entry + */ + function get_song_count() { - return TRUE; - } + $sql = "SELECT COUNT(id) FROM playlist_data WHERE playlist='" . sql_escape($this->id) . "'"; + $db_results = mysql_query($sql, dbh()); - return FALSE; + $results = mysql_fetch_row($db_results); - } // add_songs + return $results['0']; + } // get_song_count - /*! - @function remove_songs - @discussion Reads an array of song_ids to remove from the playlist - */ - function remove_songs($song_ids) { + /** + * update_type + * This updates the playlist type, it calls the generic update_item function + */ + function update_type($new_type) { - $dbh = dbh(); + if ($this->_update_item('type',$new_type,'100')) { + $this->type = $new_type; + } - if ($this->id && isset($song_ids) && is_array($song_ids)) { - foreach ($song_ids as $song_id) { - $sql = "DELETE FROM playlist_data" . - " WHERE song = '$song_id'" . - " AND playlist = '$this->id'"; - $db_results = mysql_query($sql, $dbh); - } + } // update_type - // Refresh the playlist object - $this->refresh_object(); + /** + * update_name + * This updates the playlist name, it calls the generic update_item function + */ + function update_name($new_name) { - return TRUE; + if ($this->_update_item('name',$new_name,'100')) { + $this->name = $new_name; } - return FALSE; + } // update_name - } + /** + * update_item + * This is the generic update function, it does the escaping and error checking + */ + function update_item($field,$value,$level) { + if ($GLOBALS['user']->username != $this->user AND !$GLOBALS['user']->has_access($level)) { + return false; + } - /*! - @function check_type - @discussion Checks for a valid playlist type - */ - function check_type($type) { + $value = sql_escape($value); - if (isset($type)) { - if ($type === 'public' || $type === 'private') { - return TRUE; - } - } + $sql = "UPDATE playlist SET $field='$value' WHERE id='" . sql_escape($this->id) . "'"; + $db_results = mysql_query($sql, dbh()); - return FALSE; + return $db_results; - } + } // update_item + /** + * update_track_numbers + + * This function takes an array of $array['song_id'] $array['track'] where song_id is really the + * playlist_data.id and updates them + */ + function update_track_numbers($data) { - /*! - @function update_type - @discussion Updates the playlist type - */ - function update_type($type) { + foreach ($data as $change) { + + $track = sql_escape($change['track']); + $id = sql_escape($change['song_id']); - $dbh = dbh(); + $sql = "UPDATE playlist_data SET track='$track' WHERE id='$id'"; + $db_results = mysql_query($sql, dbh()); - if ($this->id && isset($type) && $this->check_type($type)) { - $sql = "UPDATE playlist SET type = '$type'" . - " WHERE id = '$this->id'"; - $db_results = mysql_query($sql, $dbh); + } // end foreach - // Refresh the playlist object - $this->refresh_object(); + } // update_track_numbers - return TRUE; - } + /** + * add_songs + * This takes an array of song_ids and then adds it to the playlist + * if you want to add a dyn_song you need to use the one shot function + * add_dyn_song + */ + function add_songs($song_ids=array()) { - return FALSE; + foreach ($song_ids as $song_id) { + /* We need the songs track */ + $song = new Song($song_id); - } + $track = sql_escape($song->track); + $id = sql_escape($song->id); + $pl_id = sql_escape($this->id); + /* Don't insert dead songs */ + if ($id) { + $sql = "INSERT INTO playlist_data (`playlist`,`song`,`track`) " . + " VALUES ('$pl_id','$id','$track')"; + $db_results = mysql_query($sql, dbh()); + } // if valid id - /*! - @function update_name - @discussion Updates the playlist name - */ - function update_name($name) { + } // end foreach songs - $dbh = dbh(); + } // add_songs - if ($this->id && isset($name)) { - $name = sql_escape($name); - $sql = "UPDATE playlist SET name = '$name'" . - " WHERE id = '$this->id'"; - $db_results = mysql_query($sql, $dbh); + /** + * create + * This function creates an empty playlist, gives it a name and type + * Assumes $GLOBALS['user']->username as the user + */ + function create($name,$type) { - // Refresh the playlist object - $this->refresh_object(); + $name = sql_escape($name); + $type = sql_escape($type); + $user = sql_escape($GLOBALS['user']->username); + $date = time(); - return TRUE; - } + $sql = "INSERT INTO playlist (`name`,`user`,`type`,`date`) " . + " VALUES ('$name','$user','$type','$date')"; + $db_results = mysql_query($sql, dbh()); - return FALSE; + $insert_id = mysql_insert_id(dbh()); - } + return $insert_id; - /*! - @function normalize_tracks - @discussion this takes the crazy out of order tracks - and numbers them in a liner fashion, not allowing for - the same track # twice, this is an optional funcition - */ - function normalize_tracks() { + } //create_paylist - /* First get all of the songs in order of their tracks */ - $sql = "SELECT id FROM playlist_data WHERE playlist='$this->id' ORDER BY track ASC"; - $db_results = mysql_query($sql, dbh()); + /** + * set_items + * This calles the get_items function and sets it to $this->items which is an array in this object + */ + function set_items() { - $i = 1; + $this->items = $this->get_items(); - while ($r = mysql_fetch_assoc($db_results)) { - $new_data = array(); - $new_data['id'] = $r['id']; - $new_data['track'] = $i; - $results[] = $new_data; - $i++; - } // end while results - - foreach($results as $data) { - $sql = "UPDATE playlist_data SET track='" . $data['track'] . "' WHERE" . - " id='" . $data['id'] . "'"; - $db_results = mysql_query($sql, dbh()); - } // foreach re-ordered results + } // set_items - return true; + /** + * normalize_tracks + * this takes the crazy out of order tracks + * and numbers them in a liner fashion, not allowing for + * the same track # twice, this is an optional funcition + */ + function normalize_tracks() { - } // normalize_tracks - + /* First get all of the songs in order of their tracks */ + $sql = "SELECT id FROM playlist_data WHERE playlist='" . sql_escape($this->id) . "' ORDER BY track ASC"; + $db_results = mysql_query($sql, dbh()); - /*! - @function get_songs - @discussion Returns an array of song_ids for the playlist - */ - function get_songs() { + $i = 1; - $song_ids = array(); + while ($r = mysql_fetch_assoc($db_results)) { + $new_data = array(); + $new_data['id'] = $r['id']; + $new_data['track'] = $i; + $results[] = $new_data; + $i++; + } // end while results - if ($this->id && is_array($this->items)) { - foreach ($this->items as $item) { - $song_ids[] = $item['song_id']; - } - } + foreach($results as $data) { + $sql = "UPDATE playlist_data SET track='" . $data['track'] . "' WHERE" . + " id='" . $data['id'] . "'"; + $db_results = mysql_query($sql, dbh()); + } // foreach re-ordered results - return $song_ids; + return true; - } // get_songs + } // normalize_tracks + + /** + * check_type + * This validates a type to make sure it's legit + */ + function check_type($type) { - /*! - @function get_random_songs - @discussion gets a random set of the songs in this - playlist - */ - function get_random_songs() { + if ($type == 'public' || $type == 'private') { return true; } + + return false; - $sql = "SELECT COUNT(song) FROM playlist_data WHERE playlist = '$this->id'"; - $db_results = mysql_query($sql, dbh()); + } // check_type - $total_songs = mysql_fetch_row($db_results); + /** + * remove_songs + * This is the polar opposite of the add_songs function... with one little + * change. it works off of the playlist_data.id rather then song_id + */ + function remove_songs($data) { + + foreach ($data as $value) { - // Fetch playlist items - $sql = "SELECT song, track FROM playlist_data" . - " WHERE playlist = '$this->id'" . - " ORDER BY RAND()"; - $db_results = mysql_query($sql, dbh()); - while ($r = mysql_fetch_object($db_results)) { - $song_ids[] = $r->song; - } + $id = sql_escape($value); + + $sql = "DELETE FROM playlist_data WHERE id='$id'"; + $db_results = mysql_query($sql, dbh()); - return $song_ids; - } // get_random_songs + } // end foreach dead songs + + } // remove_songs + + /** + * delete + * This deletes the current playlist and all assoicated data + */ + function delete() { - /*! - @function show_import - @discussion shows the import from file template - */ - function show_import() { + $id = sql_escape($this->id); - require (conf('prefix') . "/templates/show_import_playlist.inc.php"); + $sql = "DELETE FROM playlist_data WHERE playlist = '$id'"; + $db_results = mysql_query($sql, dbh()); - } // show_import + $sql = "DELETE FROM playlist WHERE id='$id'"; + $db_results = mysql_query($sql, dbh()); -} //end of playlist class + $sql = "DELETE FROM playlist_permission WHERE playlist='$id'"; + $db_results = mysql_query($sql, dbh()); + + return true; + + } // delete -?> +} // class Playlist diff --git a/lib/playlist.lib.php b/lib/playlist.lib.php new file mode 100644 index 00000000..0d0e3fe6 --- /dev/null +++ b/lib/playlist.lib.php @@ -0,0 +1,91 @@ +<?php +/* + + Copyright (c) 2001 - 2006 Ampache.org + All Rights Reserved + + 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 2 + 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, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ +/** + * Playlist Library + * This file should contain the functions that don't fit inside the object, but + * still are related to handling playlists + */ + +/** + * show_playlists + * This shows all of the current playlists. Depending on your rights you may just + * get to see Public + Yours Private or if you're an admin then you get to see + * Public + Yours + Private + */ +function show_playlists() { + + /* Always show yours first */ + $playlists = get_playlists('private'); + $type = 'Private'; + require (conf('prefix') . '/templates/show_playlists.inc.php'); + + /* Now for some Admin? */ + if ($GLOBALS['user']->has_access(100)) { + $playlists = get_playlists('adminprivate'); + $type = 'Admin'; + require (conf('prefix') . '/templates/show_playlists.inc.php'); + } + + /* Always Show Public */ + $playlists = get_playlists('public'); + $type = 'Public'; + require (conf('prefix') . '/templates/show_playlists.inc.php'); + +} // show_playlists + +/** + * get_playlists + * This function takes private,adminprivate or public and returns an array of playlist objects + * that match, it checks permission + */ +function get_playlists($type) { + + switch ($type) { + case 'private': + $sql = "SELECT id FROM playlist WHERE user='" . sql_escape($GLOBALS['user']->username) . "'" . + " AND type='private'"; + break; + case 'adminprivate': + if (!$GLOBALS['user']->has_access(100)) { return false; } + $sql = "SELECT id FROM playlist WHERE user!='" . sql_escape($GLOBALS['user']->username) . "'" . + " AND type='private'"; + break; + default: + case 'public': + $sql = "SELECT id FROM playlist WHERE type='public'"; + break; + } // end switch + + $db_results = mysql_query($sql, dbh()); + + $results = array(); + + while ($r = mysql_fetch_assoc($db_results)) { + $playlist = new Playlist($r['id']); + $results[] = $playlist; + } + + return $results; + +} // get_playlists + +?> diff --git a/lib/song.php b/lib/song.php index ba453d5c..b24c94db 100644 --- a/lib/song.php +++ b/lib/song.php @@ -19,12 +19,13 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* - @header Song Library - @discussion This library handles song related functions.... woohoo! - This library is defunt, please try use the song class if possible -*/ +/** + * Song Library + * This is for functions that don't make sense in the class because we aren't looking + * at a specific song... these should be general function that return arrays of songs + * and the like + */ /*! @function get_songs @@ -35,9 +36,6 @@ function get_songs($sql, $action=0) { $db_results = mysql_query($sql, dbh()); while ($r = mysql_fetch_array($db_results)) { -// $song_info = get_songinfo($r['id']); -// if ($action === 'format') { $song = format_song($song_info); } -// else { $song = $song_info; } $results[] = $r['id']; } @@ -89,6 +87,4 @@ function get_popular_songs( $threshold, $type, $user_id = '' ) { } // get_popular_songs() - - ?> diff --git a/lib/ui.lib.php b/lib/ui.lib.php index 6862ce57..2cf05a46 100644 --- a/lib/ui.lib.php +++ b/lib/ui.lib.php @@ -442,20 +442,20 @@ function show_edit_profile($username) { } // show_edit_profile /** - * show_playlist - * this shows the current playlist + * show_playlist + * This function takes a playlist object and calls show_songs after + * runing get_items() */ -function show_playlist($playlist_id) { +function show_playlist($playlist) { /* Create the Playlist */ - $playlist = new Playlist($playlist_id); - $song_ids = $playlist->get_songs(); + $song_ids = $playlist->get_items(); if (count($song_ids) > 0) { show_songs($song_ids, $playlist->id); } else { - echo "<p>" . _("No songs in this playlist.") . "</p>\n"; + echo "<div class=\"text-box\">" . _("No songs in this playlist.") . "</div>\n"; } } // show_playlist @@ -1213,4 +1213,15 @@ function show_registration_agreement() { } // show_registration_agreement + +/** + * show_playlist_import + * This shows the playlist import templates + */ +function show_playlist_import() { + + require (conf('prefix') . '/templates/show_import_playlist.inc.php'); + +} // show_playlist_import + ?> |