XF 2.0 Proper way to send a warning out?

Jaxel

Well-known member
I want to progmatically send a warning out. I have the `warning_definition_id`, an array of `User` entities I want to send the warning out to, and a `user_id` of the person I want a resulting conversation to be from. What would be the proper way to do this?
 
Okay, I got it working with this:

Code:
    $warningDefinition = $this->finder('XF:WarningDefinition')
        ->where('warning_definition_id', $this->options()->EWRmember_mad_warning)
        ->fetchOne();

    $warningFromUser = $this->finder('XF:User')
        ->where('user_id', $this->options()->EWRmember_mad_user)
        ->fetchOne();
        
    if ($warningDefinition && $warningFromUser)
    {
        $warnService = $this->service('XF:User\Warn', $visitor, 'user', $visitor, $warningFromUser);
        $warnService->setFromDefinition($warningDefinition);
        
        if ($this->options()->EWRmember_mad_options['startc'])
        {
            $warnService->withConversation(
                $warningDefinition->conversation_title, $warningDefinition->conversation_text, [
                    'open_invite' => $this->options()->EWRmember_mad_options['invite'],
                    'conversation_open' => !$this->options()->EWRmember_mad_options['locked']
                ]
            );
        }
        
        $warnService->save();
    }

It gives the warning, and starts the conversation... however, it doesn't do any of the replacements for {name} or {staff}.
 
There doesn't seem to be a way to add more people to the warning conversation however... Nor a way to get the ID of the conversation.
 
Yeah, I can't see any way to do that out of the box. You could extend the service and store it in a property:

PHP:
/**
* @var \XF\Entity\ConversationMaster|null
*/
protected $conversation;

/**
* @return \XF\Entity\ConversationMaster|null
*/
public function getConversation()
{
    return $this->conversation;
}

/**
* @param \XF\Entity\Warning $warning
*
* @return \XF\Entity\ConversationMaster|null
*/
protected function sendConversation(\XF\Entity\Warning $warning)
{
    $conversation = parent::sendConversation($warning);

    $this->conversation = $conversation;

    return $conversation;
}
 
I decided to just not use the $warnService->withConversation() function and ran the conversation creator myself.
 
Back
Top Bottom