summaryrefslogtreecommitdiffstats
path: root/albums.php
blob: a2237875284cda72a3b17ea23400b73b0e8feaff (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/*

 Copyright (c) 2001 - 2007 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 v2
 as published by the Free Software Foundation.

 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.

*/

require_once 'lib/init.php';

show_template('header');

// We'll set any input parameters here
if(!isset($_REQUEST['match'])) { $_REQUEST['match'] = "Browse"; }
if(isset($_REQUEST['match'])) $match = scrub_in($_REQUEST['match']);
if(isset($_REQUEST['album'])) $album = scrub_in($_REQUEST['album']);
if(isset($_REQUEST['artist'])) $artist = scrub_in($_REQUEST['artist']);
$_REQUEST['artist_id'] = scrub_in($_REQUEST['artist_id']);
$min_album_size = conf('min_object_count');
if ($min_album_size == '') { 
	$min_album_size = '0';
}

$action = scrub_in($_REQUEST['action']); 

/* Switch on Action */
switch ($action) { 
	case 'clear_art':
		if (!$GLOBALS['user']->has_access('75')) { access_denied(); } 
		$album = new Album($_REQUEST['album_id']);
		$album->clear_art();
		show_confirmation(_('Album Art Cleared'),_('Album Art information has been removed from the database'),"/albums.php?action=show&amp;album=" . $album->id);
	break;
	case 'show':	
		$album = new Album($_REQUEST['album']);
		$album->format();

		require (conf('prefix') . '/templates/show_album.inc');
	
		/* Get the song ids for this album */
		$song_ids = $album->get_song_ids($_REQUEST['artist']);
	
		show_songs($song_ids,0,$album);
	break;
	// Upload album art
	case 'upload_art':

		// we didn't find anything 
		if (empty($_FILES['file']['tmp_name'])) { 
			show_confirmation(_('Album Art Not Located'),_('Album Art could not be located at this time. This may be due to write access error, or the file is not received corectly.'),"/albums.php?action=show&amp;album=" . $album->id);
			break;
		}

		$album = new Album($_REQUEST['album_id']); 
		
		// Pull the image information
		$data = array('file'=>$_FILES['file']['tmp_name']); 
		$image_data = get_image_from_source($data); 

		// If we got something back insert it
		if ($image_data) { 
			$album->insert_art($image_data,$_FILES['file']['type']);
			show_confirmation(_('Album Art Inserted'),'',"/albums.php?action=show&album=" . $album->id);
		} 
		// Else it failed
		else { 
			show_confirmation(_('Album Art Not Located'),_('Album Art could not be located at this time. This may be due to write access error, or the file is not received corectly.'),"/albums.php?action=show&amp;album=" . $album->id);
		} 

	break; 
	case 'find_art':

		// If not a user then kick em out
		if (!$GLOBALS['user']->has_access('25')) { access_denied(); exit; }

		// get the Album information
	        $album = new Album($_REQUEST['album_id']);
		$images = array(); 
		$cover_url = array(); 

		// If we've got an upload ignore the rest and just insert it
		if (!empty($_FILES['file']['tmp_name'])) { 
			$path_info = pathinfo($_FILES['file']['name']); 
			$upload['file'] = $_FILES['file']['tmp_name'];
			$upload['mime'] = 'image/' . $path_info['extension']; 
			$image_data = get_image_from_source($upload); 

			if ($image_data) { 
				$album->insert_art($image_data,$upload['0']['mime']); 
				show_confirmation(_('Album Art Inserted'),'',"/albums.php?action=show&album=" . $_REQUEST['album_id']);
				break;

			} // if image data

		} // if it's an upload
		
		// Build the options for our search
		if (isset($_REQUEST['artist_name'])) { 
			$artist = scrub_in($_REQUEST['artist_name']);
		} 
		elseif ($album->artist_count == '1') { 
			$artist = $album->artist;
		}
		if (isset($_REQUEST['album_name'])) { 
			$album_name = scrub_in($_REQUEST['album_name']);
		}
		else { 
			$album_name = $album->name;
		}
	
		$options['artist'] 	= $artist; 
		$options['album_name']	= $album_name; 
		$options['keyword']	= $artist . " " . $album_name; 
		// HACK that makes baby jesus cry...
		$options['skip_id3']	= true; 
	
		// Attempt to find the art. 
		$images = $album->find_art($options,'6');

		if (!empty($_REQUEST['cover'])) { 
			$path_info = pathinfo($_REQUEST['cover']); 
			$cover_url[0]['url'] 	= scrub_in($_REQUEST['cover']); 
			$cover_url[0]['mime'] 	= 'image/' . $path_info['extension'];
		}
		$images = array_merge($cover_url,$images); 

		// If we've found anything then go for it!		
		if (count($images)) {
			// We don't want to store raw's in here so we need to strip them out into a seperate array
			foreach ($images as $index=>$image) { 
				if (isset($image['raw'])) { 
					//unset($images[$index]); 
					$images[$index]['raw'] = ''; 
				} 
			} // end foreach  

			// Store the results for further use
			$_SESSION['form']['images'] = $images;
			require_once(conf('prefix') . '/templates/show_album_art.inc.php');
		}
		// Else nothing
		else {
			show_confirmation(_('Album Art Not Located'),_('Album Art could not be located at this time. This may be due to write access error, or the file is not received corectly.'),"/albums.php?action=show&amp;album=" . $album->id);
		}
	  
		$albumname = $album->name;
		$artistname = $artist;
		
		// Remember the last typed entry, if there was one
		if (isset($_REQUEST['album_name'])) {   $albumname = scrub_in($_REQUEST['album_name']); }
		if (isset($_REQUEST['artist_name'])) {  $artistname = scrub_in($_REQUEST['artist_name']); }
	
		require_once(conf('prefix') . '/templates/show_get_albumart.inc.php');
	
	break;
	case 'select_art':	

		/* Check to see if we have the image url still */
		$image_id = $_REQUEST['image'];
		$album_id = $_REQUEST['album_id'];
		
		$image 	= get_image_from_source($_SESSION['form']['images'][$image_id]);
		$mime	= $_SESSION['form']['images'][$image_id]['mime'];
	
		$album = new Album($album_id);
		$album->insert_art($image,$mime);


		show_confirmation(_('Album Art Inserted'),'',"/albums.php?action=show&album=$album_id");
	break;
	case 'update_from_tags':
	
		$album = new Album($_REQUEST['album_id']);

		show_box_top(_('Starting Update from Tags')); 

		$catalog = new Catalog();
		$catalog->update_single_item('album',$_REQUEST['album_id']);

		echo "<br /><b>" . _('Update From Tags Complete') . "</b> &nbsp;&nbsp;";
		echo "<a href=\"" . conf('web_path') . "/albums.php?action=show&amp;album=" . scrub_out($_REQUEST['album_id']) . "\">[" . _('Return') . "]</a>";
		show_box_bottom(); 
	break;
	// Browse by Album
	default: 
		if (strlen($_REQUEST['match']) < '1') { $match = 'a'; }

		// Setup the View Ojbect
	        $view = new View();
	        $view->import_session_view();

		if ($match == 'Show_all' || $match == 'Show_missing_art' || $match == 'Browse') { $chr = ''; } 
		else { $chr = $match; } 

		require (conf('prefix') . '/templates/show_box_top.inc.php');
		show_alphabet_list('albums','albums.php',$match);
		show_alphabet_form($chr,_('Show Albums starting with'),"albums.php?action=match");
		require (conf('prefix') . '/templates/show_box_bottom.inc.php');
	
		switch($match) {
			case 'Show_all':
				$offset_limit = 99999;
	                        $sql = "SELECT album.id FROM song,album ".
	                               " WHERE song.album=album.id ".
	                               "GROUP BY song.album ".
	                               "  HAVING COUNT(song.id) > $min_album_size ";
			break;
	                case 'Show_missing_art':
	                        $offset_limit = 99999;
	                        $sql = "SELECT album.id FROM song,album ".
	                               " WHERE song.album=album.id ".
	                               "   AND album.art is null ".
	                               "GROUP BY song.album ".
	                               "  HAVING COUNT(song.id) > $min_album_size ";
                        break; 
			case 'Browse':
			case 'show_albums':
                	        $sql = "SELECT album.id FROM song,album ".
	                               " WHERE song.album=album.id ".
	                               "GROUP BY song.album ".
	                               "  HAVING COUNT(song.id) > $min_album_size ";
			break;
			default:
        	                $sql = "SELECT album.id FROM song,album ".
	                               " WHERE song.album=album.id ".
                	               "   AND album.name LIKE '$match%'".
	                               "GROUP BY song.album ".
	                               "  HAVING COUNT(song.id) > $min_album_size ";
		} // end switch

		switch ($_REQUEST['type']) { 
			case 'album_sort':
				if ($match != 'Browse' && $match != 'Show_missing_art' && $match != 'Show_all') { 
					$match_string = " AND album.name LIKE '$match%'";
				}
				$sort_sql = "SELECT album.id, IF(COUNT(DISTINCT(song.artist)) > 1,'Various', artist.name) AS artist_name " . 
					"FROM song,artist,album WHERE song.album=album.id AND song.artist=artist.id $match_string" . 
					"GROUP BY album.name,album.year ".
	                                "HAVING COUNT(song.id) > $min_album_size ";
				$sort_order = 'artist.name';
			break;
			default:
	
			break;
		} // switch on special sort types

		// if we are returning
		if ($_REQUEST['keep_view']) { 
	                $view->initialize($sort_sql);
		}

		// If we aren't keeping the view then initlize it
		elseif ($sql) {
			if (!$sort_order) { $sort_order = 'name'; } 
			$db_results = mysql_query($sql, dbh());
			$total_items = mysql_num_rows($db_results);
			if ($match != "Show_all") { $offset_limit = $_SESSION['userdata']['offset_limit']; }
			$view = new View($sql, 'albums.php',$sort_order,$total_items,$offset_limit);	
		} 
	
		else { $view = false; }
	
		if ($view->base_sql) { 
			$albums = get_albums($view->sql);
			require conf('prefix') . '/templates/show_albums.inc.php';
		}
	
	break;
} // end switch on action

show_footer();
?>