Changing username display to custom user attributes first name in discussion "monitoring" emails

Permalink Browser Info Environment
I need to adjust the email received when users monitor a discussion / forum to show custom user attribute first name (Fname) and last name (Lname) to replace the username ($name) and poster's username ($postername) in the following code found in /packages/discussion/mail/discussion_track.php. Any help would be appreciated

<?php 
if (defined('DISCUSSION_MONITOR_FROM_EMAIL')) {
   $from = array(DISCUSSION_MONITOR_FROM_EMAIL, DISCUSSION_MONITOR_FROM_NAME);
}
$subject = "{$thread_title}: {$title}"; 
$body = t('Dear')." {$name},
{$posterName} ".t('has posted to a discussion you\'re monitoring')." 
".t('Subject').":
{$post_title}
".t('Message').":
{$post_body}
".t('Click the link below to view the discussion').":
{$link}";
if (ENABLE_USER_PROFILES) {
$body .= "

Type: Discussion
Status: Archived
xigo
View Replies: View Best Answer
ryan replied on at Permalink Reply
ryan
You'd change that in /packages/discussion/models/discussion_track.php
in the notify() method around line 121 you'll see:
<?php
$mh->addParameter('name', $u->getUserName());
?>


If you wanted to contain your changes to the mail template only you could get the user info object by the username too.

<?php
$ui = UserInfo::getByUserName($name);
$name = $ui->getAttribute('fName'). " ". $ui->getAttribute('lName');
?>
xigo replied on at Permalink Reply
xigo
Many thanks for the quick reply, Ryan. When I replace the code with that given above, no monitor email is sent. Maybe I am missing something?
EvanCooper replied on at Permalink Reply
EvanCooper
The example code Ryan provided includes php tags, but you wouldn't include the surrounding php tags in your code. Was that what was occurring?
xigo replied on at Permalink Reply
xigo
No, I did not include the php tags
EvanCooper replied on at Permalink Reply
EvanCooper
Can you post the code snippet as you changed it? It's hard to troubleshoot without knowing what you've changed.
xigo replied on at Permalink Reply
xigo
Many thanks Evan

Original code from around line 121 of packages/discussion/models/discussion_track.php ...
//this is the forum title
         $mh->addParameter('title', $this->c->getCollectionName()); 
         $threadTitle=(strlen($this->thread_title))? $this->thread_title: $this->post_title;
         $mh->addParameter('thread_title', $threadTitle);      
         $mh->addParameter('post_title', $this->post_title);
         $mh->addParameter('post_body', $this->post_body);
         $mh->addParameter('name', $u->getUserName());
         $mh->addParameter('posterName', $posting_u->getUserName());
         $mh->addParameter('posterProfile', BASE_URL . View::url('/profile',$poster_uID));
         $mh->to($ui->getUserEmail());
         $mh->load('discussion_track', 'discussion');
         Loader::library("mail/importer");
         $mi = MailImporter::getByHandle("discussion_post_reply");
         if (is_object($mi) && is_object($newEntry) && $mi->isMailImporterEnabled()) {
            $data = new stdClass;


going on the above post I tried a few things but in the first place tried the following ...

//this is the forum title
         $mh->addParameter('title', $this->c->getCollectionName()); 
         $threadTitle=(strlen($this->thread_title))? $this->thread_title: $this->post_title;
         $mh->addParameter('thread_title', $threadTitle);      
         $mh->addParameter('post_title', $this->post_title);
         $mh->addParameter('post_body', $this->post_body);
                        $ui = UserInfo::getByUserName($name);
                        $name = $ui->getAttribute('fName'). " ". $ui->getAttribute('lName');
         $mh->addParameter('name', $u->getUserName());
                         $mh->addParameter('posterName', $posting_u->getUserName());
         $mh->addParameter('posterProfile', BASE_URL . View::url('/profile',$poster_uID));
                          $mh->to($ui->getUserEmail());
         $mh->load('discussion_track', 'discussion');
         Loader::library("mail/importer");
         $mi = MailImporter::getByHandle("discussion_post_reply");
EvanCooper replied on at Permalink Reply
EvanCooper
I see. The issue is that you don't have $name defined before you use it to get the username by name.

Instead of:

$ui = UserInfo::getByUserName($name);


It should be:

$ui = UserInfo::getByUserName($u->getUserName());


Then I think you want the 'name' parameter to be this first name + last name combined string, so instead of:

$mh->addParameter('name', $u->getUserName());


It should be:

$mh->addParameter('name', $name);


Since $name now contains that combined first name / last name string.

Does that make sense?

Also, with files like this that kind of run in the "background" you can check your logs (type "logs" in intelligent search) and it will show you errors that are occurring. In this case, I think you're calling methods on $ui, but $ui hasn't been defined since the username provided in UserInfo::getByUserName is null.

Edit: whoops, typo previously in first "instead" :).
xigo replied on at Permalink Best Answer Reply
xigo
Many thanks Evan - that works great!

Here is the code block for others looking to also have personalised discussion forum notification emails with user's first name and poster's first name and last name displayed in the notification email. A copy of the file packages/discussion/models/discussion_track.php was placed in folder models/discussion_track.php. And the code block at around line 121 was replaced with the below. Note this is using custom user attributes 'fname' and 'lname' for first name and last name respectively ...

//this is the forum title
         $mh->addParameter('title', $this->c->getCollectionName()); 
         $threadTitle=(strlen($this->thread_title))? $this->thread_title: $this->post_title;
         $mh->addParameter('thread_title', $threadTitle);      
         $mh->addParameter('post_title', $this->post_title);
         $mh->addParameter('post_body', $this->post_body);
         $ui = UserInfo::getByUserName($u->getUserName());
         $name = $ui->getAttribute('fname');
         $mh->addParameter('name', $name);
         $up = UserInfo::getByUserName($posting_u->getUserName());
         $postername = $up->getAttribute('fname'). " ". $up->getAttribute('lname');
         $mh->addParameter('posterName', $postername);
         $mh->addParameter('posterProfile', BASE_URL . View::url('/profile',$poster_uID));
         $mh->to($ui->getUserEmail());
         $mh->load('discussion_track', 'discussion');
EvanCooper replied on at Permalink Reply
EvanCooper
Sweet! Good to hear.
ConcreteCMS replied on at Permalink Reply
ConcreteCMS
Attention: Since there has been no activity on this issue for two weeks, this issue has been automatically archived.

To re-open this issue, reply to this message.

concrete5 Environment Information

Browser User-Agent String

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You have not specified a license for this support ticket. You must have a valid license assigned to a support ticket to request a refund.