/
UmbracoEventHandler.cs
130 lines (109 loc) · 4.87 KB
/
UmbracoEventHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
namespace Merchello.FastTrack.Ui
{
using System.Linq;
using Merchello.Core;
using Merchello.Core.Configuration;
using Merchello.Core.Events;
using Merchello.Core.Gateways.Payment;
using Merchello.Core.Models;
using Merchello.Core.Services;
using Merchello.Web.Models.VirtualContent;
using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Notification = Merchello.Core.Notification;
/// <summary>
/// Registers Umbraco event handlers.
/// </summary>
public class UmbracoEventHandler : ApplicationEventHandler
{
/// <summary>
/// Handles Umbraco Starting.
/// </summary>
/// <param name="umbracoApplication">
/// The <see cref="UmbracoApplicationBase"/>.
/// </param>
/// <param name="applicationContext">
/// Umbraco <see cref="ApplicationContext"/>.
/// </param>
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication,
ApplicationContext applicationContext)
{
base.ApplicationStarting(umbracoApplication, applicationContext);
// We handle the Initializing event so that we can set the parent node of the virtual content to the store
// so that published content queries in views will work correctly
ProductContentFactory.Initializing += ProductContentFactoryOnInitializing;
MemberService.Saved += MemberServiceSaved;
// ShipmentService.StatusChanged += ShipmentServiceStatusChanged;
}
///// <summary>
///// Example shipment shipped / delivered notification handler
///// </summary>
///// <param name="sender">
///// The sender.
///// </param>
///// <param name="e">
///// The <see cref="StatusChangeEventArgs{IShipment}"/>
///// </param>
//private void ShipmentServiceStatusChanged(IShipmentService sender, StatusChangeEventArgs<IShipment> e)
//{
// var validKeys = new[]
// {
// Core.Constants.DefaultKeys.ShipmentStatus.Delivered,
// Core.Constants.DefaultKeys.ShipmentStatus.Shipped
// };
// foreach (var shipment in e.StatusChangedEntities)
// {
// if (!validKeys.Contains(shipment.ShipmentStatus.Key)) continue;
// LogHelper.Info<UmbracoEventHandler>(string.Format("Raising notification trigger for shippment no. {0}", shipment.ShipmentNumber));
// Notification.Trigger("OrderShipped", shipment, Core.Observation.Topic.Notifications);
// }
//}
//// Event handler methods
/// <summary>
/// Handles the <see cref="ProductContentFactory"/> on initializing event.
/// </summary>
/// <param name="sender">
/// The <see cref="ProductContentFactory"/>.
/// </param>
/// <param name="e">
/// The <see cref="VirtualContentEventArgs"/>.
/// </param>
/// <remarks>
/// This is required to set the parent id of the virtual content
/// </remarks>
private static void ProductContentFactoryOnInitializing(ProductContentFactory sender, VirtualContentEventArgs e)
{
var store = ExampleUiHelper.Content.GetStoreRoot();
e.Parent = store;
}
/// <summary>
/// Copies first name, last name and email address from saved member to Merchello customer.
/// </summary>
/// <param name="sender">
/// The <see cref="IMemberService"/>.
/// </param>
/// <param name="e">
/// The saved <see cref="IMember"/>s.
/// </param>
private static void MemberServiceSaved(IMemberService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IMember> e)
{
var members = e.SavedEntities.ToArray();
// Allowed member types for Merchello customers
var customerMemberTypes = MerchelloConfiguration.Current.CustomerMemberTypes.ToArray();
// Get a reference to Merchello's customer service
var customerService = MerchelloContext.Current.Services.CustomerService;
foreach (var member in members)
{
// verify the member is a customer type
if (!customerMemberTypes.Contains(member.ContentTypeAlias)) continue;
var customer = customerService.GetByLoginName(member.Username);
if (customer == null) continue;
customer.FirstName = member.GetValue<string>("firstName") ?? string.Empty;
customer.LastName = member.GetValue<string>("lastName") ?? string.Empty;
customerService.Save(customer);
}
}
}
}