Skip to content

Commit 01dfdab

Browse files
Added Plugin
(V1.0) Added Plugin
1 parent 85c8859 commit 01dfdab

File tree

54 files changed

+16726
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+16726
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using Nop.Core;
4+
using Nop.Core.Infrastructure;
5+
using Nop.Services.Cms;
6+
using Nop.Services.Configuration;
7+
using Nop.Services.Localization;
8+
using Nop.Services.Media;
9+
using Nop.Services.Plugins;
10+
using Nop.Web.Framework.Infrastructure;
11+
12+
namespace Nop.Plugin.Widgets.AzPersonalizer
13+
{
14+
/// <summary>
15+
/// Represents the plugin implementation
16+
/// </summary>
17+
public class AzPersonalizerPlugin : BasePlugin, IWidgetPlugin
18+
{
19+
#region fields
20+
private readonly ILocalizationService _localizationService;
21+
private readonly IPictureService _pictureService;
22+
private readonly ISettingService _settingService;
23+
private readonly IWebHelper _webHelper;
24+
private readonly INopFileProvider _fileProvider;
25+
#endregion
26+
27+
#region ctor
28+
public AzPersonalizerPlugin(ILocalizationService localizationService,
29+
IPictureService pictureService,
30+
ISettingService settingService,
31+
IWebHelper webHelper,
32+
INopFileProvider fileProvider)
33+
{
34+
_localizationService = localizationService;
35+
_pictureService = pictureService;
36+
_settingService = settingService;
37+
_webHelper = webHelper;
38+
_fileProvider = fileProvider;
39+
}
40+
#endregion
41+
#region methods
42+
/// <summary>
43+
/// Gets widget zones where this widget should be rendered
44+
/// </summary>
45+
/// <returns>
46+
/// A task that represents the asynchronous operation
47+
/// The task result contains the widget zones
48+
/// </returns>
49+
public Task<IList<string>> GetWidgetZonesAsync()
50+
{
51+
return Task.FromResult<IList<string>>(new List<string> {
52+
PublicWidgetZones.ProductDetailsEssentialBottom
53+
});
54+
}
55+
56+
/// <summary>
57+
/// Gets a configuration page URL
58+
/// </summary>
59+
public override string GetConfigurationPageUrl()
60+
{
61+
return _webHelper.GetStoreLocation() + "Admin/AzPersonalizer/Configure";
62+
}
63+
64+
/// <summary>
65+
/// Gets a name of a view component for displaying widget
66+
/// </summary>
67+
/// <param name="widgetZone">Name of the widget zone</param>
68+
/// <returns>View component name</returns>
69+
public string GetWidgetViewComponentName(string widgetZone)
70+
{
71+
return "WidgetsAzPersonalizer";
72+
}
73+
74+
/// <summary>
75+
/// Install plugin
76+
/// </summary>
77+
/// <returns>A task that represents the asynchronous operation</returns>
78+
public override async Task InstallAsync()
79+
{
80+
81+
//settings
82+
var settings = new AzPersonalizerSettings
83+
{
84+
APIkey = "",
85+
Endpoint = ""
86+
87+
};
88+
await _settingService.SaveSettingAsync(settings);
89+
90+
await _localizationService.AddLocaleResourceAsync(new Dictionary<string, string>
91+
{
92+
["Plugins.Widgets.AzPersonalizer.ApiKey"] = "Api key",
93+
["Plugins.Widgets.AzPersonalizer.Endpoint"] = "Endpoint",
94+
95+
});
96+
97+
await base.InstallAsync();
98+
}
99+
100+
/// <summary>
101+
/// Uninstall plugin
102+
/// </summary>
103+
/// <returns>A task that represents the asynchronous operation</returns>
104+
public override async Task UninstallAsync()
105+
{
106+
//settings
107+
await _settingService.DeleteSettingAsync<AzPersonalizerSettings>();
108+
109+
//locales
110+
await _localizationService.DeleteLocaleResourcesAsync("Plugins.Widgets.AzPersonalizer");
111+
112+
await base.UninstallAsync();
113+
}
114+
115+
/// <summary>
116+
/// Gets a value indicating whether to hide this plugin on the widget list page in the admin area
117+
/// </summary>
118+
public bool HideInWidgetList => false;
119+
#endregion
120+
}
121+
122+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Nop.Core.Configuration;
2+
3+
namespace Nop.Plugin.Widgets.AzPersonalizer
4+
{
5+
/// <summary>
6+
/// Represents plugin settings
7+
/// </summary>
8+
public class AzPersonalizerSettings : ISettings
9+
{
10+
11+
/// <summary>
12+
/// Gets or sets the azure personalizer resource Api key
13+
/// </summary>
14+
public string APIkey { get; set; }
15+
16+
/// <summary>
17+
/// Gets or sets the azure personalizer resource endpoint
18+
/// </summary>
19+
public string Endpoint { get; set; }
20+
21+
22+
}
23+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Nop.Core;
7+
using Nop.Core.Domain.Catalog;
8+
using Nop.Plugin.Widgets.AzPersonalizer.Controllers;
9+
using Nop.Plugin.Widgets.AzPersonalizer.Models;
10+
using Nop.Plugin.Widgets.AzPersonalizer.Services;
11+
using Nop.Services.Catalog;
12+
using Nop.Services.Configuration;
13+
using Nop.Services.Logging;
14+
using Nop.Web.Factories;
15+
using Nop.Web.Framework.Components;
16+
using Nop.Web.Framework.Infrastructure;
17+
using Nop.Web.Models.Catalog;
18+
19+
namespace Nop.Plugin.Widgets.AzPersonalizer.Components
20+
{
21+
/// <summary>
22+
/// Represents the view component to place a widget into pages
23+
/// </summary>
24+
[ViewComponent(Name = "WidgetsAzPersonalizer")]
25+
public class WidgetsAzPersonalizerViewComponent : NopViewComponent
26+
{
27+
private readonly IStoreContext _storeContext;
28+
private readonly ISettingService _settingService;
29+
private readonly IWebHelper _webHelper;
30+
private readonly IProductService _productService;
31+
private readonly IProductModelFactory _productModelFactory;
32+
private readonly ILogger _logger;
33+
private readonly PersonalizerClientService _personalizerClientService;
34+
private static bool debugging = true;
35+
36+
37+
public WidgetsAzPersonalizerViewComponent(IStoreContext storeContext,
38+
ISettingService settingService,
39+
PersonalizerClientService personalizerClientService,
40+
IProductService productService,
41+
IProductModelFactory productModelFactory,
42+
IWebHelper webHelper,
43+
ILogger logger)
44+
{
45+
_logger = logger;
46+
_storeContext = storeContext;
47+
_settingService = settingService;
48+
_webHelper = webHelper;
49+
_personalizerClientService = personalizerClientService;
50+
_productService = productService;
51+
_productModelFactory = productModelFactory;
52+
}
53+
54+
/// <summary>
55+
/// Invoke view component
56+
/// </summary>
57+
/// <param name="widgetZone">Widget zone name</param>
58+
/// <param name="additionalData">Additional data</param>
59+
/// <returns>
60+
/// A task that represents the asynchronous operation
61+
/// The task result contains the view component result
62+
/// </returns>
63+
public async Task<IViewComponentResult> InvokeAsync(string widgetZone, object additionalData)
64+
{
65+
var azPersonalizerSettings = await _settingService.LoadSettingAsync<AzPersonalizerSettings>((await _storeContext.GetCurrentStoreAsync()).Id);
66+
if (widgetZone.Equals(PublicWidgetZones.ProductDetailsEssentialBottom))
67+
{
68+
string[] url = (_webHelper.GetThisPageUrl(true))?.Split("?");
69+
if (TempData["RewardID"] != null
70+
&& TempData["OrderedIDs"] != null
71+
&& url.Length > 1 && url[1].Contains("azrec")) // TODO remover url?
72+
{
73+
string id = TempData["RewardID"] is string rewardID ? rewardID : null;
74+
string pos = TempData["OrderedIDs"] is string position ? position : null;
75+
76+
await ProcessRewardAsync(id, pos, additionalData);
77+
}
78+
return await ReccomendedList(azPersonalizerSettings, additionalData);
79+
80+
}
81+
else
82+
{
83+
// While there is only one widget zone with reccomendations doesn't return anything. But to add
84+
// recomendations to multiple widget zones should add else if's.
85+
return View();
86+
}
87+
88+
}
89+
90+
91+
/// <summary>
92+
/// Prepares the view component with a list of Ranked Actions
93+
/// </summary>
94+
/// <param name="azPersonalizerSettings"></param>
95+
/// <param name="additionalData"></param>
96+
/// <returns>
97+
/// A task that represents the asynchronous operation
98+
/// The task result contains the view component result
99+
/// </returns>
100+
private async Task<IViewComponentResult> ReccomendedList(AzPersonalizerSettings azPersonalizerSettings, object additionalData)
101+
{
102+
103+
try
104+
{
105+
var a = await _personalizerClientService?.getRankedActions(additionalData);
106+
var model = a.Ranking;
107+
TempDataTokenController tdt = new TempDataTokenController(a.EventId); // Design mau?
108+
TempData["RewardID"] = a.EventId;
109+
IList<Product> products = new List<Product>(model.Count);
110+
string odds = a.EventId + ": ";
111+
for(int i = 0; i< model.Count; i++)
112+
{
113+
odds +=("(" + model[i]?.Id + "," + model[i].Probability +");");
114+
products.Insert(i,await _productService?.GetProductByIdAsync(int.Parse(model[i]?.Id)));
115+
tdt.AddId(model[i]?.Id);
116+
}
117+
TempData["OrderedIDs"] = tdt.OrderedIDs;
118+
if(debugging) await _logger.WarningAsync(odds);
119+
120+
return View("~/Plugins/Widgets.AzPersonalizer/Views/Reccomendations.cshtml", new ProductOverviewToRankingList( (await _productModelFactory.PrepareProductOverviewModelsAsync(products)).ToList(), a.EventId));
121+
122+
}catch(Exception e)
123+
{
124+
await _logger.ErrorAsync("Failed to get reccomendations");
125+
return View();
126+
}
127+
128+
}
129+
130+
/// <summary>
131+
/// Calculates and sends a reward to a given Event.
132+
/// </summary>
133+
/// <param name="ID"></param>
134+
/// <param name="OrderIDs"></param>
135+
/// <param name="additionalData"></param>
136+
/// <returns>An empty task.</returns>
137+
private async Task ProcessRewardAsync(string eventID, string OrderIDs, object additionalData)
138+
{
139+
var product = additionalData is ProductDetailsModel model ? model : null;
140+
if (string.IsNullOrEmpty(eventID) || string.IsNullOrEmpty(OrderIDs) || product == null)
141+
return;
142+
string[] products = OrderIDs.Split("-");
143+
for(int i = 0;i<products.Length; i++)
144+
{
145+
if(products[i] == product.Id.ToString()) // TODO mudar para .Contains?
146+
{
147+
await _personalizerClientService.RewardAnAction(eventID, i);
148+
return;
149+
}
150+
}
151+
152+
}
153+
154+
}
155+
}

0 commit comments

Comments
 (0)