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.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 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 AddressTypeCodes { get { return Internals.AddressTypeCodes; } }
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");
}
//-------------------------------------------------------------------
///
/// 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());
}
}
//-------------------------------------------------------------------
// 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);
}
//-------------------------------------------------------------------
// 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.FindAll(item => item.Type != (int)Sax.RegionType.Universe && item.Type != (int)Sax.RegionType.Galaxy), "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.
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 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; }
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 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);
}
//-------------------------------------------------------------------
}
//=======================================================================
}