From c83decbf0da20cd30d0784fdd2c5d0e60ca65dae Mon Sep 17 00:00:00 2001
From: Brendan Andersen <brendan@omnifik.com>
Date: Sun, 13 Mar 2011 22:23:48 -0700
Subject: [PATCH] Issue #967384: Enable unbanning.

---
 chatroom.forms.inc |    6 +++---
 chatroom.module    |   24 ++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/chatroom.forms.inc b/chatroom.forms.inc
index 6077b5e..3431456 100644
--- a/chatroom.forms.inc
+++ b/chatroom.forms.inc
@@ -370,14 +370,14 @@ function chatroom_chat_form(&$node, $form_state) {
     '#rows' => 3,
     '#description' => t('This text will appear on the page banned users are sent to. Defaults to, "You have been banned from %chatroom."', array('%chatroom' => t('chat-room'))),
   );
-  if (!empty($node->banned_users)) {
+  if (!empty($node->chat->banned_list)) {
     $form['chatroom_banned_users'] = array(
       '#type' => 'fieldset',
       '#title' => t('Manage banned users'),
       '#collapsible' => TRUE,
     );
-    foreach ($node->banned_users as $banned_user) {
-      $banned_users[$banned_user->uid] = check_plain($banned_user->name);
+    foreach ($node->chat->banned_list as $banned_user) {
+      $banned_users[$banned_user->uid] = user_load($banned_user->uid)->name;
     }
     $form['chatroom_banned_users']['unban_list'] = array(
       '#type' => 'checkboxes',
diff --git a/chatroom.module b/chatroom.module
index 2f8cad1..752baa0 100644
--- a/chatroom.module
+++ b/chatroom.module
@@ -448,6 +448,30 @@ function chatroom_chat_update($node) {
     'imagecache_preset' => isset($node->imagecache_preset) ? $node->imagecache_preset : '',
   );
   drupal_write_record('chatroom_chat', $chat, 'nid');
+
+  // Unban any users selected to be unbanned.
+  $uids = array();
+  if (!empty($node->unban_list)) {
+    $uids = array_diff($node->unban_list, array(0));
+  }
+  if (!empty($uids)) {
+    chatroom_chat_unban_multiple($node->nid, $uids);
+  }
+}
+
+/**
+ * Unban one or more users from a chat.
+ *
+ * @param $nid
+ *   The nid of the node the users have been banned from.
+ * @param $uids
+ *   An array of user IDs.
+ */
+function chatroom_chat_unban_multiple($nid, $uids) {
+  foreach ($uids as $uid) {
+    db_query("DELETE FROM {chatroom_chat_ban_list} WHERE uid = %d", $uid);
+  }
+  cache_clear_all('chatroom_chat_ban_list_' . $nid, 'cache');
 }
 
 /**
-- 
1.7.3.4

