You are here

user_relationship_invites.module in User Relationships 5.2

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

Drupal Module: User Relationship Invites

@author: Jeff Smick <sprsquish [at] gmail [dot] com> @author: Alex Karshakevich (maintainer) http://drupal.org/user/183217 @author: scottgifford (contributor) http://drupal.org/user/245699

Automatically create a relationship between inviter and invitee

File

plugins/user_relationship_invites/user_relationship_invites.module
View source
<?php

/**
 * Drupal Module: User Relationship Invites
 *
 * @author: Jeff Smick <sprsquish [at] gmail [dot] com>
 * @author: Alex Karshakevich (maintainer) http://drupal.org/user/183217
 * @author: scottgifford (contributor) http://drupal.org/user/245699
 * @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;
}

/**
 * Implements hook_form_alter()
*/
function user_relationship_invites_form_alter($form_id, &$form) {
  if (!(module_exists('invite') && module_exists('user_relationships'))) {
    return;
  }
  global $form_values;
  global $user;
  switch ($form_id) {
    case 'user_register':
      if (($code = arg(2)) && ($invite = user_relationship_invites_get_invite($code))) {
        $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('Are you a %relationship_name of !name?', array(
              '!name' => theme('username', $inviter),
              '%relationship_name' => $relationship_type->name,
            )),
            '#default_value' => isset($form_values['relationship_invite_approve']) ? $form_values['relationship_invite_approve'] : 'approve',
            '#options' => array(
              'approve' => t('Yes'),
              'disapprove' => t('No'),
            ),
          );
        }
        else {
          $form['relationship_invite_approve'] = array(
            '#type' => 'value',
            '#value' => 'approve',
          );
        }
        $form['relationship_invite_requester'] = array(
          '#type' => 'value',
          '#value' => $inviter,
        );
        $form['relationship_type'] = array(
          '#type' => 'value',
          '#value' => $relationship_type,
        );
        $form['invite_code'] = array(
          '#type' => 'value',
          '#value' => $code,
        );
      }
      break;
    case 'invite_form':
      $user = user_load(array(
        'uid' => $user->uid,
      ));
      $new_user = drupal_anonymous_user();
      $form += user_relationships_request_form($user, $new_user, $form_values);
      break;
  }
}

/**
 * Implements hook_invite()
*/
function user_relationship_invites_invite($action, &$args) {
  global $form_values;
  switch ($action) {
    case 'invite':
      if ($form_values['rtid']) {
        db_query("INSERT INTO {user_relationship_invites} (inviter_uid, rtid, invite_code) VALUES (%d, %d, '%s')", $args['inviter']->uid, $form_values['rtid'], $args['code']);
      }
      break;
    case 'cancel':
      if (arg(3)) {
        db_query("DELETE FROM {user_relationship_invites} WHERE invite_code = '%s'", arg(3));
      }
      break;
  }
}

/**
 * Implements hook_user()
*/
function user_relationship_invites_user($type, &$edit, &$account, $category = NULL) {
  if (!(module_exists('invite') && module_exists('user_relationships'))) {
    return;
  }
  switch ($type) {
    case 'insert':
      if (isset($edit['relationship_invite_approve']) && isset($edit['invite_code'])) {
        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);
        }
      }
      break;
  }
}

/**
 * #542450 create the special tokens invite-if-rel-start and invite-if-rel-end which can be used 
 * to hide all text between them if there is no relationship when used along with the patch at http://drupal.org/node/542436.
 */
function user_relationship_invites_token_values($type = 'all', $object = NULL) {
  $values = array();
  if ($type == 'invite' && is_object($object)) {
    if (($rtid = $object->form['rtid']) && ($rel = user_relationships_type_load($rtid))) {
      $values['invite-rel-rtid'] = $rtid;
      $values['invite-rel-name'] = $rel->name;
      $values['invite-rel-plural-name'] = $rel->plural_name;
      $values['invite-if-rel-start'] = '';
      $values['invite-if-rel-end'] = '';
    }
    else {
      $values['invite-rel-rtid'] = $values['invite-rel-name'] = $values['invite-rel-plural-name'] = '';
      $values['invite-if-rel-start'] = '**HIDEME**';
      $values['invite-if-rel-end'] = '**ENDHIDE**';
    }
  }
  return $values;
}

/**
 * #542450 create the special tokens invite-if-rel-start and invite-if-rel-end which can be used 
 * to hide all text between them if there is no relationship when used along with the patch at http://drupal.org/node/542436.
 */
function user_relationship_invites_token_list($type = 'all') {
  if ($type == 'invite' || $type == 'all') {
    $tokens['invite']['invite-rel'] = t('ID of requested relationship');
    $tokens['invite']['invite-rel-name'] = t('Name of requested relationship');
    $tokens['invite']['invite-rel-plural-name'] = t('Plural name of requested relationship');
    $tokens['invite']['invite-if-rel-start'] = t('Hide if no relationship');
    $tokens['invite']['invite-if-rel-end'] = t('Unhide if no relationship');
    return $tokens;
  }
}

Functions

Namesort descending Description
user_relationship_invites_form_alter Implements hook_form_alter()
user_relationship_invites_get_invite Public API to grab the basic invite info
user_relationship_invites_invite Implements hook_invite()
user_relationship_invites_token_list #542450 create the special tokens invite-if-rel-start and invite-if-rel-end which can be used to hide all text between them if there is no relationship when used along with the patch at http://drupal.org/node/542436.
user_relationship_invites_token_values #542450 create the special tokens invite-if-rel-start and invite-if-rel-end which can be used to hide all text between them if there is no relationship when used along with the patch at http://drupal.org/node/542436.
user_relationship_invites_user Implements hook_user()