2014-01-13 16:13:53 +11:00
using System ;
using System.Globalization ;
using Umbraco.Core ;
using Umbraco.Core.Configuration ;
2014-05-12 14:32:34 +10:00
using Umbraco.Core.Logging ;
2014-01-13 16:13:53 +11:00
using Umbraco.Core.Models.EntityBase ;
2014-05-12 14:32:34 +10:00
using Umbraco.Core.Models.Membership ;
2014-01-13 16:13:53 +11:00
using Umbraco.Core.Services ;
using umbraco ;
using umbraco.BusinessLogic.Actions ;
using umbraco.interfaces ;
namespace Umbraco.Web
{
2014-05-12 14:32:34 +10:00
//NOTE: all of these require an UmbracoContext because currently to send the notifications we need an HttpContext, this is based on legacy code
// for which probably requires updating so that these can be sent outside of the http context.
2014-01-13 16:13:53 +11:00
internal static class NotificationServiceExtensions
{
internal static void SendNotification ( this INotificationService service , IUmbracoEntity entity , IAction action , ApplicationContext applicationContext )
{
2014-05-12 14:32:34 +10:00
if ( UmbracoContext . Current == null )
{
LogHelper . Warn ( typeof ( NotificationServiceExtensions ), "Cannot send notifications, there is no current UmbracoContext" );
return ;
}
service . SendNotification ( entity , action , UmbracoContext . Current );
2014-01-13 16:13:53 +11:00
}
internal static void SendNotification ( this INotificationService service , IUmbracoEntity entity , IAction action , UmbracoContext umbracoContext )
{
2014-05-12 14:32:34 +10:00
if ( umbracoContext == null )
{
LogHelper . Warn ( typeof ( NotificationServiceExtensions ), "Cannot send notifications, there is no current UmbracoContext" );
return ;
}
2014-01-13 16:13:53 +11:00
service . SendNotification ( entity , action , umbracoContext , umbracoContext . Application );
}
internal static void SendNotification ( this INotificationService service , IUmbracoEntity entity , IAction action , UmbracoContext umbracoContext , ApplicationContext applicationContext )
{
2014-05-12 14:32:34 +10:00
if ( umbracoContext == null )
{
LogHelper . Warn ( typeof ( NotificationServiceExtensions ), "Cannot send notifications, there is no current UmbracoContext" );
return ;
}
var user = umbracoContext . Security . CurrentUser ;
//if there is no current user, then use the admin
if ( user == null )
{
2015-01-12 21:45:52 +11:00
LogHelper . Debug ( typeof ( NotificationServiceExtensions ), "There is no current Umbraco user logged in, the notifications will be sent from the administrator" );
2014-05-12 14:32:34 +10:00
user = applicationContext . Services . UserService . GetUserById ( 0 );
if ( user == null )
{
LogHelper . Warn ( typeof ( NotificationServiceExtensions ), "Noticiations can not be sent, no admin user with id 0 could be resolved" );
return ;
}
}
service . SendNotification ( user , entity , action , umbracoContext , applicationContext );
}
internal static void SendNotification ( this INotificationService service , IUser sender , IUmbracoEntity entity , IAction action , UmbracoContext umbracoContext , ApplicationContext applicationContext )
{
if ( sender == null ) throw new ArgumentNullException ( "sender" );
2014-01-13 16:13:53 +11:00
if ( umbracoContext == null ) throw new ArgumentNullException ( "umbracoContext" );
if ( applicationContext == null ) throw new ArgumentNullException ( "applicationContext" );
2014-05-12 14:32:34 +10:00
2014-01-13 16:13:53 +11:00
applicationContext . Services . NotificationService . SendNotifications (
2014-05-12 14:32:34 +10:00
sender ,
2014-01-13 16:13:53 +11:00
entity ,
action . Letter . ToString ( CultureInfo . InvariantCulture ),
ui . Text ( "actions" , action . Alias ),
umbracoContext . HttpContext ,
( mailingUser , strings ) => ui . Text ( "notifications" , "mailSubject" , strings , mailingUser ),
( mailingUser , strings ) => UmbracoConfig . For . UmbracoSettings (). Content . DisableHtmlEmail
? ui . Text ( "notifications" , "mailBody" , strings , mailingUser )
: ui . Text ( "notifications" , "mailBodyHtml" , strings , mailingUser ));
}
}
}