using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Rendering; using Sarand.Net.Standard.Base.App; using Sarand.Net.Standard.Base.Bus; using Sarand.Net.Standard.Base.Core; 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.Helpers; using System; using System.Collections.Generic; namespace Saxonite.Net.Core.Web.Admin.Models { //======================================================================= public class Model_Organisation : SaxModel_Admin { //------------------------------------------------------------------- // Construction. public Model_Organisation(SaxLocaliser localiser) : base(localiser) { AdditionalFiles_JS.Add("js/sax-org.js"); Address = new ModelData_Address(this, ""); } //------------------------------------------------------------------- // Organisation filter properties. public class FilterData { public String Organisation { get; set; } public String OrganisationTypeCode { get; set; } public String OrganisationCategoryCode { get; set; } public String OrganisationSportId { get; set; } public String OrganisationSportName { get; set; } public String OrganisationRegionId { get; set; } public String OrganisationRegionName { get; set; } public String OrganisationStatusCategoryCode { get; set; } } public FilterData Filter { get; } = new FilterData(); public Sax.OrganisationType FilterOrganisationType { get { return (Sax.OrganisationType)OrganisationTypeCodes.GetTypeForCode(Filter.OrganisationTypeCode); } } public Sax.OrganisationCategory FilterOrganisationCategory { get { return (Sax.OrganisationCategory)OrganisationCategoryCodes.GetTypeForCode(Filter.OrganisationCategoryCode); } } public SaxOrganisation.StatusCat FilterOrganisationStatusCategory { get { return EnumUtil.ParseEnum(Filter.OrganisationStatusCategoryCode, SaxOrganisation.StatusCat.Any); } } /// Organisation Status Categories for filter. public IEnumerable OrganisationFilterStatusCategoriesForSelect { get { List statusCategories = new List(); statusCategories.Add(SaxOrganisation.StatusCat.Any.ToString(), Localiser["label.any"]); statusCategories.Add(SaxOrganisation.StatusCat.AwaitingOnly.ToString(), "Awaiting"); statusCategories.Add(SaxOrganisation.StatusCat.ActiveOnly.ToString(), "Active"); statusCategories.Add(SaxOrganisation.StatusCat.InactiveOnly.ToString(), "Inactive"); statusCategories.Add(SaxOrganisation.StatusCat.NotDeleted.ToString(), "Not deleted"); statusCategories.Add(SaxOrganisation.StatusCat.DeletedOnly.ToString(), "Deleted"); return new SelectList(statusCategories, "Code", "Desc", SaxOrganisation.StatusCat.Any.ToString()); } } //------------------------------------------------------------------- // Organisation properties. public String OrganisationId { get; set; } public String OrganisationName { get; set; } public String OrganisationTypeValue { get; set; } public Sax.OrganisationType OrganisationType { get { return (Sax.OrganisationType)OrganisationTypeCodes.GetTypeForCode(OrganisationTypeValue); } } public String OrganisationCategoryValue { get; set; } public Sax.OrganisationCategory OrganisationCategory { get { return (Sax.OrganisationCategory)OrganisationCategoryCodes.GetTypeForCode(OrganisationCategoryValue); } } public String OrganisationSportId { get; set; } public String OrganisationRegionIdPrimary { get; set; } public String OrganisationAbout { get; set; } public String OrganisationFacilities { get; set; } // Common properties. public String OrganisationNotes { get; set; } public String OrganisationStatusCode { get; set; } public SaStatus OrganisationStatus { get { return SacsSystem.GetStatusForCode(OrganisationStatusCode); } } // Contact Methods. /// Placeholder field value to indicate the number of Contact Methods in form (even if none are populated). public String ContactMethods { get; set; } public String[] ContactMethod { get; set; } public String[] ContactValue { get; set; } /// Contact methods to be populated during validation. public SaxContactData ContactData { get; set; } // Address. public ModelData_Address Address { get; } //------------------------------------------------------------------- // Images. public List OrganisationLogoImage { get; set; } //------------------------------------------------------------------- // Utilities. public bool IsSportMandatory() { return SaxOrganisation.IsSportMandatory(OrganisationCategory); } //------------------------------------------------------------------- // Validation. //------------------------------------------------------------------- // Organisation validation. public bool ValidateOrganisationData(SaxController_Base.AjaxResponse_Data responseData) { String fn = this.GetType().Name + ".ValidateOrganisationData"; // Organisation name. if (OrganisationName != null) { responseData.SetControlValue(nameof(OrganisationName), OrganisationName); if (StringUtil.IsEmpty(OrganisationName)) responseData.SetControlError(nameof(OrganisationName), Localiser["msg.validation.mandatory.value"]); else if (StringUtil.GetLength(OrganisationName) > GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Organisation_Name)) responseData.SetControlError(nameof(OrganisationName), Localiser["msg.validation.maximum.length", GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Organisation_Name)]); } // Organisation type. if (OrganisationTypeValue != null) { if (StringUtil.IsEmpty(OrganisationTypeValue)) responseData.SetControlError(nameof(OrganisationTypeValue), Localiser["msg.validation.mandatory.value"]); } // Organisation category. if (OrganisationCategoryValue != null) { if (StringUtil.IsEmpty(OrganisationCategoryValue)) responseData.SetControlError(nameof(OrganisationCategoryValue), Localiser["msg.validation.mandatory.value"]); } // Organisation sport. if (OrganisationSportId != null) { if (SysId.IsValid(OrganisationSportId.ToSysId()) || IsSportMandatory()) { using (SaxSport sport = ReferenceMgr.GetSportById(OrganisationSportId.ToSysId())) { if (sport == null) responseData.SetControlError(nameof(OrganisationSportId), Localiser["msg.validation.invalid.value"]); } } } // Organisation primary region. if (OrganisationRegionIdPrimary != null) { using (SaxRegion region = ReferenceMgr.GetRegionById(OrganisationRegionIdPrimary.ToSysId())) { if (region == null) responseData.SetControlError(nameof(OrganisationRegionIdPrimary), Localiser["msg.validation.invalid.value"]); else if (region.RegionType != Sax.RegionType.Country && region.RegionType != Sax.RegionType.Continent && region.RegionType != Sax.RegionType.World) responseData.SetControlError(nameof(OrganisationRegionIdPrimary), "Organisation region must be a country or international region"); } if (!StringUtil.IsEmpty(OrganisationName)) { // Name must be unqiue within region. SaxOrganisation.Params prmOrganisation = new SaxOrganisation.Params(); if (SysId.IsValid(OrganisationId.ToSysId())) prmOrganisation.IdsExcluded.Add(OrganisationId.ToSysId()); prmOrganisation.NameFull = OrganisationName; prmOrganisation.RegionIdsIncluded.Add(OrganisationRegionIdPrimary.ToSysId()); prmOrganisation.StatusCat = SaxOrganisation.StatusCat.Any; if (AccountMgr.GetOrganisationCount(prmOrganisation) > 0) responseData.SetControlError(nameof(OrganisationName), Localiser["msg.validation.duplicate.value"]); } } // Organisation about. if (OrganisationAbout != null) { responseData.SetControlValue(nameof(OrganisationAbout), OrganisationAbout); if (StringUtil.GetLength(OrganisationAbout) > GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Organisation_About)) responseData.SetControlError(nameof(OrganisationAbout), Localiser["msg.validation.maximum.length", GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Organisation_About)]); } // Organisation facilities. if (OrganisationFacilities != null) { responseData.SetControlValue(nameof(OrganisationFacilities), OrganisationFacilities); if (StringUtil.GetLength(OrganisationFacilities) > GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Organisation_Facilities)) responseData.SetControlError(nameof(OrganisationFacilities), Localiser["msg.validation.maximum.length", GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Organisation_Facilities)]); } // Contact Methods. if (ContactMethods != null) { ContactData = new SaxContactData(""); ContactMethod = ContactMethod ?? new String[0]; ContactValue = ContactValue ?? new String[0]; for (int idx = 0; idx < ContactMethod.Length && idx < ContactValue.Length; idx++) { String methodValue = ContactMethod[idx]; String value = ContactValue[idx]; if (methodValue == null && value == null) continue; Sax.ContactMethod method = EnumUtil.ParseEnum(methodValue, Sax.ContactMethod.Unknown); if (method > 0 && !value.IsEmpty() && SaxContactMethodHelper.IsValidContactMethodValue(method, value)) ContactData.SetContactMethodValue(method, value); else SaLogger.Warn(fn + " Invalid Contact Method data. Method: " + methodValue + ". Value: " + value); } if (ContactData.Methods.Count != ContactMethods.ToInt()) { SaLogger.Warn(fn + " Inconsistent Contact Methods received. Expected: " + ContactMethods + ". Received: " + ContactData.Methods.Count); responseData.SetMessage(Localiser["caption.contact.method"], "System error"); } } // Organisation notes. if (OrganisationNotes != null) { responseData.SetControlValue(nameof(OrganisationNotes), OrganisationNotes); if (StringUtil.GetLength(OrganisationNotes) > GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Organisation_Notes)) responseData.SetControlError(nameof(OrganisationNotes), Localiser["msg.validation.maximum.length", GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Organisation_Notes)]); } // Organisation status. if (OrganisationStatusCode != null) { if (StringUtil.IsEmpty(OrganisationStatusCode)) responseData.SetControlError(nameof(OrganisationStatus), Localiser["msg.validation.mandatory.value"]); else if (!SaxOrganisation.ValidStatuses.Contains(OrganisationStatus)) responseData.SetControlError(nameof(OrganisationStatus), Localiser["msg.validation.invalid.value"]); } // Validate address fields. if (Address != null) Address.ValidateAddressData(this, responseData); return !responseData.HasControlErrors(); } //------------------------------------------------------------------- } //======================================================================= }