using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; 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.Core.Web.Base.Controllers; using Saxonite.Net.Standard.Base.App; using Saxonite.Net.Standard.Base.Bus; using Saxonite.Net.Standard.Base.Consoles; using Saxonite.Net.Standard.Base.Mgr; using System; using System.Collections.Generic; using System.Globalization; 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 SaxAppConfig AppConfig { get { return App.AppConfig; } } protected SaxSystem_Web System { get { return App.System_Web; } } public String SessionCookieName { get { return App.SessionCookieName; } } public SaxLocaliser Localiser { get; } public bool DebugMode { get { return SaxSystem.DebugMode; } } // Session properties. 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); } } // Request properties. public SaxController_Base Controller { get; set; } public CultureInfo SessionCulture { get { return Controller?.SessionCulture ?? CultureInfo.InvariantCulture; } } //------------------------------------------------------------------- // 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.AppConfig.Instance) ? App.AppConfig.Instance + " v" + Sax.Version.GetFullVersion() : "v" + Sax.Version.GetAppVersion(); } } public bool IncludeGoogleAnalytics { get; set; } = false; //------------------------------------------------------------------- // Business components. protected SaxInternals Internals { get { if (internals == null) internals = new SaxInternals(SessionContext); return internals; } } // Consoles. protected SaxDataCacheConsole DataCacheConsole { get { return Internals.DataCacheConsole; } } protected SaxSessionDataConsole SessionDataConsole { get { return Internals.SessionDataConsole; } } protected SaxActivityConsole ActivityConsole { get { return Internals.ActivityConsole; } } // Managers. public SaxReferenceMgr ReferenceMgr { get { return Internals.ReferenceMgr; } } public SaxAccountMgr AccountMgr { get { return Internals.AccountMgr; } } public SaxActivityMgr ActivityMgr { get { return Internals.ActivityMgr; } } public SaxItemDataMgr ItemDataMgr { get { return Internals.ItemDataMgr; } } // System codes. public List DataItemTypeCodes { get { return Internals.DataItemTypeCodes; } } public List YesNoCodes { get { return Internals.YesNoCodes; } } public List SportSpecialityLabelCodes { get { return Internals.SportSpecialityLabelCodes; } } 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 AddressTypeCodes { get { return Internals.AddressTypeCodes; } } public List EventTypeCodes { get { return Internals.EventTypeCodes; } } public List OpportunityTypeCodes { get { return Internals.OpportunityTypeCodes; } } public List ActivityReactionCodes { get { return Internals.ActivityReactionCodes; } } //------------------------------------------------------------------- // Common data components. // User Accounts. public SaxUserAccount GetUserAccount(SysId userAccountId) { return Internals.GetUserAccount(userAccountId); } public String GetUserAccountDisplayName(SysId userAccountId) { return Internals.GetUserAccountDisplayName(userAccountId); } // Organisations. public SaxOrganisation GetOrganisation(SysId organisationId) { return Internals.GetOrganisation(organisationId); } public String GetOrganisationName(SysId organisationId) { return Internals.GetOrganisationName(organisationId); } //------------------------------------------------------------------- // 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"); } //------------------------------------------------------------------- // 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(); //------------------------------------------------------------------- // Link URLs. public class LinksData { public String Facebook { get; } = "https://www.facebook.com/sportierre/"; public String LinkedIn { get; } = "https://www.linkedin.com/company/sportierre/about/"; } public LinksData Links { get; } = new LinksData(); //------------------------------------------------------------------- // 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()); } } //------------------------------------------------------------------- // 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); } //------------------------------------------------------------------- /// /// 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; } //------------------------------------------------------------------- // Display data - Dates. public String FormatDateForDisplay(DateTime value) { return DateUtil.Format(value, "d-MMM-yy", SessionCulture); } public String FormatDateTimeForDisplay(DateTime value) { return StringUtil.TrimAll(DateUtil.Format(value, "d-MMM-yy HH:mm", SessionCulture)); } //------------------------------------------------------------------- // 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 DateUtil.Format(value, "dd-MMM-yyyy", SessionCulture); } public DateTime ParseDateFromForm(String value) { return DateUtil.ParseExact(value, "dd-MMM-yyyy", SessionCulture); } //------------------------------------------------------------------- // Form data. //------------------------------------------------------------------- // Common form values. public String FormAction { get; set; } //------------------------------------------------------------------- /// List of Yes/No options for dropdown. public IEnumerable YesNoOptionsForSelect { get { List values = new List(); foreach (TypeCodeDesc yesNo in YesNoCodes) values.Add(new SelectListItem(Localiser.GetYesNoCaption((Sax.YesNo)yesNo.Type), yesNo.Code)); return values; } } /// List of Sport Participation options for dropdown. public IEnumerable SportSpecialityLabelCodesForSelect { get { List values = new List(); values.Add(new SelectListItem("Default", "")); foreach (TypeCodeDesc sportSpecialityLabel in SportSpecialityLabelCodes) values.Add(new SelectListItem(Localiser.GetSportSpecialityLabelCaption((Sax.SportSpecialityLabel)sportSpecialityLabel.Type), sportSpecialityLabel.Code)); return values; } } /// List of Sport Participation options for dropdown. public IEnumerable SportParticipationCodesForSelect { get { List values = new List(); foreach (TypeCodeDesc sportParticipation in SportParticipationCodes) values.Add(new SelectListItem(Localiser.GetSportParticipationCaption((Sax.SportParticipation)sportParticipation.Type), sportParticipation.Code)); return values; } } /// List of Sport Officiating options for dropdown. public IEnumerable SportOfficiatingCodesForSelect { get { List values = new List(); foreach (TypeCodeDesc sportOfficiating in SportOfficiatingCodes) values.Add(new SelectListItem(Localiser.GetSportOfficiatingCaption((Sax.SportOfficiating)sportOfficiating.Type), sportOfficiating.Code)); return values; } } /// List of Region Types for dropdown. public IEnumerable RegionTypesForSelect { get { List values = new List(); foreach (TypeCodeDesc organisationType in RegionTypeCodes.FindAll(item => item.Type != (int)Sax.RegionType.Universe && item.Type != (int)Sax.RegionType.Galaxy)) values.Add(new SelectListItem(Localiser.GetRegionTypeCaption((Sax.RegionType)organisationType.Type), organisationType.Code)); return values; } } /// 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 { List values = new List(); foreach (TypeCodeDesc organisationType in OrganisationTypeCodes) values.Add(new SelectListItem(Localiser.GetOrganisationTypeCaption((Sax.OrganisationType)organisationType.Type), organisationType.Code)); return values; } } /// /// List of Organisation Categories for dropdown. /// NB Does not include 'Partner' or 'Other'. /// public IEnumerable OrganisationCategoriesForSelect { get { List values = new List(); foreach (TypeCodeDesc organisationCategory in OrganisationCategoryCodes.FindAll(item => item.Type != (int)Sax.OrganisationCategory.Partner && item.Type != (int)Sax.OrganisationCategory.Other)) values.Add(new SelectListItem(Localiser.GetOrganisationCategoryCaption((Sax.OrganisationCategory)organisationCategory.Type), organisationCategory.Code)); return values; } } /// /// List of Organisation Categories for Admin dropdown. /// Includes complete set of categories. /// public IEnumerable AdminOrganisationCategoriesForSelect { get { List values = new List(); foreach (TypeCodeDesc organisationCategory in OrganisationCategoryCodes) values.Add(new SelectListItem(Localiser.GetOrganisationCategoryCaption((Sax.OrganisationCategory)organisationCategory.Type), organisationCategory.Code)); return values; } } //------------------------------------------------------------------- // 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); } //------------------------------------------------------------------- } //======================================================================= }