You are here

user_relationships.module in User Relationships 5

Same filename and directory in other branches
  1. 5.2 user_relationships.module
  2. 7 user_relationships.module

File

user_relationships.module
View source
<?php

/**
 *
 * This file contains utility functions for the User Relationships module
 * Please consult either the other files for public functions
 *
 */
foreach (array(
  'api',
  'theme',
  'hooks',
  'pages',
  'forms',
  'actions',
) as $file_to_include) {
  $modulepath = drupal_get_path('module', 'user_relationships');
  include_once "{$modulepath}/user_relationships_{$file_to_include}.inc";
}

/**
 * Invoke hook_user_relationships()
 */
function _user_relationships_invoke($type, &$relationship, $category = NULL) {
  foreach (module_list() as $module) {
    $function = $module . '_user_relationships';
    if (function_exists($function)) {
      $function($type, $relationship, $category);
    }
  }
}

/**
 * Adds autocompletion capability
 */
function _user_relationships_autocomplete_types($string = '') {
  $matches = array();
  if ($string) {
    $result = db_query_range("SELECT name FROM {user_relationship_types} WHERE LOWER(name) LIKE LOWER('%%%s%%')", $string, 0, 10);
    while ($relationship = db_fetch_object($result)) {
      $matches[$relationship->name] = check_plain($relationship->name);
    }
  }
  print drupal_to_js($matches);
  exit;
}

/*
 * Notify the user of pending relationship requests
 */
function _user_relationships_set_notifications(&$account) {
  global $user;

  //only do this for the active user
  if ($account->uid != $user->uid) {
    return;
  }
  $notifications = drupal_get_messages('status', FALSE);
  $notifications = $notifications['status'] ? $notifications['status'] : array();
  $account_relationships = user_relationships_load_all_for_user($account->uid);
  foreach ($account_relationships as $relationship) {
    if (!$relationship->approved && $account->uid != $relationship->requester_id) {
      $msg = user_relationships_get_message('pending', array(
        '!requester' => theme('username', user_load(array(
          'uid' => $relationship->requester_id,
        ))),
        '%relationship_name' => $relationship->name,
        '%relationship_plural_name' => $relationship->plural_name,
        '!pending_relationship_requests' => l(t('pending relationship requests'), 'relationships/' . $account->uid . '/requests'),
      ));
      if (!in_array($msg, $notifications)) {
        drupal_set_message($msg);
      }
    }
  }
}

/**
 * List of relationships between two users
 */
function _user_relationships_between(&$viewer, &$viewed) {
  $list = array();
  $viewer_relationships = user_relationships_load_all_for_user($viewer->uid);
  foreach ($viewer_relationships as $relationship) {
    $related = $relationship->requestee_id == $viewed->uid || ($relationship->is_oneway ? FALSE : $relationship->requester_id == $viewed->uid);
    if ($relationship->approved && $related) {
      $list[] = t('%relationship_name (!remove_link)', array(
        '%relationship_name' => $relationship->name,
        '!remove_link' => theme('user_relationships_remove_link', $viewed->uid, $relationship),
      ));
    }
  }
  return $list;
}

/**
 * List of pending relationships with between two users
 */
function _user_relationships_actions_between(&$viewer, &$viewed) {
  if ($viewer->uid == $viewed->uid) {
    return;
  }
  $list = array();
  $viewer_relationships = user_relationships_load_all_for_user($viewer->uid);
  foreach ($viewer_relationships as $relationship) {
    if (!$relationship->approved) {

      // Viewer is the requester
      if ($relationship->requestee_id == $viewed->uid) {
        $list[] = t('You have requested to be a %relationship_name of this user. (!pending_requests)', array(
          '%relationship_name' => $relationship->name,
          '!pending_requests' => l(t('pending requests'), "relationships/{$viewer->uid}/requests"),
        ));
      }
      else {
        if ($relationship->requester_id == $viewed->uid) {
          $list[] = t('This user believes that they are a %relationship_name of yours. (!pending_requests)', array(
            '%relationship_name' => $relationship->name,
            '!pending_requests' => l(t('pending requests'), "relationships/{$viewer->uid}/requests"),
          ));
        }
      }
    }
  }
  $count = 0;
  foreach ($viewer_relationships as $relationship) {
    if ($relationship->requester_id == $viewed->uid || $relationship->requestee_id == $viewed->uid) {
      $count++;
    }
  }
  $size = db_result(db_query("SELECT COUNT(*) FROM {user_relationship_types}"));
  if (variable_get('user_relationships_allow_multiple', TRUE) && $count < $size) {
    $list[] = theme('user_relationships_request_relationship_link', $viewed);
  }
  return $list;
}
function _user_relationships_current_relationships(&$requester, &$requestee) {
  $current_relationships = array();
  $requester_relationships = user_relationships_load_all_for_user($requester->uid);
  foreach ($requester_relationships as $relationship) {
    if ($relationship->requestee_id == $requestee->uid || ($relationship->is_oneway ? FALSE : $relationship->requester_id == $requestee->uid)) {
      $current_relationships[$relationship->rtid] = $relationship;
    }
  }
  return $current_relationships;
}

/*
 * Returns an array of posible actions (html) for the viewing user,
 * e.g. a link to request a relationship with the user
 */
function user_relationships_get_relationship_actions(&$viewing_user, &$viewed_user) {
  $actions = array();
  if (!user_access('maintain relationships') || !user_access('can have relationship', $viewing_user) || !user_access('can have relationship', $viewed_user) || $viewing_user->uid == $viewed_user->uid) {
    return $actions;
  }
  $relationships = user_relationships_get_relationships_for($viewing_user->uid, $viewed_user->uid);
  if (variable_get('user_relationships_require_approval', TRUE)) {
    $requests = user_relationships_get_requests($viewing_user->uid, $viewed_user->uid);
    $requestees = user_relationships_get_requestees($viewing_user->uid, $viewed_user->uid);
    if (sizeof($requests) > 0) {
      foreach ($requests as $relationship => $request) {
        $actions[] = t('You have requested to be a %relationship_name of this user. (See !your_pending_requests)', array(
          '%relationship_name' => $relationship,
          '!your_pending_requests' => l(t('your pending requests'), 'relationships/' . $viewing_user->uid . '/requests'),
        ));
      }
    }
    if (sizeof($requestees) > 0) {
      foreach ($requestees as $requestee) {
        $actions[] = t('This user believes that they are a %relationship_name of yours. (See !your_pending_requests)', array(
          '%relationship_name' => $requestee->name,
          '!your_pending_requests' => l(t('your pending requests'), 'relationships/' . $viewing_user->uid . '/requests'),
        ));
      }
    }
  }
  if (variable_get('user_relationships_allow_multiple', 1) || sizeof($relationships) == 0) {
    $count = db_result(db_query("SELECT COUNT(*) AS size FROM {user_relationship_types}"));
    if (sizeof($requests) + sizeof($relationships) < $count) {
      $actions[] = theme('user_relationships_request_relationship_link', $viewed_user);
    }
  }
  return $actions;
}

/**
 * Returns a nested array of default messages
 */
function user_relationships_default_messages() {
  return array(
    'informational' => array(
      'submitted' => 'Your request to be a %relationship_name of %requestee has been submitted.',
      'accepted' => 'You are now a %relationship_name of !requestee.',
      'denied' => "!requester's request to be a %relationship_name of !requestee has been removed.",
      'default' => 'No action has been taken.',
      'removed' => '!requester is no longer a %relationship_name of !requestee.',
      'pending' => '!requester believes they are a %relationship_name of yours.  Please view your !pending_relationship_requests to approve them.',
    ),
    'error' => array(
      'too_many_relationships' => 'You already relate to this user in every possible way.',
      'existing_request' => 'You have already requested to be a %relationship_name of %requestee',
      'existing_relationship' => 'You are already a %relationship_name of %requestee',
      'not_accepting_requests' => 'This user is not accepting relationship requests.',
      'self_request' => 'You are not allowed to create a relationship to yourself.',
      'non_existant_user' => 'This user does not exist.',
    ),
  );
}

/**
 * Recursively search an array for a key and return the value attached to it
 */
function _user_relationships_get_from_array($needle, &$haystack) {
  foreach ($haystack as $key => $value) {
    if ($key == $needle) {
      return $value;
    }
    else {
      if (is_array($value)) {
        if ($msg = _user_relationships_get_from_array($needle, $value)) {
          return $msg;
        }
      }
    }
  }
}

/**
 * Helper function to build the settings form for the notification messages
 */
function _user_relationships_message_settings_form(&$form, $defaults = array()) {
  foreach ($defaults as $key => $value) {
    if (is_array($value)) {
      $form[$key] = array(
        '#type' => 'fieldset',
        '#title' => ucfirst(str_replace('_', ' ', $key)),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      _user_relationships_message_settings_form($form[$key], $value);
    }
    else {
      $form["user_relationships_msg_{$key}"] = array(
        '#type' => 'textfield',
        '#title' => ucfirst(str_replace('_', ' ', $key)),
        '#default_value' => variable_get("user_relationships_msg_{$key}", $value),
      );
    }
  }
}

Functions

Namesort descending Description
user_relationships_default_messages Returns a nested array of default messages
user_relationships_get_relationship_actions
_user_relationships_actions_between List of pending relationships with between two users
_user_relationships_autocomplete_types Adds autocompletion capability
_user_relationships_between List of relationships between two users
_user_relationships_current_relationships
_user_relationships_get_from_array Recursively search an array for a key and return the value attached to it
_user_relationships_invoke Invoke hook_user_relationships()
_user_relationships_message_settings_form Helper function to build the settings form for the notification messages
_user_relationships_set_notifications