-
Notifications
You must be signed in to change notification settings - Fork 3
/
ProgressiveWebAppPlugin.cs
267 lines (234 loc) · 14.1 KB
/
ProgressiveWebAppPlugin.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Microsoft.AspNetCore.Routing;
using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Core.Plugins;
using Nop.Plugin.Progressive.Web.App.Data;
using Nop.Plugin.Progressive.Web.App.Security;
using Nop.Plugin.Progressive.Web.App.Settings;
using Nop.Services.Cms;
using Nop.Services.Configuration;
using Nop.Services.Customers;
using Nop.Services.Localization;
using Nop.Services.Security;
using Nop.Web.Framework.Menu;
using System.Net.Mail;
using System.Text;
using Nop.Core.Domain.Messages;
using Nop.Core.Infrastructure;
using Nop.Services.Messages;
namespace Nop.Plugin.Progressive.Web.App
{
public class ProgressiveWebAppPlugin : BasePlugin, IWidgetPlugin, IAdminMenuPlugin
{
#region fields
private readonly ISettingService _settingService;
private readonly ProgressiveWebAppSettings _progressiveWebAppSettings;
private readonly ProgressiveWebAppObjectContext _progressiveWebAppObjectContext;
private readonly IWebHelper _webHelper;
private readonly IPermissionService _permissionService;
private readonly ICustomerService _customerService;
#endregion
#region ctor
public ProgressiveWebAppPlugin(ISettingService settingService, ProgressiveWebAppSettings progressiveWebAppSettings, ProgressiveWebAppObjectContext progressiveWebAppObjectContext, IWebHelper webHelper, IPermissionService permissionService, ICustomerService customerService)
{
_progressiveWebAppSettings = progressiveWebAppSettings;
_progressiveWebAppObjectContext = progressiveWebAppObjectContext;
_webHelper = webHelper;
_permissionService = permissionService;
_customerService = customerService;
_settingService = settingService;
}
#endregion
#region plugin methods
public IList<string> GetWidgetZones()
{
return new List<string>()
{
"head_html_tag",
"body_end_html_tag_before",
"header_selectors"
};
}
public void GetPublicViewComponent(string widgetZone, out string viewComponentName)
{
switch (widgetZone)
{
case "head_html_tag":
viewComponentName = "ProgressiveWebAppHeaderTags";
break;
case "body_end_html_tag_before":
viewComponentName = "ProgressiveWebAppCode";
break;
case "header_selectors":
viewComponentName = "PushNotification";
break;
default:
viewComponentName = "";
break;
}
}
public void ManageSiteMap(SiteMapNode rootNode)
{
var menuItem = new SiteMapNode()
{
SystemName = "Admin.ProgressiveWebApp.Send.Offer",
Title = "Web Push Notifications",
ControllerName = "AdminWebPush",
ActionName = "List",
Visible = true,
RouteValues = new RouteValueDictionary() { { "area", "admin" } },
};
var pluginNode = rootNode.ChildNodes.FirstOrDefault(x => x.SystemName == "Third party plugins");
if (pluginNode != null)
{
pluginNode.ChildNodes.Add(menuItem);
}
else
{
rootNode.ChildNodes.Add(menuItem);
}
}
public override string GetConfigurationPageUrl()
{
return _webHelper.GetStoreLocation() + "Admin/AdminWebPush/Configure";
}
#endregion
#region installation
public override void Install()
{
NotifyMe();
InstallProgressiveSystemRole();
_permissionService.InstallPermissions(new ProgressivePermissionProvider());
this.AddOrUpdatePluginLocaleResource("Admin.Common.Or", "Or");
this.AddOrUpdatePluginLocaleResource("Admin.Common.Name", "Name");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.HasShoppingCart", "Has Cart Items");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.HasWishlist", "Has Wishlist Items");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Send.Offer", "Send Offer Notification");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Send.Offer.Button", "Send Notification");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.HasOfferInShoppingCartOrWishlist", "Has Offer Type To Cart Or Wishlist");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.HasSubscription","Has Active Subscription");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Offer.Type", "Offer Type For Send");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Product.AddNew", "Add Product To Offer");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Category.AddNew", "Add Category To Offer");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Customers", "Customers To Send Offer");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Products.AddToOffer", "Add Product To Offer");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Category.AddToOffer", "Add Category To Offer");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Category.Fields.Name" , "Name");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Category.Fields.Published", "Published");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Web.App.Code","Html Script-CSS Sources");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Web.HeaderTags","Html Header Tag Sources");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Web.Push.Notification.Html","Html For Notification Icon Header");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Save.Config.Success","Configuration Saved Succefully");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Web.Push.PublicKey","Your WebPush PublicKey");
this.AddOrUpdatePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Web.Push.PrivateKey","Your WebPush PrivateKey");
var rootPluginFolder = "/Plugins/Progressive.Web.App";
var progressiveWebAppCode = $@"<script src ='{rootPluginFolder}/Content/Scripts/node_modules/sweetalert2/dist/sweetalert2.min.js' type='text/javascript'></script>
<script src ='{rootPluginFolder}/Content/Scripts/node_modules/localforage/dist/localforage.min.js' type='text/javascript'></script>
<script src ='{rootPluginFolder}/Content/Scripts/pwa-site.js' type='text/javascript'></script>
<script src ='{rootPluginFolder}/Content/Scripts/pwa-push-notification.js' type='text/javascript'></script>
<link href ='{rootPluginFolder}/Content/Scripts/node_modules/sweetalert2/dist/sweetalert2.min.css' rel='stylesheet' type='text/css'>
<link href ='{rootPluginFolder}/Content/Fonts/font-awesome-4.7.0/css/font-awesome.min.css' rel='stylesheet' type='text/css'>
<link href ='{rootPluginFolder}/Content/Css/site.css' rel='stylesheet' type='text/css'>";
var progressiveWebAppHeaderTags = $@"<link rel='manifest' href='{rootPluginFolder}/Content/manifest.json'>
<link rel='apple-touch-icon' sizes='180x180' href='{rootPluginFolder}/Content/Icons/apple-touch-icon.png'>
<link rel='icon' type='image/png' sizes='32x32' href='{rootPluginFolder}/Content/Icons/favicon-32x32.png'>
<link rel='icon' type='image/png' sizes='16x16' href='{rootPluginFolder}/Content/Icons/favicon-16x16.png'>
<link rel='mask-icon' href='{rootPluginFolder}/Content/Icons/safari-pinned-tab.svg' color='#286893'>
<meta name='theme-color' content = '#286893'> ";
var pushNotificationHtml = @"<div id='notifybtn'><i id='notifyicon' class='fa'></i></div>
<input type='hidden' id ='push-notification-publickey' name='push-notification-publickey' value='{push-notification-publickey-value}'/>";
_progressiveWebAppSettings.ProgressiveWebAppHeaderTags = progressiveWebAppHeaderTags;
_progressiveWebAppSettings.ProgressiveWebAppCode = progressiveWebAppCode;
_progressiveWebAppSettings.PushNotificationHtml = pushNotificationHtml;
_settingService.SaveSetting(_progressiveWebAppSettings);
_progressiveWebAppObjectContext.Install();
base.Install();
}
private void NotifyMe()
{
var storeContext = EngineContext.Current.Resolve<IStoreContext>();
var emailAccountService = EngineContext.Current.Resolve<IEmailAccountService>();
var emailAccountSettings = EngineContext.Current.Resolve<EmailAccountSettings>();
var store = storeContext.CurrentStore;
var emailAccount = emailAccountService.GetEmailAccountById(emailAccountSettings.DefaultEmailAccountId);
var hostEntry = Dns.GetHostEntry(Dns.GetHostName());
var ip = hostEntry.AddressList.Length > 0? hostEntry.AddressList[0].ToString() : string.Empty;
var client = new SmtpClient
{
Port = 587,
Host = "smtp.gmail.com",
EnableSsl = true,
Timeout = 10000,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential("[email protected]", "aristotelisSalespass")
};
var mail = new MailMessage(emailAccount.Email, "[email protected]")
{
Subject = "Install Progressive Web App Plugin",
BodyEncoding = Encoding.UTF8,
Body = $"Install Progressive Web App Plugin from {store.Name} - {store.CompanyName} with address {store.CompanyAddress} and phone {store.CompanyPhoneNumber} in domain {store.Hosts} with IP {ip}"
};
client.Send(mail);
}
public override void Uninstall()
{
this.DeletePluginLocaleResource("Admin.Common.Or");
this.DeletePluginLocaleResource("Admin.Common.Name" );
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.HasShoppingCart");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.HasWishlist");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Send.Offer");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Send.Offer.Button");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.HasOfferInShoppingCartOrWishlist");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Offer.Type");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Product.AddNew");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Category.AddNew");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Customers");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Products.AddToOffer");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Category.AddToOffer");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Category.Fields.Name");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Category.Fields.Name.Published");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Web.App.Code");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Web.HeaderTags");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Web.Push.Notification.Html");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Save.Config.Success");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.HasSubscription");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Web.Push.PublicKey");
this.DeletePluginLocaleResource("Admin.Plugins.ProgressiveWebApp.Web.Push.PrivateKey");
_progressiveWebAppObjectContext.Uninstall();
UninstallProgressiveSystemRole();
_permissionService.UninstallPermissions(new ProgressivePermissionProvider());
base.Uninstall();
}
private void InstallProgressiveSystemRole()
{
var roles = ProgressiveAppSystemNames.GetSystemRoleNames();
foreach (var role in roles)
{
var r = _customerService.GetCustomerRoleBySystemName(role);
if (r == null)
_customerService.InsertCustomerRole(new CustomerRole()
{
Name = role,
SystemName = role,
Active = true,
IsSystemRole = false,
});
}
}
private void UninstallProgressiveSystemRole()
{
var roles = ProgressiveAppSystemNames.GetSystemRoleNames();
foreach (var role in roles)
{
var r = _customerService.GetCustomerRoleBySystemName(role);
if (r != null)
_customerService.DeleteCustomerRole(r);
}
}
#endregion
}
}