using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Mvc.Rendering; using Sarand.Net.Standard.Base.Bus; using Sarand.Net.Standard.Base.Core; using Sarand.Net.Standard.Base.Dbas; using Sarand.Net.Standard.Base.Utils; using Saxonite.Base.App; using Saxonite.Net.Core.Web.Base.App; using Saxonite.Net.Standard.Base.App; using Saxonite.Net.Standard.Base.Consoles; using Saxonite.Net.Standard.Base.Mgr; using System; using System.Collections.Generic; namespace Saxonite.Net.Core.Web.Base.Models { //======================================================================= public class SaxModel_Base { //------------------------------------------------------------------- // Data members. private SaxInternals internals; private readonly List infoMessages = new List(); private readonly List warningMessages = new List(); private readonly List errorMessages = new List(); //------------------------------------------------------------------- // Protected construction. protected SaxModel_Base(SaxLocaliser localiser) { Localiser = localiser; } //------------------------------------------------------------------- // Application properties. private SaxApplication_Web App { get { return SaxApplication_Web.WebApp; } } protected SaxSessionConfig SessionConfig { get { return App.SessionConfig; } } protected SaxSystem_Web System { get { return App.System_Web; } } public String SessionCookieName { get { return App.SessionCookieName; } } public SaxSessionData SessionData { get; set; } public SaxSessionContext SessionContext { get { return SessionData?.SessionContext ?? App.SessionContext; } } public DbUpdateInfo DbUpdateInfo { get { return SessionData?.DbUpdateInfo ?? new DbUpdateInfo(null, "", SaxSystem.ComputerName); } } public bool DebugMode { get { return SaxSystem.DebugMode; } } public SaxLocaliser Localiser { get; } //------------------------------------------------------------------- // Business components. protected SaxInternals Internals { get { if (internals == null) internals = new SaxInternals(SessionContext); return internals; } } // Consoles. protected SaxDataCacheConsole DataCacheConsole { get { return Internals.DataCacheConsole; } } protected SaxActivityConsole ActivityConsole { get { return Internals.ActivityConsole; } } // Managers. protected SaxReferenceMgr ReferenceMgr { get { return Internals.ReferenceMgr; } } protected SaxAccountMgr AccountMgr { get { return Internals.AccountMgr; } } protected SaxActivityMgr ActivityMgr { get { return Internals.ActivityMgr; } } protected SaxItemDataMgr ItemDataMgr { get { return Internals.ItemDataMgr; } } // System codes. public List DataItemTypeCodes { get { return Internals.DataItemTypeCodes; } } public List SportParticipationCodes { get { return Internals.SportParticipationCodes; } } public List SportOfficiatingCodes { get { return Internals.SportOfficiatingCodes; } } public List RegionTypeCodes { get { return Internals.RegionTypeCodes; } } public List UserRoleCodes { get { return Internals.UserRoleCodes; } } public List OrganisationTypeCodes { get { return Internals.OrganisationTypeCodes; } } public List OrganisationCategoryCodes { get { return Internals.OrganisationCategoryCodes; } } public List OrganisationRoleCodes { get { return Internals.OrganisationRoleCodes; } } public List OpportunityTypeCodes { get { return Internals.OpportunityTypeCodes; } } public List ActivityReactionCodes { get { return Internals.ActivityReactionCodes; } } //------------------------------------------------------------------- // Page configuration. public bool OK { get { return !HasMessages(); } } public virtual bool ProhibitCache { get; set; } = true; public String PageCaption { get; set; } public String InstanceVersion { get { return !StringUtil.IsEmpty(App.SessionConfig.Instance) ? App.SessionConfig.Instance + " v" + Sax.Version.GetFullVersion() : "v" + Sax.Version.GetAppVersion(); } } public bool IncludeGoogleAnalytics { get; set; } = false; //------------------------------------------------------------------- // Security. public bool ValidUserSession { get { return SessionData?.ValidUserSession ?? false; } } public bool AwaitingLogin { get { return !ValidUserSession; } } public bool KeepLoggedIn { get { return !AwaitingLogin && SessionData.User.KeepLoggedIn; } } /// Maximum length allowed for password entry: sign-up, change password etc. public int PasswordMaxLength { get; } = 32; // Account actions. // NB The form proerty will never be used - account actions processed within page. public String AccountAction { get; set; } public IEnumerable GetAccountActions() { List items = new List(); if (SessionData?.ValidUserSession ?? false) { items.Add("Profile", Localiser["label.view.profile"]); items.Add("ChangePassword", Localiser["label.change.password"]); items.Add("SignOut", Localiser["label.sign.out"]); } return new SelectList(items, "Code", "Desc"); } //------------------------------------------------------------------- /// /// Form control value max length. /// In many cases the database columns allow plenty of extra space for the value, in case they need to be encrypted. /// Hence, several cases where the value entered in the control isn't allowed the full database length. /// public int GetControlMaxLength(SaxDbDfn_Core.ColumnIdx column) { int maxLength = (int)SaxDbDfn_Core.GetColTextLen(column); switch (column) { case SaxDbDfn_Core.ColumnIdx.UserAccount_EmailAddress: return Math.Min(maxLength, 60); case SaxDbDfn_Core.ColumnIdx.UserAccount_FirstName: case SaxDbDfn_Core.ColumnIdx.UserAccount_LastName: return Math.Min(maxLength, 40); case SaxDbDfn_Core.ColumnIdx.Organisation_Name: return Math.Min(maxLength, 80); case SaxDbDfn_Core.ColumnIdx.UserAccount_PhoneMain: case SaxDbDfn_Core.ColumnIdx.UserAccount_PhoneMobile: case SaxDbDfn_Core.ColumnIdx.Organisation_PhoneMain: case SaxDbDfn_Core.ColumnIdx.Organisation_PhoneAlt: return Math.Min(maxLength, 20); //case SaxDbDfn.ColumnIdx.UserAccount_Postcode: //case SaxDbDfn.ColumnIdx.ItemAddress_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()); } } //------------------------------------------------------------------- // Link URLs. public class LinksData { public String Facebook { get; } = "https://www.facebook.com/saxonite/"; public String LinkedIn { get; } = "https://www.linkedin.com/company/saxonite/about/"; } public LinksData Links { get; } = new LinksData(); //------------------------------------------------------------------- // Form data. /// List of Yes/No options for dropdown. public IEnumerable YesNoOptionsForSelect { get { return new SelectList(SaxSystem.GetCodesOfType(Sax.CodeType.YesNo), "Code", "Desc"); } } /// List of Sport Participation options for dropdown. public IEnumerable SportParticipationCodesForSelect { get { return new SelectList(SportParticipationCodes, "Code", "Desc"); } } /// List of Sport Officiating options for dropdown. public IEnumerable SportOfficiatingCodesForSelect { get { return new SelectList(SportOfficiatingCodes, "Code", "Desc"); } } /// List of Region Types for dropdown. public IEnumerable RegionTypesForSelect { get { return new SelectList(RegionTypeCodes, "Code", "Desc"); } } /// List of titles for dropdown. public IEnumerable TitlesForSelect { get { return new SelectList(SaxSystem.GetCodesOfType(Sax.CodeType.Title), "Code", "Desc"); } } /// List of Organisation Types for dropdown. public IEnumerable OrganisationTypesForSelect { get { return new SelectList(OrganisationTypeCodes, "Code", "Desc"); } } /// List of Organisation Categories for dropdown. public IEnumerable OrganisationCategoriesForSelect { get { return new SelectList(OrganisationCategoryCodes, "Code", "Desc"); } } //------------------------------------------------------------------- // Version parameter for includes. public String HrefVersion { get { DateTime versionDate = SaxSystem.AssemblyBuildAt; if (!DateUtil.IsValid(versionDate) || SaxSystem.DebugMode) versionDate = DateTime.Now; return DateUtil.Format(versionDate, "yyyyMMdd" /*+ (SacsSystem.Debug ? "HHmm" : "")*/); } } // Additional includes. public List AdditionalFiles_CSS { get; set; } = new List(); public List AdditionalFiles_JS { get; set; } = new List(); //------------------------------------------------------------------- // Redirection locations. public class RedirectLocationData { // Properties populated in Controller_Base.OnActionExecuted. public String Base { get; internal set; } public String Home { get; internal set; } public String Home_SessionExpired { get; internal set; } } public RedirectLocationData RedirectLocation { get; } = new RedirectLocationData(); //------------------------------------------------------------------- // Paging properties. private 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 ushort PageLength { get { return paging.PageLength; } set { paging.PageLength = value; } } public uint PageCount { get { return (PageLength > 0 ? (uint)Math.Ceiling((decimal)ItemTotal / PageLength) : 0); } } public ushort ItemCount { get; set; } public ulong ItemTotal { get; set; } } private DataPaging Paging { get; } = new DataPaging(); public bool DataPaged { get { return Paging.IsPaged; } } public uint DataPageNo { get { return Paging.PageNo; } set { Paging.PageNo = value; } } public ushort DataPageLength { get { return Paging.PageLength; } set { Paging.PageLength = value; } } public uint DataPageCount { get { return (DataPageLength > 0 ? (uint)Math.Ceiling((decimal)DataItemTotal / DataPageLength) : 0); } } public ushort DataItemCount { get; set; } public ulong DataItemTotal { get; set; } public void PopulatePagingTo(DbQuery.Paging resultsPaging) { Paging.PopulatePagingTo(resultsPaging); } //------------------------------------------------------------------- // Form properties. /// /// User session index to identify the site resources, eg. database. /// Equivalent to the Site index on the login form. /// Populates value on logout form to identify default site for next login. /// public ushort SessionIdx { get { return SessionContext.Index; } } public String FormAction { get; set; } //------------------------------------------------------------------- // Messages. //------------------------------------------------------------------- public bool HasMessages() { return infoMessages.Count > 0 || warningMessages.Count > 0 || errorMessages.Count > 0; } public List InfoMessages { get { return new List(infoMessages); } } public List WarningMessages { get { return new List(warningMessages); } } public List ErrorMessages { get { return new List(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); } //------------------------------------------------------------------- } //======================================================================= }