Archetype.PropertyEditors.ArchetypePropertyEditor.ArchetypePropertyValueEditor.ConvertEditorToDb C# (CSharp) Метод

ConvertEditorToDb() публичный Метод

A method to deserialize the string value that has been saved in the content editor to an object to be stored in the database.
By default this will attempt to automatically convert the string value to the value type supplied by ValueType. If overridden then the object returned must match the type supplied in the ValueType, otherwise persisting the value to the DB will fail when it tries to validate the value type.
public ConvertEditorToDb ( Umbraco.Core.Models.Editors.ContentPropertyData editorValue, object currentValue ) : object
editorValue Umbraco.Core.Models.Editors.ContentPropertyData
currentValue object The current value that has been persisted to the database for this editor. This value may be usesful for /// how the value then get's deserialized again to be re-persisted. In most cases it will probably not be used.
Результат object
            public override object ConvertEditorToDb(ContentPropertyData editorValue, object currentValue)
            {
                if(editorValue.Value == null || editorValue.Value.ToString() == "")
                    return string.Empty;

                // attempt to deserialize the current property value as an Archetype
                var currentArchetype = currentValue != null ? ArchetypeHelper.Instance.DeserializeJsonToArchetype(currentValue.ToString(), editorValue.PreValues) : null;
                var archetype = ArchetypeHelper.Instance.DeserializeJsonToArchetype(editorValue.Value.ToString(), editorValue.PreValues);

                // get all files uploaded via the file manager (if any)
                var uploadedFiles = editorValue.AdditionalData.ContainsKey("files") ? editorValue.AdditionalData["files"] as IEnumerable<ContentItemFile> : null;
                foreach (var fieldset in archetype.Fieldsets)
                {
                    // make sure the publishing dates are in UTC
                    fieldset.ReleaseDate = EnsureUtcDate(fieldset.ReleaseDate);
                    fieldset.ExpireDate = EnsureUtcDate(fieldset.ExpireDate);

                    // assign an id to the fieldset if it has none (e.g. newly created fieldset)
                    fieldset.Id = fieldset.Id == Guid.Empty ? Guid.NewGuid() : fieldset.Id;
                    // find the corresponding fieldset in the current Archetype value (if any)
                    var currentFieldset = currentArchetype != null ? currentArchetype.Fieldsets.FirstOrDefault(f => f.Id == fieldset.Id) : null;
                    foreach (var propDef in fieldset.Properties)
                    {
                        try
                        {
                            // find the corresponding property in the current Archetype value (if any)
                            var currentProperty = currentFieldset != null ? currentFieldset.Properties.FirstOrDefault(p => p.Alias == propDef.Alias) : null;
                            var dtd = ArchetypeHelper.Instance.GetDataTypeByGuid(Guid.Parse(propDef.DataTypeGuid));
                            var preValues = ApplicationContext.Current.Services.DataTypeService.GetPreValuesCollectionByDataTypeId(dtd.Id);

                            var additionalData = new Dictionary<string, object>();

                            // figure out if we need to pass a files collection in the additional data to the property value editor
                            if(uploadedFiles != null)
                            {
                                if(dtd.PropertyEditorAlias == Constants.PropertyEditorAlias)
                                {
                                    // it's a nested Archetype - just pass all uploaded files to the value editor
                                    additionalData["files"] = uploadedFiles.ToList();
                                }
                                else if (propDef.EditorState != null && propDef.EditorState.FileNames != null && propDef.EditorState.FileNames.Any())
                                {
                                    // pass the uploaded files that belongs to this property (if any) to the value editor
                                    var propertyFiles = propDef.EditorState.FileNames.Select(f => uploadedFiles.FirstOrDefault(u => u.FileName == f)).Where(f => f != null).ToList();
                                    if(propertyFiles.Any())
                                    {
                                        additionalData["files"] = propertyFiles;
                                    }
                                }
                            }
                            var propData = new ContentPropertyData(propDef.Value, preValues, additionalData);
                            var propEditor = PropertyEditorResolver.Current.GetByAlias(dtd.PropertyEditorAlias);
                            // make sure to send the current property value (if any) to the PE ValueEditor
                            propDef.Value = propEditor.ValueEditor.ConvertEditorToDb(propData, currentProperty != null ? currentProperty.Value : null);
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error<ArchetypePropertyValueEditor>(ex.Message, ex);
                        }
                    }
                }

                return archetype.SerializeForPersistence();
            }