public IPublishedElement ConvertToElement(BlockItemData data, PropertyCacheLevel referenceCacheLevel, bool preview) { var publishedContentCache = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot().Content; // Only convert element types - content types will cause an exception when PublishedModelFactory creates the model var publishedContentType = publishedContentCache.GetContentType(data.ContentTypeKey); if (publishedContentType == null || publishedContentType.IsElement == false) { return(null); } var propertyValues = data.RawPropertyValues; // Get the UDI from the deserialized object. If this is empty, we can fallback to checking the 'key' if there is one var key = (data.Udi is GuidUdi gudi) ? gudi.Guid : Guid.Empty; if (key == Guid.Empty && propertyValues.TryGetValue("key", out var keyo)) { Guid.TryParse(keyo.ToString(), out key); } IPublishedElement element = new PublishedElement(publishedContentType, key, propertyValues, preview, referenceCacheLevel, _publishedSnapshotAccessor); element = _publishedModelFactory.CreateModel(element); return(element); }
// beware what you use that one for - you don't want to cache its result private IAppCache?GetAppropriateCache() { var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot(); var cache = publishedSnapshot == null ? null : ((IsPreviewing == false || PublishedSnapshotService.FullCacheWhenPreviewing) && (ContentType.ItemType != PublishedItemType.Member) ? publishedSnapshot.ElementsCache : publishedSnapshot.SnapshotCache); return(cache); }
protected IPublishedElement?ConvertToElement(JObject sourceObject, PropertyCacheLevel referenceCacheLevel, bool preview) { var elementTypeAlias = sourceObject[NestedContentPropertyEditor.ContentTypeAliasPropertyKey]?.ToObject <string>(); if (string.IsNullOrEmpty(elementTypeAlias)) { return(null); } var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot(); // Only convert element types - content types will cause an exception when PublishedModelFactory creates the model var publishedContentType = publishedSnapshot.Content?.GetContentType(elementTypeAlias); if (publishedContentType is null || publishedContentType.IsElement == false) { return(null); } var propertyValues = sourceObject.ToObject <Dictionary <string, object?> >(); if (propertyValues is null || !propertyValues.TryGetValue("key", out var keyo) || !Guid.TryParse(keyo?.ToString(), out var key)) { key = Guid.Empty; } IPublishedElement element = new PublishedElement(publishedContentType, key, propertyValues, preview, referenceCacheLevel, _publishedSnapshotAccessor); element = PublishedModelFactory.CreateModel(element); return(element); }
private CacheValues GetCacheValues(PropertyCacheLevel cacheLevel) { CacheValues cacheValues; switch (cacheLevel) { case PropertyCacheLevel.None: // never cache anything cacheValues = new CacheValues(); break; case PropertyCacheLevel.Element: // cache within the property object itself, ie within the content object cacheValues = _cacheValues ?? (_cacheValues = new CacheValues()); break; case PropertyCacheLevel.Elements: // cache within the elements cache, depending... var snapshotCache = GetSnapshotCache(); cacheValues = (CacheValues?)snapshotCache?.Get(ValuesCacheKey, () => new CacheValues()) ?? new CacheValues(); break; case PropertyCacheLevel.Snapshot: var publishedSnapshot = _publishedSnapshotAccessor?.GetRequiredPublishedSnapshot(); // cache within the snapshot cache var facadeCache = publishedSnapshot?.SnapshotCache; cacheValues = (CacheValues?)facadeCache?.Get(ValuesCacheKey, () => new CacheValues()) ?? new CacheValues(); break; default: throw new InvalidOperationException("Invalid cache level."); } return(cacheValues); }
public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel cacheLevel, object source, bool preview) { var isMultiple = IsMultipleDataType(propertyType.DataType); var udis = (Udi[])source; var mediaItems = new List <IPublishedContent>(); if (source == null) { return(isMultiple ? mediaItems : null); } if (udis.Any()) { var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot(); foreach (var udi in udis) { var guidUdi = udi as GuidUdi; if (guidUdi == null) { continue; } var item = publishedSnapshot.Media.GetById(guidUdi.Guid); if (item != null) { mediaItems.Add(item); } } return(isMultiple ? mediaItems : FirstOrDefault(mediaItems)); } return(source); }
public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview) { using (_proflog.DebugDuration <MultiUrlPickerValueConverter>($"ConvertPropertyToLinks ({propertyType.DataType.Id})")) { var maxNumber = propertyType.DataType.ConfigurationAs <MultiUrlPickerConfiguration>().MaxNumber; if (inter == null) { return(maxNumber == 1 ? null : Enumerable.Empty <Link>()); } var links = new List <Link>(); var dtos = _jsonSerializer.Deserialize <IEnumerable <MultiUrlPickerValueEditor.LinkDto> >(inter.ToString()); var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot(); foreach (var dto in dtos) { var type = LinkType.External; var url = dto.Url; if (dto.Udi != null) { type = dto.Udi.EntityType == Constants.UdiEntityType.Media ? LinkType.Media : LinkType.Content; var content = type == LinkType.Media ? publishedSnapshot.Media.GetById(preview, dto.Udi.Guid) : publishedSnapshot.Content.GetById(preview, dto.Udi.Guid); if (content == null || content.ContentType.ItemType == PublishedItemType.Element) { continue; } url = content.Url(_publishedUrlProvider); } links.Add( new Link { Name = dto.Name, Target = dto.Target, Type = type, Udi = dto.Udi, Url = url + dto.QueryString, } ); } if (maxNumber == 1) { return(links.FirstOrDefault()); } if (maxNumber > 0) { return(links.Take(maxNumber)); } return(links); } }
private CacheValues GetCacheValues(PropertyCacheLevel cacheLevel) { CacheValues cacheValues; IPublishedSnapshot publishedSnapshot; IAppCache cache; switch (cacheLevel) { case PropertyCacheLevel.None: // never cache anything cacheValues = new CacheValues(); break; case PropertyCacheLevel.Element: // cache within the property object itself, ie within the content object cacheValues = _cacheValues ?? (_cacheValues = new CacheValues()); break; case PropertyCacheLevel.Elements: // cache within the elements cache, unless previewing, then use the snapshot or // elements cache (if we don't want to pollute the elements cache with short-lived // data) depending on settings // for members, always cache in the snapshot cache - never pollute elements cache publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot(); cache = publishedSnapshot == null ? null : ((_isPreviewing == false || PublishedSnapshotService.FullCacheWhenPreviewing) && (_isMember == false) ? publishedSnapshot.ElementsCache : publishedSnapshot.SnapshotCache); cacheValues = GetCacheValues(cache); break; case PropertyCacheLevel.Snapshot: // cache within the snapshot cache publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot(); cache = publishedSnapshot?.SnapshotCache; cacheValues = GetCacheValues(cache); break; default: throw new InvalidOperationException("Invalid cache level."); } return(cacheValues); }
public object ConvertIntermediateToObject( IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview) { var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot(); return(publishedSnapshot.Content.GetById((int)inter)); }
public IPublishedContent GetPublishedMember(MemberIdentityUser user) { if (user == null) { return(null); } IMember member = _memberService.GetByKey(user.Key); if (member == null) { return(null); } var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot(); return(publishedSnapshot.Members.Get(member)); }
public override object?ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel cacheLevel, object?source, bool preview) { if (source == null) { return(null); } IPublishedContent? member; IPublishedSnapshot publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot(); if (source is int id) { IMember?m = _memberService.GetById(id); if (m == null) { return(null); } member = publishedSnapshot?.Members?.Get(m); if (member != null) { return(member); } } else { if (source is not GuidUdi sourceUdi) { return(null); } IMember?m = _memberService.GetByKey(sourceUdi.Guid); if (m == null) { return(null); } member = publishedSnapshot?.Members?.Get(m); if (member != null) { return(member); } } return(source); }
public override object?ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object?inter, bool preview) { var isMultiple = IsMultipleDataType(propertyType.DataType); if (string.IsNullOrEmpty(inter?.ToString())) { // Short-circuit on empty value return(isMultiple ? Enumerable.Empty <MediaWithCrops>() : null); } var mediaItems = new List <MediaWithCrops>(); IEnumerable <MediaPicker3PropertyEditor.MediaPicker3PropertyValueEditor.MediaWithCropsDto> dtos = MediaPicker3PropertyEditor.MediaPicker3PropertyValueEditor.Deserialize(_jsonSerializer, inter); MediaPicker3Configuration?configuration = propertyType.DataType.ConfigurationAs <MediaPicker3Configuration>(); IPublishedSnapshot publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot(); foreach (MediaPicker3PropertyEditor.MediaPicker3PropertyValueEditor.MediaWithCropsDto dto in dtos) { IPublishedContent?mediaItem = publishedSnapshot.Media?.GetById(preview, dto.MediaKey); if (mediaItem != null) { var localCrops = new ImageCropperValue { Crops = dto.Crops, FocalPoint = dto.FocalPoint, Src = mediaItem.Url(_publishedUrlProvider), }; localCrops.ApplyConfiguration(configuration); // TODO: This should be optimized/cached, as calling Activator.CreateInstance is slow Type mediaWithCropsType = typeof(MediaWithCrops <>).MakeGenericType(mediaItem.GetType()); var mediaWithCrops = (MediaWithCrops)Activator.CreateInstance(mediaWithCropsType, mediaItem, _publishedValueFallback, localCrops) !; mediaItems.Add(mediaWithCrops); if (!isMultiple) { // Short-circuit on single item break; } } } return(isMultiple ? mediaItems : mediaItems.FirstOrDefault()); }
// looks safer but probably useless... ppl should not call these methods directly // and if they do... they have to take care about not doing stupid things //public static PublishedPropertyType GetModelPropertyType2<T>(Expression<Func<T, object>> selector) // where T : PublishedContentModel //{ // var type = typeof (T); // var s1 = type.GetField("ModelTypeAlias", BindingFlags.Public | BindingFlags.Static); // var alias = (s1.IsLiteral && s1.IsInitOnly && s1.FieldType == typeof(string)) ? (string)s1.GetValue(null) : null; // var s2 = type.GetField("ModelItemType", BindingFlags.Public | BindingFlags.Static); // var itemType = (s2.IsLiteral && s2.IsInitOnly && s2.FieldType == typeof(PublishedItemType)) ? (PublishedItemType)s2.GetValue(null) : 0; // var contentType = PublishedContentType.Get(itemType, alias); // // etc... //} public static IPublishedContentType?GetModelContentType(IPublishedSnapshotAccessor publishedSnapshotAccessor, PublishedItemType itemType, string alias) { var publishedSnapshot = publishedSnapshotAccessor.GetRequiredPublishedSnapshot(); switch (itemType) { case PublishedItemType.Content: return(publishedSnapshot.Content?.GetContentType(alias)); case PublishedItemType.Media: return(publishedSnapshot.Media?.GetContentType(alias)); case PublishedItemType.Member: return(publishedSnapshot.Members?.GetContentType(alias)); default: throw new ArgumentOutOfRangeException(nameof(itemType)); } }
public override object ToEditor(IProperty property, string culture = null, string segment = null) { var value = property.GetValue(culture, segment)?.ToString(); if (string.IsNullOrEmpty(value)) { return(Enumerable.Empty <object>()); } try { var links = JsonConvert.DeserializeObject <List <MultiUrlPickerValueEditor.LinkDto> >(value); var documentLinks = links.FindAll(link => link.Udi != null && link.Udi.EntityType == Constants.UdiEntityType.Document); var mediaLinks = links.FindAll(link => link.Udi != null && link.Udi.EntityType == Constants.UdiEntityType.Media); var entities = new List <IEntitySlim>(); if (documentLinks.Count > 0) { entities.AddRange( _entityService.GetAll(UmbracoObjectTypes.Document, documentLinks.Select(link => link.Udi.Guid).ToArray()) ); } if (mediaLinks.Count > 0) { entities.AddRange( _entityService.GetAll(UmbracoObjectTypes.Media, mediaLinks.Select(link => link.Udi.Guid).ToArray()) ); } var result = new List <LinkDisplay>(); foreach (var dto in links) { GuidUdi udi = null; var icon = "icon-link"; var published = true; var trashed = false; var url = dto.Url; if (dto.Udi != null) { IUmbracoEntity entity = entities.Find(e => e.Key == dto.Udi.Guid); if (entity == null) { continue; } var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot(); if (entity is IDocumentEntitySlim documentEntity) { icon = documentEntity.ContentTypeIcon; published = culture == null ? documentEntity.Published : documentEntity.PublishedCultures.Contains(culture); udi = new GuidUdi(Constants.UdiEntityType.Document, documentEntity.Key); url = publishedSnapshot.Content.GetById(entity.Key)?.Url(_publishedUrlProvider) ?? "#"; trashed = documentEntity.Trashed; } else if (entity is IContentEntitySlim contentEntity) { icon = contentEntity.ContentTypeIcon; published = !contentEntity.Trashed; udi = new GuidUdi(Constants.UdiEntityType.Media, contentEntity.Key); url = publishedSnapshot.Media.GetById(entity.Key)?.Url(_publishedUrlProvider) ?? "#"; trashed = contentEntity.Trashed; } else { // Not supported continue; } } result.Add(new LinkDisplay { Icon = icon, Name = dto.Name, Target = dto.Target, Trashed = trashed, Published = published, QueryString = dto.QueryString, Udi = udi, Url = url ?? "" }); } return(result); } catch (Exception ex) { _logger.LogError(ex, "Error getting links"); } return(base.ToEditor(property, culture, segment)); }
public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel cacheLevel, object source, bool preview) { if (source == null) { return(null); } // TODO: Inject an UmbracoHelper and create a GetUmbracoHelper method based on either injected or singleton if (_umbracoContextAccessor.TryGetUmbracoContext(out _)) { if (propertyType.EditorAlias.Equals(Constants.PropertyEditors.Aliases.MultiNodeTreePicker)) { var udis = (Udi[])source; var isSingleNodePicker = IsSingleNodePicker(propertyType); if ((propertyType.Alias != null && PropertiesToExclude.InvariantContains(propertyType.Alias)) == false) { var multiNodeTreePicker = new List <IPublishedContent>(); var objectType = UmbracoObjectTypes.Unknown; var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot(); foreach (var udi in udis) { var guidUdi = udi as GuidUdi; if (guidUdi == null) { continue; } IPublishedContent multiNodeTreePickerItem = null; switch (udi.EntityType) { case Constants.UdiEntityType.Document: multiNodeTreePickerItem = GetPublishedContent(udi, ref objectType, UmbracoObjectTypes.Document, id => publishedSnapshot.Content.GetById(guidUdi.Guid)); break; case Constants.UdiEntityType.Media: multiNodeTreePickerItem = GetPublishedContent(udi, ref objectType, UmbracoObjectTypes.Media, id => publishedSnapshot.Media.GetById(guidUdi.Guid)); break; case Constants.UdiEntityType.Member: multiNodeTreePickerItem = GetPublishedContent(udi, ref objectType, UmbracoObjectTypes.Member, id => { IMember m = _memberService.GetByKey(guidUdi.Guid); if (m == null) { return(null); } IPublishedContent member = publishedSnapshot.Members.Get(m); return(member); }); break; } if (multiNodeTreePickerItem != null && multiNodeTreePickerItem.ContentType.ItemType != PublishedItemType.Element) { multiNodeTreePicker.Add(multiNodeTreePickerItem); if (isSingleNodePicker) { break; } } } if (isSingleNodePicker) { return(multiNodeTreePicker.FirstOrDefault()); } return(multiNodeTreePicker); } // return the first nodeId as this is one of the excluded properties that expects a single id return(udis.FirstOrDefault()); } } return(source); }