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

Custom Alerts

Discussion in 'XenForo Development Discussions' started by wasif, Jan 22, 2015.

  1. wasif

    wasif Member

    I am working on an addon in which i have to send alerts to user. I was searching on it and come to know that I have to set some content type first before sending some alert.

    Now I was trying to set the content type for the content type but still not able to figure out what is the process of doing that?

    Anybody please help me how can I set the content type for custom alerts?

    Thanks :)
     
  2. Lawrence

    Lawrence Well-Known Member

    You will need an installer for that. Here is an example of an installer for the content type, and calling the alert:

    Code:
        public static function install($previous)
        {
            $db = XenForo_Application::getDb();
    
            if (XenForo_Application::$versionId < 1020070)
            {
                throw new XenForo_Exception('This add-on requires XenForo 1.2.0 or higher.', true);
            }
        
            if (!$previous)
            {
                 foreach (self::getData() AS $dataSql)
                {
                    $db->query($dataSql);
                }
            }
            else
            {
                if ($previous['version_id'] < 1010012)
                {
                    // for upgrades
                }
            }
    
            XenForo_Application::set('contentTypes', XenForo_Model::create('XenForo_Model_ContentType')->getContentTypesForCache());
        }
    
        public static function getData()
        {
            $data = array();
    
            $data['xf_content_type'] = "
                INSERT IGNORE INTO xf_content_type
                    (content_type, addon_id, fields)
                VALUES
                    ('iwdcharacter', 'iwdIcewindDale', '')
            ";
    
            $data['xf_content_type_field'] = "
                INSERT IGNORE INTO xf_content_type_field
                    (content_type, field_name, field_value)
                VALUES
                    ('iwdcharacter', 'alert_handler_class', 'Icewind_IcewindDale_AlertHandler_Character')
            ";
            return $data;
        }
    And then you will need the alert handler class that was inserted into the field_value above:

    Code:
    class Icewind_IcewindDale_AlertHandler_Character extends XenForo_AlertHandler_Abstract
    {
        public function getContentByIds(array $contentIds, $model, $userId, array $viewingUser)
        {
            return $model->getModelFromCache('Icewind_IcewindDale_Model_Character')->getCharResetsByIds($contentIds);
        }
    }
    And finally the template: the template name must start with alert_ followed by the content_type (iwdcharacter_) followed by the action defined when you call the alert handler. For my example, from the alert being called below it is: reset. So the template name is: alert_iwdcharacter_reset
    Code:
    XenForo_Model_Alert::alert($user_Id, $reset_by_id, $reset_by, 'iwdcharacter', $user_Id, 'reset');
    HTML:
    {xen:phrase iwd_icewinddale_your_character_was_reset_by_x,
        'username={xen:helper username, $user, 'subject'}'}
    Remember to include an uninstall function to remove the custom content type, :)
     
    wasif likes this.
  3. wasif

    wasif Member

    Hi Lawrence,

    Thanks a lot for your reply in detail. You really mentioned all the process in detail. :)


    Just one more question, can you please tell me what does this function "getContentByIds" will do. How this will affect the process ? Although I know its an abstract function and must be implemented.

    Thanks again. :)
     
  4. wasif

    wasif Member

    Hi,

    Just figured out how to play with that method and also created my custom alerts successfully using your tutorial. ;)

    Thanks a lot for your help. :)
     
    Lawrence likes this.
  5. Lawrence

    Lawrence Well-Known Member

    Just to add, my AlertHandler above is simple as there is nothing that needs to be done to the data before it is sent to the alerted user. For example this is where you can run the fetched contents through a censor string before it is sent to the user.
     

Share This Page