From 5229aea70d73aaaf005a275fefe92ddb3b638dec Mon Sep 17 00:00:00 2001
From: Marco Antonio Villegas Vega <marvil07@gmail.com>
Date: Wed, 2 Dec 2009 11:58:42 -0500
Subject: [PATCH] integrate og_username_helper to OG

this patch includes:
- Salimane Adjao Moustapha work
  - form alter in drupal way for access
  - don't add a username if already in the textarea
  - don't add if no input in autocomplete textbox
  - trim all usernames before adding them to the textarea
- fix for IE:
  - use jquery inArray instead of indexOf. IE have problems with indexOf, so avoid
it.
- pass coder review :D
- avoid define for permission string

use keyup event instead of keypress and avoid button

let us avoid adding start portion of the name when using the keyboard to
select

no more need to:
- use a button, since it's not really necessary
- change css(now there's no button)
---
 og.module             |   32 +++++++++++++++++++++++++++++++-
 og_username_helper.js |   32 ++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 1 deletions(-)
 create mode 100644 og_username_helper.js

diff --git a/og.module b/og.module
index 1a364ae..590864b 100644
--- a/og.module
+++ b/og.module
@@ -25,6 +25,10 @@ define('OG_DELETE_ORPHANS', 1);
 define('OG_DELETE_MOVE_NODES', 2);
 define('OG_DELETE_MOVE_NODES_MEMBERSHIPS', 3);
 
+// og forms where username helper is shown
+define('OG_INVITE_FORM', 'og-invite-form');
+define('OG_ADD_USERS', 'og-add-users');
+
 function og_help($path, $arg) {
   if ($path) {
     switch ($path) {
@@ -174,6 +178,16 @@ function og_menu() {
     'weight' => 7
   );
 
+  // username helper autocomplete
+  $items['og/username_helper/autocomplete'] = array(
+    'title' => 'Autocomplete username',
+    'page callback' => 'user_autocomplete',
+    'access arguments' => array('access og username helper'),
+    'type' => MENU_CALLBACK,
+    'file' => 'user.pages.inc',
+    'file path' => drupal_get_path('module', 'user'),
+  );
+
   return $items;
 }
 
@@ -442,7 +456,7 @@ function og_set_language($node) {
  * Implementation of hook_perm().
  */
 function og_perm() {
-  return array('administer organic groups');
+  return array('administer organic groups', 'access og username helper');
 }
 
 /**
@@ -1359,6 +1373,22 @@ function og_form_alter(&$form, &$form_state, $form_id) {
       og_form_add_og_audience($form, $form_state);
     }
   }
+
+  // Add username helper
+  if ($form['#id'] == OG_INVITE_FORM || $form['#id'] == OG_ADD_USERS) {
+    drupal_add_js(drupal_get_path('module', 'og') .'/og_username_helper.js');
+    $helper_access = user_access('access og username helper');
+    $form['og_username_helper_text'] = array(
+      '#type' => 'textfield',
+      '#description' => t('Lookup a username and add it to the list below.'),
+      '#autocomplete_path' => 'og/username_helper/autocomplete',
+      '#size' => 40,
+      '#maxlength' => 60,
+      '#weight' => -2,
+      '#access' => $helper_access,
+    );
+  }
+
 }
 
 /**
diff --git a/og_username_helper.js b/og_username_helper.js
new file mode 100644
index 0000000..f5b7da5
--- /dev/null
+++ b/og_username_helper.js
@@ -0,0 +1,32 @@
+/* Add usernames to the one of the fields supported */
+
+Drupal.og_username_helperAutoAttach = function() {
+  $("#edit-og-username-helper-text").keyup(function(e) {
+    /* we only take action on enter */
+    if (e.which != 13) {
+      return true;
+    }
+    $("#edit-og-username-helper-text").val($.trim($("#edit-og-username-helper-text").val()));
+    if ($("#edit-og-username-helper-text").val() != undefined && $("#edit-og-username-helper-text").val() != '') {
+      var coma=',';
+      if ( $("#edit-mails").val() != undefined) {
+        if ( $("#edit-mails").val() == '')
+          coma='';
+        if ( $.inArray($("#edit-og-username-helper-text").val(), $("#edit-mails").val().split(",")) < 0)
+          $("#edit-mails").val($("#edit-mails").val()+coma+$("#edit-og-username-helper-text").val());
+      }
+      else if ( $("#edit-og-names").val() != undefined ) {
+        if ($("#edit-og-names").val()=='')
+          coma='';
+        if ( $.inArray($("#edit-og-username-helper-text").val(), $("#edit-og-names").val().split(",")) < 0)
+          $("#edit-og-names").val($("#edit-og-names").val()+coma+$("#edit-og-username-helper-text").val());
+      }
+    }
+    $("#edit-og-username-helper-text").val('');
+    return false;
+  });
+}
+
+if (Drupal.jsEnabled) {
+  $(document).ready(Drupal.og_username_helperAutoAttach);
+}
-- 
1.6.5

