Files
2025-02-26 18:43:18 +00:00

187 lines
7.1 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Rendering;
using Sarand.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.CodeTypes;
using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Dbas;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Sacs.Web.Components
{
//=======================================================================
public abstract class SaModel : SacsDisposable
{
//-------------------------------------------------------------------
// Data members.
private readonly List<String> infoMessages = new List<string>();
private readonly List<String> warningMessages = new List<string>();
private readonly List<String> errorMessages = new List<string>();
//-------------------------------------------------------------------
/// <summary>
/// Protected construction - use derived objects.
/// </summary>
protected SaModel()
{
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
}
}
//---------------------------------------------------------------
// Properties.
public bool DebugMode { get; } = SacsSystem.DebugMode;
public bool OK { get { return !HasMessages(); } }
public virtual bool ProhibitCache { get; set; } = true;
public String PageCaption { get; set; }
public bool IncludeGoogleAnalytics { get; set; } = false;
//-------------------------------------------------------------------
// Page configuration.
// Views can be registered to avoid multiple inclusions in a page.
private HashSet<String> registeredViews = new HashSet<String>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Views can be registered to avoid multiple inclusions in a page.
/// <para>Only generate view content if this method returns true.</para>
/// </summary>
public bool RegisterView(ViewContext viewContext)
{
// View identity.
String viewPath = viewContext.ExecutingFilePath;
if (StringUtil.IsBlank(viewPath)) return false;
// Action identity.
Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor controllerActionDescriptor = viewContext.ActionDescriptor as Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor;
String actionName = viewContext.ActionDescriptor.DisplayName;
if (controllerActionDescriptor != null)
actionName = $"{controllerActionDescriptor.ControllerName}.{controllerActionDescriptor.ActionName}";
// Check if view already registered.
if (registeredViews.Contains(viewPath))
{
SaLogger.Debug($"{SysUtil.Fn} View already registered: {viewPath}. Action: {actionName}.");
return false;
}
// Attempt to add this view to those registered on this page, returning false if already registered.
return registeredViews.Add(viewPath);
}
//-------------------------------------------------------------------
// Version parameter for includes.
public String HrefVersion
{
get
{
DateTime versionDate = SacsSystem.AssemblyBuildAt;
if (!DateUtil.IsValid(versionDate) || SacsSystem.DebugMode)
versionDate = DateTime.Now;
return DateUtil.Format(versionDate, "yyyyMMdd" /*+ (SacsSystem.Debug ? "HHmm" : "")*/);
}
}
// Additional includes.
public HashSet<String> AdditionalFiles_CSS { get; set; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
public HashSet<String> AdditionalFiles_JS { get; set; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
//-------------------------------------------------------------------
// Redirection locations.
public class RedirectLocationData
{
// Properties populated in Controller_Base.OnActionExecuted.
public String Base { get; set; }
public String Home { get; set; }
public String Home_SessionExpired { get; set; }
}
public RedirectLocationData RedirectLocation { get; } = new RedirectLocationData();
//-------------------------------------------------------------------
// Paging properties.
public class DataPaging
{
private DbQuery.Paging paging { get; } = new DbQuery.Paging();
public void PopulatePagingTo(DbQuery.Paging resultsPaging)
{
resultsPaging.PageLength = paging.PageLength;
resultsPaging.PageNo = paging.PageNo;
}
public bool IsPaged { get { return paging.Paged; } }
public uint PageNo { get { return paging.PageNo; } set { paging.PageNo = value; } }
public String PageNoValue { get { return paging.PageNo.ToString(); } set { paging.PageNo = (uint)StringUtil.ToInt(value); } }
public ushort PageLength { get { return paging.PageLength; } set { paging.PageLength = value; } }
public String PageLengthValue { get { return paging.PageLength.ToString(); } set { paging.PageLength = (ushort)StringUtil.ToShort(value); } }
public uint PageCount { get { return (PageLength > 0 ? (uint)Math.Ceiling((decimal)ItemTotal / paging.PageLength) : 0); } }
public ushort ItemCount { get; set; }
private ulong itemTotal = 0;
public ulong ItemTotal
{
get { return itemTotal; }
set
{
itemTotal = value;
// Check whether the total number of items is less than the current page and reduce page no if needed.
if (IsPaged && (PageNo - 1) * PageLength > itemTotal)
PageNo = (uint)((itemTotal - 1) / PageLength + 1);
}
}
}
public DataPaging Paging { get; } = new DataPaging();
public void PopulatePagingTo(DbQuery.Paging resultsPaging)
{
Paging.PopulatePagingTo(resultsPaging);
}
//-------------------------------------------------------------------
// View data.
/// <summary>Empty list of values for dropdown.</summary>
public IEnumerable<SelectListItem> GetEmptyListForSelect()
{
return new SelectList(Enumerable.Empty<SelectListItem>());
}
//-------------------------------------------------------------------
// Text & localisation.
public abstract String GetText(String textKey, String defaultText);
//-------------------------------------------------------------------
// Messages.
//-------------------------------------------------------------------
public bool HasMessages()
{
return infoMessages.Count > 0 || warningMessages.Count > 0 || errorMessages.Count > 0;
}
public List<String> InfoMessages { get { return new List<string>(infoMessages); } }
public List<String> WarningMessages { get { return new List<string>(warningMessages); } }
public List<String> ErrorMessages { get { return new List<string>(errorMessages); } }
//-------------------------------------------------------------------
// Form messages.
public void AddInfoMessage(String message)
{
if (StringUtil.IsEmpty(StringUtil.TrimAll(message))) return;
infoMessages.Add(message);
}
public void AddWarningMessage(String message)
{
if (StringUtil.IsEmpty(StringUtil.TrimAll(message))) return;
warningMessages.Add(message);
}
public void AddErrorMessage(String message)
{
if (StringUtil.IsEmpty(StringUtil.TrimAll(message))) return;
errorMessages.Add(message);
}
//---------------------------------------------------------------
}
//=======================================================================
}