From 3baa72a62fc69dda8306674da6d150efbf2cd55b Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Wed, 31 Mar 2010 10:53:02 -0400
Subject: [PATCH] Implement group renaming/deleting + use more consistent names for commands and actions (#1486587)

---
 index.php                                |    9 +-
 program/steps/addressbook/groups.inc     |   46 ++++++++---
 skins/default/templates/addressbook.html |    6 
 program/js/app.js                        |   85 +++++++++++++++++++--
 program/include/rcube_contacts.php       |   62 ++++++++++++--
 5 files changed, 169 insertions(+), 39 deletions(-)

diff --git a/index.php b/index.php
index 02a6f14..f7a3bd3 100644
--- a/index.php
+++ b/index.php
@@ -217,10 +217,11 @@
   
   'addressbook' => array(
     'add' => 'edit.inc',
-    'create-group' => 'groups.inc',
-    'delete-group' => 'groups.inc',
-    'removefromgroup' => 'groups.inc',
-    'add2group' => 'groups.inc',
+    'group-create' => 'groups.inc',
+    'group-rename' => 'groups.inc',
+    'group-delete' => 'groups.inc',
+    'group-addmember' => 'groups.inc',
+    'group-delmenber' => 'groups.inc',
   ),
   
   'settings' => array(
diff --git a/program/include/rcube_contacts.php b/program/include/rcube_contacts.php
index 41f47ea..4bc70a9 100644
--- a/program/include/rcube_contacts.php
+++ b/program/include/rcube_contacts.php
@@ -417,18 +417,9 @@
   function create_group($name)
   {
     $result = false;
-    
-    $sql_result = $this->db->query(
-      "SELECT * FROM ".get_table_name('contactgroups')."
-       WHERE  del<>1
-       AND    user_id=?
-       AND    name LIKE ?",
-      $this->user_id,
-      $name . '%');
-    
+
     // make sure we have a unique name
-    if ($num = $this->db->num_rows($sql_result))
-      $name .= ' ' . ($num+1);
+    $name = $this->unique_groupname($name);
     
     $this->db->query(
       "INSERT INTO ".get_table_name('contactgroups')." (user_id, changed, name)
@@ -445,6 +436,7 @@
    * Delete the given group and all linked group members
    *
    * @param string Group identifier
+   * @return boolean True on success, false if no data was changed
    */
   function delete_group($gid)
   {
@@ -460,6 +452,27 @@
       $gid);
     
     return $this->db->affected_rows();
+  }
+  
+  /**
+   * Rename a specific contact group
+   *
+   * @param string Group identifier
+   * @param string New name to set for this group
+   * @return boolean New name on success, false if no data was changed
+   */
+  function rename_group($gid, $newname)
+  {
+    // make sure we have a unique name
+    $name = $this->unique_groupname($newname);
+    
+    $sql_result = $this->db->query(
+      "UPDATE ".get_table_name('contactgroups')."
+       SET name=".$this->db->quote($name).", changed=".$this->db->now()."
+       WHERE  contactgroup_id=?",
+      $gid);
+    
+    return $this->db->affected_rows() ? $name : false;
   }
 
   /**
@@ -517,4 +530,31 @@
     return $this->db->affected_rows();
   }
   
+  /**
+   * Check for existing groups with the same name
+   *
+   * @param string Name to check
+   * @return string A group name which is unique for the current use
+   */
+  private function unique_groupname($name)
+  {
+    $checkname = $name;
+    $num = 2; $hit = false;
+    
+    do {
+      $sql_result = $this->db->query(
+        "SELECT 1 FROM ".get_table_name('contactgroups')."
+         WHERE  del<>1
+         AND    user_id=?
+         AND    name LIKE ?",
+        $this->user_id,
+        $checkname);
+    
+      // append number to make name unique
+      if ($hit = $this->db->num_rows($sql_result))
+        $checkname = $name . ' ' . $num++;
+    } while ($hit > 0);
+    
+    return $checkname;
+  }
 }
diff --git a/program/js/app.js b/program/js/app.js
index e73dec6..22eedf6 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -317,7 +317,7 @@
         
         if (this.env.address_sources && this.env.address_sources[this.env.source] && !this.env.address_sources[this.env.source].readonly) {
           this.enable_command('add', 'import', true);
-          this.enable_command('add-group', this.env.address_sources[this.env.source].groups);
+          this.enable_command('group-create', this.env.address_sources[this.env.source].groups);
         }
         
         if (this.env.cid)
@@ -1001,8 +1001,16 @@
           this.list_contacts(this.env.source, this.env.group);
         break;
 
-      case 'add-group':
+      case 'group-create':
         this.add_contact_group(props)
+        break;
+        
+      case 'group-rename':
+        this.rename_contact_group();
+        break;
+        
+      case 'group-delete':
+        this.delete_contact_group();
         break;
 
       case 'import':
@@ -3501,7 +3509,7 @@
       cid = this.contact_list.get_selection().join(',');
 
     if (to.type == 'group')
-      this.http_post('add2group', '_cid='+urlencode(cid)+'&_source='+urlencode(this.env.source)+'&_gid='+urlencode(to.id));
+      this.http_post('group-addmember', '_cid='+urlencode(cid)+'&_source='+urlencode(this.env.source)+'&_gid='+urlencode(to.id));
     else if (to.id != this.env.source && cid && this.env.address_sources[to.id] && !this.env.address_sources[to.id].readonly)
       this.http_post('copy', '_cid='+urlencode(cid)+'&_source='+urlencode(this.env.source)+'&_to='+urlencode(to.id));
     };
@@ -3540,7 +3548,7 @@
 
     // send request to server
     if (this.env.group)
-      this.http_post('removefromgroup', '_cid='+urlencode(a_cids.join(','))+'&_source='+urlencode(this.env.source)+'&_gid='+urlencode(this.env.group)+qs);
+      this.http_post('group-delmember', '_cid='+urlencode(a_cids.join(','))+'&_source='+urlencode(this.env.source)+'&_gid='+urlencode(this.env.group)+qs);
     else
       this.http_post('delete', '_cid='+urlencode(a_cids.join(','))+'&_source='+urlencode(this.env.source)+'&_from='+(this.env.action ? this.env.action : '')+qs);
       
@@ -3618,6 +3626,45 @@
     this.name_input.select();
   };
   
+  this.rename_contact_group = function()
+  {
+    if (!this.env.group || !this.gui_objects.folderlist)
+      return;
+    
+    if (!this.name_input) {
+      this.name_input = document.createElement('input');
+      this.name_input.type = 'text';
+      this.name_input.value = this.env.contactgroups['G'+this.env.group].name;
+      this.name_input.onkeypress = function(e){ return rcmail.add_input_keypress(e); };
+      this.env.group_renaming = true;
+
+      var link, li = this.get_folder_li(this.env.group, 'rcmliG');
+      if (li && (link = li.firstChild)) {
+        $(link).hide();
+        li.insertBefore(this.name_input, link);
+      }
+    }
+
+    this.name_input.select();
+  };
+  
+  this.delete_contact_group = function()
+  {
+    if (this.env.group)
+      this.http_post('group-delete', '_source='+urlencode(this.env.source)+'&_gid='+urlencode(this.env.group), true);
+  };
+  
+  // callback from server upon group-delete command
+  this.remove_group_item = function(id)
+  {
+    var li, key = 'G'+id;
+    if ((li = this.get_folder_li(key))) {
+      li.parentNode.removeChild(li);
+      delete this.env.contactfolders[key];
+      delete this.env.contactgroups[key];
+    }
+  };
+  
   // handler for keyboard events on the input field
   this.add_input_keypress = function(e)
   {
@@ -3629,7 +3676,10 @@
       
       if (newname) {
         this.set_busy(true, 'loading');
-        this.http_post('create-group', '_source='+urlencode(this.env.source)+'&_name='+urlencode(newname), true);
+        if (this.env.group_renaming)
+          this.http_post('group-rename', '_source='+urlencode(this.env.source)+'&_gid='+urlencode(this.env.group)+'&_name='+urlencode(newname), true);
+        else
+          this.http_post('group-create', '_source='+urlencode(this.env.source)+'&_name='+urlencode(newname), true);
       }
       return false;
     }
@@ -3643,6 +3693,12 @@
   this.reset_add_input = function()
   {
     if (this.name_input) {
+      if (this.env.group_renaming) {
+        var li = this.name_input.parentNode;
+        $(li.lastChild).show();
+        this.env.group_renaming = false;
+      }
+      
       this.name_input.parentNode.removeChild(this.name_input);
       this.name_input = null;
     }
@@ -3660,7 +3716,20 @@
     var link = $('<a>').attr('href', '#').attr('onclick', "return rcmail.command('listgroup','"+prop.id+"',this)").html(prop.name);
     var li = $('<li>').attr('id', 'rcmli'+key).addClass('contactgroup').append(link);
     $(this.gui_objects.folderlist).append(li);
-  }
+  };
+  
+  // callback for renaming a contact group
+  this.update_contact_group = function(id, name)
+  {
+    this.reset_add_input();
+    
+    var key = 'G'+id;
+    var link, li = this.get_folder_li(key);
+    if (li && (link = li.firstChild) && link.tagName.toLowerCase() == 'a')
+      link.innerHTML = name;
+    
+    this.env.contactfolders[key].name = this.env.contactgroups[key].name = name;
+  };
 
 
   /*********************************************************/
@@ -4830,8 +4899,8 @@
           this.enable_command('export', (this.contact_list && this.contact_list.rowcount > 0));
           
           if (response.action == 'list') {
-            this.enable_command('add-group', this.env.address_sources[this.env.source].groups);
-            // disabeld for now: this.enable_command('rename-group', 'delete-group', this.env.address_sources[this.env.source].groups && this.env.group);
+            this.enable_command('group-create', this.env.address_sources[this.env.source].groups);
+            this.enable_command('group-rename', 'group-delete', this.env.address_sources[this.env.source].groups && this.env.group);
             this.triggerEvent('listupdate', { folder:this.env.source, rowcount:this.contact_list.rowcount });
           }
         }
diff --git a/program/steps/addressbook/groups.inc b/program/steps/addressbook/groups.inc
index 8c7aef6..0141926 100644
--- a/program/steps/addressbook/groups.inc
+++ b/program/steps/addressbook/groups.inc
@@ -25,7 +25,23 @@
   $OUTPUT->send();
 }
 
-if ($RCMAIL->action == 'create-group') {
+if ($RCMAIL->action == 'group-addmember') {
+  if (($gid = get_input_value('_gid', RCUBE_INPUT_POST)) && ($ids = get_input_value('_cid', RCUBE_INPUT_POST)))
+  if ($CONTACTS->add_to_group($gid, $ids))
+    $OUTPUT->show_message('contactaddedtogroup');
+  //else
+  //  $OUTPUT->show_message('erroraddingcontact', 'warning');
+}
+
+else if ($RCMAIL->action == 'group-delmember') {
+  if (($gid = get_input_value('_gid', RCUBE_INPUT_POST)) && ($ids = get_input_value('_cid', RCUBE_INPUT_POST)))
+  if ($CONTACTS->remove_from_group($gid, $ids))
+    $OUTPUT->show_message('contactremovedfromgroup');
+  //else
+  //  $OUTPUT->show_message('erroraddingcontact', 'warning');
+}
+
+else if ($RCMAIL->action == 'group-create') {
   if (!empty($_POST['_name'])) {
     $name = trim(get_input_value('_name', RCUBE_INPUT_POST));
     $created = $CONTACTS->create_group($name);
@@ -39,20 +55,24 @@
   }
 }
 
-else if ($RCMAIL->action == 'add2group') {
-  if (($gid = get_input_value('_gid', RCUBE_INPUT_POST)) && ($ids = get_input_value('_cid', RCUBE_INPUT_POST)))
-  if ($CONTACTS->add_to_group($gid, $ids))
-    $OUTPUT->show_message('contactaddedtogroup');
-  //else
-  //  $OUTPUT->show_message('erroraddingcontact', 'warning');
+else if ($RCMAIL->action == 'group-rename') {
+  if (($gid = get_input_value('_gid', RCUBE_INPUT_POST)) && ($name = trim(get_input_value('_name', RCUBE_INPUT_POST))))
+    $newname = $CONTACTS->rename_group($gid, $name);
+
+  if ($newname && $OUTPUT->ajax_call)
+    $OUTPUT->command('update_contact_group', $gid, $newname);
+  else if (!$newname)
+    $OUTPUT->show_message('errorsaving', 'error');
 }
 
-else if ($RCMAIL->action == 'removefromgroup') {
-  if (($gid = get_input_value('_gid', RCUBE_INPUT_POST)) && ($ids = get_input_value('_cid', RCUBE_INPUT_POST)))
-  if ($CONTACTS->remove_from_group($gid, $ids))
-    $OUTPUT->show_message('contactremovedfromgroup');
-  //else
-  //  $OUTPUT->show_message('erroraddingcontact', 'warning');
+else if ($RCMAIL->action == 'group-delete') {
+  if ($gid = get_input_value('_gid', RCUBE_INPUT_POST))
+    $deleted = $CONTACTS->delete_group($gid);
+
+  if ($deleted)
+    $OUTPUT->command('remove_group_item', $gid);
+  else
+    $OUTPUT->show_message('errorsaving', 'error');
 }
 
 // send response
diff --git a/skins/default/templates/addressbook.html b/skins/default/templates/addressbook.html
index f479315..d1455f3 100644
--- a/skins/default/templates/addressbook.html
+++ b/skins/default/templates/addressbook.html
@@ -41,15 +41,15 @@
   <roundcube:object name="groupslist" id="contactgroupslist" />
 </div>
 <div id="directoylistbuttons">
-  <roundcube:button command="add-group" type="link" title="newcontactgroup" class="buttonPas addgroup" classAct="button addgroup" content=" " />
+  <roundcube:button command="group-create" type="link" title="newcontactgroup" class="buttonPas addgroup" classAct="button addgroup" content=" " />
   <roundcube:button name="groupactions" id="groupactionslink" type="link" title="groupactions" class="button groupactions" onclick="rcmail_ui.show_groupmenu();return false" content=" " />
 </div>
 </div>
 
 <div id="groupoptionsmenu" class="popupmenu">
   <ul>
-    <li><roundcube:button command="rename-group" label="rename" classAct="active" /></li>
-    <li><roundcube:button command="delete-group" label="delete" classAct="active" /></li>
+    <li><roundcube:button command="group-rename" label="rename" classAct="active" /></li>
+    <li><roundcube:button command="group-delete" label="delete" classAct="active" /></li>
   </ul>
 </div>
 

--
Gitblit v1.9.1