function _privatemsg_parse_userstring in Privatemsg 7
Same name and namespace in other branches
- 6.2 privatemsg.module \_privatemsg_parse_userstring()
- 6 privatemsg.module \_privatemsg_parse_userstring()
- 7.2 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
$type Array of recipient types this should be limited to.
Array, first element is an array of loaded user objects, second an array with invalid names.
4 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_new_validate in ./
privatemsg.pages.inc
File
- ./
privatemsg.module, line 926 - 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;
// 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;
}
// Apply 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,
);
}