Exemplo n.º 1
0
        private string GetLayoutFieldValueFromJson([NotNull] Item item, [NotNull] FieldEditorField field)
        {
            var writer = new StringWriter();
            var output = new XmlTextWriter(writer)
            {
                Formatting = System.Xml.Formatting.Indented
            };

            output.WriteStartElement("r");

            var root         = JObject.Parse(field.Value);
            var devicesArray = root.Value <JArray>("devices");

            foreach (var device in devicesArray.OfType <JObject>())
            {
                output.WriteStartElement("d");

                output.WriteAttributeStringIf("id", ResolveItemFromIdOrName(item.Database, device.Value <string>("deviceId"), device.Value <string>("deviceName")));
                output.WriteAttributeStringIf("l", ResolveItemFromIdOrName(item.Database, device.Value <string>("layoutId"), device.Value <string>("layoutName")));

                foreach (var rendering in device.Value <JArray>("renderings").OfType <JObject>())
                {
                    var uniqueId = rendering.Value <string>("uniqueId");
                    if (string.IsNullOrEmpty(uniqueId))
                    {
                        uniqueId = ID.NewID.ToString();
                    }

                    output.WriteStartElement("r");

                    output.WriteAttributeString("id", ResolveItemFromIdOrName(item.Database, rendering.Value <string>("renderingId"), rendering.Value <string>("renderingName")));
                    output.WriteAttributeString("uid", uniqueId);
                    output.WriteAttributeString("ds", rendering.Value <string>("datasource"));
                    output.WriteAttributeString("ph", rendering.Value <string>("placeholder"));

                    output.WriteAttributeStringIf("cac", rendering.Value <bool>("cacheable"));
                    output.WriteAttributeStringIf("vbd", rendering.Value <bool>("varyByData"));
                    output.WriteAttributeStringIf("vbdev", rendering.Value <bool>("varyByDevice"));
                    output.WriteAttributeStringIf("vbl", rendering.Value <bool>("varyByLogin"));
                    output.WriteAttributeStringIf("vbp", rendering.Value <bool>("varyByParameters"));
                    output.WriteAttributeStringIf("vbqs", rendering.Value <bool>("varyByQueryString"));
                    output.WriteAttributeStringIf("vbu", rendering.Value <bool>("varyByUser"));

                    var parametersArray = rendering.Value <JArray>("parameters");
                    if (parametersArray != null && parametersArray.Count > 0)
                    {
                        var parameters = new UrlString();
                        foreach (var parm in parametersArray.OfType <JObject>())
                        {
                            var key   = parm.Value <string>("key");
                            var value = parm.Value <string>("value") ?? string.Empty;
                            parameters.Parameters[key] = value;
                        }

                        output.WriteAttributeString("par", parameters.ToString());
                    }

                    output.WriteEndElement();
                }

                output.WriteEndElement();
            }

            output.WriteEndElement();

            return(writer.ToString());
        }