From 02ecc1c7a6c65f792c8ff2f367258ce212f996d7 Mon Sep 17 00:00:00 2001 From: Banana Date: Wed, 8 Jun 2011 14:53:18 +0200 Subject: [PATCH] tag rename working --- ChangeLog | 1 + lib/Insipid/Tags.pm | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index eeadd79..c0dbc24 100755 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,6 @@ 06.2011 CEST jumpin.banana * tag suggest was not working correctly. + * tag rename feature implemented Tue Jul 14 21:38 EST 2009 Luke Reeve diff --git a/lib/Insipid/Tags.pm b/lib/Insipid/Tags.pm index 88200fb..418a992 100755 --- a/lib/Insipid/Tags.pm +++ b/lib/Insipid/Tags.pm @@ -55,17 +55,43 @@ sub tag_operations { my $newTag = param('newName'); my $oldTagName = param('rename'); + my $oldTagId = get_tag_id_by_name($oldTagName); # is the new name already a tag ? - my $check = already_a_tag($newTag); + # if check then check is the new tagId + my $check = get_tag_id_by_name($newTag); if($check) { - # delete the old one and rewrite the id + my $sql = "SELECT bookmark_id, tag_id + FROM `$tbl_bookmark_tags` + WHERE tag_id = ?"; + my $sth = $dbh->prepare($sql); + $sth->execute($oldTagId); + while(my ($bid, $tid) = $sth->fetchrow_array) { + #print Dumper $bid; + my $sql1 = "DELETE FROM `$tbl_bookmark_tags` + WHERE bookmark_id = ? + AND tag_id = ?"; + my $sth1 = $dbh->prepare($sql1); + $sth1->execute($bid,$tid); + + my $sql2 = "INSERT IGNORE INTO `$tbl_bookmark_tags` + SET bookmark_id = ?, + tag_id = ?"; + my $sth2 = $dbh->prepare($sql2); + $sth2->execute($bid,$check); + } + + # remove the old one + my $sql = "DELETE FROM `$tbl_tags` WHERE id = ?"; + my $sth = $dbh->prepare($sql); + $sth->execute($oldTagId); + } else { # just rename the tag my $sql = "UPDATE $tbl_tags SET name = ? WHERE name = ?"; my $sth = $dbh->prepare($sql); - $sth->execute($newTag, $oldTagName); + $sth->execute($oldTagId); } print 'Done !'; @@ -272,7 +298,7 @@ sub set_tags { # check if given word is an existing tag # if so return the tag id -sub already_a_tag { +sub get_tag_id_by_name { my ($tag) = (@_); my($tagId, $sql, $sth, @result); @@ -282,9 +308,10 @@ sub already_a_tag { $sth = $dbh->prepare($sql); $sth->execute($tag); @result = $sth->fetchrow_array(); - #print "..".$result[0]; $tagId = $result[0]; } + + return $tagId; } 1; -- 2.39.5