You are here

user_relationship_invites.module in User Relationships 6

Same filename and directory in other branches
  1. 7 user_relationship_invites/user_relationship_invites.module

Drupal Module: User Relationship Invites

@author Jeff Smick <sprsquish [at] gmail [dot] com> @author Jeff Warrington <jeff [at] qoolio [dot] org (Drupal 6 port)>

Automatically create a relationship between inviter and invitee

File

user_relationship_invites/user_relationship_invites.module
View source
<?php

/**
 * Drupal Module: User Relationship Invites
 *
 * @author Jeff Smick <sprsquish [at] gmail [dot] com>
 * @author Jeff Warrington <jeff [at] qoolio [dot] org (Drupal 6 port)>
 * @file
 *   Automatically create a relationship between inviter and invitee
 */

/**
 * Public API to grab the basic invite info
 *
 * @param $code
 *    string of the invite code
 *
 * @return
 *    object with the invite data including the inviter user object and relationship_type object
*/
function user_relationship_invites_get_invite($code) {
  if ($invite = db_fetch_object(db_query("SELECT * FROM {user_relationship_invites} WHERE invite_code = '%s'", $code))) {
    $invite->inviter = user_load(array(
      'uid' => $invite->inviter_uid,
    ));
    $invite->relationship_type = user_relationships_type_load($invite->rtid);
  }
  return $invite;
}

/**
 * Implementation of hook_form_alter().
*/
function user_relationship_invites_form_alter(&$form, $form_state, $form_id) {
  if (!(module_exists('invite') && module_exists('user_relationships_api') && module_exists('user_relationships_ui'))) {
    return;
  }
  if ($form_id == 'invite_form') {
    global $user;
    $new_user = drupal_anonymous_user();
    module_load_include('inc', 'user_relationships_ui', 'user_relationships_ui.forms');
    $form += user_relationships_ui_request_form($user, $new_user, $form_state['values']);
    $form['rtid']['#weight'] = 0;
    $form['#validate'][] = 'user_relationship_invites_invite_form_validate';
  }
}

/**
 * Custom form validation handler for the 'invite_form' form from Invite module
 * This custom handler acts to store the User Relationships relationship type ID
 * so that it can be used in the hook_invite implementation as it is not otherwise available.
 */
function user_relationship_invites_invite_form_validate($form, &$form_state) {
  global $user;
  $user->rtid = $form_state['values']['rtid'];
}

/**
 * Implementation of hook_invite().
*/
function user_relationship_invites_invite($op, $args) {
  switch ($op) {
    case 'invite':
      db_query("INSERT INTO {user_relationship_invites} (inviter_uid, rtid, invite_code) VALUES (%d, %d, '%s')", $args['inviter']->uid, $args['inviter']->rtid, $args['code']);
      break;
  }
}

/**
 * Implementation of hook_user().
*/
function user_relationship_invites_user($op, &$edit, &$account, $category = NULL) {
  if (!(module_exists('invite') && module_exists('user_relationships_api'))) {
    return;
  }
  switch ($op) {
    case 'register':
      if (($code = $_SESSION[INVITE_SESSION]) && ($invite = user_relationship_invites_get_invite($code)) && $invite->relationship_type) {
        $form = array();
        $inviter = $invite->inviter;
        $relationship_type = $invite->relationship_type;

        // approval is required so ask for it
        if (!$relationship_type->is_oneway || $relationship_type->requires_approval) {
          $form['relationship_invite_approve'] = array(
            '#type' => 'radios',
            '#title' => t('Please confirm the %relationship_name request from !name', array(
              '!name' => theme('username', $inviter),
              '%relationship_name' => $relationship_type->name,
            )),
            '#default_value' => isset($edit['relationship_invite_approve']) ? $edit['relationship_invite_approve'] : 'approve',
            '#options' => array(
              'approve' => t('Yes'),
              'disapprove' => t('No'),
            ),
          );
        }
        else {
          $form['relationship_invite_approve'] = array(
            '#type' => 'value',
            '#value' => 'approve',
          );
        }
        $form['invite_code'] = array(
          '#type' => 'value',
          '#value' => $code,
        );
        $form['relationship_invite_requester'] = array(
          '#type' => 'value',
          '#value' => $inviter,
        );
        $form['relationship_type'] = array(
          '#type' => 'value',
          '#value' => $relationship_type,
        );
        return $form;
      }
      break;
    case 'insert':
      if (isset($edit['relationship_invite_approve'])) {
        db_query("DELETE FROM {user_relationship_invites} WHERE invite_code = '%s'", $edit['invite_code']);
        if ($edit['relationship_invite_approve'] == 'approve') {
          user_relationships_request_relationship($edit['relationship_invite_requester'], $account, $edit['relationship_type'], TRUE);
        }
        $edit['invite_code'] = NULL;
        $edit['relationship_invite_approve'] = NULL;
        $edit['relationship_invite_requester'] = NULL;
        $edit['relationship_type'] = NULL;
      }
      break;
  }
}

Functions

Namesort descending Description
user_relationship_invites_form_alter Implementation of hook_form_alter().
user_relationship_invites_get_invite Public API to grab the basic invite info
user_relationship_invites_invite Implementation of hook_invite().
user_relationship_invites_invite_form_validate Custom form validation handler for the 'invite_form' form from Invite module This custom handler acts to store the User Relationships relationship type ID so that it can be used in the hook_invite implementation as it is not otherwise available.
user_relationship_invites_user Implementation of hook_user().