You are here

function _privatemsg_parse_userstring in Privatemsg 6.2

Same name and namespace in other branches
  1. 6 privatemsg.module \_privatemsg_parse_userstring()
  2. 7.2 privatemsg.module \_privatemsg_parse_userstring()
  3. 7 privatemsg.module \_privatemsg_parse_userstring()

Extract the valid usernames of a string and loads them.

This function is used to parse a string supplied by a username autocomplete field and load all user objects.

Parameters

$string: A string in the form "usernameA, usernameB, ...".

Return value

Array, first element is an array of loaded user objects, second an array with invalid names.

7 calls to _privatemsg_parse_userstring()
pm_block_user_block_validate in pm_block_user/pm_block_user.pages.inc
Validate user names.
privatemsg_filter_dropdown_submit in privatemsg_filter/privatemsg_filter.module
privatemsg_filter_get_filter in privatemsg_filter/privatemsg_filter.module
privatemsg_forward_form_validate in privatemsg_forward/privatemsg_forward.module
Validate function for forward form.
privatemsg_new in ./privatemsg.pages.inc
Form builder function; Write a new private message.

... See full list

File

./privatemsg.module, line 913
Allows users to send private messages to other users.

Code

function _privatemsg_parse_userstring($input, $types_limitations = array()) {
  if (is_string($input)) {
    $input = explode(',', $input);
  }

  // Start working through the input array.
  $invalid = array();
  $recipients = array();
  $duplicates = array();
  $denieds = array();
  foreach ($input as $string) {
    $string = trim($string);

    // Ignore spaces.
    if (!empty($string)) {

      // First, collect all matches.
      $matches = array();

      // Remember if a possible match denies access.
      $access_denied = FALSE;

      // Load recipient types.
      $types = privatemsg_recipient_get_types();

      // Collect matches from hook implementations.
      foreach (module_implements('privatemsg_name_lookup') as $module) {
        $function = $module . '_privatemsg_name_lookup';
        $return = $function($string);
        if (isset($return) && is_array($return)) {
          foreach ($return as $recipient) {

            // Save recipients under their key to merge recipients which were
            // loaded multiple times.
            if (empty($recipient->type)) {
              $recipient->type = 'user';
              $recipient->recipient = $recipient->uid;
            }
            $matches[privatemsg_recipient_key($recipient)] = $recipient;
          }
        }
      }
      foreach ($matches as $key => $recipient) {

        // Check permissions, remove any recipients the user doesn't have write
        // access for.
        if (!privatemsg_recipient_access($recipient->type, 'write', $recipient)) {
          unset($matches[$key]);
          $access_denied = TRUE;
        }

        // Appliy limitations.
        if (!empty($types_limitations) && !in_array($recipient->type, $types_limitations)) {
          unset($matches[$key]);
        }
      }

      // Allow modules to alter the found matches.
      drupal_alter('privatemsg_name_lookup_matches', $matches, $string);

      // Check if there are any matches.
      $number_of_matches = count($matches);
      switch ($number_of_matches) {
        case 1:

          // Only a single match found, add to recipients.
          $recipients += $matches;
          break;
        case 0:

          // No match found, check if access was denied.
          if ($access_denied) {

            // There were possible matches, but access was denied.
            $denieds[$string] = $string;
          }
          else {

            // The string does not contain any valid recipients.
            $invalid[$string] = $string;
          }
          break;
        default:

          // Multiple matches were found. The user has to specify which one he
          // meant.
          $duplicates[$string] = $matches;
          break;
      }
    }
  }

  // Todo: Provide better API.
  return array(
    $recipients,
    $invalid,
    $duplicates,
    $denieds,
  );
}