• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Sending an alert

AndyB

Well-known member
#1
Hello,

I have an add-on called Birthday Thread where I create a birthday thread via Cron. I was asked if I can send an alert when the thread is created. I'm real close with this code, but something is missing:

PHP:
		foreach ($userIds as $user)
		{
			// add username to subject
			$subjectNew = str_replace('{username}', $user['username'], $subject);
			
			// add username to message
			$messageNew = str_replace('{username}', $user['username'], $message);			
			
			$writer = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
			$writer->set('user_id', $starterId);
			$writer->set('username', $starterName);
			$writer->set('title', $subjectNew);
			$postWriter = $writer->getFirstMessageDw();
			$postWriter->set('message', $messageNew);
			$postWriter->setOption(XenForo_DataWriter_DiscussionMessage::OPTION_SET_IP_ADDRESS, false);
			
			$writer->set('node_id', $forumId);
			$writer->preSave();
			$writer->save();
			$thread = $writer->getMergedData();	

			// define variable
			$contentType = 'thread';
			
			// define variable
			$action = '';

			// define variable
			$extraData = array(
				'title' => $thread['title'],
				'link' => XenForo_Link::buildPublicLink('threads', $thread),
				'reason' => ''
			);
			
			// send alert
			XenForo_Model_Alert::alert($user['user_id'], $starterId, $starterName, $contentType, $thread['thread_id'], $action, $extraData); 
		}
The alert I get looks like this:

pic001.jpg

Thank you.
 

truonglv

Formerly Nobita.Kun
#2
Are you added the template for alert? As I see, you should have an template: alert_thread. I recommend dont leave action field to empty.
 

Xon

Well-known member
#3
You need to populate a template.

The template name that alerts use follows the form:
Code:
'alert_'.$contentType.'_'.$action
 

AndyB

Well-known member
#4
Hi Nobita.Kun and Xon,

Thank you both for taking a look at this.

I think the template that I want to use is called "alert_post_insert". So I tried defining the variable $action as that, but that didn't work.

PHP:
			// define variable
			$action = 'alert_post_insert';
 

truonglv

Formerly Nobita.Kun
#5
Hi Nobita.Kun and Xon,

Thank you both for taking a look at this.

I think the template that I want to use is called "alert_post_insert". So I tried defining the variable $action as that, but that didn't work.

PHP:
            // define variable
            $action = 'alert_post_insert';
The action should be: 'insert' and $contentType should be 'post'.
 

AndyB

Well-known member
#7
Thank you both!

This works perfect. :)

PHP:
		foreach ($userIds as $user)
		{
			// add username to subject
			$subjectNew = str_replace('{username}', $user['username'], $subject);
			
			// add username to message
			$messageNew = str_replace('{username}', $user['username'], $message);			
			
			$writer = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
			$writer->set('user_id', $starterId);
			$writer->set('username', $starterName);
			$writer->set('title', $subjectNew);
			$postWriter = $writer->getFirstMessageDw();
			$postWriter->set('message', $messageNew);
			$postWriter->setOption(XenForo_DataWriter_DiscussionMessage::OPTION_SET_IP_ADDRESS, false);
			
			$writer->set('node_id', $forumId);
			$writer->preSave();
			$writer->save();
			$thread = $writer->getMergedData();	

			// define contentType
			$contentType = 'post';
			
			// define action
			$action = 'insert';

			// define extraData
			$extraData = array();
			
			// send alert
			XenForo_Model_Alert::alert($user['user_id'], $starterId, $starterName, $contentType, $thread['first_post_id'], $action, $extraData); 
		}