Create a function that takes a single UID as parameter and returns a raw URL to create a message form with user name pre-filled in the 'to' field.

Notify Michelle so she can use it in adv forum module.

Comments

litwol’s picture

Priority: Normal » Critical

Raising importance level for important people :)

naheemsays’s picture

Status: Needs work » Postponed (maintainer needs more info)

Is this to be different from "messages/new/%"?

litwol’s picture

for the sakes of the argument lets assume that our URL format standards will not change from the above. then this API function would return 'messages/new/%' where % is the UID of the user the link was requested for.

that being said, i know that we may change our URL structure. to help other modules integrate with privatemsg module i want to provide a function that when called will always return the correct current URL. so other modules use this function once and never have to worry for correctness of URLs with regard to privatemsg module.

i hope this made sense.

michelle’s picture

I normally loathe "subscribe" posts but since this issue is directed at me, it would be good to have it in my tracker. ;)

Michelle

naheemsays’s picture

so something like

function privatemsg_sent_to($username) {
  $uid = db_query(db_result("SELECT uid FROM {users} WHERE name = '%s'", $username));
  drupal_goto('messages/new/' . $uid);
}

???

(not sure that is actually valid code...)

litwol’s picture

Status: Postponed (maintainer needs more info) » Needs work
<?php
function privatemsg_sent_to($account) {
  return 'messages/new/' . $account->uid;
}
?>

it needs better name. otherwise this is the major idea.

in the future when we change our URL namespace, we will just change the return string from that function so other modules dont have to worry about it.

naheemsays’s picture

"pm_send_to_user"?

mrtoner’s picture

I had actually whipped this up last night:

/**
 * API function
 *
 * Return a URL to send a message to a specific user.
 */
function privatemsg_send_message_url($uid = NULL) {
  global $user;
  if ($uid && $uid != $user->uid) {
    return 'messages/new/'. $uid;
  }
}

but I had some questions about access control, etc.:

Should we assume Michelle and other module writers won't use a UID for the for the currently logged-n user? Do we return a link in that case? Do we return a link if the current user doesn't have "write" permission? Do we return a link if the recipient doesn't have "read" privileges?

litwol’s picture

we should try to have the same access control that we have for the rest of the module.

also if the user doesnt pass the permission check, we need to return FALSE. that way we give some feedback if there was a failure.

michelle’s picture

Interesting. Hadn't thought about putting the access check in this function. FWIW, here is the .inc for making privatemsg 5 work with advforum 5. You can see all the checking it goes through.

/**
 * @file
 *   This file provides advanced forum preprocess functions for the private message module.
 */

/**
 * Implementation of hook_preprocess_forum_user().
 */
function privatemsg_preprocess_forum_user(&$variables) {
  // We don't want to do this for anonymous users.
  if ($variables['account']->uid == 0) {
    return;
  }
  
  global $user;
  $account = $variables['account'];

  // Send private message
  if (($account->uid != $user->uid) &&
     user_access('access private messages') && 
     (isset($account->privatemsg_allow) ? $account->privatemsg_allow : 1)) {
    $variables['privatemsg_icon'] = theme('image', advanced_forum_path_to_images() . '/user_comment.png', 'Private Message', 'Private Message', NULL, TRUE);
    $variables['privatemsg_text'] = t('Send PM');
    $variables['privatemsg_link'] = 'privatemsg/new/' . $variables['account']->uid;
    $variables['privatemsg'] = '<div class="privatemsg">' .
      l($variables['privatemsg_icon'] . ' ' .
      $variables['privatemsg_text'], $variables['privatemsg_link'], NULL, NULL, NULL, NULL, TRUE) .
      '</div>';
  }
}

Michelle

litwol’s picture

  $variables['privatemsg_link'] = 'privatemsg/new/' . $variables['account']->uid;

This is the line that would idealy be replaced with

  $variables['privatemsg_link'] = privatemsg_send_message_url( $variables['account']->uid);

we should consider whether its important to ask for $user object or just UID. if we have UID then we can load the user if necessary. but if we have the user object then we skip any loading in the future.

naheemsays’s picture

That may not be enough - if we go ahead with a scheme such as "user/%/messages/new/%", we would also need to know the current user's UID.

I do't see the need to get the full user object as we will be loading the user data again anyway. (unless there is an acceptable way to carry all the data across).

mrtoner’s picture

We don't need to have the current user's UID passed to the function -- that's already available in $user.

litwol’s picture

The function will work for any UID passed in, not only for user that is currently logged in.

one of the applications is to present 'send message to' link to a profile of some one else while _i_ am logged in.

Lets improve on code in #8.

michelle’s picture

Why would you do #12? The only reason I can think of is to allow someone to send a private message as another user. I can see some convenience for an admin that has a personal account, maybe, but I don't know if it's worth that. I certianly wouldn't put that ability in any integration I do and the sending user would always be the logged in user.

I think this is in danger of getting into bikeshed territory here... It really doesn't need to be that complicated. Just pick a URL that has the currently logged in user sending a PM to a given UID and there ya go.

Michelle

naheemsays’s picture

Call that lack-of-sleep-stupidity! Forget what I wrote, the code proposed by litwol is enough.

berdir’s picture

Status: Needs work » Closed (duplicate)

I'm closing this as a duplicate of #288183: Provide api function for other modules to send messages.. The patch does have a privatemsg_get_link($recipient, $account) function and privatemsg.author-pane.inc is now part of our module.