XF 1.5 Sending xenForo Alert via PHP

KILLOVER

Member
Hello there!
I try to develop a small script that allows me to send Alerts to members. This works pretty well so far, I just cannot find out, how I can send text.

I jump on top of the xenForo SDK with this function:
PHP:
    public function sendAlert($user, $message) {
        $temp = $this->getUser($user);
        
        $username = $temp['username'];
        
        $alertModel = XenForo_Model::create('XenForo_Model_Alert');
        return $alertModel->alertUser($user, 2, "ILLUX", "user", 2, "https://...", array("alert_text" => $message));
    }

And basic:
PHP:
    public function sendAlert($userToSendTo, $message) {
        
        $alertModel = XenForo_Model::create('XenForo_Model_Alert');
        return $alertModel->alertUser($userToSendTo, AdminUserID, "AdminUserName", "ContentType", ContentID, "action", array("alert_text" => $message));
    }

And I'm calling the script this way:
PHP:
$sdk->sendAlert(1, "Testing...");

Here is the result, no errors:
Unbenannt.PNG

Somewhere any idea how I get text in there? Maybe how I add a link?

Regards,
 

Gobb

Member
For 1.5, you must create the alert handler. I had something like this in my old addon.

YourAddonDirectory/AlertHandler/AlertHandler.php

Code:
<?php
class YourAddonDirectory_AlertHandler_AlertHandler extends XenForo_AlertHandler_Abstract
{
    public function getContentByIds(array $contentIds, $model, $userId, array $viewingUser)
       {
         $myDataModel = $model->getModelFromCache('YourModel');

         return $myDataModel->getMyDataByIds($userId, $contentIds);
       }
  
    public function _prepareCreate(array $item)
   {
     if ($item['extra_data'])
     {
       $item['extra'] = unserialize($item['extra_data']);
     }
  
     unset($item['extra_data']);

     return $item;
   }
}

You then need to add the content types to the database.

Code:
INSERT IGNORE INTO xf_content_type
                    (content_type, addon_id, fields)
                VALUES
                    ('content-type', 'addon_id', '')

INSERT IGNORE INTO xf_content_type_field
                    (content_type, field_name, field_value)
                VALUES
                    ('content-type', 'alert_handler_class', 'YourAddonDirectory_AlertHandler_AlertHandler')

Rebuild caches. You then need to make the relevant template for the alert. alert_content-type_action should be how the template is named. {$extra.alert_text} should then render your $message variable.

Not really sure if this will much help given that you seem to be doing it external to XF, however looks like you're intending to view the alerts within it so it should be of use.
 
Last edited:
Top