summaryrefslogtreecommitdiffstats
path: root/lib/playlist.lib.php
blob: c695579b32f4ff25bc84dd7c98f719069ab87c11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?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() { 

	show_playlist_menu();
	
	/* 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

/**
 * show_playlist_edit
 * This function shows the edit form for a playlist, nothing special here
 */
function show_playlist_edit($playlist_id) { 

	$playlist = new Playlist($playlist_id);
	/* Chuck em out if they don't have the rights */
	if (!$playlist->has_access()) { access_denied(); return false; }

	require_once (conf('prefix') . '/templates/show_playlist_edit.inc.php');

} // show_playlist_edit

/**
 * 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']->id) . "'" . 
				" AND type='private' ORDER BY name";
		break;
		case 'adminprivate':
			if (!$GLOBALS['user']->has_access(100)) { return false; }
			$sql = "SELECT id FROM playlist WHERE user!='" . sql_escape($GLOBALS['user']->id) . "'" . 
				" AND type='private' ORDER BY name";
		break;
		default:
		case 'public':
			$sql = "SELECT id FROM playlist WHERE type='public' ORDER BY name";
		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

/**
 * prune_empty_playlists
 * This function goes through and deletes any playlists which have
 * no songs in them. This can only be done by a full admin
 */
function prune_empty_playlists() { 


	$sql = "SELECT playlist.id FROM playlist LEFT JOIN playlist_data ON playlist.id=playlist_data.playlist " . 
		"WHERE playlist_data.id IS NULL";
	$db_results = mysql_query($sql, dbh());

	$results = array();

	while ($r = mysql_fetch_assoc($db_results)) { 
		$results[] = $r['id'];
	}

	/* Delete the Playlists */
	foreach ($results as $playlist_id) { 
		$playlist = new Playlist($playlist_id);
		$playlist->delete();
	}
	
	return true;

} // prune_empty_playlists

/**
 * show_playlist_select
 * This shows the playlist select box, it takes a playlist ID and a type as an optional
 * param, the type is 'normal','democratic','dynamic' these are hacks and make baby vollmer cry
 * but I'm too lazy to fix them right now
 */
function show_playlist_select($playlist_id=0,$type='') { 

	/* If democratic show everything, else normal */
	if ($type == 'democratic') { 
		$where_sql = '1=1';
	}
	else { 
		$where_sql = " `user` = '" . Dba::escape($GLOBALS['user']->id) . "'";
	}

	$sql = "SELECT id,name FROM playlist " . 
		"WHERE " . $where_sql . " ORDER BY `name`";
	$db_results = Dba::query($sql);

	echo "<select name=\"playlist_id\">\n";
	
	/* The first value changes depending on our type */
	if ($type == 'democratic') { 
		echo "\t<option value=\"0\">" . _('All') . "</option>\n";
	}
	elseif ($type != 'dynamic') { 
		echo "\t<option value=\"0\"> -" . _('New Playlist') . "- </option>\n"; 
	}  

	while ($r = mysql_fetch_assoc($db_results)) { 
		$select_txt = '';
		if ($playlist_id == $r['id']) { $select_txt = ' selected="selected" '; } 

		echo "\t<option value=\"" . scrub_out($r['id']) . "\"$select_txt>" . scrub_out($r['name']) . "</option>\n";

	} // end while

	echo '</select>';

} // show_playlist_select

?>