From b6974710f2d391c134e4e32bac8efc66afb83a97 Mon Sep 17 00:00:00 2001
From: Marco Antonio Villegas Vega <marco.villegas@pucp.edu.pe>
Date: Fri, 8 Aug 2008 13:49:18 -0500
Subject: [PATCH] integrate og_username_helper to OG 6.x HEAD


diff --git a/og.css b/og.css
index fe18fa8..669a59d 100644
--- a/og.css
+++ b/og.css
@@ -55,3 +55,9 @@
   font-style: italic;
 }
 
+.og-addlink {
+  color: #2763a5;
+  text-decoration: underline;
+  cursor: pointer;
+  float: right;
+}
diff --git a/og.module b/og.module
index 9c56eb6..ddafc92 100644
--- a/og.module
+++ b/og.module
@@ -24,6 +24,10 @@ define('OG_NOTIFICATION_NEVER', 0);
 define('OG_NOTIFICATION_ALWAYS', 1);
 define('OG_NOTIFICATION_SELECTIVE', 2);
 
+// 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) {
@@ -163,6 +167,14 @@ function og_menu() {
       'weight' => 7
     );
 
+  // username helper autocomplete
+  $items['og/username_helper/autocomplete'] = array(
+    'title' => 'Autocomplete username',
+    'page callback' => 'og_username_helper_autocomplete',
+    'access arguments' => array('access og username helper'),
+    'type' => MENU_CALLBACK,
+  );
+
   return $items;
 }
 
@@ -369,7 +381,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');
 }
 
 /**
@@ -1675,6 +1687,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');
+    $form['og_username_helper'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Username helper'),
+      '#tree' => TRUE,
+      '#weight' => -1,
+    );
+    $form['og_username_helper']['text'] = array(
+      '#type' => 'item',
+      '#value' => '<span id="addusername" class="og-addlink">'. t('Add') .'</span>',
+      '#weight' => 1,
+    );
+    $form['og_username_helper']['add'] = array(
+      '#type' => 'textfield',
+      '#description' => t('Let you add a username to the following box.'),
+      '#autocomplete_path' => 'og/username_helper/autocomplete',
+      '#size' => 40,
+      '#maxlength' => 60,
+      '#weight' => 2,
+    );
+  }
+
 }
 
 function og_form_node_type_form_alter(&$form, &$form_state) {
@@ -2910,3 +2947,17 @@ function og_get_token($nid) {
 function og_check_token($token, $seed) {
   return drupal_get_token($seed) == $token;
 }
+
+/**
+ * Get a list of usernames that starts with $string
+ */
+function og_username_helper_autocomplete($string = '') {
+  $matches = array();
+  if ($string) {
+    $result = db_query_range("SELECT name FROM {users} WHERE LOWER(name) LIKE LOWER('%s%%') AND uid>1", $string, 0, 10);
+    while ($user = db_fetch_object($result)) {
+      $matches[$user->name] = check_plain($user->name);
+    }
+  }
+  print drupal_to_js($matches);
+}
diff --git a/og_username_helper.js b/og_username_helper.js
new file mode 100644
index 0000000..782ab65
--- /dev/null
+++ b/og_username_helper.js
@@ -0,0 +1,22 @@
+/* Add usernames to the one of the fields supported */
+Drupal.og_username_helperAutoAttach = function() {
+  $("#addusername").click(function() {
+    if ( $(this).val() != undefined ) {
+      var coma=',';
+      if ( $("#edit-mails").val() != undefined) {
+        if ( $("#edit-mails").val() == '')
+          coma='';
+          $("#edit-mails").val($("#edit-mails").val()+coma+$("#edit-og-username-helper-add").val());
+      } else if ( $("#edit-og-names").val() != undefined ) {
+        if ($("#edit-og-names").val()=='')
+          coma='';
+          $("#edit-og-names").val($("#edit-og-names").val()+coma+$("#edit-og-username-helper-add").val());
+      }
+      $("#edit-og-username-helper-add").val('');
+    }
+  });
+}
+
+if (Drupal.jsEnabled) {
+  $(document).ready(Drupal.og_username_helperAutoAttach);
+}
-- 
1.5.6.3

