Esempio n. 1
0
        /// <inheritdoc />
        public IEnumerable <string> GetBlockPlaceholderValues(Guid blockId, string propertyGroupAlias, CancellationToken cancellationToken)
        {
            var block           = _futureNhsContentService.GetPublishedContent(blockId, cancellationToken);
            var contentType     = _contentTypeService.Get(block.ContentType.Alias);
            var propertyGroup   = contentType.PropertyGroups.FirstOrDefault(x => x.Alias == propertyGroupAlias);
            var groupProperties = propertyGroup?.PropertyTypes.Select(x => x.Alias);

            return((from property in block.Properties.Where(x => groupProperties != null && groupProperties.Contains(x.Alias))
                    let converter = _converters.FirstOrDefault(x => x.EditorAlias.Equals(property.PropertyType.EditorAlias))
                                    where converter != null
                                    select property.GetValue().ToString()).ToList());
        }
Esempio n. 2
0
        public ContentModel ResolveContent(IPublishedElement content, Dictionary <string, object>?options = null)
        {
            try
            {
                if (content == null)
                {
                    throw new ArgumentNullException(nameof(content));
                }

                var contentModel = new ContentModel
                {
                    System = new SystemModel
                    {
                        Id          = content.Key,
                        ContentType = content.ContentType.Alias,
                        Type        = content.ContentType.ItemType.ToString()
                    }
                };

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


                if (content is IPublishedContent publishedContent)
                {
                    contentModel.System.CreatedAt  = publishedContent.CreateDate;
                    contentModel.System.EditedAt   = publishedContent.UpdateDate;
                    contentModel.System.Locale     = _variationContextAccessor.VariationContext.Culture;
                    contentModel.System.Name       = publishedContent.Name;
                    contentModel.System.UrlSegment = publishedContent.UrlSegment;

                    if (options != null &&
                        options.ContainsKey("addUrl") &&
                        bool.TryParse(options["addUrl"].ToString(), out var addUrl) &&
                        addUrl)
                    {
                        contentModel.System.Url = publishedContent.Url(mode: UrlMode.Absolute);
                    }
                }

                foreach (var property in content.Properties)
                {
                    var converter =
                        _converters.FirstOrDefault(x => x.EditorAlias.Equals(property.PropertyType.EditorAlias));
                    if (converter != null)
                    {
                        var prop = property.Value(_publishedValueFallback);

                        if (prop == null)
                        {
                            continue;
                        }


                        prop = converter.Convert(prop, options?.ToDictionary(x => x.Key, x => x.Value));

                        if (prop != null)
                        {
                            dict.Add(property.Alias, prop);
                        }
                    }
                    else
                    {
                        dict.Add(
                            property.Alias,
                            $"No converter implemented for editor: {property.PropertyType.EditorAlias}");
                    }
                }

                contentModel.Fields = dict;
                return(contentModel);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "An exceptional exception happened, see the inner exception for details");
                throw;
            }
        }
        public ContentModelData ResolveContent(IPublishedElement content, string propertyGroupAlias, Dictionary <string, object>?options = null)
        {
            try
            {
                if (content is null)
                {
                    return(new ContentModelData());
                }

                var contentType  = _contentTypeService.Get(content.ContentType.Alias);
                var contentModel = new ContentModelData
                {
                    Item = new ContentModelItemData
                    {
                        Id          = content.Key,
                        ContentType = content.ContentType.Alias,
                        Type        = content.ContentType.ItemType.ToString(),
                    }
                };

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

                if (content is IPublishedContent publishedContent)
                {
                    contentModel.Item.CreatedAt  = publishedContent.CreateDate;
                    contentModel.Item.EditedAt   = publishedContent.UpdateDate;
                    contentModel.Item.Locale     = _variationContextAccessor.VariationContext.Culture;
                    contentModel.Item.Name       = publishedContent.Name;
                    contentModel.Item.UrlSegment = publishedContent.UrlSegment;

                    if (options is not null &&
                        options.ContainsKey("addUrl") &&
                        bool.TryParse(options["addUrl"].ToString(), out var addUrl) &&
                        addUrl)
                    {
                        contentModel.Item.Url = publishedContent.Url(mode: UrlMode.Absolute);
                    }

                    // Add blocks to content if applicable (loops through all nested content)
                    List <ContentModelData> contentModelDataList = new();
                    if (publishedContent.Children is not null && publishedContent.Children.Any())
                    {
                        foreach (var child in publishedContent.Children.Where(x => x.ContentType.Alias is not GeneralWebPage.ModelTypeAlias))
                        {
                            contentModelDataList.Add(ResolveContent(child, "content"));
                        }
                        contentDictionary.Add("blocks", contentModelDataList);
                    }
                }

                var group           = contentType.PropertyGroups.FirstOrDefault(x => x.Alias == propertyGroupAlias);
                var groupProperties = group?.PropertyTypes.Select(x => x.Alias);
                if (groupProperties is not null)
                {
                    foreach (var property in content.Properties.Where(x => groupProperties.Contains(x.Alias)))
                    {
                        var converter =
                            _converters.FirstOrDefault(x => x.EditorAlias.Equals(property.PropertyType.EditorAlias));
                        if (converter is not null)
                        {
                            var propertyValue = property.Value(_publishedValueFallback);

                            if (propertyValue is null)
                            {
                                contentDictionary.Add(property.Alias, null);
                                continue;
                            }

                            propertyValue = converter.Convert(propertyValue, options?.ToDictionary(x => x.Key, x => x.Value));

                            if (propertyValue is not null)
                            {
                                contentDictionary.Add(property.Alias, propertyValue);
                            }
                        }
                        else
                        {
                            contentDictionary.Add(
                                property.Alias,
                                $"No converter implemented for editor: {property.PropertyType.EditorAlias}");
                        }
                    }
                }

                contentModel.Content = contentDictionary;
                return(contentModel);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "An exception occured when resolving content, see the inner exception for details");
                throw;
            }
        }