We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

Commit d0be37ff authored by Jonas Haraldsson's avatar Jonas Haraldsson

Clean up addTags, removeTag

addTags return True if any tags were added, False if no tag was added.
removeTag return True if tag was removed, False if not.
parent aaf2dd2b
......@@ -34,24 +34,32 @@ class TrackXML {
public static function addTags($userid, $artist, $album, $trackName, $tags) {
try {
$track = new Track($trackName, $artist);
$track->addTags($tags, $userid);
$res = $track->addTags($tags, $userid);
} catch (Exception $e) {
return(XML::error('failed', '7', 'Invalid resource specified'));
}
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
if(!$res) {
$xml = XML::error('failed', '7', 'Invalid resource specified');
} else {
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
}
return $xml;
}
public static function removeTag($userid, $artist, $trackName, $tag) {
try {
$track = new Track($trackName, $artist);
$track->removeTag($tag, $userid);
$res = $track->removeTag($tag, $userid);
} catch (Exception $e) {
return(XML::error('failed', '7', 'Invalid resource specified'));
}
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
if(!$res) {
$xml = XML::error('failed', '7', 'Invalid resource specified');
} else {
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
}
return $xml;
}
......@@ -98,7 +106,6 @@ class TrackXML {
$root->addAttribute('artist', $track->artist_name);
$root->addAttribute('track', $track->name);
$i = $offset + 1;
foreach($res as &$row) {
try {
$user = new User($row['username']);
......@@ -114,7 +121,6 @@ class TrackXML {
$image_large->addAttribute('size', 'large');
$user_node->addChild('weight', $row['freq']);
} catch (Exception $e) {}
$i++;
}
return $xml;
......
......@@ -326,27 +326,31 @@ class Track {
/**
* Add a list of tags to a track
*
* @param string $tags A comma-separated list of tags
* @param int $userid The user adding these tags
* @param string $tags A comma-separated list of tags.
* @param int $userid The user adding these tags.
* @return bool True if any tag was added, False if no tags were added.
*/
function addTags($tags, $userid) {
global $adodb;
$tags = explode(',', strtolower($tags));
$query = 'INSERT INTO Tags (tag, artist, album, track, userid) VALUES(?,?,?,?,?)';
foreach($tags as $tag) {
$tag = trim($tag);
if(strlen($tag) == 0) {
continue;
}
$params = array($tag, $this->artist_name, $this->album_name, $this->name, (int) $userid);
try {
$adodb->Execute('INSERT INTO Tags VALUES ('
. $adodb->qstr($tag) . ','
. $adodb->qstr($this->artist_name) . ', '
. $adodb->qstr($this->album_name) . ', '
. $adodb->qstr($this->name) . ', '
. $userid . ')');
} catch (Exception $e) {}
$adodb->Execute($query, $params);
if ($adodb->Affected_Rows()) {
$res = $res + 1;
}
} catch (Exception $e) {
reportError($e->GetMessage(), $e->GetTraceAsString());
}
}
return (bool) $res;
}
/**
......@@ -354,7 +358,6 @@ class Track {
*
* @param int $userid The user loving this track.
* @return bool True on success, False on fail.
*
*/
function love($userid) {
global $adodb;
......@@ -493,6 +496,7 @@ class Track {
*
* @param string $tag The tag to be removed
* @param int $userid The user removing the tag
* @return bool True on success, False on fail.
*/
function removeTag($tag, $userid) {
global $adodb;
......@@ -501,13 +505,16 @@ class Track {
if(strlen($tag) == 0) {
return;
}
$query = 'DELETE FROM Tags WHERE tag = ? AND lower(artist) = lower(?) AND lower(track) = lower(?) AND userid = ?';
$params = array($tag, $this->artist_name, $this->name, $userid);
$query = 'DELETE FROM Tags WHERE tag=? AND artist=? AND track=? AND userid = ?';
$params = array($tag, $this->artist_name, $this->name, (int) $userid);
try {
$adodb->Execute($query, $params);
$res = $adodb->Affected_Rows();
} catch (Exception $e) {
reportError($e->getMessage(), $e->getTraceAsString());
return False;
}
return (bool) $res;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment