2015-12-19 20:49:13 +01:00
|
|
|
|
using Orchard.ContentManagement;
|
2015-12-13 22:21:02 +01:00
|
|
|
|
using Orchard.ContentManagement.Drivers;
|
|
|
|
|
|
using Orchard.ContentManagement.Handlers;
|
|
|
|
|
|
using Orchard.Fields.Fields;
|
|
|
|
|
|
using Orchard.Fields.Settings;
|
|
|
|
|
|
using Orchard.Fields.ViewModels;
|
2015-12-19 20:49:13 +01:00
|
|
|
|
using Orchard.Localization;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Globalization;
|
2015-12-13 22:21:02 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Orchard.Fields.Drivers {
|
|
|
|
|
|
public class NumericFieldDriver : ContentFieldDriver<NumericField> {
|
|
|
|
|
|
public IOrchardServices Services { get; set; }
|
|
|
|
|
|
private const string TemplateName = "Fields/Numeric.Edit";
|
|
|
|
|
|
private readonly Lazy<CultureInfo> _cultureInfo;
|
|
|
|
|
|
|
2016-04-26 18:50:02 +02:00
|
|
|
|
public NumericFieldDriver(IOrchardServices services) {
|
2015-12-13 22:21:02 +01:00
|
|
|
|
Services = services;
|
|
|
|
|
|
T = NullLocalizer.Instance;
|
|
|
|
|
|
|
|
|
|
|
|
_cultureInfo = new Lazy<CultureInfo>(() => CultureInfo.GetCultureInfo(Services.WorkContext.CurrentCulture));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Localizer T { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
private static string GetPrefix(ContentField field, ContentPart part) {
|
|
|
|
|
|
return part.PartDefinition.Name + "." + field.Name;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static string GetDifferentiator(NumericField field, ContentPart part) {
|
|
|
|
|
|
return field.Name;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override DriverResult Display(ContentPart part, NumericField field, string displayType, dynamic shapeHelper) {
|
|
|
|
|
|
return ContentShape("Fields_Numeric", GetDifferentiator(field, part), () => {
|
|
|
|
|
|
return shapeHelper.Fields_Numeric()
|
|
|
|
|
|
.Settings(field.PartFieldDefinition.Settings.GetModel<NumericFieldSettings>())
|
|
|
|
|
|
.Value(Convert.ToString(field.Value, _cultureInfo.Value));
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override DriverResult Editor(ContentPart part, NumericField field, dynamic shapeHelper) {
|
|
|
|
|
|
return ContentShape("Fields_Numeric_Edit", GetDifferentiator(field, part),
|
|
|
|
|
|
() => {
|
2016-04-26 18:50:02 +02:00
|
|
|
|
var settings = field.PartFieldDefinition.Settings.GetModel<NumericFieldSettings>();
|
2017-01-12 21:13:42 +01:00
|
|
|
|
var value = part.IsNew() && field.Value == null ? settings.DefaultValue : Convert.ToString(field.Value, _cultureInfo.Value);
|
2016-04-26 18:50:02 +02:00
|
|
|
|
|
2015-12-13 22:21:02 +01:00
|
|
|
|
var model = new NumericFieldViewModel {
|
|
|
|
|
|
Field = field,
|
2016-04-26 18:50:02 +02:00
|
|
|
|
Settings = settings,
|
|
|
|
|
|
Value = value
|
2015-12-13 22:21:02 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: model, Prefix: GetPrefix(field, part));
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override DriverResult Editor(ContentPart part, NumericField field, IUpdateModel updater, dynamic shapeHelper) {
|
|
|
|
|
|
var viewModel = new NumericFieldViewModel();
|
|
|
|
|
|
|
|
|
|
|
|
if (updater.TryUpdateModel(viewModel, GetPrefix(field, part), null, null)) {
|
|
|
|
|
|
Decimal value;
|
|
|
|
|
|
|
|
|
|
|
|
var settings = field.PartFieldDefinition.Settings.GetModel<NumericFieldSettings>();
|
|
|
|
|
|
|
2016-03-25 04:13:41 +01:00
|
|
|
|
field.Value = null;
|
|
|
|
|
|
|
|
|
|
|
|
if (String.IsNullOrWhiteSpace(viewModel.Value)) {
|
|
|
|
|
|
if (settings.Required) {
|
2016-09-22 21:02:10 +01:00
|
|
|
|
updater.AddModelError(GetPrefix(field, part), T("The {0} field is required.", T(field.DisplayName)));
|
2015-12-19 20:49:13 +01:00
|
|
|
|
}
|
2015-12-13 22:21:02 +01:00
|
|
|
|
}
|
2016-03-25 04:13:41 +01:00
|
|
|
|
else if (!Decimal.TryParse(viewModel.Value, NumberStyles.Any, _cultureInfo.Value, out value)) {
|
2016-09-22 21:02:10 +01:00
|
|
|
|
updater.AddModelError(GetPrefix(field, part), T("{0} is an invalid number.", T(field.DisplayName)));
|
2015-12-13 22:21:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
2016-03-25 04:13:41 +01:00
|
|
|
|
field.Value = value;
|
2015-12-13 22:21:02 +01:00
|
|
|
|
|
2016-03-25 04:13:41 +01:00
|
|
|
|
if (settings.Minimum.HasValue && value < settings.Minimum.Value) {
|
2016-09-22 21:02:10 +01:00
|
|
|
|
updater.AddModelError(GetPrefix(field, part), T("The value must be greater than {0}.", settings.Minimum.Value));
|
2016-03-25 04:13:41 +01:00
|
|
|
|
}
|
2015-12-13 22:21:02 +01:00
|
|
|
|
|
2016-03-25 04:13:41 +01:00
|
|
|
|
if (settings.Maximum.HasValue && value > settings.Maximum.Value) {
|
2016-09-22 21:02:10 +01:00
|
|
|
|
updater.AddModelError(GetPrefix(field, part), T("The value must be less than {0}.", settings.Maximum.Value));
|
2015-12-13 22:21:02 +01:00
|
|
|
|
}
|
2016-03-25 04:13:41 +01:00
|
|
|
|
|
|
|
|
|
|
// checking the number of decimals
|
|
|
|
|
|
if (Math.Round(value, settings.Scale) != value) {
|
|
|
|
|
|
if (settings.Scale == 0) {
|
2016-09-22 21:02:10 +01:00
|
|
|
|
updater.AddModelError(GetPrefix(field, part), T("The {0} field must be an integer.", T(field.DisplayName)));
|
2016-03-25 04:13:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
else {
|
2016-09-22 21:02:10 +01:00
|
|
|
|
updater.AddModelError(GetPrefix(field, part), T("Invalid number of digits for {0}, max allowed: {1}.", T(field.DisplayName), settings.Scale));
|
2016-03-25 04:13:41 +01:00
|
|
|
|
}
|
2015-12-13 22:21:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Editor(part, field, shapeHelper);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void Importing(ContentPart part, NumericField field, ImportContentContext context) {
|
2022-10-14 08:57:44 +02:00
|
|
|
|
Action empty = (() => field.Value = (decimal?)null);
|
|
|
|
|
|
var element = context.Data.Element(field.FieldDefinition.Name + "." + field.Name);
|
|
|
|
|
|
// If element is not in the ImportContentContext, field must not be reset.
|
|
|
|
|
|
if (element == null) {
|
|
|
|
|
|
empty = () => { };
|
|
|
|
|
|
}
|
|
|
|
|
|
context.ImportAttribute(field.FieldDefinition.Name + "." + field.Name, "Value", v => field.Value = decimal.Parse(v, CultureInfo.InvariantCulture), empty);
|
2015-12-13 22:21:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void Exporting(ContentPart part, NumericField field, ExportContentContext context) {
|
2017-02-09 18:00:18 +05:00
|
|
|
|
context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Value", !field.Value.HasValue ? String.Empty : field.Value.Value.ToString(CultureInfo.InvariantCulture));
|
2015-12-13 22:21:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-27 18:44:26 +02:00
|
|
|
|
protected override void Cloning(ContentPart part, NumericField originalField, NumericField cloneField, CloneContentContext context) {
|
|
|
|
|
|
cloneField.Value = originalField.Value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-13 22:21:02 +01:00
|
|
|
|
protected override void Describe(DescribeMembersContext context) {
|
|
|
|
|
|
context
|
|
|
|
|
|
.Member(null, typeof(decimal), T("Value"), T("The value of the field."))
|
|
|
|
|
|
.Enumerate<NumericField> (() => field => new[] { field.Value });
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|