public IActionResult OnGet() { var initResult = Init(); if (initResult != null) { return(initResult); } InitPage(); if (ErpEntity == null) { return(NotFound()); } if (String.IsNullOrWhiteSpace(ReturnUrl)) { ReturnUrl = $"/sdk/objects/entity/r/{ParentRecordId}/rl/fields/c/select"; } HeaderToolbar.AddRange(AdminPageUtils.GetEntityAdminSubNav(ErpEntity, "fields")); ErpRequestContext.PageContext = PageContext; BeforeRender(); return(Page()); }
public void PageInit() { Init(); #region << Init Page >> var pageServ = new PageService(); ErpPage = pageServ.GetPage(RecordId ?? Guid.Empty); if (ErpPage != null) { Weight = ErpPage.Weight; Label = ErpPage.Label; LabelTranslations = ErpPage.LabelTranslations; Name = ErpPage.Name; IconClass = ErpPage.IconClass; System = ErpPage.System; Type = ErpPage.Type; AppId = ErpPage.AppId; EntityId = ErpPage.EntityId; AreaId = ErpPage.AreaId; NodeId = ErpPage.NodeId; IsRazorBody = ErpPage.IsRazorBody; Body = ErpPage.Body; Layout = ErpPage.Layout; } #endregion if (String.IsNullOrWhiteSpace(ReturnUrl)) { ReturnUrl = "/sdk/objects/page/l/list"; } #region << Init App >> var appServ = new AppService(); var app = appServ.GetApplication(ErpPage.AppId ?? Guid.Empty); if (app != null) { if (app.Access != null && app.Access.Count > 0) { Access = app.Access.Select(x => x.ToString()).ToList(); } } #endregion #region << Init User Role Options >> var roles = new SecurityManager().GetAllRoles().OrderBy(x => x.Name).ToList(); foreach (var role in roles) { RoleOptions.Add(new SelectOption() { Value = role.Id.ToString(), Label = role.Name }); } #endregion HeaderToolbar.AddRange(AdminPageUtils.GetPageAdminSubNav(ErpPage, "details")); PagePublicUrl = PageUtils.CalculatePageUrl(ErpPage.Id); }
public IActionResult OnPost() { if (!ModelState.IsValid) { throw new Exception("Antiforgery check failed."); } var initResult = Init(); if (initResult != null) { return(initResult); } InitPage(false); if (ErpEntity == null) { return(NotFound()); } if (String.IsNullOrWhiteSpace(ReturnUrl)) { ReturnUrl = $"/sdk/objects/entity/r/{RecordId}/rl/fields/c/select"; } //empty html input is not posted, so we init it with string.empty if (DefaultValue == null) { DefaultValue = string.Empty; } var entMan = new EntityManager(); try { var newFieldId = Guid.NewGuid(); if (Id != null) { newFieldId = Id.Value; } var response = new FieldResponse(); InputField input = null; switch (Type) { case FieldType.AutoNumberField: { decimal defaultDecimal = 1; if (Decimal.TryParse(DefaultValue, out decimal result)) { defaultDecimal = result; } input = new InputAutoNumberField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, DefaultValue = defaultDecimal, StartingNumber = StartingNumber, DisplayFormat = DisplayFormat, EnableSecurity = EnableSecurity }; } break; case FieldType.CheckboxField: { bool?defaultValue = null; if (Boolean.TryParse(DefaultValue, out bool result)) { defaultValue = result; } input = new InputCheckboxField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, DefaultValue = defaultValue, EnableSecurity = EnableSecurity }; } break; case FieldType.CurrencyField: { decimal?defaultDecimal = null; if (Decimal.TryParse(DefaultValue, out decimal result)) { defaultDecimal = result; } input = new InputCurrencyField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, DefaultValue = defaultDecimal, EnableSecurity = EnableSecurity, MinValue = MinValue, MaxValue = MaxValue, Currency = Helpers.GetCurrencyType(CurrencyCode) }; } break; case FieldType.DateField: { DateTime?defaultValue = null; if (DateTime.TryParse(DefaultValue, out DateTime result)) { defaultValue = result; } input = new InputDateField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, DefaultValue = defaultValue, EnableSecurity = EnableSecurity, Format = Format, UseCurrentTimeAsDefaultValue = UseCurrentTimeAsDefaultValue }; } break; case FieldType.DateTimeField: { DateTime?defaultValue = null; if (DateTime.TryParse(DefaultValue, out DateTime result)) { defaultValue = result; } input = new InputDateTimeField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, DefaultValue = defaultValue, EnableSecurity = EnableSecurity, Format = Format, UseCurrentTimeAsDefaultValue = UseCurrentTimeAsDefaultValue }; } break; case FieldType.EmailField: { string defaultValue = null; if (DefaultValue.ToLowerInvariant() != "null") { defaultValue = DefaultValue; } input = new InputEmailField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, DefaultValue = defaultValue, EnableSecurity = EnableSecurity, MaxLength = MaxLength }; } break; case FieldType.FileField: { string defaultValue = null; if (DefaultValue.ToLowerInvariant() != "null") { defaultValue = DefaultValue; } input = new InputFileField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, DefaultValue = defaultValue, EnableSecurity = EnableSecurity }; } break; case FieldType.HtmlField: { string defaultValue = null; if (DefaultValue.ToLowerInvariant() != "null") { defaultValue = DefaultValue; } input = new InputHtmlField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, DefaultValue = defaultValue, EnableSecurity = EnableSecurity }; } break; case FieldType.ImageField: { string defaultValue = null; if (DefaultValue.ToLowerInvariant() != "null") { defaultValue = DefaultValue; } input = new InputImageField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, DefaultValue = defaultValue, EnableSecurity = EnableSecurity }; } break; case FieldType.MultiLineTextField: { string defaultValue = null; if (DefaultValue.ToLowerInvariant() != "null") { defaultValue = DefaultValue; } input = new InputMultiLineTextField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, DefaultValue = defaultValue, EnableSecurity = EnableSecurity, MaxLength = MaxLength }; } break; case FieldType.GeographyField: { string defaultValue = null; if (DefaultValue.ToLowerInvariant() != "null") { defaultValue = DefaultValue; } GeographyFieldFormat format = (GeographyFieldFormat)int.Parse(GeographyFormat); input = new InputGeographyField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, DefaultValue = defaultValue, EnableSecurity = EnableSecurity, MaxLength = MaxLength, SRID = SRID, Format = format }; } break; case FieldType.MultiSelectField: { var selectOptions = (SelectOptions ?? string.Empty).Split(Environment.NewLine); var defaultOptions = (DefaultValue ?? string.Empty).Split(Environment.NewLine); var multiSelectOptions = new List <SelectOption>(); var defaultValues = new List <string>(); foreach (var option in selectOptions) { if (!String.IsNullOrWhiteSpace(option)) { var optionArray = option.Split(','); var key = ""; var value = ""; var iconClass = ""; var color = ""; if (optionArray.Length > 0 && !String.IsNullOrWhiteSpace(optionArray[0])) { key = optionArray[0].Trim().ToLowerInvariant(); } if (optionArray.Length > 1 && !String.IsNullOrWhiteSpace(optionArray[1])) { value = optionArray[1].Trim(); } else if (optionArray.Length > 0 && !String.IsNullOrWhiteSpace(optionArray[0])) { value = key; } if (optionArray.Length > 2 && !String.IsNullOrWhiteSpace(optionArray[2])) { iconClass = optionArray[2].Trim(); } if (optionArray.Length > 3 && !String.IsNullOrWhiteSpace(optionArray[3])) { color = optionArray[3].Trim(); } if (!String.IsNullOrWhiteSpace(key) && !String.IsNullOrWhiteSpace(value)) { multiSelectOptions.Add(new SelectOption() { Value = key, Label = value, IconClass = iconClass, Color = color }); } } } foreach (var option in defaultOptions) { var fixedOption = option.Trim().ToLowerInvariant(); if (!String.IsNullOrWhiteSpace(option) && multiSelectOptions.Any(x => x.Value == fixedOption)) { defaultValues.Add(fixedOption); } else if (!String.IsNullOrWhiteSpace(option) && !multiSelectOptions.Any(x => x.Value == fixedOption)) { Validation.Errors.Add(new ValidationError("DefaultValue", "one or more of the default values are not found as select options")); throw Validation; } } input = new InputMultiSelectField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, EnableSecurity = EnableSecurity, Options = multiSelectOptions, DefaultValue = defaultValues }; } break; case FieldType.NumberField: { decimal?defaultDecimal = null; if (Decimal.TryParse(DefaultValue, out decimal result)) { defaultDecimal = result; } input = new InputNumberField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, DefaultValue = defaultDecimal, EnableSecurity = EnableSecurity, MinValue = MinValue, MaxValue = MaxValue, DecimalPlaces = DecimalPlaces }; } break; case FieldType.PasswordField: { input = new InputPasswordField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, MinLength = MinLength, Encrypted = Encrypted, EnableSecurity = EnableSecurity, MaxLength = MaxLength }; } break; case FieldType.PercentField: { decimal?defaultDecimal = null; if (Decimal.TryParse(DefaultValue, out decimal result)) { defaultDecimal = result; } input = new InputNumberField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, DefaultValue = defaultDecimal, EnableSecurity = EnableSecurity, MinValue = MinValue, MaxValue = MaxValue, DecimalPlaces = DecimalPlaces }; } break; case FieldType.PhoneField: { string defaultValue = null; if (DefaultValue.ToLowerInvariant() != "null") { defaultValue = DefaultValue; } input = new InputPhoneField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, DefaultValue = defaultValue, EnableSecurity = EnableSecurity, MaxLength = MaxLength }; } break; case FieldType.GuidField: { Guid?defaultGuid = null; if (Guid.TryParse(DefaultValue, out Guid result)) { defaultGuid = result; } input = new InputGuidField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, DefaultValue = defaultGuid, EnableSecurity = EnableSecurity, GenerateNewId = GenerateNewId }; } break; case FieldType.SelectField: { var selectOptions = SelectOptions.Split(Environment.NewLine); var modelSelectOptions = new List <SelectOption>(); foreach (var option in selectOptions) { if (!String.IsNullOrWhiteSpace(option)) { var optionArray = option.Split(','); var key = ""; var value = ""; var iconClass = ""; var color = ""; if (optionArray.Length > 0 && !String.IsNullOrWhiteSpace(optionArray[0])) { key = optionArray[0].Trim().ToLowerInvariant(); } if (optionArray.Length > 1 && !String.IsNullOrWhiteSpace(optionArray[1])) { value = optionArray[1].Trim(); } else if (optionArray.Length > 0 && !String.IsNullOrWhiteSpace(optionArray[0])) { value = key; } if (optionArray.Length > 2 && !String.IsNullOrWhiteSpace(optionArray[2])) { iconClass = optionArray[2].Trim(); } if (optionArray.Length > 3 && !String.IsNullOrWhiteSpace(optionArray[3])) { color = optionArray[3].Trim(); } if (!String.IsNullOrWhiteSpace(key) && !String.IsNullOrWhiteSpace(value)) { modelSelectOptions.Add(new SelectOption() { Value = key, Label = value, IconClass = iconClass, Color = color }); } } } DefaultValue = DefaultValue.Trim().ToLowerInvariant(); if (!String.IsNullOrWhiteSpace(DefaultValue) && !modelSelectOptions.Any(x => x.Value == DefaultValue)) { Validation.Errors.Add(new ValidationError("DefaultValue", "one or more of the default values are not found as select options")); throw Validation; } input = new InputSelectField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, DefaultValue = DefaultValue, EnableSecurity = EnableSecurity, Options = modelSelectOptions }; } break; case FieldType.UrlField: { string defaultValue = null; if (DefaultValue.ToLowerInvariant() != "null") { defaultValue = DefaultValue; } input = new InputUrlField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, DefaultValue = defaultValue, EnableSecurity = EnableSecurity, MaxLength = MaxLength, OpenTargetInNewWindow = OpenTargetInNewWindow }; } break; case FieldType.TextField: { string defaultValue = null; if (DefaultValue.ToLowerInvariant() != "null") { defaultValue = DefaultValue; } input = new InputTextField() { Id = newFieldId, Name = Name, Label = Label, Required = Required, Description = Description, Unique = Unique, HelpText = HelpText, System = System, PlaceholderText = PlaceholderText, Searchable = Searchable, DefaultValue = defaultValue, EnableSecurity = EnableSecurity, MaxLength = MaxLength }; } break; default: throw new Exception("Field Type not recognized"); } var recordPermissionsKeyValues = JsonConvert.DeserializeObject <List <KeyStringList> >(FieldPermissions); input.Permissions = new FieldPermissions(); input.Permissions.CanRead = new List <Guid>(); input.Permissions.CanUpdate = new List <Guid>(); foreach (var role in recordPermissionsKeyValues) { var roleId = Guid.Empty; if (Guid.TryParse(role.Key, out Guid result)) { roleId = result; } if (roleId != Guid.Empty) { if (role.Values.Contains("read")) { input.Permissions.CanRead.Add(roleId); } if (role.Values.Contains("update")) { input.Permissions.CanUpdate.Add(roleId); } } } response = entMan.CreateField(ErpEntity.Id, input); if (!response.Success) { var exception = new ValidationException(response.Message); exception.Errors = response.Errors.MapTo <ValidationError>(); throw exception; } // because of https://github.com/aspnet/Mvc/issues/6711, i added TempDataExtensions int // WebVella.Erp.Web.Utils and using Put and Get<> instead of // TempData["ScreenMessage"] = new ScreenMessage() { Message = "Field created successfully" }; TempData.Put("ScreenMessage", new ScreenMessage() { Message = "Field created successfully" }); return(Redirect($"/sdk/objects/entity/r/{ErpEntity.Id}/rl/fields/l")); } catch (ValidationException ex) { Validation.Message = ex.Message; Validation.Errors = ex.Errors; } catch (Exception ex) { Validation.Message = ex.Message; Validation.Errors.Add(new ValidationError("", ex.Message, isSystem: true)); } HeaderToolbar.AddRange(AdminPageUtils.GetEntityAdminSubNav(ErpEntity, "fields")); ErpRequestContext.PageContext = PageContext; BeforeRender(); return(Page()); }