private EmbeddedContentPropertyDisplay GetProperty(PropertyType propertyType, object value) { var property = new EmbeddedContentPropertyDisplay { Editor = propertyType.PropertyEditorAlias, Label = UmbracoDictionaryTranslate(propertyType.Name), Description = UmbracoDictionaryTranslate(propertyType.Description), Alias = propertyType.Alias, Value = value }; PropertyEditor propertyEditor = _propertyEditorResolver.GetByAlias(propertyType.PropertyEditorAlias); PreValueCollection preValues = _dataTypeService.GetPreValuesCollectionByDataTypeId(propertyType.DataTypeDefinitionId); property.Value = propertyEditor.ValueEditor.ConvertDbToEditor( new Property(propertyType, value?.ToString()), propertyType, _dataTypeService ); propertyEditor.ValueEditor.ConfigureForDisplay(preValues); property.Config = propertyEditor.PreValueEditor.ConvertDbToEditor(propertyEditor.DefaultPreValues, preValues); property.View = propertyEditor.ValueEditor.View; property.HideLabel = propertyEditor.ValueEditor.HideLabel; property.Validation.Mandatory = propertyType.Mandatory; property.Validation.Pattern = propertyType.ValidationRegExp; return(property); }
public IEnumerable <ValidationResult> Validate(object value, PreValueCollection preValues, PropertyEditor editor) { if (value == null) { yield break; } List <IContentType> contentTypes = _contentTypeService.GetAllContentTypes().ToList(); List <EmbeddedContentItemDisplay> itemsDisplay = JsonConvert.DeserializeObject <IEnumerable <EmbeddedContentItemDisplay> >(value.ToString()).ToList(); foreach (EmbeddedContentItemDisplay itemDisplay in itemsDisplay) { IContentType contentType = contentTypes.FirstOrDefault(x => x.Alias == itemDisplay.ContentTypeAlias); if (contentType == null) { continue; } IEnumerable <EmbeddedContentPropertyDisplay> properties = itemDisplay.Tabs.SelectMany(x => x.Properties).ToList(); foreach (PropertyType propertyType in contentType.CompositionPropertyGroups.SelectMany(x => x.PropertyTypes)) { EmbeddedContentPropertyDisplay property = properties.FirstOrDefault(x => x.Alias == propertyType.Alias); if (property == null) { continue; } PropertyEditor propertyEditor = PropertyEditorResolver.Current.GetByAlias(propertyType.PropertyEditorAlias); if (propertyEditor == null) { continue; } PreValueCollection propertyPreValues = _dataTypeService.GetPreValuesCollectionByDataTypeId(propertyType.DataTypeDefinitionId); if (propertyType.Mandatory) { foreach (ValidationResult result in propertyEditor.ValueEditor.RequiredValidator.Validate(property.Value, "", propertyPreValues, editor)) { yield return(new ValidationResult(result.ErrorMessage, result.MemberNames.Select(x => $"item-{itemDisplay.Key}-{property.Alias}-{x}"))); } } if (false == propertyType.ValidationRegExp.IsNullOrWhiteSpace()) { var str = property.Value as string; if (property.Value != null && false == str.IsNullOrWhiteSpace() || propertyType.Mandatory) { foreach (ValidationResult result in propertyEditor.ValueEditor.RegexValidator.Validate(property.Value, propertyType.ValidationRegExp, propertyPreValues, editor)) { yield return(new ValidationResult(result.ErrorMessage, result.MemberNames.Select(x => $"item-{itemDisplay.Key}-{property.Alias}-{x}"))); } } } foreach (IPropertyValidator validator in propertyEditor.ValueEditor.Validators) { foreach (ValidationResult result in validator.Validate(property.Value, propertyPreValues, editor)) { yield return(new ValidationResult(result.ErrorMessage, result.MemberNames.Select(x => $"item-{itemDisplay.Key}-{property.Alias}-{x}"))); } } } } }
public override object ConvertEditorToDb(ContentPropertyData editorValue, object currentValue) { if (string.IsNullOrEmpty(editorValue.Value?.ToString())) { return(null); } using (_profilingLogger.DebugDuration <EmbeddedContentPropertyEditor>("ConvertEditorToDb()")) { List <IContentType> contentTypes = _contentTypeService.GetAllContentTypes().ToList(); var itemsDisplay = JsonConvert.DeserializeObject <EmbeddedContentItemDisplay[]>(editorValue.Value.ToString()); var currentItems = JsonConvert.DeserializeObject <EmbeddedContentItem[]>(currentValue?.ToString() ?? "[]"); var items = new List <EmbeddedContentItem>(); IEnumerable <ContentItemFile> files = null; if (editorValue.AdditionalData.TryGetValue("files", out object tmp)) { files = tmp as IEnumerable <ContentItemFile>; } foreach (EmbeddedContentItemDisplay itemDisplay in itemsDisplay) { var item = new EmbeddedContentItem { ContentTypeAlias = itemDisplay.ContentTypeAlias, Key = itemDisplay.Key, Name = itemDisplay.Name, Published = itemDisplay.Published, CreateDate = itemDisplay.CreateDate, UpdateDate = itemDisplay.UpdateDate, CreatorId = itemDisplay.CreatorId, WriterId = itemDisplay.WriterId }; IContentType contentType = contentTypes.FirstOrDefault(x => x.Alias == itemDisplay.ContentTypeAlias); if (contentType == null) { continue; } if (item.CreateDate == DateTime.MinValue) { item.CreateDate = DateTime.UtcNow; if (_security?.CurrentUser != null) { item.CreatorId = _security.CurrentUser.Id; } } EmbeddedContentItem currentItem = currentItems.FirstOrDefault(x => x.Key == itemDisplay.Key); if (currentItem != null) { item.CreateDate = currentItem.CreateDate; item.UpdateDate = currentItem.UpdateDate; item.CreatorId = currentItem.CreatorId; item.WriterId = currentItem.WriterId; } foreach (PropertyType propertyType in contentType.CompositionPropertyGroups.SelectMany(x => x.PropertyTypes)) { IEnumerable <Tab <EmbeddedContentPropertyDisplay> > tabs = itemDisplay.Tabs; if (itemDisplay.SettingsTab != null) { tabs = tabs.Concat(new[] { itemDisplay.SettingsTab }); } EmbeddedContentPropertyDisplay property = tabs.SelectMany(x => x.Properties).FirstOrDefault(x => x.Alias == propertyType.Alias); if (property == null) { continue; } PropertyEditor propertyEditor = _propertyEditorResolver.GetByAlias(propertyType.PropertyEditorAlias); PreValueCollection preValues = _dataTypeService.GetPreValuesCollectionByDataTypeId(propertyType.DataTypeDefinitionId); var additionalData = new Dictionary <string, object>(); if (files != null) { if (propertyEditor.Alias == EmbeddedContent.Constants.PropertyEditorAlias) { additionalData["files"] = files; } else if (property.SelectedFiles != null) { additionalData["files"] = files.Where(x => property.SelectedFiles.Contains(x.FileName)); } additionalData["cuid"] = editorValue.AdditionalData["cuid"]; additionalData["puid"] = editorValue.AdditionalData["puid"]; } var propData = new ContentPropertyData(property.Value, preValues, additionalData); object currentPropertyValue = null; if (currentItem != null && currentItem.Properties.TryGetValue(property.Alias, out currentPropertyValue)) { } item.Properties[propertyType.Alias] = propertyEditor.ValueEditor.ConvertEditorToDb(propData, currentPropertyValue); } if (currentItem == null || currentItem.Name != item.Name || currentItem.Published != item.Published || JsonConvert.SerializeObject(currentItem.Properties) != JsonConvert.SerializeObject(item.Properties)) { item.UpdateDate = DateTime.UtcNow; if (_security?.CurrentUser != null) { item.WriterId = _security.CurrentUser.Id; } } items.Add(item); } if (items.Count == 0) { return(null); } return(JsonConvert.SerializeObject(items)); } }