From 360bd3ea740073b83817183581893681e13a8570 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Tue, 15 Feb 2011 16:00:48 -0500
Subject: [PATCH] Allow group identifiers to be changed upon renaming (used in LDAP)

---
 program/steps/addressbook/groups.inc  |    4 ++--
 program/include/rcube_addressbook.php |    3 ++-
 program/include/rcube_ldap.php        |    4 +++-
 program/js/app.js                     |   27 ++++++++++++++++++++++++---
 4 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/program/include/rcube_addressbook.php b/program/include/rcube_addressbook.php
index d1b0f62..9e8254c 100644
--- a/program/include/rcube_addressbook.php
+++ b/program/include/rcube_addressbook.php
@@ -310,9 +310,10 @@
      *
      * @param string Group identifier
      * @param string New name to set for this group
+     * @param string New group identifier (if changed, otherwise don't set)
      * @return boolean New name on success, false if no data was changed
      */
-    function rename_group($gid, $newname)
+    function rename_group($gid, $newname, &$newid)
     {
         /* empty for address books don't supporting groups */
         return false;
diff --git a/program/include/rcube_ldap.php b/program/include/rcube_ldap.php
index 3cb4748..308c4f2 100644
--- a/program/include/rcube_ldap.php
+++ b/program/include/rcube_ldap.php
@@ -995,9 +995,10 @@
      *
      * @param string Group identifier
      * @param string New name to set for this group
+     * @param string New group identifier (if changed, otherwise don't set)
      * @return boolean New name on success, false if no data was changed
      */
-    function rename_group($group_id, $new_name)
+    function rename_group($group_id, $new_name, &$new_id)
     {
         if (!$this->group_cache)
             $this->list_groups();
@@ -1006,6 +1007,7 @@
         $group_name = $this->group_cache[$group_id]['name'];
         $old_dn = "cn=$group_name,$base_dn";
         $new_rdn = "cn=$new_name";
+        $new_id = base64_encode($new_rdn . ",$base_dn");
 
         $res = ldap_rename($this->conn, $old_dn, $new_rdn, NULL, TRUE);
         if ($res === false)
diff --git a/program/js/app.js b/program/js/app.js
index 43be6ae..712400a 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -3948,6 +3948,7 @@
     this.env.contactfolders[key] = this.env.contactgroups[key] = prop;
 
     var link = $('<a>').attr('href', '#')
+      .attr('rel', prop.source+':'+prop.id)
       .bind('click', function() { return rcmail.command('listgroup', prop, this);})
       .html(prop.name);
     var li = $('<li>').attr('id', 'rcmli'+key.replace(this.identifier_expr, '_'))
@@ -3963,13 +3964,33 @@
   {
     this.reset_add_input();
 
-    var key = 'G'+prop.source+prop.id, link, li = this.get_folder_li(key);
+    var key = 'G'+prop.source+prop.id,
+      li = this.get_folder_li(key),
+      link;
 
-    if (li && (link = li.firstChild) && link.tagName.toLowerCase() == 'a')
+    // group ID has changed, replace link node and identifiers
+    if (li && prop.newid) {
+      var newkey = 'G'+prop.source+prop.newid;
+      li.id = String('rcmli'+newkey).replace(this.identifier_expr, '_');
+      this.env.contactfolders[newkey] = this.env.contactfolders[key];
+      this.env.group = prop.newid;
+      
+      var newprop = $.extend({}, prop);
+      newprop.id = prop.newid;
+      newprop.type = 'group';
+      
+      link = $('<a>').attr('href', '#')
+        .attr('rel', prop.source+':'+prop.newid)
+        .bind('click', function() { return rcmail.command('listgroup', newprop, this);})
+        .html(prop.name);
+      $(li).children().replaceWith(link);
+    }
+    // update displayed group name
+    else if (li && (link = li.firstChild) && link.tagName.toLowerCase() == 'a')
       link.innerHTML = prop.name;
 
     this.env.contactfolders[key].name = this.env.contactgroups[key].name = prop.name;
-    this.triggerEvent('group_update', { id:prop.id, source:prop.source, name:prop.name, li:li[0] });
+    this.triggerEvent('group_update', { id:prop.id, source:prop.source, name:prop.name, li:li[0], newid:prop.newid });
   };
 
 
diff --git a/program/steps/addressbook/groups.inc b/program/steps/addressbook/groups.inc
index b70bbf2..4c70bf1 100644
--- a/program/steps/addressbook/groups.inc
+++ b/program/steps/addressbook/groups.inc
@@ -91,7 +91,7 @@
     $plugin = $RCMAIL->plugins->exec_hook('group_rename', array('group_id' => $gid, 'name' => $name, 'source' => $source));
 
     if (!$plugin['abort'])
-      $newname = $CONTACTS->rename_group($gid, $plugin['name']);
+      $newname = $CONTACTS->rename_group($gid, $plugin['name'], $newgid);
     else
       $newname = $plugin['result'];
   }
@@ -99,7 +99,7 @@
   if ($newname && $OUTPUT->ajax_call) {
     $OUTPUT->show_message('grouprenamed', 'confirmation');
     $OUTPUT->command('update_contact_group', array(
-      'source' => $source, 'id' => $gid, 'name' => $newname));
+      'source' => $source, 'id' => $gid, 'name' => $newname, 'newid' => $newgid));
   }
   else if (!$newname)
     $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error');

--
Gitblit v1.9.1