Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

OmnisendPlugin.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using Nop.Core.Domain.Cms;
using Nop.Core.Domain.Media;
using Nop.Plugin.Misc.Omnisend.Components;
using Nop.Services.Cms;
using Nop.Services.Common;
using Nop.Services.Configuration;
using Nop.Services.Localization;
using Nop.Services.Plugins;
using Nop.Web.Framework.Infrastructure;

namespace Nop.Plugin.Misc.Omnisend;

/// 
/// Represents Omnisend plugin
/// 
public class OmnisendPlugin : BasePlugin, IMiscPlugin, IWidgetPlugin
{
    #region Fields

    private readonly IActionContextAccessor _actionContextAccessor;
    private readonly ILocalizationService _localizationService;
    private readonly ISettingService _settingService;
    private readonly IUrlHelperFactory _urlHelperFactory;
    private readonly WidgetSettings _widgetSettings;

    #endregion

    #region Ctor

    public OmnisendPlugin(IActionContextAccessor actionContextAccessor,
        ILocalizationService localizationService,
        ISettingService settingService,
        IUrlHelperFactory urlHelperFactory,
        WidgetSettings widgetSettings)
    {
        _actionContextAccessor = actionContextAccessor;
        _localizationService = localizationService;
        _settingService = settingService;
        _urlHelperFactory = urlHelperFactory;
        _widgetSettings = widgetSettings;
    }

    #endregion

    #region Methods

    /// 
    /// Gets a configuration page URL
    /// 
    public override string GetConfigurationPageUrl()
    {
        if (_actionContextAccessor.ActionContext != null)
            return _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext)
                .RouteUrl(OmnisendDefaults.ConfigurationRouteName);

        return base.GetConfigurationPageUrl();
    }

    /// 
    /// Gets a type of a view component for displaying widget
    /// 
    /// Name of the widget zone
    /// View component type
    public Type GetWidgetViewComponent(string widgetZone)
    {
        if (widgetZone is null)
            throw new ArgumentNullException(nameof(widgetZone));

        var zones = GetWidgetZonesAsync().Result;

        return zones.Any(widgetZone.Equals) ? typeof(WidgetsOmnisendViewComponent) : null;
    }

    /// 
    /// Gets widget zones where this widget should be rendered
    /// 
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the widget zones
    /// 
    public Task> GetWidgetZonesAsync()
    {
        return Task.FromResult>(new List { PublicWidgetZones.BodyStartHtmlTagAfter, PublicWidgetZones.ProductDetailsBottom });
    }

    /// 
    /// Install plugin
    /// 
    /// A task that represents the asynchronous operation
    public override async Task InstallAsync()
    {
        //ensure MediaSettings.UseAbsoluteImagePath is enabled (used for images uploading)
        await _settingService.SetSettingAsync($"{nameof(MediaSettings)}.{nameof(MediaSettings.UseAbsoluteImagePath)}", true, clearCache: false);

        var omnisendTrackingScript =
            @$"

";
        var omnisendProductPageScript =
            @$"

";
        var identifyContactScript = @$"";

        var settings = new OmnisendSettings
        {
            ApiKey = "",
            TrackingScript = omnisendTrackingScript,
            ProductScript = omnisendProductPageScript,
            IdentifyContactScript = identifyContactScript,
            UseTracking = true,
            LogRequests = false,
            LogRequestErrors = true,
            PageSize = OmnisendDefaults.PageSize,
            RequestTimeout = OmnisendDefaults.RequestTimeout
        };

        await _settingService.SaveSettingAsync(settings);

        if (!_widgetSettings.ActiveWidgetSystemNames.Contains(OmnisendDefaults.SystemName))
        {
            _widgetSettings.ActiveWidgetSystemNames.Add(OmnisendDefaults.SystemName);
            await _settingService.SaveSettingAsync(_widgetSettings);
        }

        await _localizationService.AddOrUpdateLocaleResourceAsync(new Dictionary
        {
            ["Plugins.Misc.Omnisend.CantGetBrandId"] = "Failed to get the required data from the Omnisend server. Please check if the API key is correct and save the settings again. Error details can be found in the Log page",
            ["Plugins.Misc.Omnisend.Credentials"] = "Credentials",
            ["Plugins.Misc.Omnisend.Synchronization"] = "Synchronization",
            ["Plugins.Misc.Omnisend.SyncContacts"] = "Sync contacts",
            ["Plugins.Misc.Omnisend.SyncProducts"] = "Sync products",
            ["Plugins.Misc.Omnisend.SyncOrders"] = "Sync orders",
            ["Plugins.Misc.Omnisend.SyncCategories"] = "Sync categories",
            ["Plugins.Misc.Omnisend.BatchesInProcess"] = "Batches in process",
            ["Plugins.Misc.Omnisend.BatchesInProcess.StartedAt"] = "Started at",
            ["Plugins.Misc.Omnisend.BatchesInProcess.Status"] = "Status",
            ["Plugins.Misc.Omnisend.BatchesInProcess.TotalCount"] = "Total count",
            ["Plugins.Misc.Omnisend.BatchesInProcess.FinishedCount"] = "Finished count",
            ["Plugins.Misc.Omnisend.BatchesInProcess.ErrorsCount"] = "Error count",
            ["Plugins.Misc.Omnisend.BatchesInProcess.EndedAt"] = "Ended at",
            ["Plugins.Misc.Omnisend.BatchesInProcess.SyncType"] = "Type of sync",
            ["Plugins.Misc.Omnisend.Fields.ApiKey"] = "API key",
            ["Plugins.Misc.Omnisend.Fields.ApiKey.Hint"] = "Enter the Omnisend integration API key.",
            ["Plugins.Misc.Omnisend.Fields.ApiKey.Required"] = "API key is required",
            ["Plugins.Misc.Omnisend.Fields.UseTracking"] = "Use tracking",
            ["Plugins.Misc.Omnisend.Fields.UseTracking.Hint"] = "Determine whether to use tracking to get statistics with Omnisend, such as the behavior of individual subsribers on your website, including purchases made, movements on your site and subsequent segmentation."
        });

        await base.InstallAsync();
    }

    /// 
    /// Uninstall the plugin
    /// 
    /// A task that represents the asynchronous operation
    public override async Task UninstallAsync()
    {
        if (_widgetSettings.ActiveWidgetSystemNames.Contains(OmnisendDefaults.SystemName))
        {
            _widgetSettings.ActiveWidgetSystemNames.Remove(OmnisendDefaults.SystemName);
            await _settingService.SaveSettingAsync(_widgetSettings);
        }
        await _settingService.DeleteSettingAsync();

        await _localizationService.DeleteLocaleResourcesAsync("Plugins.Misc.Omnisend");

        await base.UninstallAsync();
    }

    #endregion

    #region Properties

    /// 
    /// Gets a value indicating whether to hide this plugin on the widget list page in the admin area
    /// 
    public bool HideInWidgetList => true;

    #endregion
}