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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
<?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.
*/
/**
* Browse Class
* This handles all of the sql/filtering
* on the data before it's thrown out to the templates
* it also handles pulling back the object_ids and then
* calling the correct template for the object we are displaying
*/
class Browse {
// Public static vars that are cached
public static $sql;
public static $start = '0';
public static $total_objects;
/**
* constructor
* This should never be called
*/
private function __construct() {
// Rien a faire
} // __construct
/**
* set_filter
* This saves the filter data we pass it into the session
* This is done here so that it's easy to modify where the data
* is saved should I change my mind in the future. It also makes
* a single point for whitelist tweaks etc
*/
public static function set_filter($key,$value) {
switch ($key) {
case 'show_art':
case 'min_count':
case 'unplayed':
case 'rated':
$key = $_REQUEST['key'];
if ($_SESSION['browse']['filter'][$key]) {
unset($_SESSION['browse']['filter'][$key]);
}
else {
$_SESSION['browse']['filter'][$key] = 1;
}
break;
case 'alpha_match':
if ($value == _('All')) { $value = ''; }
$_SESSION['browse']['filter'][$key] = $value;
break;
default:
// Rien a faire
break;
} // end switch
} // set_filter
/**
* get_filter
* returns the specified filter value
*/
public static function get_filter($key) {
// Simple enough, but if we ever move this crap
return $_SESSION['browse']['filter'][$key];
} // get_filter
/**
* set_type
* This sets the type of object that we want to browse by
* we do this here so we only have to maintain a single whitelist
* and if I want to change the location I only have to do it here
*/
public static function set_type($type) {
switch($type) {
case 'user':
case 'playlist':
case 'song':
case 'catalog':
case 'album':
case 'artist':
case 'genre':
case 'live_stream':
$_SESSION['browse']['type'] = $type;
break;
default:
// Rien a faire
break;
} // end type whitelist
} // set_type
/**
* set_sort
* This sets the current sort(s)
*/
public static function set_sort($sort) {
switch ($_SESSION['browse']['type']) {
case 'song':
$valid_array = array('title','year');
break;
case 'artist':
$valid_array = array('name');
break;
case 'album':
$valid_array = array('name','year');
break;
} // end switch
// If it's not in our list, smeg off!
if (!in_array($sort,$valid_array)) {
return false;
}
if ($_SESSION['browse']['sort'][$sort] == 'DESC') {
$_SESSION['browse']['sort'][$sort] = 'ASC';
}
else {
$_SESSION['browse']['sort'][$sort] = 'DESC';
}
} // set_sort
/**
* set_start
* This sets the start point for our show functions
*/
public static function set_start($start) {
self::$start = intval($start);
} // set_start
/**
* get_saved
* This looks in the session for the saved
* stuff and returns what it finds
*/
public static function get_saved() {
$objects = $_SESSION['browse']['save'];
return $objects;
} // get_saved
/**
* get_objects
* This gets an array of the ids of the objects that we are
* currently browsing by it applies the sql and logic based
* filters
*/
public static function get_objects() {
// First we need to get the SQL statement we are going to run
// This has to run against any possible filters (dependent on type)
$sql = self::get_sql();
$db_results = Dba::query($sql);
$results = array();
while ($data = Dba::fetch_assoc($db_results)) {
// Make sure that this object passes the logic filter
if (self::logic_filter($data['id'])) {
$results[] = $data['id'];
}
} // end while
// Save what we've found and then return it
self::save_objects($results);
return $results;
} // get_objects
/**
* get_sql
* This returns the sql statement we are going to use this has to be run
* every time we get the objects because it depends on the filters and the
* type of object we are currently browsing
*/
public static function get_sql() {
// Get our base SQL must always return ID
switch ($_SESSION['browse']['type']) {
case 'album':
$sql = "SELECT `album`.`id` FROM `album` ";
break;
case 'artist':
$sql = "SELECT `artist`.`id` FROM `artist` ";
break;
case 'genre':
$sql = "SELECT `genre`.`id` FROM `genre` ";
break;
case 'user':
$sql = "SELECT `user`.`id` FROM `user` ";
break;
case 'live_stream':
$sql = "SELECT `live_stream`.`id` FROM `live_stream` ";
break;
case 'playlist':
$sql = "SELECT `playlist`.`id` FROM `playlist` ";
break;
case 'song':
default:
$sql = "SELECT `song`.`id` FROM `song` ";
break;
} // end base sql
// No sense to go further if we don't have filters
if (is_array($_SESSION['browse']['filter'])) {
// Foreach the filters and see if any of them can be applied
// as part of a where statement in this sql (type dependent)
$where_sql = "WHERE 1=1 AND ";
foreach ($_SESSION['browse']['filter'] as $key=>$value) {
$where_sql .= self::sql_filter($key,$value);
} // end foreach
$where_sql = rtrim($where_sql,'AND ');
$sql .= $where_sql;
} // if filters
// Now Add the Order
$order_sql = "ORDER BY ";
// If we don't have a sort, then go ahead and return it now
if (!is_array($_SESSION['browse']['sort'])) { return $sql; }
foreach ($_SESSION['browse']['sort'] as $key=>$value) {
$order_sql .= self::sql_sort($key,$value);
}
// Clean her up
$order_sql = rtrim($order_sql,"ORDER BY ");
$order_sql = rtrim($order_sql,",");
$sql = $sql . $order_sql;
return $sql;
} // get_sql
/**
* sql_filter
* This takes a filter name and value and if it is possible
* to filter by this name on this type returns the approiate sql
* if not returns nothing
*/
private static function sql_filter($filter,$value) {
$filter_sql = '';
if ($_SESSION['browse']['type'] == 'song') {
switch($filter) {
case 'alpha_match':
$filter_sql = " `song`.`title` LIKE '" . Dba::escape($value) . "%' AND ";
break;
case 'unplayed':
$filter_sql = " `song`.`played`='0' AND ";
break;
default:
// Rien a faire
break;
} // end list of sqlable filters
} // if it is a song
elseif ($_SESSION['browse']['type'] == 'album') {
switch($filter) {
case 'alpha_match':
$filter_sql = " `album`.`name` LIKE '" . Dba::escape($value) . "%' AND ";
break;
case 'min_count':
break;
default:
// Rien a faire
break;
}
} // end album
elseif ($_SESSION['browse']['type'] == 'artist') {
switch($filter) {
case 'alpha_match':
$filter_sql = " `artist`.`name` LIKE '" . Dba::escape($value) . "%' AND ";
break;
default:
// Rien a faire
break;
} // end filter
} // end artist
elseif ($_SESSION['browse']['type'] == 'genre') {
switch ($filter) {
case 'alpha_match':
$filter_sql = " `genre`.`name` LIKE '" . Dba::escape($value) . "%' AND ";
break;
default:
// Rien a faire
break;
} // end filter
} // end if genre
elseif ($_SESSION['browse']['type'] == 'live_stream') {
switch ($filter) {
case 'alpha_match':
$filter_sql = " `live_stream`.`name` LIKE '" . Dba::escape($value) . "%' AND ";
break;
default:
// Rien a faire
break;
} // end filter
} // end live_stream
elseif ($_SESSION['browse']['type'] == 'playlist') {
switch ($filter) {
case 'alpha_match':
$filter_sql = " `playlist`.`name` LIKE '" . Dba::escape($value) . "%' AND ";
break;
default;
// Rien a faire
break;
} // end filter
} // end playlist
return $filter_sql;
} // sql_filter
/**
* logic_filter
* This runs the filters that we can't easily apply
* to the sql so they have to be done after the fact
* these should be limited as they are often intensive and
* require additional queries per object... :(
*/
private static function logic_filter($object_id) {
return true;
} // logic_filter
/**
* sql_sort
* This builds any order bys we need to do
* to sort the results as best we can, there is also
* a logic based sort that will come later as that's
* a lot more complicated
*/
private static function sql_sort($field,$order) {
if ($order != 'DESC') { $order == 'ASC'; }
switch ($_SESSION['browse']['type']) {
case 'song':
switch($field) {
case 'title';
$sql = "`song`.`title`";
break;
case 'year':
$sql = "`song`.`year`";
break;
default:
// Rien a faire
break;
} // end switch
break;
case 'album':
switch($field) {
case 'name':
$sql = "`album`.`name`";
break;
case 'year':
$sql = "`album`.`year`";
break;
} // end switch
break;
case 'artist':
switch ($field) {
case 'name':
$sql = "`artist`.`name`";
break;
} // end switch
break;
default:
// Rien a faire
break;
} // end switch
if ($sql) { $sql_sort = "$sql $order,"; }
return $sql_sort;
} // sql_sort
/**
* show_objects
* This takes an array of objects
* and requires the correct template based on the
* type that we are currently browsing
*/
public static function show_objects($object_ids='') {
$object_ids = $object_ids ? $object_ids : self::get_saved();
// Reset the total items
self::$total_objects = count($object_ids);
// Limit is based on the users preferences
$limit = $GLOBALS['user']->prefs['offset_limit'] ? $GLOBALS['user']->prefs['offset_limit'] : '25';
if (count($object_ids) > $start) {
$object_ids = array_slice($object_ids,self::$start,$limit);
}
// Format any matches we have so we can show them to the masses
$match = $_SESSION['browse']['filter']['alpha_match'] ? ' (' . $_SESSION['browse']['filter']['alpha_match'] . ')' : '';
switch ($_SESSION['browse']['type']) {
case 'song':
show_box_top(_('Songs') . $match);
require_once Config::get('prefix') . '/templates/show_songs.inc.php';
show_box_bottom();
break;
case 'album':
show_box_top(_('Albums') . $match);
require_once Config::get('prefix') . '/templates/show_albums.inc.php';
show_box_bottom();
break;
case 'genre':
show_box_top(_('Genres') . $match);
require_once Config::get('prefix') . '/templates/show_genres.inc.php';
show_box_bottom();
break;
case 'user':
show_box_top(_('Manage Users') . $match);
require_once Config::get('prefix') . '/templates/show_users.inc.php';
show_box_bottom();
break;
case 'artist':
show_box_top(_('Artists') . $match);
require_once Config::get('prefix') . '/templates/show_artists.inc.php';
show_box_bottom();
break;
case 'live_stream':
show_box_top(_('Radion Stations') . $match);
require_once Config::get('prefix') . '/templates/show_live_streams.inc.php';
show_box_bottom();
break;
case 'playlist':
show_box_top(_('Playlists') . $match);
require_once Config::get('prefix') . '/templates/show_playlists.inc.php';
show_box_bottom();
break;
case 'catalog':
show_box_top(_('Catalogs'));
require_once Config::get('prefix') . '/templates/show_catalogs.inc.php';
show_box_bottom();
break;
default:
// Rien a faire
break;
} // end switch on type
} // show_object
/**
* save_objects
* This takes the full array of object ides, often passed into show and then
* if nessecary it saves them into the session
*/
public static function save_objects($object_ids) {
// save these objects
$_SESSION['browse']['save'] = $object_ids;
self::$total_objects = count($object_ids);
return true;
} // save_objects
} // browse
|