madeon (madeon) wrote in changelog,
madeon
madeon
changelog

[livejournal] r20894: LJSV-2014: Ability to rename support tag...

Committer: sbelyaev
LJSV-2014: Ability to rename support tags
U   trunk/cgi-bin/LJ/Support/Request/Tag.pm
U   trunk/htdocs/support/manage_tags.bml
Modified: trunk/cgi-bin/LJ/Support/Request/Tag.pm
===================================================================
--- trunk/cgi-bin/LJ/Support/Request/Tag.pm	2012-01-13 12:44:35 UTC (rev 20893)
+++ trunk/cgi-bin/LJ/Support/Request/Tag.pm	2012-01-13 13:32:13 UTC (rev 20894)
@@ -93,6 +93,146 @@
     return $tags->{$spid};
 }
 
+
+# rename_tag() : rename tag
+# calling format
+# rename_tag($tagid, $new_tag_name, $everywhere)
+sub rename_tag {
+    my ($opts) = @_;
+
+    my $sptagid     = $opts->{'sptagid'};
+    my $spcatid     = $opts->{'spcatid'};
+    my $new_name    = $opts->{'new_name'};
+    my $everywhere  = $opts->{'everywhere'};
+    my $allowmerge  = $opts->{'allowmerge'};
+
+    my $dbh = LJ::get_db_writer();
+    my $old_name = LJ::Support::Request::Tag::tag_id_to_name($sptagid);
+
+    if ($everywhere) {
+        # receive all categories where rename is expected 
+        my $source
+            = $dbh->selectall_hashref( "SELECT sptagid, spcatid FROM supporttag " .
+                                       "WHERE name=?",
+                                       'spcatid',
+                                       undef,
+                                       $old_name );
+
+        my @old_spcatids = keys %$source;
+        my $old_spcatids_str = join(',', @old_spcatids);
+
+        # receive exists tags
+        my $destination
+            = $dbh->selectall_hashref( "SELECT sptagid, spcatid FROM supporttag " .
+                                       "WHERE name=? AND spcatid IN ($old_spcatids_str)",
+                                       'spcatid',
+                                       undef,
+                                       $new_name );
+
+        # Does name exist already?
+        if (!$destination) {
+            # just rename 
+            my $row = $dbh->do( "UPDATE supporttag SET name=? WHERE name=?",
+                                undef,
+                                $new_name,
+                                $old_name );
+        } elsif ($allowmerge) {
+
+            # update all in 'supporttag'
+            foreach my $spcatid (keys %$source) {
+                my $source_hash       = delete $source->{$spcatid};
+                my $source_id         = $source_hash->{'sptagid'};
+                my $destination_hash  = $destination->{$spcatid};
+                my $destination_id    = $destination_hash->{'sptagid'};
+
+                my ($current_spid) = $dbh->selectrow_array( 'SELECT spid ' .
+                                                       'FROM supporttagmap ' .
+                                                       'WHERE sptagid = ?',
+                                                       undef,
+                                                       $source_id);
+
+
+                my $spids = $dbh->selectcol_arrayref( 'SELECT spid ' .
+                                                      'FROM supporttagmap ' .
+                                                      'WHERE sptagid = ?',
+                                                      undef,
+                                                      $sptagid );
+
+                $dbh->do( "DELETE FROM supporttag WHERE sptagid = $source_id");
+                $dbh->do( "DELETE FROM supporttagmap WHERE sptagid = $source_id");
+
+                foreach my $spid (@$spids) {
+                    if ($spid == $current_spid) {
+                        next;
+                    }
+
+                    $dbh->do( 'INSERT INTO supporttagmap (spid, sptagid) ' .
+                              'VALUES (?, ?) ',
+                              undef,
+                              $spid,
+                              $destination_id );
+                }
+
+                my $update_list = join(',', keys %$source);
+                $dbh->do( 'UPDATE supporttag ' . 
+                          'SET name=? ' .
+                          "WHERE sptagid IN ($update_list)",
+                          undef,
+                          $new_name,
+                          $sptagid );
+            }
+        } else {
+            return 0;
+        }
+        return 1;
+    }
+
+    my ($exists_stagid)
+            = $dbh->selectrow_array( "SELECT sptagid FROM supporttag " .
+                                     "WHERE name=? AND spcatid = ?",
+                                     undef,
+                                     $new_name,
+                                     $spcatid );
+
+    my $name_exists = !!$exists_stagid;
+
+    if (!$name_exists) {
+        $dbh->do( 'UPDATE supporttag SET name=? WHERE sptagid=?',
+                  undef,
+                  $new_name,
+                  $sptagid );
+    } elsif ($name_exists && $allowmerge) {
+        my ($current_spid) = $dbh->selectrow_array( 'SELECT spid ' .
+                                               'FROM supporttagmap ' .
+                                               'WHERE sptagid = ?',
+                                               undef,
+                                               $source_id);
+
+
+        my $spid = $dbh->selectrow_array( 'SELECT spid ' .
+                                          'FROM supporttagmap '.
+                                          'WHERE sptagid=?',
+                                          undef,
+                                          $sptagid );
+
+        $dbh->do( "DELETE FROM supporttag WHERE sptagid = $sptagid" );
+        $dbh->do( "DELETE FROM supporttagmap WHERE sptagid = $sptagid" );
+
+        # if duplicate entry
+        if ($current_spid != $spid) {
+            $dbh->do( 'INSERT INTO supporttagmap (spid, sptagid) ' .
+                      'VALUES (?, ?)',
+                      undef,
+                      $spid,
+                      $exists_stagid );
+        }
+    } else {
+        return 0;
+    }
+
+    return 1;
+}
+
 # set_request_tags(): sets tags for a given request
 # calling format:
 # set_request_tags($spid, $sptagid1, $sptag2, ...)
@@ -305,4 +445,4 @@
     $dbh->do("DELETE FROM supporttagmap WHERE sptagid IN ($sptagids_cond)");
 }
 
-1;
\ No newline at end of file
+1;

Modified: trunk/htdocs/support/manage_tags.bml
===================================================================
--- trunk/htdocs/support/manage_tags.bml	2012-01-13 12:44:35 UTC (rev 20893)
+++ trunk/htdocs/support/manage_tags.bml	2012-01-13 13:32:13 UTC (rev 20894)
@@ -2,6 +2,35 @@
 title=><?_ml .title _ml?>
 head<=
 <?_code return LJ::robot_meta_tags(); _code?>
+<script type="text/javascript" charset="UTF-8">
+
+function rename_support_tag(sptagid, spcatid, text) {
+    var wopts = 'width=200,height=130';
+    var redirect_url = "<?_code return $LJ::SITEROOT.'/support/manage_tags.bml'; _code?>";
+    var new_window = window.open('', "Rename: " + text, wopts)
+    with(new_window) {
+        document.body.innerHTML= ""
+        document.write('<html><head>');
+        document.write('<meta http-equiv="content-type" content="text/html; charset=utf-8">');
+        document.write('</head><body>');
+        document.write('<form>New name: <input id="new_name" type="text" value="' + text +'"/><br/>');
+        document.write('rename everywhere <input id="everywhere" type="checkbox"/><br/>');
+        document.write('<input id="rename" type="button" value="rename" onclick="javascript:void(0)"/>');
+        document.write('<input type="button" value="cancel" onclick="javascript:window.close()"/>');
+        document.write('</form>');
+        document.write('</body></html>');;
+    }
+
+    jQuery(new_window.document).contents().find("#rename").click( function() {
+        var every = new_window.document.getElementById('everywhere').checked;
+        var name = new_window.document.getElementById('new_name').value;
+        new_window.close();
+
+        jQuery.post(redirect_url, {renamed: sptagid, catid: spcatid, new_name: name, everywhere: every});
+        location.reload();
+    });
+}
+</script>
 <=head
 body<=
 <?_code
@@ -41,7 +70,23 @@
     
     my %can_see_cats = map { $_ => 1 } @can_see_cats;
     my %can_manage_cats = map { $_ => 1 } @can_manage_cats;
-    
+
+    if (LJ::did_post() && $FORM{'renamed'}) {
+        my $sptagid     = int($FORM{'renamed'});
+        my $spcatid     = int($FORM{'catid'});
+        my $everywhere  = $FORM{'everywhere'};
+        my $allowmerge  = $FORM{'allowmerge'};
+        my $new_name    = $FORM{'new_name'};
+
+        LJ::Support::Request::Tag::rename_tag( {'sptagid'    => $sptagid,
+                                                'spcatid'    => $spcatid,
+                                                'new_name'   => $new_name,
+                                                'everywhere' => $everywhere eq 'true',
+                                                'allowmerge' => 1} );
+
+        return BML::redirect($LJ::SITEROOT . '/support/manage_tags.bml');
+    }
+
     if (LJ::did_post()) {
         my @delete;
         foreach my $k (keys %FORM) {
@@ -49,12 +94,12 @@
         }
         
         LJ::Support::Request::Tag::drop_tags(\@delete, \@can_manage_cats);
-        
         return BML::redirect($LJ::SITEROOT . '/support/manage_tags.bml');
     }
 
+
+
     my $ret;
-
     $ret .= '<form action="" method="post">';
 
     foreach my $spcat (values %$cats) {
@@ -84,6 +129,12 @@
                     $ret .=
                         ' [<a href="'.$LJ::SITEROOT.'/support/help.bml?' .
                         'tags=' . $name . '">' . $ML{'.requests'} . '</a>]';
+
+                    $ret .= 
+                        '<a href="javascript:rename_support_tag('.
+                        $sptagid . ','.
+                        $spcatid . ',\''.
+                        $name . '\')">[rename]</a>';
                 } else {
                     $ret .= '<a href="'.$LJ::SITEROOT.'/support/help.bml?' .
                         'tags=' . $name . '">' . $name . '</a>';
@@ -105,4 +156,4 @@
 _code?>
 
 <=body
-page?>
\ No newline at end of file
+page?>

Tags: bml, livejournal, madeon, pm, sbelyaev
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments