Adding Custom Notifications

One of the questions I get asked most frequently is "how can I get notified if X happens", with "X" being a new user subscribing, a page being updated, and so on. Thanks to Mura CMS Events, it is easy to add custom notifications like these and many others.

Mura CMS has a deep and expansive Event model. An event is pretty much as it sounds, an occurance of some particular action inside of Mura. There are several different categories of Mura Events, such as Contextual Events, Rendering Events, and Life-Cycle Events. Together, they allow a developer to  modify how Mura behaves, or to add a wide range of extended functionality. In our post today, I'm going to look at how we can fire off Email notifications when a particular event occurs.

Contextual Events

Contextual Events in Mura are events that fire in the context of a specific action that occurs, i.e. a page being saved. There are times when a site administrator might want to have an enhanced awareness of these actions, such as for monitoring or security purposes. By listening to the Contextual Event associated with these actions, it is very easy to add this sort of enhanced functionality.

For instance, say you wanted to be notified each time a new user created a profile on your Mura website. There are quite a few user-related contextual events, but the best choice in this case is likely onAfterUserSave.

public void function onAfterUserSave(required struct $) {
    
    var userBean = $.event('userBean');
    var contactEmail=settingsManager.getSite( $.event('siteID') ).getContactEmail();
    
    if( userBean.getIsNew() ) {
        $.getBean('mailer').sendText("New User #userBean.getFname()# #userBean.getLName()# has signed up!",
            contactEmail,
            contactEmail,
            "New User #userBean.getFname()# #userBean.getLName()# has signed up!",
            $.event('siteID')
            );
    }
    
}

In the above example, we are sending a simple Email notification (directed towards the contact Email address of the site) that a new profile has been created. If you wanted to target a specific user sub-type (i.e. Class Extension) you could do that by using something like onAfterUserSubscriberSave().

Another example might be sending a notification each time a Change Set has been published:

public void function onAfterChangeSetPublish(required struct $) {
    
    var changesetBean = $.getBean('changesetBean');
    var userAdmin= $.getBean('user').loadBy(userName='admin',siteID=$.event('siteID'));
    
    if( userBean.getIsNew() ) {
        $.getBean('mailer').sendText("Change Set #changesetBean.getName()# has been published.",
            userAdmin.getEmail(),
            userAdmin.getEmail(),
            "Change Set #changesetBean.getName()# has been published.",
            $.event('siteID')
            );
    }        
}

Here we are sending a notification to the user "admin" that a Change Set has just been published. If we wanted to send notifications only in the case where the Change Set failed to publish, we could instead use onAfterChangeSetPublishFailure().

As you can see, sending custom notifications using Contextual Events inside of Mura is quite easy. You could easily extend these examples to write logs, update database records, or even modify the data before it is saved by using the "Before" version of the events.

Hope this was helpful in showing you how easy it is to customize your Mura site with enhanced notifications.

If you want to know more about Mura CMS Events, I'd suggest reading this blog on Mura Event Handlers, as well as the review the Event documentation.