Inheritance: Orchard.DisplayManagement.Shapes.Shape
        protected override DriverResult Editor(CommonPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            var settings = part.TypePartDefinition.Settings.GetModel <DateEditorSettings>();

            if (!settings.ShowDateEditor)
            {
                return(null);
            }

            return(ContentShape(
                       "Parts_Common_Date_Edit",
                       () => {
                DateEditorViewModel model = shapeHelper.Parts_Common_Date_Edit(typeof(DateEditorViewModel));

                model.Editor = new DateTimeEditor()
                {
                    ShowDate = true,
                    ShowTime = true
                };

                if (part.CreatedUtc != null)
                {
                    // show CreatedUtc only if is has been "touched",
                    // i.e. it has been published once, or CreatedUtc has been set

                    var itemHasNeverBeenPublished = part.PublishedUtc == null;
                    var thisIsTheInitialVersionRecord = part.ContentItem.Version < 2;

                    // Dates are assumed the same if the millisecond part is the only difference.
                    // This is because SqlCe doesn't support high precision times (Datetime2) and the infoset does
                    // implying some discrepancies between values read from different storage mechanism.
                    var theDatesHaveNotBeenModified = DateUtils.DatesAreEquivalent(part.CreatedUtc, part.VersionCreatedUtc);

                    var theEditorShouldBeBlank =
                        itemHasNeverBeenPublished &&
                        thisIsTheInitialVersionRecord &&
                        theDatesHaveNotBeenModified;

                    if (!theEditorShouldBeBlank)
                    {
                        model.Editor.Date = _dateLocalizationServices.ConvertToLocalizedDateString(part.CreatedUtc);
                        model.Editor.Time = _dateLocalizationServices.ConvertToLocalizedTimeString(part.CreatedUtc);
                    }
                }

                if (updater != null)
                {
                    updater.TryUpdateModel(model, Prefix, null, null);

                    if (!String.IsNullOrWhiteSpace(model.Editor.Date) && !String.IsNullOrWhiteSpace(model.Editor.Time))
                    {
                        try {
                            var utcDateTime = _dateLocalizationServices.ConvertFromLocalizedString(model.Editor.Date, model.Editor.Time);
                            part.CreatedUtc = utcDateTime;
                        }
                        catch (FormatException) {
                            updater.AddModelError(Prefix, T("'{0} {1}' could not be parsed as a valid date and time.", model.Editor.Date, model.Editor.Time));
                        }
                    }
                    else if (!String.IsNullOrWhiteSpace(model.Editor.Date) || !String.IsNullOrWhiteSpace(model.Editor.Time))
                    {
                        updater.AddModelError(Prefix, T("Both the date and time need to be specified."));
                    }

                    // Neither date/time part is specified => do nothing.
                }

                return model;
            }));
        }
Esempio n. 2
0
        protected override DriverResult Editor(CommonPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            var settings = part.TypePartDefinition.Settings.GetModel <DateEditorSettings>();

            if (!settings.ShowDateEditor)
            {
                return(null);
            }

            return(ContentShape(
                       "Parts_Common_Date_Edit",
                       () => {
                DateEditorViewModel model = shapeHelper.Parts_Common_Date_Edit(typeof(DateEditorViewModel));

                if (part.CreatedUtc != null)
                {
                    // show CreatedUtc only if is has been "touched",
                    // i.e. it has been published once, or CreatedUtc has been set

                    var itemHasNeverBeenPublished = part.PublishedUtc == null;
                    var thisIsTheInitialVersionRecord = part.ContentItem.Version < 2;
                    var theDatesHaveNotBeenModified = part.CreatedUtc == part.VersionCreatedUtc;

                    var theEditorShouldBeBlank =
                        itemHasNeverBeenPublished &&
                        thisIsTheInitialVersionRecord &&
                        theDatesHaveNotBeenModified;

                    if (theEditorShouldBeBlank == false)
                    {
                        // date and time are formatted using the same patterns as DateTimePicker is, preventing other cultures issues
                        var createdLocal = TimeZoneInfo.ConvertTimeFromUtc(part.CreatedUtc.Value, Services.WorkContext.CurrentTimeZone);

                        model.CreatedDate = createdLocal.ToString("d", _cultureInfo.Value);
                        model.CreatedTime = createdLocal.ToString("t", _cultureInfo.Value);
                    }
                }

                if (updater != null)
                {
                    updater.TryUpdateModel(model, Prefix, null, null);

                    if (!string.IsNullOrWhiteSpace(model.CreatedDate) && !string.IsNullOrWhiteSpace(model.CreatedTime))
                    {
                        DateTime createdUtc;

                        string parseDateTime = String.Concat(model.CreatedDate, " ", model.CreatedTime);

                        // use current culture
                        if (DateTime.TryParse(parseDateTime, _cultureInfo.Value, DateTimeStyles.None, out createdUtc))
                        {
                            // the date time is entered locally for the configured timezone
                            part.CreatedUtc = TimeZoneInfo.ConvertTimeToUtc(createdUtc, Services.WorkContext.CurrentTimeZone);
                        }
                        else
                        {
                            updater.AddModelError(Prefix, T("{0} is an invalid date and time", parseDateTime));
                        }
                    }
                    else if (!string.IsNullOrWhiteSpace(model.CreatedDate) || !string.IsNullOrWhiteSpace(model.CreatedTime))
                    {
                        // only one part is specified
                        updater.AddModelError(Prefix, T("Both the date and time need to be specified."));
                    }

                    // none date/time part is specified => do nothing
                }

                return model;
            }));
        }
Esempio n. 3
0
        protected override DriverResult Editor(CommonPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            var settings = part.TypePartDefinition.Settings.GetModel <DateEditorSettings>();

            if (!settings.ShowDateEditor)
            {
                return(null);
            }

            return(ContentShape(
                       "Parts_Common_Date_Edit",
                       () => {
                DateEditorViewModel model = shapeHelper.Parts_Common_Date_Edit(typeof(DateEditorViewModel));

                model.Editor = new DateTimeEditor()
                {
                    ShowDate = true,
                    ShowTime = true
                };

                if (part.CreatedUtc != null)
                {
                    // show CreatedUtc only if is has been "touched",
                    // i.e. it has been published once, or CreatedUtc has been set

                    var itemHasNeverBeenPublished = part.PublishedUtc == null;
                    var thisIsTheInitialVersionRecord = part.ContentItem.Version < 2;
                    var theDatesHaveNotBeenModified = part.CreatedUtc == part.VersionCreatedUtc;

                    var theEditorShouldBeBlank =
                        itemHasNeverBeenPublished &&
                        thisIsTheInitialVersionRecord &&
                        theDatesHaveNotBeenModified;

                    if (!theEditorShouldBeBlank)
                    {
                        model.Editor.Date = _dateServices.ConvertToLocalDateString(part.CreatedUtc, "");
                        model.Editor.Time = _dateServices.ConvertToLocalTimeString(part.CreatedUtc, "");
                    }
                }

                if (updater != null)
                {
                    updater.TryUpdateModel(model, Prefix, null, null);

                    if (!String.IsNullOrWhiteSpace(model.Editor.Date) && !String.IsNullOrWhiteSpace(model.Editor.Time))
                    {
                        try {
                            var utcDateTime = _dateServices.ConvertFromLocalString(model.Editor.Date, model.Editor.Time);
                            part.CreatedUtc = utcDateTime;
                            part.VersionCreatedUtc = utcDateTime;
                        }
                        catch (FormatException) {
                            updater.AddModelError(Prefix, T("'{0} {1}' could not be parsed as a valid date and time.", model.Editor.Date, model.Editor.Time));
                        }
                    }
                    else if (!String.IsNullOrWhiteSpace(model.Editor.Date) || !String.IsNullOrWhiteSpace(model.Editor.Time))
                    {
                        updater.AddModelError(Prefix, T("Both the date and time need to be specified."));
                    }

                    // Neither date/time part is specified => do nothing.
                }

                return model;
            }));
        }