From 23ca53371682af96b25ce3eb9440b55c6f947a3a Mon Sep 17 00:00:00 2001
From: Timo Tiuraniemi <timo.tiuraniemi@iki.fi>
Date: Thu, 31 May 2012 09:24:31 +0300
Subject: [PATCH] Added possibility to assign roles per invite

---
 invite.module |   41 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 41 insertions(+), 0 deletions(-)

diff --git a/invite.module b/invite.module
index e2f0d01..785f68a 100644
--- a/invite.module
+++ b/invite.module
@@ -99,6 +99,10 @@ function invite_permission() {
       'title' => t('Send mass invitations'),
       'description' => t('Send invitations to multiple recipients.'),
     ),
+    'assign invitation roles' => array(
+  	  'title' => t('Assign roles to invitations'),
+  	  'description' => t('Assign roles to invitations.'),
+    ),
     'track invitations' => array(
       'title' => t('Track invitations'),
       'description' => t('Track sent invitations.'),
@@ -775,6 +779,14 @@ function invite_target_roles($invite, $account) {
       $targets[$target] = $target;
     }
   }
+  // Map to explicitly set roles, if any
+  $result = db_query("SELECT DISTINCT r.rid FROM {invite_roles} ir " 
+                     ."INNER JOIN {role} r ON (ir.rid = r.rid) "
+                     ."INNER JOIN {invite} i ON (ir.iid = i.iid) " 
+                     ."WHERE i.iid = :iid", array(':iid' => $invite->iid));
+  foreach($result as $role){
+    $targets[$role->rid] = $role->rid;
+  }
 
   invite_invoke_all('invite_target_roles', $targets, $invite, $account);
 
@@ -1068,6 +1080,27 @@ function invite_page_form($remaining_invites, $edit = array()) {
       '#value' => $edit['email'],
     );
   }
+  
+  // Specify roles if permitted
+  if (!$edit && user_access('assign invitation roles')){
+
+    $selected_roles = array();    
+    $roles = user_roles(TRUE);
+    
+    // Need to unset all the target roles that are assigned automatically:
+    // the default and role specific. 
+    unset($roles[variable_get('invite_target_role_default', DRUPAL_AUTHENTICATED_RID)]);
+    foreach($user->roles as $rid => $role) {
+      unset($roles[variable_get('invite_target_role_' . $rid, DRUPAL_AUTHENTICATED_RID)]);
+    }
+    $form['invite_roles'] = array(
+        '#type' => 'checkboxes',
+        '#title' => t('Roles'),
+        '#default_value' => $selected_roles,
+        '#options' => $roles,
+        '#disabled' => FALSE,
+    );
+  }
 
   // Message subject.
   if ($edit && !empty($edit['data']['subject'])) {
@@ -1290,6 +1323,14 @@ function invite_form_submit($form, &$form_state) {
 
     if (invite_send($invite, !$show_only)) {
       $num_succeeded++;
+      // Update the role mappings
+      $result = db_query('DELETE FROM {invite_roles} WHERE iid = :iid', array(':iid' => $invite->iid));
+      if (isset($form_state['values']['invite_roles'])) {
+        $active_roles = array_filter($form_state['values']['invite_roles']);
+        foreach($active_roles as $rid => $role) {
+          $result = db_query('INSERT INTO {invite_roles} (iid, rid) VALUES (:iid, :rid)', array(':iid' => $invite->iid, ':rid' => $rid));
+        }
+      }
       $processed_invites[] = $invite;
     }
     else {
-- 
1.7.3.4

