From 47d3a816492699fb02eefb75f375258a9ed2e1a8 Mon Sep 17 00:00:00 2001
From: Marco Antonio Villegas Vega <marvil07@gmail.com>
Date: Sun, 14 Jun 2009 23:05:59 -0500
Subject: [PATCH] Reworked og_username_helper including:

- 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
---
 og.module             |   41 ++++++++++++++++++++++++++++++++++++++++-
 og_username_helper.js |   28 ++++++++++++++++++++++++++++
 theme/og.css          |    9 +++++++++
 3 files changed, 77 insertions(+), 1 deletions(-)
 create mode 100644 og_username_helper.js

diff --git a/og.module b/og.module
index 6614f15..a844e76 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;
 }
 
@@ -439,7 +453,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');
 }
 
 /**
@@ -1326,6 +1340,31 @@ 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,
+      '#prefix' => '<div class="container-inline">',
+      '#access' => $helper_access,
+    );
+    $form['og_username_helper_add_button'] = array(
+      '#type' => 'button',
+      '#value' => t('Add'),
+      '#button_type' => 'add',
+      '#weight' => -1,
+      '#suffix' => '</div>',
+      '#access' => $helper_access,
+    );
+  }
+
 }
 
 /**
diff --git a/og_username_helper.js b/og_username_helper.js
new file mode 100644
index 0000000..b8bae10
--- /dev/null
+++ b/og_username_helper.js
@@ -0,0 +1,28 @@
+/* Add usernames to the one of the fields supported */
+
+Drupal.og_username_helperAutoAttach = function() {
+  $("#edit-og-username-helper-add-button").click(function() {
+    $("#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);
+}
diff --git a/theme/og.css b/theme/og.css
index bf24ac5..f67da50 100644
--- a/theme/og.css
+++ b/theme/og.css
@@ -55,3 +55,12 @@
   font-style: italic;
 }
 
+/* og username helper: let the description appear in the correct position */
+#og-invite-form .container-inline .description, #og-add-users .container-inline .description {
+  float: left;
+}
+
+#og-invite-form .form-item label, #og-add-users .form-item label {
+  clear: left;
+}
+
-- 
1.5.6.5

