Webiant Logo Webiant Logo
  1. No results found.

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

WidgetModelFactory.cs

using Nop.Core;
using Nop.Services.Cms;
using Nop.Web.Areas.Admin.Infrastructure.Mapper.Extensions;
using Nop.Web.Areas.Admin.Models.Cms;
using Nop.Web.Framework.Models.Extensions;

namespace Nop.Web.Areas.Admin.Factories;

/// 
/// Represents the widget model factory implementation
/// 
public partial class WidgetModelFactory : IWidgetModelFactory
{
    #region Fields

    protected readonly IWidgetPluginManager _widgetPluginManager;
    protected readonly IWorkContext _workContext;

    #endregion

    #region Ctor

    public WidgetModelFactory(IWidgetPluginManager widgetPluginManager,
        IWorkContext workContext)
    {
        _widgetPluginManager = widgetPluginManager;
        _workContext = workContext;
    }

    #endregion

    #region Methods

    /// 
    /// Prepare widget search model
    /// 
    /// Widget search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the widget search model
    /// 
    public virtual Task PrepareWidgetSearchModelAsync(WidgetSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //prepare page parameters
        searchModel.SetGridPageSize();

        return Task.FromResult(searchModel);
    }

    /// 
    /// Prepare paged widget list model
    /// 
    /// Widget search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the widget list model
    /// 
    public virtual async Task PrepareWidgetListModelAsync(WidgetSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //get widgets
        var widgets = (await _widgetPluginManager.LoadAllPluginsAsync())
            .Where(widget => !widget.HideInWidgetList).ToList()
            .ToPagedList(searchModel);

        //prepare grid model
        var model = new WidgetListModel().PrepareToGrid(searchModel, widgets, () =>
        {
            return widgets.Select(widget =>
            {
                //fill in model values from the entity
                var widgetMethodModel = widget.ToPluginModel();

                //fill in additional values (not existing in the entity)
                widgetMethodModel.IsActive = _widgetPluginManager.IsPluginActive(widget);
                widgetMethodModel.ConfigurationUrl = widget.GetConfigurationPageUrl();

                return widgetMethodModel;
            });
        });

        return model;
    }

    #endregion
}