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

Sending an alert

Discussion in 'XenForo Development Discussions' started by AndyB, Feb 24, 2015.

  1. AndyB

    AndyB Well-Known Member

    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_ADDRESSfalse);
                
                
    $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.
     
  2. Nobita.Kun

    Nobita.Kun Well-Known Member

    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.
     
  3. Xon

    Xon Well-Known Member

    You need to populate a template.

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

    AndyB Well-Known Member

    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';
     
  5. Nobita.Kun

    Nobita.Kun Well-Known Member

    The action should be: 'insert' and $contentType should be 'post'.
     
    Xon likes this.
  6. Xon

    Xon Well-Known Member

    @AndyB To make the alert system use that template, call XenForo_Model_Alert::alert with:
    Code:
    $contentType = 'post';
    $action= 'insert';
    
     
    Marcus likes this.
  7. AndyB

    AndyB Well-Known Member

    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_ADDRESSfalse);
                
                
    $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); 
            }
     
    Steve F likes this.

Share This Page