Webiant Logo Webiant Logo
  1. No results found.

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

BaseAdminMenuCreatedEventConsumer.cs

using Nop.Services.Events;
using Nop.Services.Plugins;
using Nop.Web.Framework.Menu;

namespace Nop.Web.Framework.Events;

/// 
/// Base admin menu created event consumer
/// 
public abstract class BaseAdminMenuCreatedEventConsumer : IConsumer
{
    #region Fields

    protected readonly IPluginManager _pluginManager;

    #endregion

    #region Ctor

    /// 
    /// Ctor
    /// 
    protected BaseAdminMenuCreatedEventConsumer(IPluginManager pluginManager)
    {
        _pluginManager = pluginManager;
    }

    #endregion

    #region Utilities

    /// 
    /// Checks is the current customer has rights to access this menu item
    /// By default, always return true
    /// 
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the true if access is granted, otherwise false
    /// 
    protected virtual Task CheckAccessAsync()
    {
        return Task.FromResult(true);
    }

    /// 
    /// Gets the menu item
    /// 
    /// The instance of  interface
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the instance of 
    /// 
    protected virtual Task GetAdminMenuItemAsync(IPlugin plugin)
    {
        var menuItem = plugin?.GetAdminMenuItem();

        return Task.FromResult(menuItem);
    }

    #endregion

    #region Methods

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public virtual async Task HandleEventAsync(AdminMenuCreatedEvent eventMessage)
    {
        if (!await CheckAccessAsync())
            return;

        var plugin = await _pluginManager.LoadPluginBySystemNameAsync(PluginSystemName);

        //the LoadPluginBySystemNameAsync method returns only plugins that are already fully installed,
        //while the IConsumer event can be called before the installation is complete
        if (plugin == null)
            return;

        var newItem = await GetAdminMenuItemAsync(plugin);

        if (newItem == null)
            return;

        switch (InsertType)
        {
            case MenuItemInsertType.After:
                eventMessage.RootMenuItem.InsertAfter(AfterMenuSystemName, newItem);
                break;
            case MenuItemInsertType.Before:
                eventMessage.RootMenuItem.InsertBefore(BeforeMenuSystemName, newItem);
                break;
            case MenuItemInsertType.TryAfterThanBefore:
                if (!eventMessage.RootMenuItem.InsertAfter(AfterMenuSystemName, newItem))
                    eventMessage.RootMenuItem.InsertBefore(BeforeMenuSystemName, newItem);
                break;
            case MenuItemInsertType.TryBeforeThanAfter:
                if (!eventMessage.RootMenuItem.InsertBefore(BeforeMenuSystemName, newItem))
                    eventMessage.RootMenuItem.InsertAfter(AfterMenuSystemName, newItem);
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }
    }

    #endregion

    #region Properties

    /// 
    /// Gets the plugin system name
    /// 
    protected abstract string PluginSystemName { get; }

    /// 
    /// Menu item insertion type (by default: )
    /// 
    protected virtual MenuItemInsertType InsertType => MenuItemInsertType.Before;

    /// 
    /// The system name of the menu item after with need to insert the current one
    /// 
    protected virtual string AfterMenuSystemName => string.Empty;

    /// 
    /// The system name of the menu item before with need to insert the current one
    /// 
    protected virtual string BeforeMenuSystemName => string.Empty;

    #endregion
}