From 4bfd21badd2ba5a1abf5e6185bd013711fe88781 Mon Sep 17 00:00:00 2001
From: mrded <mrded@556088.no-reply.drupal.org>
Date: Wed, 16 Oct 2013 13:59:57 +0100
Subject: [PATCH] Upgrade user_relationship_invites to work with Invite 4.x
 branch

---
 .../user_relationship_invites.module               | 65 +++++++++++++++++++---
 1 file changed, 57 insertions(+), 8 deletions(-)

diff --git a/user_relationship_invites/user_relationship_invites.module b/user_relationship_invites/user_relationship_invites.module
index 48049fa..79661e2 100644
--- a/user_relationship_invites/user_relationship_invites.module
+++ b/user_relationship_invites/user_relationship_invites.module
@@ -5,6 +5,8 @@
  *
  * @author Jeff Smick <sprsquish [at] gmail [dot] com>
  * @author Jeff Warrington <jeff [at] qoolio [dot] org (Drupal 6 port)>
+ * @author Chris Muktar <chrism2671 chris [at] wikijob [dot] co [dot] uk> Upgrade D7 to support Invite 4.x
+ * Invite 4.x limitation - currently applies to all invite types.
  * @file
  *   Automatically create a relationship between inviter and invitee
  */
@@ -66,14 +68,30 @@ function user_relationship_invites_invite_form_validate($form, &$form_state) {
   global $user;
   if (!empty($form_state['values']['rtid']) && $form_state['values']['ur_request']) {
     $user->rtid = $form_state['values']['rtid'];
-    $emails = _invite_get_emails($form_state['values']['email']);
-    if ($failed_emails = _invite_filter_registered_emails($emails)) {
-      foreach ($failed_emails as $email) {
-        $account = user_load_by_mail($email);
-        if ($actions = user_relationships_ui_actions_between($user, $account, array('add' => 1, 'requested' => 1, 'received' => 1))) {
-          drupal_set_message(theme('item_list', array('items' => $actions)), 'error');
+
+    switch (_user_relationship_invites_version()) {
+      case 2:
+        $emails = _invite_get_emails($form_state['values']['email']);
+        if ($failed_emails = _invite_filter_registered_emails($emails)) {
+          foreach ($failed_emails as $email) {
+            $account = user_load_by_mail($email);
+            if ($actions = user_relationships_ui_actions_between($user, $account, array('add' => 1, 'requested' => 1, 'received' => 1))) {
+              drupal_set_message(theme('item_list', array('items' => $actions)), 'error');
+            }
+          }
         }
-      }
+        break;
+
+      case 4:
+        if ($form_state['invite']->type == 'invite_by_email') {
+          $invitee = user_load_by_mail($form_state['values']['field_invitation_email_address']['und'][0]['value']);
+
+          if ($invitee && $invitee->status == 1) {
+            // This is an existing, non-blocked user, identified by email address. Create relationship request.
+            user_relationships_request_relationship($user, $invitee, $form_state['values']['rtid'], FALSE);
+          }
+        }
+        break;
     }
   }
   else {
@@ -103,7 +121,11 @@ function user_relationship_invites_invite_send($invite) {
  * Implements hook_user().
  */
 function user_relationship_invites_form_user_register_form_alter(&$form, &$form_state) {
-  if (!empty($_SESSION[INVITE_SESSION]) && ($code = $_SESSION[INVITE_SESSION]) && ($invite = user_relationship_invites_get_invite($code)) && $invite->relationship_type) {
+  $session_var = (_user_relationship_invites_version() == 2)
+    ? INVITE_SESSION
+    : INVITE_SESSION_CODE;
+
+  if (!empty($_SESSION[$session_var]) && ($code = $_SESSION[$session_var]) && ($invite = user_relationship_invites_get_invite($code)) && $invite->relationship_type) {
 
     $inviter = $invite->inviter;
     $relationship_type = $invite->relationship_type;
@@ -156,3 +178,30 @@ function user_relationship_invites_user_insert(&$edit, $account, $category) {
     }
   }
 }
+
+/**
+ * Get invite module version.
+ *
+ * @return integer
+ *  - Version of invite module.
+ */
+function _user_relationship_invites_version() {
+  $invite_module_info = system_get_info('module', 'invite');
+  $version = substr($invite_module_info['version'], 4, 1);
+  return $version;
+}
+
+/**
+ * Implements hook_invite_accept().
+ */
+function user_relationship_invites_invite_accept($invite) {
+  if ($ur_invite = user_relationship_invites_get_invite($invite->reg_code)) {
+    db_delete('user_relationship_invites')
+      ->condition('invite_code', $invite->reg_code)
+      ->execute();
+    $invitee = user_load($invite->invitee);
+    if (user_relationships_can_receive($invitee, $ur_invite->relationship_type)) {
+      user_relationships_request_relationship($invite->uid, $invitee, $ur_invite->relationship_type, TRUE);
+    }
+  }
+}
-- 
1.7.12.4 (Apple Git-37)

