From 366b604ccb149afee1e5c7f22d9f59b1f65a43bb Mon Sep 17 00:00:00 2001
From: Marco Antonio Villegas Vega <marco.villegas@pucp.edu.pe>
Date: Fri, 8 Aug 2008 14:13:43 -0500
Subject: [PATCH] integrate og_username_helper inside OG-7.3


diff --git a/og.css b/og.css
index c9ff7ba..4bc4b70 100644
--- a/og.css
+++ b/og.css
@@ -50,4 +50,9 @@ td.og-picture-grid-item .group-role {
   font-style: italic;
 }
 
-
+.og-addlink {
+  color: #2763a5;
+  text-decoration: underline;
+  cursor: pointer;
+  float: right;
+}
diff --git a/og.module b/og.module
index 5713733..c179d5f 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($section) {
   switch ($section) {
     case !empty($section) && strstr($section, 'admin/build/block/configure/og'):
@@ -72,6 +76,12 @@ function og_menu($may_cache) {
       'access' => user_access('administer site configuration')
       );
     $items[] = array('path' => 'admin/og/og', 'callback' => 'drupal_get_form', 'callback arguments' => array('og_admin_settings'), 'title' => t('Organic groups configuration'), 'description' => t('Configure the main Organic groups module (og)'), 'weight' => -5,);
+    $items[] = array(
+      'path' => 'og/username_helper/autocomplete',
+      'callback' => 'og_username_helper_autocomplete',
+      'access' => user_access('access og username helper'),
+      'type' => MENU_CALLBACK
+    );
   }
   else {
     drupal_add_css(drupal_get_path('module', 'og'). '/og.css');
@@ -194,7 +204,7 @@ function og_set_locale() {
  * Implementation of hook_perm().
  */
 function og_perm() {
-  return array('administer organic groups');
+  return array('administer organic groups', 'access og username helper');
 }
 
 /**
@@ -1555,6 +1565,29 @@ function og_form_alter($form_id, &$form) {
       '#description' => t('Specify how organic groups should treat nodes of this type. Nodes may behave as a group, as group posts, or may not participate in organic groups at all.'),
     );
   }
+
+  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',
+      '#size' => 40,
+      '#maxlength' => 60,
+      '#autocomplete_path' => 'og/username_helper/autocomplete',
+      '#weight' => 2,
+      '#description' => t('Let you add a username to the following box.')
+    );
+  }
 }
 
 // Rebuild node access if Usage has changed
@@ -3022,3 +3055,14 @@ function og_url_group($token, $argument, $arg) {
   }
   return FALSE;
 }
+
+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..0340af7
--- /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() != '' ) {
+      var coma=',';
+      if ( $("#edit-mails").val() != null) {
+        if ( $("#edit-mails").val() == '')
+          coma='';
+          $("#edit-mails").val($("#edit-mails").val()+coma+$("#edit-og-username-helper-add").val());
+      } else if ( $("#edit-og-names").val() != null ) {
+        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

