summaryrefslogtreecommitdiffstats
path: root/lib/class
diff options
context:
space:
mode:
authorKarl 'vollmerk' Vollmer <vollmer@ampache.org>2005-12-18 03:04:59 +0000
committerKarl 'vollmerk' Vollmer <vollmer@ampache.org>2005-12-18 03:04:59 +0000
commit0154f3736070c0847c5912dca88954b6bebe6001 (patch)
treebb40a15d7d9b475f5ff5c0ed85d32a6e932d1428 /lib/class
parent1ce04520cdadbc03726da96608a8b5bbb20ee95a (diff)
downloadampache-0154f3736070c0847c5912dca88954b6bebe6001.tar.gz
ampache-0154f3736070c0847c5912dca88954b6bebe6001.tar.bz2
ampache-0154f3736070c0847c5912dca88954b6bebe6001.zip
added ability to rename/merge artists thx spcombs
Diffstat (limited to 'lib/class')
-rw-r--r--lib/class/artist.class.php81
-rw-r--r--lib/class/catalog.class.php55
-rw-r--r--lib/class/error.class.php2
3 files changed, 136 insertions, 2 deletions
diff --git a/lib/class/artist.class.php b/lib/class/artist.class.php
index 2a6e3308..534281d6 100644
--- a/lib/class/artist.class.php
+++ b/lib/class/artist.class.php
@@ -180,6 +180,87 @@ class Artist {
} // format_artist
+ /*!
+ @function rename
+ @discussion changes the name of the artist in the db,
+ and then merge()s songs
+ @param $newname the artist's new name, either a new
+ artist will be created or songs added to existing
+ artist if name exists already
+ @return the id of the new artist
+ */
+ function rename($newname) {
+
+ /*
+ * There is this nifty function called check_artists in catalog that does exactly what we want it to do
+ * to use it, we first have to hax us a catalog
+ */
+ $catalog = new Catalog();
+
+ /* now we can get the new artist id in question */
+ $newid = $catalog->check_artist($newname);
+
+ /* check that it wasn't just whitespace that we were called to change */
+ if ($newid == $this->id) {
+ $GLOBALS['error']->add_error('artist_name',_("Error: Name Identical"));
+ return $newid;
+ }
+
+ /* now we can just call merge */
+ $this->merge($newid);
+
+ //now return id
+ return $newid;
+
+ } // rename
+
+ /*!
+ @function merge
+ @discussion changes the artist id of all songs by this artist
+ to the given id and deletes self from db
+ @param $newid the new artist id that this artist's songs should have
+ */
+ function merge($newid) {
+
+ $catalog = new Catalog();
+
+ /* Make sure this is a valid ID */
+ if (!is_numeric($newid)) {
+ $GLOBALS['error']->add_error('general',"Error: Invalid Artist ID");
+ return false;
+ }
+
+ // First check newid exists
+ $check_exists_qstring = "SELECT name FROM artist WHERE id='" . sql_escape($newid) . "'";
+ $check_exists_query = mysql_query($check_exists_qstring, dbh());
+
+ if (mysql_num_rows($check_exists_query)) {
+
+ // Get the name, for use in output
+ $check_exists_result = mysql_fetch_assoc($check_exists_query);
+
+ $NewName = $check_exists_result['name'];
+
+ // Now the query
+ $sql = "UPDATE song SET artist='" . sql_escape($newid) . "' " .
+ "WHERE artist='" . sql_escape($this->id) . "'";
+ $db_results = mysql_query($sql, dbh());
+
+ $num_stats_changed = $catalog->merge_stats("artist",$this->id,$newid);
+
+ /* If we've done the merege we need to clean up
+ $catalog->clean_artists();
+ $catalog->clean_albums();
+
+ }
+
+ else {
+ $GLOBALS['error']->add_error('general',"Error: Invalid Artist ID");
+ return false;
+ }
+
+ } // merge
+
/*!
@function show_albums
diff --git a/lib/class/catalog.class.php b/lib/class/catalog.class.php
index 5e79fa81..75736af0 100644
--- a/lib/class/catalog.class.php
+++ b/lib/class/catalog.class.php
@@ -2003,6 +2003,61 @@ class Catalog {
} // import_m3u
+ /*!
+ @function merge_stats
+ @discussion merge stats entries
+ @param $type the object_type row in object_count to use
+ @param $oldid the old object_id
+ @param $newid the new object_id to merge to
+ @return the number of stats changed
+ @todo move this to the right file
+ */
+ function merge_stats ($type,$oldid,$newid) {
+
+ //check data
+ $accepted_types = array ("artist");
+ if (!in_array($type,$accepted_types)) { return false; }
+
+ //now retrieve all of type and oldid
+ $stats_qstring = "SELECT id,count,userid," .
+ "(SELECT id FROM object_count WHERE object_type = '$type' AND object_id = '$newid' AND userid=o.userid) AS existingid " .
+ "FROM object_count AS o WHERE object_type = '$type' AND object_id = '$oldid'";
+
+ $stats_query = mysql_query($stats_qstring,dbh());
+ $oldstats = array();
+ //now collect needed data into a array
+ while ($stats_result = mysql_fetch_array($stats_query,MYSQL_ASSOC)) {
+ $userid = $stats_result['userid'];
+ $oldstats[$userid]['id'] = $stats_result['id'];
+ $oldstats[$userid]['count'] = $stats_result['count'];
+ $oldstats[$userid]['existingid'] = $stats_result['existingid'];
+ }
+ //now foreach that array, changeing/updateing object_count and if needed deleting old row
+ $num_changed = 0;
+ foreach ($oldstats as $userid => $stats) {
+ //first check if it is a update or insert
+ if (is_numeric($stats['existingid'])) {
+
+ $stats_count_change_qstring = "UPDATE object_count SET count = count + '" . $stats['count'] . "' WHERE id = '" . $stats['existingid'] . "'";
+ mysql_query($stats_count_change_qstring,dbh());
+
+ //then, delete old row
+ $old_stats_delete_qstring = "DELETE FROM object_count WHERE id ='" . $stats['id'] . "'";
+ mysql_query($old_stats_delete_qstring,dbh());
+
+ $num_changed++;
+ } else {
+ //hasn't yet listened, just change object_id
+ $stats_artist_change_qstring = "UPDATE object_count SET object_id = '$newid' WHERE id ='" . $stats['id'] . "'";
+ mysql_query($stats_artist_change_qstring,dbh());
+ //done!
+ $num_changed++;
+ }
+ }
+ return $num_changed;
+
+ } // merge_stats
+
/*!
@function delete_catalog
@discussion Deletes the catalog and everything assoicated with it
diff --git a/lib/class/error.class.php b/lib/class/error.class.php
index 9283e29d..5515e41c 100644
--- a/lib/class/error.class.php
+++ b/lib/class/error.class.php
@@ -24,8 +24,6 @@
@header Error handler requires error_results() function
*/
-
-
class Error {
//Basic Componets