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

Sending Custom Alerts To Any Specific User And What's 'Content Type'?

Discussion in 'XenForo Development Discussions' started by TheBigK, Apr 20, 2015.

  1. TheBigK

    TheBigK Well-Known Member

    I'm planning to create an addon that lets the admin custom text as alert to any specific user. So the adminCP form my addon will have will be like this:-
    • Send Alert To: [ Write Username Here ]
    • Alert Text: [Text Box]
    • (Send)
    I've been banging my head over what my actionSend should do because I really can't figure out how to trigger alerts? I've made several failed attempts at understanding the AlertHandler in XF and also xfrocks's BD Alerts Addon that has this functionality already. I think I need to understand the concept of 'Content Type' in XF; which looks like a complicated topic to me.

    Can someone help me understand -

    1. What should my actionSend do?
    2. An Algorithm (overview) for my add-on. For example:

    Send Button Triggers the actionSend() in my ControllerAdmin -> It then triggers the AlertHandler ->Which then gathers all the data -> Checks whether user can view alerts -> If Yes, shows the alert and then fades it away.

    Thank you for your time!
     
  2. TheBigK

    TheBigK Well-Known Member

    Update:

    So I read more and have come to a conclusion that xenforo treats 'profile posts', 'conversations', 'threads/posts' as different content types - which can generate an alert. But this does NOT include the 'content type' that I'm planning to create. This means, I need to create a new content type and associate it with alerts.

    Am I right?
     
  3. TheBigK

    TheBigK Well-Known Member

    Update: Really Need A Helping Hand! Don't just smile and pass by.

    I've created new content type 'customAlerts' and content_type_field with field_value: CustomAlerts_AlertHandler_Send.

    ...and that's where my patience is coming to a dead-end.

    So far, I've an admin template that displays the input form (Send Alert To: , Alert Message: , Send Button), an installer that creates the content type and an admin route.

    The controller code does nothing except returning a responseView from the actionIndex()

    PHP:
    class CustomAlerts_ControllerAdmin_Send extends XenForo_ControllerAdmin_Abstract
    {
    public function 
    actionIndex()
    {
    $viewParams = array();return $this->responseView('CustomAlerts_ViewPublic_Index''customAlerts_form'$viewParams);}

    public function 
    actionSendAlert()
    {

    }
    }
    Need some direction on what should go inside actionSendAlert().
     
  4. Daniel Hood

    Daniel Hood Well-Known Member

    XenForo/Model/Alert.php -> control + f (find) -> "send" -> first result:

    PHP:
    /**
        * Send a user alert
        *
        * @param integer $alertUserId
        * @param integer $userId
        * @param string $username
        * @param string $contentType
        * @param integer $contentId
        * @param string $action
        * @param array $extraData
        */
        
    public static function alert($alertUserId$userId$username$contentType$contentId$action, array $extraData null)
        {
     
  5. TheBigK

    TheBigK Well-Known Member

    Thanks @Daniel Hood . Have already made some progress. I'm currently in the process of getting all the data I need to feed to the alert function.
     
  6. TheBigK

    TheBigK Well-Known Member

    Okay, I've made some more progress. Now the following code in controller is triggering an old alert for the member I specify in adminCP. I'm going totally clueless about how to troubleshoot it.

    PHP:
    <?php//Controller for 'Send Alerts' Functionality in AdminCPclass CustomAlerts_ControllerAdmin_Send extends XenForo_ControllerAdmin_Abstract
    {
    public function 
    actionIndex()
    {
    $viewParams = array();return $this->responseView('CustomAlerts_ViewPublic_Index''customAlerts_form'$viewParams);}

    public function 
    actionSendAlert()
    {
    //Variables for alert()
    $receiverUserName $this->_input->filterSingle('username'XenForo_Input::STRING);
    $receiverArray$this->getModelFromCache('XenForo_Model_User')->getUserByName($receiverUserName);
    $receiver $receiverArray['user_id'];$adminId 1;$adminName 'Bill Gates';$contentType 'customalerts';$contentId 2;$action '';
    XenForo_Model_Alert::alert($receiver$adminId$adminName$contentType$contentId$action$extraData null);

    $viewParams = array();

    return 
    $this->responseView('CustomAlerts_ViewPublic_Alert''alert_customalerts'$viewParams);}
    }
     
  7. TheBigK

    TheBigK Well-Known Member

    Update:

    I've figured out that it's the $extraData array that is responsible for storing the custom text I wish to send as an alert. I'm however unable to figure out what exactly should be the content of the this array so that the XF system can pick it up properly.

    So far, I've been able to store stuff into the xf_user_alert table and here's what my extra_data field lists:

    a:1:{i:0;s:11:"Hello World";}

    But XF' can't pick up the text and shows blank error.

    Question: If I were to hard-code the $extraData array, how should I define it?

    $extraData = array( ?, ?);
     
  8. Xon

    Xon Well-Known Member

    @TheBigK, here is an example of adding the field 'WordCount' to an Alert for a post.

    Creating the alert, adding a word count:
    Code:
                        XenForo_Model_Alert::alert(
                            $user['user_id'],
                            $reply['user_id'],
                            $reply['username'],
                            'post',
                            $reply['post_id'],
                            'insert',
                            array('WordCount' => $ApproximateWordCount)
    
    Either as a new alert handler, or extending an existing one:
    Code:
    class ExampleCode_XenForo_AlertHandler_DiscussionMessage_Post extends XFCP_ExampleCode_XenForo_AlertHandler_DiscussionMessage_Post
    {
        protected function _prepareAlertAfterAction(array $item, $content, array $viewingUser)
        {
            $item = parent::_prepareAlertAfterAction($item, $content, $viewingUser);
    
            if (isset($item['extra']['WordCount']))
            {
                $item['content']['WordCount'] = $item['extra']['WordCount'];
            }
            return $item;
        }
    }
    
    Then in your alert template, which must be named like "alert_<content_type>_<action>", where <content_type> and <action> are defined by what you call XenForo_Model_Alert::alert with.

    Code:
        {xen:phrase x_started_thread_y_may_be_more_with_wordcount,
        'name={xen:helper username, $user, 'subject'}',
        'title=<a href="{xen:link posts, $content}" class="PopupItemLink">{xen:helper threadPrefix, $content}{$content.title}</a>',
        'wordcount={$content.WordCount}'}
    
    This is taken from a private addon, with extra junk removed.
     
    Last edited: Apr 25, 2015
  9. Brogan

    Brogan XenForo Moderator Staff Member

    Just to add, the template name must begin with alert_.
     
  10. TheBigK

    TheBigK Well-Known Member

    Thanks a lot for your inputs, @Xon and @Brogan .

    The real issue was with the getContentByIds() method in my alert handler. I kept it blank because I thought I didn't need it. @xfrocks helped me fix it with this code:

    PHP:
    public function getContentByIds(array $contentIds$model$userId, array $viewingUser)
    {
    $contents = array();foreach ($contentIds as $contentId)
    {
    $contents[$contentId] = array();}

    return 
    $contents;
    }
    XenForo attempts to find multiple alert information and that's exactly it wasn't getting; that is XF was not able to find any content associated with the IDs. It therefore could not look into the proper template to load alert content from.
     

Share This Page