Mango.Nop.Plugins/Nop.Plugin.Misc.AIPlugin/FruitBankPlugin.cs

169 lines
6.4 KiB
C#
Raw Blame History

using FruitBank.Common.Server;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using Nop.Plugin.Misc.FruitBankPlugin.Components;
using Nop.Services.Cms;
using Nop.Services.Configuration;
using Nop.Services.Localization;
using Nop.Services.Plugins;
using Nop.Services.Security;
using Nop.Web.Framework.Infrastructure;
using Nop.Web.Framework.Menu;
namespace Nop.Plugin.Misc.FruitBankPlugin
{
/// <summary>
/// Main plugin class
/// </summary>
public class FruitBankPlugin : BasePlugin, IWidgetPlugin
{
protected readonly IActionContextAccessor _actionContextAccessor;
private readonly ISettingService _settingService;
//private readonly IWebHelper _webHelper;
protected readonly IPermissionService _permissionService;
protected readonly ILocalizationService _localizationService;
protected readonly IUrlHelperFactory _urlHelperFactory;
private readonly IAdminMenu _adminMenu;
//handle AdminMenuCreatedEvent
public FruitBankPlugin(IActionContextAccessor actionContextAccessor,
ISettingService settingService,
//IWebHelper webHelper,
ILocalizationService localizationService,
IPermissionService permissionService,
IUrlHelperFactory urlHelperFactory,
IAdminMenu adminMenu)
{
_actionContextAccessor = actionContextAccessor;
_settingService = settingService;
//_webHelper = webHelper;
_localizationService = localizationService;
_urlHelperFactory = urlHelperFactory;
_adminMenu = adminMenu;
_permissionService = permissionService;
}
// --- INSTALL ---
public override async Task InstallAsync()
{
//TODO: Add "Measuring" role - FruitBankConst.MeasuringRoleSystemName
//TODO: Add "NeedsToBeMeasured" product attribute if not exists
// Default settings
var settings = new OpenAiSettings
{
ApiKey = string.Empty
};
await _settingService.SaveSettingAsync(settings);
await base.InstallAsync();
}
// --- UNINSTALL ---
public override async Task UninstallAsync()
{
await _settingService.DeleteSettingAsync<OpenAiSettings>();
await base.UninstallAsync();
}
// --- WIDGETS ---
public bool HideInWidgetList => false;
public Task<IList<string>> GetWidgetZonesAsync()
{
return Task.FromResult<IList<string>>(new List<string> { PublicWidgetZones.ProductBoxAddinfoBefore, PublicWidgetZones.ProductDetailsBottom });
}
//public string GetWidgetViewComponentName(string widgetZone)
//{
// return "ProductAIWidget"; // A ViewComponent neve
//}
// --- ADMIN MEN<45> ---
//public async Task ManageSiteMapAsync(AdminMenuItem rootNode)
//{
// if (!await _permissionService.AuthorizeAsync(StandardPermission.Configuration.MANAGE_PLUGINS))
// return;
// var pluginNode = new AdminMenuItem
// {
// SystemName = "FruitBankPlugin.Configure",
// Title = "AI Assistant",
// Url = $"{_webHelper.GetStoreLocation()}Admin/FruitBankPluginAdmin/Configure",
// Visible = true
// };
// rootNode.ChildNodes.Add(pluginNode);
// //return Task.CompletedTask;
//}
public async Task ManageSiteMapAsync(AdminMenuItem rootNode)
{
if (!await _permissionService.AuthorizeAsync(StandardPermission.Configuration.MANAGE_PLUGINS))
return;
var configurationItem = rootNode.ChildNodes.FirstOrDefault(node => node.SystemName.Equals("Configuration"));
if (configurationItem is null)
return;
var shippingItem = configurationItem.ChildNodes.FirstOrDefault(node => node.SystemName.Equals("Shipping"));
var widgetsItem = configurationItem.ChildNodes.FirstOrDefault(node => node.SystemName.Equals("Widgets"));
if (shippingItem is null && widgetsItem is null)
return;
var index = shippingItem is not null ? configurationItem.ChildNodes.IndexOf(shippingItem) : -1;
if (index < 0)
index = widgetsItem is not null ? configurationItem.ChildNodes.IndexOf(widgetsItem) : -1;
if (index < 0)
return;
configurationItem.ChildNodes.Insert(index + 1, new AdminMenuItem
{
Visible = true,
SystemName = "AI plugins",
Title = await _localizationService.GetResourceAsync("Plugins.Misc.SignalRApi.Menu.AI"),
IconClass = "far fa-dot-circle",
ChildNodes = new List<AdminMenuItem>
{
new()
{
// SystemName = "FruitBankPlugin.Configure",
// Title = "AI Assistant",
// Url = $"{_webHelper.GetStoreLocation()}Admin/FruitBankPluginAdmin/Configure",
// Visible = true
Visible = true,
SystemName = PluginDescriptor.SystemName,
Title = PluginDescriptor.FriendlyName,
IconClass = "far fa-circle",
Url = _adminMenu.GetMenuItemUrl("FruitBankPlugin", "Configure"),
//Url = "Admin/SignalRApi/Configure",
//ControllerName = "SignalRApi",
//ActionName = "Configure",
//RouteValues = new RouteValueDictionary { { "area", AreaNames.ADMIN } }
}
}
});
}
public override string GetConfigurationPageUrl()
{
return _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext).RouteUrl("Plugin.Misc.FruitBankPlugin.Configure");
}
public Type GetWidgetViewComponent(string widgetZone)
{
if (widgetZone is null)
throw new ArgumentNullException(nameof(widgetZone));
var zones = GetWidgetZonesAsync().Result;
return zones.Any(widgetZone.Equals) ? typeof(ProductAIWidgetViewComponent) : null;
}
}
}