diff options
author | Karl 'vollmerk' Vollmer <vollmer@ampache.org> | 2008-08-13 03:36:29 +0000 |
---|---|---|
committer | Karl 'vollmerk' Vollmer <vollmer@ampache.org> | 2008-08-13 03:36:29 +0000 |
commit | 64fb3786bbe81bc72099f154c59c9c7fefcbd2c9 (patch) | |
tree | bdbb61c7fcb3917067c5ca13d2267472ec8781df /lib | |
parent | f49a78f512b7a8e76d5548f188536f2044c4ec60 (diff) | |
download | ampache-64fb3786bbe81bc72099f154c59c9c7fefcbd2c9.tar.gz ampache-64fb3786bbe81bc72099f154c59c9c7fefcbd2c9.tar.bz2 ampache-64fb3786bbe81bc72099f154c59c9c7fefcbd2c9.zip |
good number of changes to browse, this breaks more then it fixes...
Diffstat (limited to 'lib')
-rw-r--r-- | lib/class/artist.class.php | 86 | ||||
-rw-r--r-- | lib/class/browse.class.php | 89 | ||||
-rw-r--r-- | lib/class/user.class.php | 48 | ||||
-rw-r--r-- | lib/general.lib.php | 2 |
4 files changed, 133 insertions, 92 deletions
diff --git a/lib/class/artist.class.php b/lib/class/artist.class.php index 64160d35..7a52afaf 100644 --- a/lib/class/artist.class.php +++ b/lib/class/artist.class.php @@ -77,7 +77,7 @@ class Artist extends database_object { /** * this attempts to build a cache of the data from the passed albums all in one query */ - public static function build_cache($ids) { + public static function build_cache($ids,$extra=false) { $idlist = '(' . implode(',', $ids) . ')'; $sql = "SELECT * FROM `artist` WHERE `id` IN $idlist"; @@ -87,6 +87,18 @@ class Artist extends database_object { parent::add_to_cache('artist',$row['id'],$row); } + // If we need to also pull the extra information, this is normally only used when we are doing the human display + if ($extra) { + $sql = "SELECT `song`.`artist`, COUNT(`song`.`id`) AS `song_count`, COUNT(`song`.`album`) AS `album_count`, SUM(`song`.`time`) AS `time` FROM `song` " . + "WHERE `song`.`artist` IN $idlist GROUP BY `song`.`artist`"; + $db_results = Dba::query($sql); + + while ($row = Dba::fetch_assoc($db_results)) { + parent::add_to_cache('artist_extra',$row['artist'],$row); + } + + } // end if extra + } // build_cache /** @@ -145,24 +157,6 @@ class Artist extends database_object { } // get_songs - /** - * get_song_ids - * This gets an array of song ids that are assoicated with this artist. This is great for using - * with the show_songs function - */ - function get_song_ids() { - - $sql = "SELECT id FROM song WHERE artist='" . sql_escape($this->id) . "' ORDER BY album, track"; - $db_results = mysql_query($sql, dbh()); - - while ($r = mysql_fetch_assoc($db_results)) { - $results[] = $r['id']; - } - - return $results; - - } // get_song_ids - /** * get_random_songs * Gets the songs from this artist in a random order @@ -182,32 +176,33 @@ class Artist extends database_object { } // get_random_songs - /*! - @function get_count - @discussion gets the album and song count of - this artist - */ - function get_count() { - - /* Define vars */ - $songs = 0; - $albums = 0; - - $sql = "SELECT COUNT(song.id) FROM song WHERE song.artist='$this->id' GROUP BY song.album"; - $db_results = Dba::query($sql); + /** + * _get_extra info + * This returns the extra information for the artist, this means totals etc + */ + private function _get_extra_info() { - while ($r = Dba::fetch_row($db_results)) { - $songs += $r[0]; - $albums++; - } + // Try to find it in the cache and save ourselves the trouble + if (parent::is_cached('artist_extra',$this->id)) { + $row = parent::get_from_cache('artist_extra',$this->id); + } + else { + $uid = Dba::escape($this->id); + $sql = "SELECT `song`.`artist`,COUNT(`song`.`id`) AS `song_count`, COUNT(`song`.`album`) AS `album_count`, SUM(`song`.`time`) AS `time` FROM `song` " . + "WHERE `song`.`artist`='$uid' GROUP BY `song`.`artist`"; + $db_results = Dba::query($sql); + $row = Dba::fetch_assoc($db_results); + parent::add_to_cache('artist_extra',$row['artist'],$row); + } /* Set Object Vars */ - $this->songs = $songs; - $this->albums = $albums; + $this->songs = $row['song_count']; + $this->albums = $row['album_count']; + $this->time = $row['time']; - return true; + return $row; - } // get_count + } // _get_extra_info /** * format @@ -230,7 +225,16 @@ class Artist extends database_object { $this->f_link = Config::get('web_path') . '/artists.php?action=show&artist=' . $this->id; // Get the counts - $this->get_count(); + $extra_info = $this->_get_extra_info(); + + //Format the new time thingy that we just got + $min = sprintf("%02d",(floor($extra_info['time']/60)%60)); + + $sec = sprintf("%02d",($extra_info['time']%60)); + $hours = floor($extra_info['time']/3600); + + $this->f_time = ltrim($hours . ':' . $min . ':' . $sec,'0:'); + return true; diff --git a/lib/class/browse.class.php b/lib/class/browse.class.php index b952d749..a1edca80 100644 --- a/lib/class/browse.class.php +++ b/lib/class/browse.class.php @@ -31,6 +31,7 @@ class Browse { // Public static vars that are cached public static $sql; public static $start; + public static $offset; public static $total_objects; public static $type; @@ -145,6 +146,16 @@ class Browse { } // get_filter /** + * get_total + * This returns the toal number of obejcts for this current sort type. If it's already cached used it! + * if they pass us an array then use that! + */ + public static function get_total($objects=false) { + + + } // get_total + + /** * get_allowed_filters * This returns an array of the allowed filters based on the type of object we are working * with, this is used to display the 'filter' sidebar stuff, must be called post browse stuff @@ -311,7 +322,7 @@ class Browse { $value = make_bool($value); self::$simple_browse = $value; - $_SESSION['browse']['simple'] = $value; + $_SESSION['browse'][self::$type]['simple'] = $value; } // set_simple_browse @@ -336,6 +347,16 @@ class Browse { } // set_static_content /** + * is_simple_browse + * this returns true or false if the current browse type is set to static + */ + public static function is_simple_browse() { + + return $_SESSION['browse'][self::$type]['simple']; + + } // is_simple_browse + + /** * load_start * This returns a stored start point for the browse mojo */ @@ -514,6 +535,10 @@ class Browse { } // end foreach $sql .= $where_sql; } // if filters + + // No matter what we have to check the catalog based filters... maybe I'm not sure about this + $where_sql .= self::sql_filter('catalog',''); + $sql = rtrim($sql,'AND '); // Now Add the Order $order_sql = " ORDER BY "; @@ -528,6 +553,12 @@ class Browse { $order_sql = rtrim($order_sql,"ORDER BY "); $order_sql = rtrim($order_sql,","); + if (self::is_simple_browse()) { + // When doing a simple browse we need to get the total otherwise paging won't work + // so let's quickly do that before we add the limit + $order_sql .= ' LIMIT ' . intval(self::$start) . ',' . intval(self::$offset); + } + $sql = $sql . $order_sql; return $sql; @@ -594,7 +625,8 @@ class Browse { $filter_sql = " `tags`.`id` in $vals AND (($object_type.id = `tag_map`.`object_id` AND tag_map.object_type='$object_type') $or_sql) AND "; } - if ($_SESSION['browse']['type'] == 'song') { + + if (self::$type == 'song') { switch($filter) { case 'alpha_match': $filter_sql = " `song`.`title` LIKE '%" . Dba::escape($value) . "%' AND "; @@ -606,21 +638,23 @@ class Browse { $filter_sql = " `song`.`played`='0' AND "; break; case 'album': - if ($value) - $filter_sql = " `album`.`id` = '". - Dba::escape($value) . "' AND "; + $filter_sql = " `album`.`id` = '". Dba::escape($value) . "' AND "; break; case 'artist': - if ($value) - $filter_sql = " `artist`.`id` = '". - Dba::escape($value) . "' AND "; + $filter_sql = " `artist`.`id` = '". Dba::escape($value) . "' AND "; break; + case 'catalog': + $catalogs = $GLOBALS['user']->get_catalogs(); + if (!count($catalogs)) { break; } + $filter_sql .= " `song`.`catalog` IN (" . implode(',',$GLOBALS['user']->get_catalogs()) . ") AND "; + break; default: // Rien a faire break; } // end list of sqlable filters + } // if it is a song - elseif ($_SESSION['browse']['type'] == 'album') { + elseif (self::$type == 'album') { switch($filter) { case 'alpha_match': $filter_sql = " `album`.`name` LIKE '%" . Dba::escape($value) . "%' AND "; @@ -632,16 +666,14 @@ class Browse { break; case 'artist': - if ($value) - $filter_sql = " `artist`.`id` = '". - Dba::escape($value) . "' AND "; + $filter_sql = " `artist`.`id` = '". Dba::escape($value) . "' AND "; break; default: // Rien a faire break; } } // end album - elseif ($_SESSION['browse']['type'] == 'artist') { + elseif (self::$type == 'artist') { switch($filter) { case 'alpha_match': $filter_sql = " `artist`.`name` LIKE '%" . Dba::escape($value) . "%' AND "; @@ -654,7 +686,7 @@ class Browse { break; } // end filter } // end artist - elseif ($_SESSION['browse']['type'] == 'live_stream') { + elseif (self::$type == 'live_stream') { switch ($filter) { case 'alpha_match': $filter_sql = " `live_stream`.`name` LIKE '%" . Dba::escape($value) . "%' AND "; @@ -667,7 +699,7 @@ class Browse { break; } // end filter } // end live_stream - elseif ($_SESSION['browse']['type'] == 'playlist') { + elseif (self::$type == 'playlist') { switch ($filter) { case 'alpha_match': $filter_sql = " `playlist`.`name` LIKE '%" . Dba::escape($value) . "%' AND "; @@ -713,7 +745,8 @@ class Browse { if ($order != 'DESC') { $order == 'ASC'; } - switch ($_SESSION['browse']['type']) { + // Depending on the type of browsing we are doing we can apply different filters that apply to different fields + switch (self::$type) { case 'song': switch($field) { case 'title'; @@ -818,15 +851,18 @@ class Browse { */ public static function show_objects($object_ids=false, $ajax=false) { - $object_ids = $object_ids ? $object_ids : self::get_saved(); + if (self::is_simple_browse()) { + $object_ids = self::get_objects(); + } + else { + $object_ids = $object_ids ? $object_ids : self::get_saved(); + } // Reset the total items - self::$total_objects = count($object_ids); + self::$total_objects = self::get_total(count($object_ids)); - // Limit is based on the users preferences - $limit = Config::get('offset_limit') ? Config::get('offset_limit') : '25'; - $all_ids = $object_ids; - if (count($object_ids) > self::$start) { + // Limit is based on the users preferences if this is not a simple browse because we've got too much here + if (count($object_ids) > self::$start AND !self::is_simple_browse()) { $object_ids = array_slice($object_ids,self::$start,$limit); } @@ -839,7 +875,7 @@ class Browse { } // Set the correct classes based on type - $class = "box browse_".$_SESSION['browse']['type']; + $class = "box browse_".self::$type; // Load any additional object we need for this $extra_objects = self::get_supplemental_objects(); @@ -869,7 +905,7 @@ class Browse { break; case 'artist': show_box_top(_('Artists') . $match, $class); - Artist::build_cache($object_ids); + Artist::build_cache($object_ids,'extra'); require_once Config::get('prefix') . '/templates/show_artists.inc.php'; show_box_bottom(); break; @@ -1005,9 +1041,8 @@ class Browse { */ public static function _auto_init() { - self::$simple_browse = make_bool($_SESSION['browse']['simple']); - self::$static_content = make_bool($_SESSION['browse']['static']); - self::$start = intval($_SESSION['browse'][self::$type]['start']); + self::$offset = Config::get('offset_limit') ? Config::get('offset_limit') : '25'; + } // _auto_init diff --git a/lib/class/user.class.php b/lib/class/user.class.php index 00e7e34d..4d4d3956 100644 --- a/lib/class/user.class.php +++ b/lib/class/user.class.php @@ -119,7 +119,7 @@ class User extends database_object { $username = Dba::escape($username); $sql = "SELECT `id` FROM `user` WHERE `username`='$username'"; - $db_results = Dba::query($sql); + $db_results = Dba::read($sql); $results = Dba::fetch_assoc($db_results); $user = new User($results['id']); @@ -129,6 +129,29 @@ class User extends database_object { } // get_from_username /** + * get_catalogs + * This returns the catalogs as an array of ids that this user is allowed to access + */ + public function get_catalogs() { + + if (parent::is_cached('user_catalog',$this->id)) { + return parent::get_from_cache('user_catalog',$this->id); + } + + $sql = "SELECT * FROM `user_catalog` WHERE `user`='$user_id'"; + $db_results = Dba::read($sql); + + while ($row = Dba::fetch_assoc($db_results)) { + $catalogs[] = $row['catalog']; + } + + parent::add_to_cache('user_catalog',$this->id,$catalogs); + + return $catalogs; + + } // get_catalogs + + /** * get_preferences * This is a little more complicate now that we've got many types of preferences * This funtions pulls all of them an arranges them into a spiffy little array @@ -964,27 +987,6 @@ class User extends database_object { } // get_recently_played - /** - * get_recent - * This returns users by thier last login date - */ - function get_recent($count=0) { - - if ($count) { $limit_clause = " LIMIT $count"; } - - $results = array(); - - $sql = "SELECT username FROM user ORDER BY last_seen $limit_clause"; - $db_results = mysql_query($sql, dbh()); - - while ($r = mysql_fetch_assoc($db_results)) { - $results[] = $r['username']; - } - - return $results; - - } // get_recent - /** * get_ip_history * This returns the ip_history from the @@ -1024,7 +1026,7 @@ class User extends database_object { @function activate_user @activates the user from public_registration */ - function activate_user($username) { + public function activate_user($username) { $username = Dba::escape($username); diff --git a/lib/general.lib.php b/lib/general.lib.php index a95b6b82..d09be316 100644 --- a/lib/general.lib.php +++ b/lib/general.lib.php @@ -422,8 +422,8 @@ function get_languages() { /** * format_time * This formats seconds into minutes:seconds + * //FIXME This should be removed, no reason for it! */ - function format_time($seconds) { return sprintf ("%d:%02d", $seconds/60, $seconds % 60); |