Ejemplo n.º 1
0
        protected override DriverResult Editor(ContentPart part, Fields.DateTimeField field, IUpdateModel updater, dynamic shapeHelper)
        {
            var viewModel = new DateTimeFieldViewModel();

            if (updater.TryUpdateModel(viewModel, GetPrefix(field, part), null, null))
            {
                DateTime value;

                var settings = field.PartFieldDefinition.Settings.GetModel <DateTimeFieldSettings>();
                if (settings.Display == DateTimeFieldDisplays.DateOnly)
                {
                    viewModel.Time = DateTime.Now.ToShortTimeString();
                }

                if (settings.Display == DateTimeFieldDisplays.TimeOnly)
                {
                    viewModel.Date = DateTime.Now.ToShortDateString();
                }

                if (DateTime.TryParse(viewModel.Date + " " + viewModel.Time, out value))
                {
                    field.DateTime = value.ToUniversalTime();
                }
                else
                {
                    updater.AddModelError(GetPrefix(field, part), T("{0} is an invalid date and time", field.Name));
                    field.DateTime = null;
                }
            }

            return(Editor(part, field, shapeHelper));
        }
Ejemplo n.º 2
0
        protected override DriverResult Editor(ContentPart part, DateTimeField field, IUpdateModel updater, dynamic shapeHelper)
        {
            var viewModel = new DateTimeFieldViewModel();

            if (updater.TryUpdateModel(viewModel, GetPrefix(field, part), null, null))
            {
                var settings = field.PartFieldDefinition.Settings.GetModel <DateTimeFieldSettings>();
                if (settings.Required && (((settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.DateOnly) && String.IsNullOrWhiteSpace(viewModel.Editor.Date)) || ((settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.TimeOnly) && String.IsNullOrWhiteSpace(viewModel.Editor.Time))))
                {
                    updater.AddModelError(GetPrefix(field, part), T("{0} is required.", field.DisplayName));
                }
                else
                {
                    try {
                        var utcDateTime = DateServices.ConvertFromLocalString(viewModel.Editor.Date, viewModel.Editor.Time);
                        if (utcDateTime.HasValue)
                        {
                            field.DateTime = utcDateTime.Value;
                        }
                        else
                        {
                            field.DateTime = DateTime.MinValue;
                        }
                    }
                    catch (FormatException) {
                        updater.AddModelError(GetPrefix(field, part), T("{0} could not be parsed as a valid date and time.", field.DisplayName));
                    }
                }
            }

            return(Editor(part, field, shapeHelper));
        }
Ejemplo n.º 3
0
        protected override DriverResult Display(ContentPart part, DateTimeField field, string displayType, dynamic shapeHelper)
        {
            return(ContentShape("Fields_DateTime", // this is just a key in the Shape Table
                                GetDifferentiator(field, part),
                                () => {
                var settings = field.PartFieldDefinition.Settings.GetModel <DateTimeFieldSettings>();
                var value = field.DateTime;

                var viewModel = new DateTimeFieldViewModel {
                    Name = field.DisplayName,
                    Hint = settings.Hint,
                    IsRequired = settings.Required,
                    Editor = new DateTimeEditor()
                    {
                        Date = DateServices.ConvertToLocalDateString(value, String.Empty),
                        Time = DateServices.ConvertToLocalTimeString(value, String.Empty),
                        ShowDate = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.DateOnly,
                        ShowTime = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.TimeOnly,
                    }
                };

                return shapeHelper.Fields_DateTime(     // this is the actual Shape which will be resolved (Fields/DateTime.cshtml)
                    Model: viewModel);
            }
                                ));
        }
Ejemplo n.º 4
0
        protected override DriverResult Editor(ContentPart part, DateTimeField field, IUpdateModel updater, dynamic shapeHelper)
        {
            var viewModel = new DateTimeFieldViewModel();

            if (updater.TryUpdateModel(viewModel, GetPrefix(field, part), null, null))
            {
                var settings = field.PartFieldDefinition.Settings.GetModel <DateTimeFieldSettings>();

                var options = new DateLocalizationOptions();

                // Don't do any time zone conversion if field is semantically a date-only field, because that might mutate the date component.
                if (settings.Display == DateTimeFieldDisplays.DateOnly)
                {
                    options.EnableTimeZoneConversion = false;
                }

                // Don't do any calendar conversion if field is semantically a time-only field, because the date component might we out of allowed boundaries for the current calendar.
                if (settings.Display == DateTimeFieldDisplays.TimeOnly)
                {
                    options.EnableCalendarConversion = false;
                    options.IgnoreDate = true;
                }

                var showDate = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.DateOnly;
                var showTime = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.TimeOnly;

                DateTime?value = null;

                // Try to parse data if not required or if there are no missing fields.
                if (!settings.Required || ((!showDate || !String.IsNullOrWhiteSpace(viewModel.Editor.Date)) && (!showTime || !String.IsNullOrWhiteSpace(viewModel.Editor.Time))))
                {
                    try {
                        value = DateLocalizationServices.ConvertFromLocalizedString(viewModel.Editor.Date, viewModel.Editor.Time, options);
                    }
                    catch {
                        updater.AddModelError(GetPrefix(field, part), T("{0} could not be parsed as a valid date and time.", T(field.DisplayName)));
                    }
                }

                // Hackish workaround to make sure a time-only field with an entered time equivalent to
                // 00:00 UTC doesn't get stored as a full DateTime.MinValue in the database, resulting
                // in it being interpreted as an empty value when subsequently retrieved.
                if (value.HasValue && settings.Display == DateTimeFieldDisplays.TimeOnly && value == DateTime.MinValue)
                {
                    value = value.Value.AddDays(1);
                }

                if (settings.Required && (!value.HasValue || (settings.Display != DateTimeFieldDisplays.TimeOnly && value.Value.Date == DateTime.MinValue)))
                {
                    updater.AddModelError(GetPrefix(field, part), T("{0} is required.", T(field.DisplayName)));
                }

                field.DateTime = value.HasValue ? value.Value : DateTime.MinValue;
            }

            return(Editor(part, field, shapeHelper));
        }
        public void OnParseDateTimeValue()
        {
            // arrange
            var vm = new DateTimeFieldViewModel();
            var evnt = new ParseDateTimeEventArgs(null, null, DateTime.Now, new DateTime(1752, 1, 1), true);

            // act
            vm.OnParseDateTimeValue(null, evnt);

            // assert
            Assert.IsFalse(evnt.IsParsingSuccessful);
        }
Ejemplo n.º 6
0
        protected override DriverResult Editor(ContentPart part, Fields.DateTimeField field, IUpdateModel updater, dynamic shapeHelper)
        {
            var viewModel = new DateTimeFieldViewModel();

            if (updater.TryUpdateModel(viewModel, GetPrefix(field, part), null, null))
            {
                DateTime value;

                var settings = field.PartFieldDefinition.Settings.GetModel <DateTimeFieldSettings>();

                if (settings.Display == DateTimeFieldDisplays.DateOnly)
                {
                    viewModel.Time = new DateTime(1980, 1, 1).ToString("t", _cultureInfo.Value);
                }

                if (settings.Display == DateTimeFieldDisplays.TimeOnly)
                {
                    viewModel.Date = new DateTime(1980, 1, 1).ToString("d", _cultureInfo.Value);
                }

                string parseDateTime = String.Concat(viewModel.Date, " ", viewModel.Time);

                if (settings.Required && (String.IsNullOrWhiteSpace(viewModel.Time) || String.IsNullOrWhiteSpace(viewModel.Date)))
                {
                    updater.AddModelError(GetPrefix(field, part), T("{0} is required", field.DisplayName));
                }

                if (!settings.Required &&
                    (settings.Display != DateTimeFieldDisplays.TimeOnly && String.IsNullOrWhiteSpace(viewModel.Date)) ||
                    (settings.Display != DateTimeFieldDisplays.DateOnly && String.IsNullOrWhiteSpace(viewModel.Time))
                    )
                {
                    field.DateTime = DateTime.MinValue;
                }
                else if (DateTime.TryParse(parseDateTime, _cultureInfo.Value, DateTimeStyles.None, out value))
                {
                    field.DateTime = TimeZoneInfo.ConvertTimeToUtc(value, Services.WorkContext.CurrentTimeZone);
                }
                else
                {
                    updater.AddModelError(GetPrefix(field, part), T("{0} is an invalid date and time", field.DisplayName));
                    field.DateTime = DateTime.MinValue;
                }
            }

            return(Editor(part, field, shapeHelper));
        }
Ejemplo n.º 7
0
        protected override DriverResult Display(ContentPart part, DateTimeField field, string displayType, dynamic shapeHelper)
        {
            return(ContentShape("Fields_DateTime", // this is just a key in the Shape Table
                                GetDifferentiator(field, part),
                                () => {
                var settings = field.PartFieldDefinition.Settings.GetModel <DateTimeFieldSettings>();
                var value = field.DateTime;
                var options = new DateLocalizationOptions();

                // Don't do any time zone conversion if field is semantically a date-only field, because that might mutate the date component.
                if (settings.Display == DateTimeFieldDisplays.DateOnly)
                {
                    options.EnableTimeZoneConversion = false;
                }

                // Don't do any calendar conversion if field is semantically a time-only field, because the date component might we out of allowed boundaries for the current calendar.
                if (settings.Display == DateTimeFieldDisplays.TimeOnly)
                {
                    options.EnableCalendarConversion = false;
                    options.IgnoreDate = true;
                }

                var showDate = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.DateOnly;
                var showTime = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.TimeOnly;

                var viewModel = new DateTimeFieldViewModel {
                    Name = field.DisplayName,
                    Hint = settings.Hint,
                    IsRequired = settings.Required,
                    Editor = new DateTimeEditor()
                    {
                        Date = showDate ? DateLocalizationServices.ConvertToLocalizedDateString(value, options) : null,
                        Time = showTime ? DateLocalizationServices.ConvertToLocalizedTimeString(value, options) : null,
                        ShowDate = showDate,
                        ShowTime = showTime,
                        DatePlaceholder = settings.DatePlaceholder,
                        TimePlaceholder = settings.TimePlaceholder
                    }
                };

                return shapeHelper.Fields_DateTime(     // this is the actual Shape which will be resolved (Fields/DateTime.cshtml)
                    Model: viewModel);
            }
                                ));
        }
Ejemplo n.º 8
0
        protected override DriverResult Editor(ContentPart part, Fields.DateTimeField field, dynamic shapeHelper)
        {
            var settings = field.PartFieldDefinition.Settings.GetModel <DateTimeFieldSettings>();
            var value    = field.DateTime;

            var viewModel = new DateTimeFieldViewModel {
                Name     = field.DisplayName,
                Date     = value != DateTime.MinValue ? TimeZoneInfo.ConvertTimeFromUtc(value, Services.WorkContext.CurrentTimeZone).ToString("d", _cultureInfo.Value) : String.Empty,
                Time     = value != DateTime.MinValue ? TimeZoneInfo.ConvertTimeFromUtc(value, Services.WorkContext.CurrentTimeZone).ToString("t", _cultureInfo.Value) : String.Empty,
                ShowDate = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.DateOnly,
                ShowTime = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.TimeOnly,
                Hint     = settings.Hint,
                Required = settings.Required
            };

            return(ContentShape("Fields_DateTime_Edit", GetDifferentiator(field, part),
                                () => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: viewModel, Prefix: GetPrefix(field, part))));
        }
Ejemplo n.º 9
0
        protected override DriverResult Display(ContentPart part, Fields.DateTimeField field, string displayType, dynamic shapeHelper)
        {
            var settings = field.PartFieldDefinition.Settings.GetModel <DateTimeFieldSettings>();
            var value    = field.DateTime;

            var viewModel = new DateTimeFieldViewModel {
                Name     = field.Name,
                Date     = value.HasValue ? value.Value.ToLocalTime().ToShortDateString() : String.Empty,
                Time     = value.HasValue ? value.Value.ToLocalTime().ToShortTimeString() : String.Empty,
                ShowDate = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.DateOnly,
                ShowTime = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.TimeOnly
            };

            return(ContentShape("Fields_Contrib_DateTime",           // this is just a key in the Shape Table
                                () =>
                                shapeHelper.Fields_Contrib_DateTime( // this is the actual Shape which will be resolved (Fields/Contrib.DateTime.cshtml)
                                    ContentField: field,
                                    Model: viewModel)
                                ));
        }
Ejemplo n.º 10
0
        protected override DriverResult Editor(ContentPart part, DateTimeField field, dynamic shapeHelper)
        {
            var settings = field.PartFieldDefinition.Settings.GetModel <DateTimeFieldSettings>();
            var value    = field.DateTime;
            var options  = new DateLocalizationOptions();

            // Don't do any time zone conversion if field is semantically a date-only field, because that might mutate the date component.
            if (settings.Display == DateTimeFieldDisplays.DateOnly)
            {
                options.EnableTimeZoneConversion = false;
            }

            // Don't do any calendar conversion if field is semantically a time-only field, because the date component might we out of allowed boundaries for the current calendar.
            if (settings.Display == DateTimeFieldDisplays.TimeOnly)
            {
                options.EnableCalendarConversion = false;
                options.IgnoreDate = true;
            }

            var showDate = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.DateOnly;
            var showTime = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.TimeOnly;

            var viewModel = new DateTimeFieldViewModel {
                Name            = field.DisplayName,
                Hint            = settings.Hint,
                IsRequired      = settings.Required,
                HasDefaultValue = settings.DefaultValue.HasValue,
                Editor          = new DateTimeEditor()
                {
                    Date            = showDate ? DateLocalizationServices.ConvertToLocalizedDateString(value, options) : null,
                    Time            = showTime ? DateLocalizationServices.ConvertToLocalizedTimeString(value, options) : null,
                    ShowDate        = showDate,
                    ShowTime        = showTime,
                    DatePlaceholder = settings.DatePlaceholder,
                    TimePlaceholder = settings.TimePlaceholder
                }
            };

            return(ContentShape("Fields_DateTime_Edit", GetDifferentiator(field, part),
                                () => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: viewModel, Prefix: GetPrefix(field, part))));
        }
Ejemplo n.º 11
0
        protected override DriverResult Editor(ContentPart part, Fields.DateTimeField field, dynamic shapeHelper)
        {
            var settings = field.PartFieldDefinition.Settings.GetModel <DateTimeFieldSettings>();
            var value    = field.DateTime;

            if (value.HasValue)
            {
                value = value.Value.ToLocalTime();
            }

            var viewModel = new DateTimeFieldViewModel {
                Name     = field.Name,
                Date     = value.HasValue ? value.Value.ToLocalTime().ToShortDateString() : "",
                Time     = value.HasValue ? value.Value.ToLocalTime().ToShortTimeString() : "",
                ShowDate = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.DateOnly,
                ShowTime = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.TimeOnly
            };

            return(ContentShape("Fields_Contrib_DateTime_Edit", // this is just a key in the Shape Table
                                () => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: viewModel, Prefix: GetPrefix(field, part))));
        }
Ejemplo n.º 12
0
        protected override DriverResult Editor(ContentPart part, DateTimeField field, dynamic shapeHelper)
        {
            var settings = field.PartFieldDefinition.Settings.GetModel <DateTimeFieldSettings>();
            var value    = field.DateTime;

            var viewModel = new DateTimeFieldViewModel {
                Name       = field.DisplayName,
                Hint       = settings.Hint,
                IsRequired = settings.Required,
                Editor     = new DateTimeEditor()
                {
                    Date     = DateServices.ConvertToLocalDateString(value, String.Empty),
                    Time     = DateServices.ConvertToLocalTimeString(value, String.Empty),
                    ShowDate = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.DateOnly,
                    ShowTime = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.TimeOnly,
                }
            };

            return(ContentShape("Fields_DateTime_Edit", GetDifferentiator(field, part),
                                () => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: viewModel, Prefix: GetPrefix(field, part))));
        }
Ejemplo n.º 13
0
        protected override DriverResult Display(ContentPart part, Fields.DateTimeField field, string displayType, dynamic shapeHelper)
        {
            return(ContentShape("Fields_DateTime", // this is just a key in the Shape Table
                                GetDifferentiator(field, part),
                                () => {
                var settings = field.PartFieldDefinition.Settings.GetModel <DateTimeFieldSettings>();
                var value = field.DateTime;


                var viewModel = new DateTimeFieldViewModel {
                    Name = field.DisplayName,
                    Date = value != DateTime.MinValue ? TimeZoneInfo.ConvertTimeFromUtc(value, Services.WorkContext.CurrentTimeZone).ToString("d", _cultureInfo.Value) : String.Empty,
                    Time = value != DateTime.MinValue ? TimeZoneInfo.ConvertTimeFromUtc(value, Services.WorkContext.CurrentTimeZone).ToString("t", _cultureInfo.Value) : String.Empty,
                    ShowDate = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.DateOnly,
                    ShowTime = settings.Display == DateTimeFieldDisplays.DateAndTime || settings.Display == DateTimeFieldDisplays.TimeOnly,
                    Hint = settings.Hint,
                    Required = settings.Required
                };

                return shapeHelper.Fields_DateTime(     // this is the actual Shape which will be resolved (Fields/DateTime.cshtml)
                    Model: viewModel);
            }
                                ));
        }
Ejemplo n.º 14
0
        public void DateTimeFieldTests()
        {
            // arrange
            var fieldViewModel = new DateTimeFieldViewModel();
            var popupViewModel = new ExportFactory<DateTimeFieldViewModel>(() => new Tuple<DateTimeFieldViewModel, Action>(fieldViewModel, null));

            var fieldFactory = new FieldFactory { DateTimeFieldViewModelFactory = popupViewModel };

            var model = new ModelStub();
            var property = model.GetType().GetProperty("DateTimeField");

            // act
            var viewModel = fieldFactory.CreateField(property, null, model, new DetailsViewModel(), false);

            // assert
            Assert.AreEqual("Time", ((DateTimeFieldViewModel)viewModel).InputMode);
        }