Helping Sending an Alert

Gossamer

Active member
Hello! So, I previously had alerts working for my customization. However, I was originally just using a query to select all admins to send the alert to when it was triggered. I'm now updating that so that there is an option to select what usergroups to send the alert to.

I have the options setup, and everything else is working fine. However, the alert itself is not triggering. My php is still pretty shaky and I've been learning as I go, but this is what I have so far:

Reserves Model
PHP:
    public function sendReserveAlert($id, $alertType)
    {
        $reserve = $this->getReservebyID($id);
       
        $userGroups = XenForo_Application::getOptions()->reserves_ReserveAlertGroup;
       
        $ugModel = XenForo_Model::create('XenForo_Model_UserGroup');
       
        $users = array();
        FOREACH ($userGroups AS $usergroup)
        {
            $users[] = $ugModel->getUserIdsInUserGroup($usergroup);
        }
       
        $userModel = XenForo_Model::create('XenForo_Model_User');
       
        FOREACH ($users AS $userid)
            {
                $user = $userModel->getUserById($userid);
                XenForo_Model_Alert::alert(
                        $user['user_id'],
                        $reserve['user_id'],
                        $reserve['username'],
                        'reserve',
                        $reserve['id'],
                        $alertType,
                        array(
                            'character' => $reserve['character'],
                            'fandom' => $reserve['fandom'],
                            'count' => $reserve['count'])
                    );

                    $alerted[] = $user['user_id'];
           
            }   
    }

Any ideas?
 
Did you create a content type for 'reserve' and an alert_handler field that points to the alerthandler you've created?
 
Yep, I did. Alerts were already working. It just stopped working when I tried to adjust who was receiving the alert.

For reference, this was my original function to send the alert and it worked:

PHP:
public function sendAlertToAdmins($id, $alertType)
    {
   
            $reserve = $this->getReservebyID($id);
   
            $admins = $this->fetchAllKeyed("
            SELECT *
            FROM xf_user
            WHERE is_admin = 1", "user_id");
           
            FOREACH ($admins AS $admin)
            {
                XenForo_Model_Alert::alert(
                        $admin['user_id'],
                        $reserve['user_id'],
                        $reserve['username'],
                        'reserve',
                        $reserve['id'],
                        $alertType,
                        array(
                            'character' => $reserve['character'],
                            'fandom' => $reserve['fandom'],
                            'count' => $reserve['count'])
                    );

                    $alerted[] = $admin['user_id'];
           
            }   
    }

And I did already update the controllerpublic so that it calls the new alert function (sendReserveAlert) instead of the old one. I'm thinking there's something wrong with how I'm attempting to retrieve the people to send the alert to.
 
add Zend_Debug::dump($users); before your foreach($users...

See if it's actually populated with anything.
 
Okay, $users wasn't populating at all. I dug back a little further and realized that my options array was outputting the usergroup id as the array key, not the value.

I now have this:

PHP:
$reserve = $this->getReservebyID($id);
       
        $userGroups = XenForo_Application::getOptions()->reserves_ReserveAlertGroup;
       
        $ugModel = XenForo_Model::create('XenForo_Model_UserGroup');
       
        $users = array();
       
        FOREACH (array_keys($userGroups) AS $usergroup)
        {
            $users[] = $ugModel->getUserIdsInUserGroup($usergroup);
        }
       
        $userModel = XenForo_Model::create('XenForo_Model_User');
       
       
       
        Zend_Debug::dump($users);
        exit();
       
        FOREACH ($users AS $userid)
            {
                $user = $userModel->getUserById($userid);
                XenForo_Model_Alert::alert(
                        $user['user_id'],
                        $reserve['user_id'],
                        $reserve['username'],
                        'reserve',
                        $reserve['id'],
                        $alertType,
                        array(
                            'character' => $reserve['character'],
                            'fandom' => $reserve['fandom'],
                            'count' => $reserve['count'])
                    );

                    $alerted[] = $user['user_id'];
           
            }

My debug now outputs this:
Code:
array(2) {
[0] => array(1) {
[1] => int(0)
}
[1] => array(2) {
[1] => int(0)
[8] => int(0)
}
}

Which is better. However, the foreach $users bit only sends out alerts based on the two top-level arrays, when I want it to pull people using the keys from the subarrays. I've tried merging the arrays, but then the keys all end up renumbered and I lose my user ids.
 
Aha, figured it out! If anybody was curious, this was my solution:

PHP:
public function sendReserveAlert($id, $alertType)
    {
        $reserve = $this->getReservebyID($id);
       
        //Retrieve usergroups to send alerts to
        $userGroups = XenForo_Application::getOptions()->reserves_ReserveAlertGroup;
       
        $ugModel = XenForo_Model::create('XenForo_Model_UserGroup');
       
/*        Zend_Debug::dump($userGroups);
        exit();  */
       
        $users = array();
       
        //Retrieve group ids
        FOREACH (array_keys($userGroups) AS $usergroup)
        {
            //Retrieve userids from array key
            $keys = array_keys($ugModel->getUserIdsInUserGroup($usergroup));
           
            //Combine userids into a new array
            $users = array_merge($keys, $users);
        }
        //Remove duplicate values from user list
        $users = array_unique($users);   
       
        $userModel = XenForo_Model::create('XenForo_Model_User');

       
        FOREACH ($users AS $userid)
            {
                $user = $userModel->getUserById($userid);
                XenForo_Model_Alert::alert(
                        $user['user_id'],
                        $reserve['user_id'],
                        $reserve['username'],
                        'reserve',
                        $reserve['id'],
                        $alertType,
                        array(
                            'character' => $reserve['character'],
                            'fandom' => $reserve['fandom'],
                            'count' => $reserve['count'])
                    );

                    $alerted[] = $user['user_id'];
           
            }   
    }

I basically had to pull the keys from the resulting array and reassign them to a brand new array. Though if anybody knows a better way to do this, I'm always up to learn something more. :)
 
Top Bottom