Files
BusinessMgr-2019/SaBusMgrCoreWeb/Web/Components/SbmModel_Base.cs
T

263 lines
11 KiB
C#

using DocumentFormat.OpenXml.Drawing;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Sacs.Web.Components;
using Sarand.BusinessMgr.Net.Core.Web.App;
using Sarand.BusinessMgr.Net.Standard.Base.App;
using Sarand.BusinessMgr.Net.Standard.Base.Bus;
using Sarand.BusinessMgr.Net.Standard.Base.Consoles;
using Sarand.BusinessMgr.Net.Standard.Base.Core;
using Sarand.BusinessMgr.Net.Standard.Base.Mgr;
using Sarand.BusinessMgr.Net.Standard.Base.Mgr.Control;
using Sarand.Net.Standard.Base.CodeTypes;
using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Dbas;
using Sarand.Net.Standard.Base.Diary;
using Sarand.Net.Standard.Base.Interfaces;
using Sarand.Net.Standard.Base.ItemData;
using Sarand.Net.Standard.Base.ItemDocs;
using Sarand.Net.Standard.Base.ItemOptions;
using Sarand.Net.Standard.Base.ReferenceData;
using Sarand.Net.Standard.Base.Security;
using Sarand.Net.Standard.Base.UpdateLog;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace Sarand.BusinessMgr.Net.Core.Web.Components
{
//=======================================================================
public abstract class SbmModel_Base : SaModel
{
//-------------------------------------------------------------------
// Data members.
private SbmInternals_Control internals_Control;
//-------------------------------------------------------------------
// Protected construction.
protected SbmModel_Base(SbmLocaliser localiser)
{
Localiser = localiser;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (internals_Control != null) internals_Control.Dispose(); internals_Control = null;
}
base.Dispose(disposing);
}
//-------------------------------------------------------------------
// Application properties.
private SbmApplication_Web App { get { return SbmGlobals_Web.Application; } }
protected App.SbmAppConfig AppConfig { get { return App.AppConfig; } }
protected SbmSystem_Web System { get { return App.System_Web; } }
public string SessionCookieName { get { return App.SessionCookieName; } }
protected SbmSessionData_Web SessionData_Web { get; private set; }
internal void SetSessionData(SbmSessionData_Web sessionData) { SessionData_Web = sessionData; }
protected SbmSessionContext SessionContext { get { return SessionData_Web?.SessionContext ?? null; } }
protected DbUpdateInfo DbUpdateInfo { get { return SessionData_Web?.DbUpdateInfo ?? new DbUpdateInfo(null, "", Sarand.Net.Standard.Base.App.SacsSystem.ComputerName); } }
protected SbmLocaliser Localiser { get; }
protected virtual SbmInstanceSchemaConsole InstanceSchemaConsole { get; } = null;
// Request properties.
protected SbmController_Web Controller { get; private set; }
internal void SetController(SbmController_Web controller) { Controller = controller; }
protected CultureInfo SessionCulture { get { return Controller?.SessionCulture ?? CultureInfo.InvariantCulture; } }
//-------------------------------------------------------------------
// Page configuration.
public string InstanceVersion { get { return !App.AppConfig.Instance.IsEmpty() ? App.AppConfig.Instance + " v" + Sbm.Version.GetFullVersion() : "v" + Sbm.Version.GetAppVersion(); } }
//-------------------------------------------------------------------
// Text & localisation.
public override String GetText(String textKey, String defaultText)
{
String text = Localiser[textKey];
if (StringUtil.IsBlank(text) || text.EqualsCI(textKey))
return defaultText;
return text;
}
//-------------------------------------------------------------------
// Business components.
// NB Keep these accessors protected to avoid them being called by the model binding logic before the session data is ready.
protected SbmInternals_Control Internals_Control { get { if (internals_Control == null) internals_Control = new SbmInternals_Control(); return internals_Control; } }
// Control.
protected SbmcAccountMgr ControlAccountMgr { get { return Internals_Control.ControlAccountMgr; } }
protected SbmcInstanceMgr ControlInstanceMgr { get { return Internals_Control.ControlInstanceMgr; } }
protected SbmcItemMgr ControlItemMgr { get { return Internals_Control.ControlItemMgr; } }
//-------------------------------------------------------------------
// Security.
protected bool ValidUserSession { get { return SessionData_Web?.ValidUser ?? false; } }
public bool AwaitingLogin { get { return !ValidUserSession; } }
public bool KeepLoggedIn { get { return !AwaitingLogin && (SessionData_Web?.KeepLoggedIn ?? false); } }
/// <summary>Maximum length allowed for password entry: sign-up, change password etc.</summary>
public int PasswordMaxLength { get; } = 32;
//-------------------------------------------------------------------
// Control max lengths - based on database tabke column length.
/// <summary>
/// Form control value max length - Security tables.
/// <para>In many cases the database columns allow plenty of extra space for the value, in case they need to be encrypted.</para>
/// <para>Hence, several cases where the value entered in the control isn't allowed the full database length.</para>
/// </summary>
public virtual int GetControlMaxLength(SaSecurityDbDfn.ColumnIdx column)
{
int maxLength = (int)(InstanceSchemaConsole?.GetColTextLen(column) ?? SaSecurityDbDfn.GetColTextLen(column));
switch (column)
{
case SaSecurityDbDfn.ColumnIdx.SaSecItem_EmailAddress:
return Math.Min(maxLength, 100);
}
return maxLength;
}
/// <summary>
/// Form control value max length - Code Type tables.
/// <para>In many cases the database columns allow plenty of extra space for the value, in case they need to be encrypted.</para>
/// <para>Hence, several cases where the value entered in the control isn't allowed the full database length.</para>
/// </summary>
public virtual int GetControlMaxLength(SaCodeTypeDbDfn.ColumnIdx column)
{
int maxLength = (int)(InstanceSchemaConsole?.GetColTextLen(column) ?? SaCodeTypeDbDfn.GetColTextLen(column));
switch (column)
{
case SaCodeTypeDbDfn.ColumnIdx.SaCodeType_Name:
case SaCodeTypeDbDfn.ColumnIdx.SaCode_Description:
return Math.Min(maxLength, 100);
}
return maxLength;
}
/// <summary>
/// Form control value max length - Item Option tables.
/// <para>In many cases the database columns allow plenty of extra space for the value, in case they need to be encrypted.</para>
/// <para>Hence, several cases where the value entered in the control isn't allowed the full database length.</para>
/// </summary>
public virtual int GetControlMaxLength(SaItemOptionDbDfn.ColumnIdx column)
{
int maxLength = (int)(InstanceSchemaConsole?.GetColTextLen(column) ?? SaItemOptionDbDfn.GetColTextLen(column));
switch (column)
{
case SaItemOptionDbDfn.ColumnIdx.SaItemOption_Name:
case SaItemOptionDbDfn.ColumnIdx.SaItemOption_Context:
return Math.Min(maxLength, 100);
}
return maxLength;
}
/// <summary>
/// Form control value max length - BusinessMgr tables.
/// <para>In many cases the database columns allow plenty of extra space for the value, in case they need to be encrypted.</para>
/// <para>Hence, several cases where the value entered in the control isn't allowed the full database length.</para>
/// </summary>
public virtual int GetControlMaxLength(SbmDbDfn.ColumnIdx column)
{
int maxLength = (int)(InstanceSchemaConsole?.GetColTextLen(column) ?? SbmDbDfn.GetColTextLen(column));
switch (column)
{
case SbmDbDfn.ColumnIdx.Affiliate_Email:
return Math.Min(maxLength, 100);
case SbmDbDfn.ColumnIdx.Affiliate_FirstName:
case SbmDbDfn.ColumnIdx.Affiliate_LastName:
return Math.Min(maxLength, 40);
case SbmDbDfn.ColumnIdx.Affiliate_Name:
return Math.Min(maxLength, 80);
case SbmDbDfn.ColumnIdx.Affiliate_PhoneMain:
case SbmDbDfn.ColumnIdx.Affiliate_PhoneMobile:
case SbmDbDfn.ColumnIdx.Affiliate_PhoneAlt:
return Math.Min(maxLength, 20);
case SbmDbDfn.ColumnIdx.Address_Postcode:
return Math.Min(maxLength, 16);
}
return maxLength;
}
//-------------------------------------------------------------------
// Copyright notice.
public HtmlString CopyrightNotice
{
get
{
int initialYear = 2020;
int currentYear = DateTime.Today.Year;
return Localiser.GetHtmlText("copyright.symbol.year.html", (initialYear != currentYear ? initialYear + "-" : "") + currentYear.ToString());
}
}
//-------------------------------------------------------------------
// Display data - Dates.
public string FormatDateForDisplay(DateTime? value)
{
return value.Format("d-MMM-yy", SessionCulture);
}
public string FormatDateTimeForDisplay(DateTime? value)
{
return value.Format("d-MMM-yy HH:mm", SessionCulture).TrimAll();
}
//-------------------------------------------------------------------
// Form data - Dates.
// Format may depend on locale, but as long as these methods are used consistently,
// there should be consistent formatting, display and parsing of date values.
public string CalendarDateFormat
{
get
{
// JQuery-UI DatePicker date format.
// Format:
// d Single digit day of month
// dd Double digit day of month
// D 3 letter day of week abbreviation.
// DD Day of week.
// m Single digit month.
// mm Double digit month.
// M 3 letter month abbreviation.
// MM Month name.
// yy 4 digit year.
// Examples:
// dd/mm/yy 07/04/2012
// dd-M-yy 07-Apr-2012
// d-M-yy 7-Apr-2012
return "dd-M-yy";
}
}
public string FormatDateForForm(DateTime value)
{
return value.Format("dd-MMM-yyyy");
}
public DateTime ParseDateFromForm(string value)
{
return DateUtil.ParseExact(value, "dd-MMM-yyyy");
}
//-------------------------------------------------------------------
// Link URLs.
public class LinksData
{
public string Facebook { get; } = "https://www.facebook.com/BusinessMgr/";
public string LinkedIn { get; } = "https://www.linkedin.com/company/BusinessMgr/about/";
}
public LinksData Links { get; } = new LinksData();
//-------------------------------------------------------------------
// Form properties.
/// <summary>
/// User session index to identify the site resources, eg. database.
/// <para>Equivalent to the Site index on the login form.</para>
/// <para>Populates value on logout form to identify default site for next login.</para>
/// </summary>
public ushort SessionIdx { get { return SessionContext?.Index ?? 0; } }
public string FormAction { get; set; }
//-------------------------------------------------------------------
}
//=======================================================================
}