Beispiel #1
0
        public static IPublishedContent ConvertInnerContentToPublishedContent(
            JObject item,
            IPublishedContent parentNode = null,
            int sortOrder = 0,
            int level     = 0,
            bool preview  = false)
        {
            var publishedContentType = GetPublishedContentTypeFromItem(item);

            if (publishedContentType == null)
            {
                return(null);
            }

            var propValues = item.ToObject <Dictionary <string, object> >();
            var properties = new List <IPublishedProperty>();

            foreach (var jProp in propValues)
            {
                var propType = publishedContentType.GetPropertyType(jProp.Key);
                if (propType != null)
                {
                    properties.Add(new DetachedPublishedProperty(propType, jProp.Value, preview));
                }
            }

            // Manually parse out the special properties
            propValues.TryGetValue("name", out object nameObj);
            propValues.TryGetValue("key", out object keyObj);

            // Get the current request node we are embedded in
            var pcr           = UmbracoContext.Current.PublishedContentRequest;
            var containerNode = pcr != null && pcr.HasPublishedContent ? pcr.PublishedContent : null;

            var node = new DetachedPublishedContent(
                keyObj == null ? Guid.Empty : Guid.Parse(keyObj.ToString()),
                nameObj?.ToString(),
                publishedContentType,
                properties.ToArray(),
                containerNode,
                parentNode,
                sortOrder,
                level,
                preview);

            // Process children
            if (propValues.ContainsKey("children"))
            {
                var children = ConvertInnerContentToPublishedContent((JArray)propValues["children"], node, level + 1, preview);
                node.SetChildren(children);
            }

            if (PublishedContentModelFactoryResolver.HasCurrent && PublishedContentModelFactoryResolver.Current.HasValue)
            {
                // Let the current model factory create a typed model to wrap our model
                return(PublishedContentModelFactoryResolver.Current.Factory.CreateModel(node));
            }

            return(node);
        }
        public static object ConvertPropertyToNestedContent(this PublishedPropertyType propertyType, object source, bool preview)
        {
            using (DisposableTimer.DebugDuration <PublishedPropertyType>(string.Format("ConvertPropertyToNestedContent ({0})", propertyType.DataTypeId)))
            {
                if (source != null && !source.ToString().IsNullOrWhiteSpace())
                {
                    var rawValue       = JsonConvert.DeserializeObject <List <object> >(source.ToString());
                    var processedValue = new List <IPublishedContent>();

                    var preValueCollection = NestedContentHelper.GetPreValuesCollectionByDataTypeId(propertyType.DataTypeId);
                    var preValueDictionary = preValueCollection.PreValuesAsDictionary.ToDictionary(x => x.Key, x => x.Value.Value);

                    for (var i = 0; i < rawValue.Count; i++)
                    {
                        var item = (JObject)rawValue[i];

                        // Convert from old style (v.0.1.1) data format if necessary
                        // - Please note: This call has virtually no impact on rendering performance for new style (>v0.1.1).
                        //                Even so, this should be removed eventually, when it's safe to assume that there is
                        //                no longer any need for conversion.
                        NestedContentHelper.ConvertItemValueFromV011(item, propertyType.DataTypeId, ref preValueCollection);

                        var contentTypeAlias = NestedContentHelper.GetContentTypeAliasFromItem(item);
                        if (string.IsNullOrEmpty(contentTypeAlias))
                        {
                            continue;
                        }

                        var publishedContentType = PublishedContentType.Get(PublishedItemType.Content, contentTypeAlias);
                        if (publishedContentType == null)
                        {
                            continue;
                        }

                        var propValues = item.ToObject <Dictionary <string, object> >();
                        var properties = new List <IPublishedProperty>();

                        foreach (var jProp in propValues)
                        {
                            var propType = publishedContentType.GetPropertyType(jProp.Key);
                            if (propType != null)
                            {
                                properties.Add(new DetachedPublishedProperty(propType, jProp.Value, preview));
                            }
                        }

                        // Parse out the name manually
                        object nameObj = null;
                        if (propValues.TryGetValue("name", out nameObj))
                        {
                            // Do nothing, we just want to parse out the name if we can
                        }

                        // Get the current request node we are embedded in
                        var pcr           = UmbracoContext.Current == null ? null : UmbracoContext.Current.PublishedContentRequest;
                        var containerNode = pcr != null && pcr.HasPublishedContent ? pcr.PublishedContent : null;

                        // Create the model based on our implementation of IPublishedContent
                        IPublishedContent content = new DetachedPublishedContent(
                            nameObj == null ? null : nameObj.ToString(),
                            publishedContentType,
                            properties.ToArray(),
                            containerNode,
                            i,
                            preview);

                        if (PublishedContentModelFactoryResolver.HasCurrent && PublishedContentModelFactoryResolver.Current.HasValue)
                        {
                            // Let the current model factory create a typed model to wrap our model
                            content = PublishedContentModelFactoryResolver.Current.Factory.CreateModel(content);
                        }

                        // Add the (typed) model as a result
                        processedValue.Add(content);
                    }

                    if (propertyType.IsSingleNestedContentProperty())
                    {
                        return(processedValue.FirstOrDefault());
                    }

                    return(processedValue);
                }
            }

            return(null);
        }
        public static IPublishedContent ConvertInnerContentToPublishedContent(JObject item,
                                                                              IPublishedContent parentNode = null,
                                                                              int sortOrder = 0,
                                                                              int level     = 0,
                                                                              bool preview  = false)
        {
            var contentTypeAlias = GetContentTypeAliasFromItem(item);

            if (string.IsNullOrEmpty(contentTypeAlias))
            {
                return(null);
            }

            var publishedContentType = PublishedContentType.Get(PublishedItemType.Content, contentTypeAlias);

            if (publishedContentType == null)
            {
                return(null);
            }

            var propValues = item.ToObject <Dictionary <string, object> >();
            var properties = new List <IPublishedProperty>();

            foreach (var jProp in propValues)
            {
                var propType = publishedContentType.GetPropertyType(jProp.Key);
                if (propType != null)
                {
                    properties.Add(new DetachedPublishedProperty(propType, jProp.Value, preview));
                }
            }

            // Parse out the name manually
            object nameObj;

            if (propValues.TryGetValue("name", out nameObj))
            {
                // Do nothing, we just want to parse out the name if we can
            }

            // Parse out key manually
            object keyObj;

            if (propValues.TryGetValue("key", out keyObj))
            {
                // Do nothing, we just want to parse out the key if we can
            }

            // Get the current request node we are embedded in
            var pcr           = UmbracoContext.Current.PublishedContentRequest;
            var containerNode = pcr != null && pcr.HasPublishedContent ? pcr.PublishedContent : null;

            var node = new DetachedPublishedContent(
                keyObj == null ? Guid.Empty : Guid.Parse(keyObj.ToString()),
                nameObj?.ToString(),
                publishedContentType,
                properties.ToArray(),
                containerNode,
                parentNode,
                sortOrder,
                level,
                preview);

            // Process children
            if (propValues.ContainsKey("children"))
            {
                var children = ConvertInnerContentToPublishedContent((JArray)propValues["children"], node, level + 1, preview);
                node.SetChildren(children);
            }

            return(node);
        }
Beispiel #4
0
        private static IPublishedContent ConvertValue(string id, string contentTypeAlias, string dataJson)
        {
            using (ApplicationContext.Current.ProfilingLogger.DebugDuration <DocTypeGridEditorHelper>(string.Format("ConvertValue ({0}, {1})", id, contentTypeAlias)))
            {
                var contentTypes = GetContentTypesByAlias(contentTypeAlias);
                var properties   = new List <IPublishedProperty>();

                // Convert all the properties
                var data       = JsonConvert.DeserializeObject(dataJson);
                var propValues = ((JObject)data).ToObject <Dictionary <string, object> >();
                foreach (var jProp in propValues)
                {
                    var propType = contentTypes.PublishedContentType.GetPropertyType(jProp.Key);
                    if (propType != null)
                    {
                        /* Because we never store the value in the database, we never run the property editors
                         * "ConvertEditorToDb" method however the property editors will expect their value to
                         * be in a "DB" state so to get round this, we run the "ConvertEditorToDb" here before
                         * we go on to convert the value for the view.
                         */
                        var propEditor    = PropertyEditorResolver.Current.GetByAlias(propType.PropertyEditorAlias);
                        var propPreValues = GetPreValuesCollectionByDataTypeId(propType.DataTypeId);

                        var contentPropData = new ContentPropertyData(
                            jProp.Value,
                            propPreValues,
                            new Dictionary <string, object>());

                        var newValue = propEditor.ValueEditor.ConvertEditorToDb(contentPropData, jProp.Value);

                        /* Now that we have the DB stored value, we actually need to then convert it into its
                         * XML serialized state as expected by the published property by calling ConvertDbToString
                         */
                        var propType2 = contentTypes.ContentType.CompositionPropertyTypes.First(x => x.Alias.InvariantEquals(propType.PropertyTypeAlias));

                        Property prop2 = null;
                        try
                        {
                            /* HACK: [LK:2016-04-01] When using the "Umbraco.Tags" property-editor, the converted DB value does
                             * not match the datatypes underlying db-column type. So it throws a "Type validation failed" exception.
                             * We feel that the Umbraco core isn't handling the Tags value correctly, as it should be the responsiblity
                             * of the "Umbraco.Tags" property-editor to handle the value conversion into the correct type.
                             * See: http://issues.umbraco.org/issue/U4-8279
                             */
                            prop2 = new Property(propType2, newValue);
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error <DocTypeGridEditorHelper>("[DocTypeGridEditor] Error creating Property object.", ex);
                        }

                        if (prop2 != null)
                        {
                            var newValue2 = propEditor.ValueEditor.ConvertDbToString(prop2, propType2, Services.DataTypeService);

                            properties.Add(new DetachedPublishedProperty(propType, newValue2));
                        }
                    }
                }

                // Parse out the name manually
                if (propValues.TryGetValue("name", out object nameObj))
                {
                    // Do nothing, we just want to parse out the name if we can
                }

                // Get the current request node we are embedded in
                var pcr           = UmbracoContext.Current?.PublishedContentRequest;
                var containerNode = pcr != null && pcr.HasPublishedContent ? pcr.PublishedContent : null;

                // Create the model based on our implementation of IPublishedContent
                IPublishedContent content = new DetachedPublishedContent(
                    nameObj?.ToString(),
                    contentTypes.PublishedContentType,
                    properties.ToArray(),
                    containerNode);

                if (PublishedContentModelFactoryResolver.HasCurrent && PublishedContentModelFactoryResolver.Current.HasValue)
                {
                    // Let the current model factory create a typed model to wrap our model
                    content = PublishedContentModelFactoryResolver.Current.Factory.CreateModel(content);
                }

                return(content);
            }
        }