requester) { $relationship->requester = user_load($relationship->requester_id); } if (!$relationship->requestee) { $relationship->requestee = user_load($relationship->requestee_id); } if (!($relationship->name || $relationship->type)) { $relationship->type = user_relationships_type_load($relationship->rtid); } $replaceables = array( '!requester' => theme('username', $relationship->requester), '!requestee' => theme('username', $relationship->requestee), '%relationship_name' => $relationship->name ? $relationship->name : $relationship->type->name, '%relationship_plural_name' => $relationship->plural_name ? $relationship->plural_name : $relationship->type->plural_name, ); $replacements = array_merge($replaceables, $replacements); } return t($msg, $replacements); } /********************************** * * "PRIVATE" FUNCTIONS * **********************************/ /** * Returns a nested array of default messages */ function _user_relationships_ui_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 !requester.', 'disapproved' => "!requester's request to be a %relationship_name of !requestee has been removed.", 'cancel' => 'Your request to be a %relationship_name of !requestee has been canceled.', '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.', 'pre_approved' => "You're request to be %relationship_name of !requestee has been pre-approved. You are now a %relationship_name of !requestee." ), 'error' => array( 'too_many_relationships' => 'You cannot establish any more relationships with this user.', '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.', 'non_existant_type' => 'That relationship type does not exist', 'unknown_error' => 'An unknown error occurred. Please contact the site administrator.', ), ); } /** * 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; } } } } /* * Notify the user of pending relationship requests */ function _user_relationships_ui_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(); $relationships = user_relationships_load(array('requestee_id' => $account->uid, 'approved' => FALSE)); foreach ($relationships as $relationship) { $msg = user_relationships_ui_get_message('pending', $relationship, array( '!pending_relationship_requests' => l(t('pending relationship requests'), "relationships/requests") )); if (!in_array($msg, $notifications)) { drupal_set_message($msg); } } } /** * Adds autocompletion capability */ function _user_relationships_ui_autocomplete_types($string = '') { $matches = array(); if ($string) { $result = db_query_range("SELECT name FROM {user_relationship_types} WHERE LOWER(name) LIKE LOWER('%%%s%%')", strtolower($string), 0, 10); while ($relationship = db_fetch_object($result)) { $matches[$relationship->name] = check_plain($relationship->name); } } print drupal_to_js($matches); exit(); } /** * List of relationships between two users */ function _user_relationships_ui_between(&$viewer, &$viewed) { $list = array(); $relationships = user_relationships_load(array('between' => array($viewer->uid, $viewed->uid))); foreach ($relationships as $relationship) { if ($relationship->approved && !$list[$relationship->rid]) { $list[$relationship->rid] = t('%relationship_name (!remove_link)', array( '%relationship_name' => $relationship->name, '!remove_link' => theme('user_relationships_remove_link', $viewer->uid, $relationship->rid) )); } } return $list; } /** * List of pending relationships with between two users */ function _user_relationships_ui_actions_between(&$viewer, &$viewed) { if ($viewer->uid == $viewed->uid) { return; } $list = array(); $relationships = user_relationships_load(array('requester_id' => $viewer->uid, 'requestee_id' => $viewed->uid, 'approved' => FALSE)); foreach ($relationships as $relationship) { $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/requests"), )); } $relationships = user_relationships_load(array('requester_id' => $viewed->uid, 'requestee_id' => $viewer->uid, 'approved' => FALSE)); foreach ($relationships as $relationship) { $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'), "user/{$viewer->uid}/relationships/requests"), )); } $types_count = (int)db_result(db_query("SELECT COUNT(*) FROM {user_relationship_types}")); $relationships_count = user_relationships_load(array('between' => array($viewer->uid, $viewed->uid)), array('count' => TRUE)); if (!$relationships_count || (variable_get('user_relationships_ui_allow_multiple', TRUE) && ($relationships_count < $types_count))) { $list[] = theme('user_relationships_request_relationship_link', $viewed); } return $list; } /** * Helper function to build the settings form for the notification messages */ function _user_relationships_ui_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_ui_message_settings_form($form[$key], $value); } else { $form["user_relationships_ui_msg_{$key}"] = array( '#type' => 'textfield', '#title' => ucfirst(str_replace('_',' ',$key)), '#default_value' => variable_get("user_relationships_ui_msg_{$key}", $value) ); } } }