This repository was archived by the owner on Feb 26, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathTextstringArrayData.cs
More file actions
102 lines (88 loc) · 2.9 KB
/
Copy pathTextstringArrayData.cs
File metadata and controls
102 lines (88 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml;
using uComponents.Core;
using umbraco.cms.businesslogic.datatype;
using Umbraco.Core;
namespace uComponents.DataTypes.TextstringArray
{
/// <summary>
/// Overrides the <see cref="umbraco.cms.businesslogic.datatype.DefaultData"/> object to return the value as XML.
/// </summary>
public class TextstringArrayData : DefaultData
{
/// <summary>
/// Field for the options.
/// </summary>
private TextstringArrayOptions options;
/// <summary>
/// Initializes a new instance of the <see cref="TextstringArrayData" /> class.
/// </summary>
/// <param name="dataType">Type of the data.</param>
/// <param name="options">The options.</param>
public TextstringArrayData(BaseDataType dataType, TextstringArrayOptions options)
: base(dataType)
{
this.options = options;
}
/// <summary>
/// Converts the data value to XML.
/// </summary>
/// <param name="data">The data to convert to XML.</param>
/// <returns>Returns the XML node for the data-type's value.</returns>
public override XmlNode ToXMl(XmlDocument data)
{
// check that the value isn't null
if (this.Value != null)
{
var value = this.Value.ToString();
var xd = new XmlDocument();
// if the value is coming from a translation task, it will always be XML
if (Helper.Xml.CouldItBeXml(value))
{
xd.LoadXml(value);
return data.ImportNode(xd.DocumentElement, true);
}
// load the values into a string array/list.
var deserializer = new JavaScriptSerializer();
var values = deserializer.Deserialize<List<string[]>>(value);
if (values != null && values.Count > 0)
{
// load the values into an XML document.
xd.LoadXml("<TextstringArray/>");
// load the config options
if (this.options.ShowColumnLabels && !string.IsNullOrWhiteSpace(this.options.ColumnLabels))
{
var xlabels = xd.CreateElement("labels");
// loop through the labels.
foreach (var label in this.options.ColumnLabels.Split(new[] { Environment.NewLine }, StringSplitOptions.None))
{
// add each label to the XML document.
var xlabel = XmlHelper.AddTextNode(xd, "label", label);
xlabels.AppendChild(xlabel);
}
xd.DocumentElement.AppendChild(xlabels);
}
// loop through the list/array items.
foreach (var row in values)
{
// add each row to the XML document.
var xrow = xd.CreateElement("values");
foreach (var item in row)
{
// add each value to the XML document.
var xvalue = XmlHelper.AddTextNode(xd, "value", item);
xrow.AppendChild(xvalue);
}
xd.DocumentElement.AppendChild(xrow);
}
// return the XML node.
return data.ImportNode(xd.DocumentElement, true);
}
}
// otherwise render the value as default (in CDATA)
return base.ToXMl(data);
}
}
}