Use elaborations note in emails

scalp - September 15, 2009 - 19:47
Project:User Relationships
Version:6.x-1.x-dev
Component:Miscellaneous
Category:feature request
Priority:normal
Assigned:alex.k
Status:needs work
Description

Would it be possible to pull the note given in elaborations when creating a relationship to be sent in the request or pre-approval email?

#1

alex.k - September 16, 2009 - 14:22

It should be possible to for UR-Elaborations inject the additional token, I'm not 100% sure. If you can look at the code that gathers available tokens that'd be great.

#2

scalp - September 16, 2009 - 17:45

Thank you for the incredibly fast reply. Unfortunately, my php is not real strong. Here's what I think is creating the available tokens list for the token module:

function token_get_values($type = 'global', $object = NULL, $flush = FALSE, $options = array()) {
static $tokens;
static $running;

// Flush the static token cache. Useful for processes that need to slog through
// huge numbers of tokens in a single execution cycle. Flushing it will keep
// them from burning through memory.
if ($flush || !isset($tokens)) {
$tokens = array();
}

// Since objects in PHP5 are always passed by reference, we ensure we're
// working on a copy of the object.
if (is_object($object)) {
$object = drupal_clone($object);
}

// Simple recursion check. This is to avoid content_view()'s potential
// for endless looping when a filter uses tokens, which load the content
// view, which calls the filter, which uses tokens, which...
if ($running) {
// We'll allow things to get two levels deep, but bail out after that
// without performing any substitutions.
$result = new stdClass();
$result->tokens = array();
$result->values = array();
return $result;
}

$running = TRUE;

token_include();

$id = _token_get_id($type, $object);
if (isset($tokens[$type][$id])) {
$tmp_tokens = $tokens[$type][$id];
}
else {
$tmp_tokens = module_invoke_all('token_values', $type, $object, $options);
$tokens[$type][$id] = $tmp_tokens;
}

// Special-case global tokens, as we always want to be able to process
// those substitutions.
if (!isset($tokens['global']['default'])) {
$tokens['global']['default'] = module_invoke_all('token_values', 'global');
}

$all = array_merge($tokens['global']['default'], $tokens[$type][$id]);

$result = new stdClass();
$result->tokens = array_keys($all);
$result->values = array_values($all);

$running = FALSE;

return $result;
}

Not sure what would be needed to make this work. Sorry for not being able to be more helpful.

#3

scalp - September 18, 2009 - 13:48

Couldn't something just be added to the user_relationship_mailer.module file to include an elaboration if it's included? Seems like the elaboration should be presented to the recipient on first contact which would be the email.

#4

scalp - October 1, 2009 - 12:03

Does anyone know how this could be done?

#5

scalp - October 23, 2009 - 19:07

I'm still trying to get this done. I'm kind of surprised that there hasn't been more interest in this. It seems to me that sending the elaboration with the email would be important especially if usernames are being used. How else will the recipient know where they know the sender from?

Here's what I've tried so far. I've added the following to the function user_relationship_mailer_replacements in /user_relationship_mailer/user_relationship_mailer_defaults.inc:

'@elaboration' => user_relationships_get_elaboration ($relationship->rid),

This adds @elaboration as a replacement token, but returns nothing. I've replaced the parameter with '32' which returns the elaboration for rid 32 into the emails so I know the function is being called properly into the email. I can't find a function that returns the rid, yet I see rid being used throughout the user relationship module. Do I need to write a new function that will retrieve the rid or is there already something in place that I could use to insert the appropriate rid into the parameter?

#6

alex.k - October 23, 2009 - 20:29

Your code looks correct, and judging by the rest of the function user_relationship_mailer_replacements(), $relationship->rid should be available. You could test this by trying out

<?php
'@elaboration' => $relationship->rid,
?>
and checking if you see it in the email. If you do, then it could be that the order of hooks firing is such that the email is sent before the elaboration record is saved. If you insert an exit() call into the function, Drupal will abort at that point, and you will be able to inspect the database to see if the elaboration record for the newly created relationship is actually there.

Also, dumb question... is the elaboration not already available in $relationship? Appreciate you working to resolve this.

#7

scalp - October 23, 2009 - 23:00

I'm glad to help on resolving this. Hopefully we can get it working. I know the elaborations are being saved. As I mentioned, I can get it to pull an old one from the db by choosing an old rid to pass as a parameter. I did try using the following:
'@elaboration' => $relationship->elaboration,
but it didn't work so I'm guessing that its not in the array by default. I looked around and couldn't find anything to contradict this.
Using

<?php
'@elaboration' => $relationship->rid,
?>

It does return the correct rid. You kind of lost me on the what to do if that happened part though. Where would I need to insert the exit?

#8

alex.k - October 24, 2009 - 16:54

No problem. What I was trying to say is that at the moment the email is sent, the elaboration record is not yet in the database. The flow in that case would be something like:

User hits submit --> .... --> save relationship --> ..... --> send email --> ..... --> save elaboration note --> ..... --> done

The way to debug this is to insert exit() anywhere in the user_relationship_mailer_replacements() function. When you request a new relationship, it will reach this point and abort. You then can look at the user_relationship_elaborations table and see if the new elaboration note is there. If not, the module needs to be edited to use the correct hook so that the record is inserted earlier in the process.

#9

scalp - October 24, 2009 - 17:43

Got it. Inserting the exit () into the function does indeed stop the elaboration from being saved. It also does not send an email. It just returns a blank empty page when a request is sent. It does create a new rid in the user relationships table though. Just nothing in the elaborations table.

#10

alex.k - October 24, 2009 - 18:37

Thank you for debugging the issue.

It's a bad design in the way the relationship is saved and the order of hooks firing. Basically, if this had been done right, $relationship->elaboration would not have been empty. There was a pretty helpful explanation in #526356: Improve extendability of API hooks on the underlying problem. That's the reason the elaboration is saved after mailer and possibly other modules have done their work. Would love to fix the issue (and the elaborations module in general could use a few cleanups), but really a stable release is a bigger priority at the moment.

#11

scalp - October 25, 2009 - 01:54

No problem. Do you have any ideas on a work around for this in the meantime? Would there be some way to effectively pause the email being sent until the end of the process?

#12

alex.k - November 8, 2009 - 17:34
Assigned to:Anonymous» alex.k
Status:active» fixed

Committed an implementation that allows @elaboration to appear in email replacements, http://drupal.org/cvs?commit=285890. Major cobwebs and less than stellar design there :)

Please test this as well as that the request process has not been affected.

#13

scalp - November 11, 2009 - 14:36

Excellent. I will definitely start testing this out. Is this included in the newest dev version or should I just apply the patches listed in the CVS?

#14

alex.k - November 11, 2009 - 15:19

Nope, it's already in CVS and the -dev release.

#15

scalp - November 11, 2009 - 19:09

A couple of things I've found so far. The elaborations are included in the email (thank you for doing this), but special characters are broken down to their html counterparts. Also, the view I had set up for seeing posts created by "friends" is now not working. It's been a while since I've messed with this, but I'm pretty sure it was working properly before. Could something have been changed that would now have the view returning JUST the logged in user's posts?

#16

scalp - November 11, 2009 - 19:35

Got the views working again. Had to change the argument "requester user" to "requestEE user".

#17

alex.k - November 11, 2009 - 20:13

For me the apostrophe gets converted to ' because @elaborations is run through check_plain. I think there should be no XSS risk in changing @elaboration to !elaboration to pass it unescaped, because the text will be sent via email and not displayed on the screen.

#18

scalp - November 11, 2009 - 20:43

Unfortunately, I don't know enough to be able to offer an opinion on this.

#19

alex.k - November 11, 2009 - 20:51
Version:6.x-1.0-rc2» 6.x-1.x-dev
Status:fixed» needs work

#20

scalp - November 11, 2009 - 23:46

As mentioned I was able to get the 2 way relationship to work again, by flip flopping the argument. I cannot get the view for the one way to work again though. I can get a view that shows nodes from people that are following me, but no matter what I try I can't get a view to show nodes from people I'm following. Pretty sure this was working before the upgrade to dev.

 
 

Drupal is a registered trademark of Dries Buytaert.