using System; using System.Collections.Generic; using System.Linq; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml.Linq; using umbraco.interfaces; namespace umbraco.editorControls.XPathCheckBoxList { /// /// Renders a CheckBoxList using with option nodes obtained by an XPath expression /// public class XPathCheckBoxListDataEditor : CompositeControl, IDataEditor { /// /// Field for the data. /// private IData data; /// /// Field for the options. /// private XPathCheckBoxListOptions options; /// /// Field for the checkbox list. /// private CheckBoxList checkBoxList = new CheckBoxList(); /// /// Gets a value indicating whether [treat as rich text editor]. /// /// /// true if [treat as rich text editor]; otherwise, false. /// public virtual bool TreatAsRichTextEditor { get { return false; } } /// /// Gets a value indicating whether [show label]. /// /// true if [show label]; otherwise, false. public virtual bool ShowLabel { get { return true; } } /// /// Gets the editor. /// /// The editor. public Control Editor { get { return this; } } /// /// Initializes a new instance of XPathCheckBoxListDataEditor /// /// /// internal XPathCheckBoxListDataEditor(IData data, XPathCheckBoxListOptions options) { this.data = data; this.options = options; } /// /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering. /// protected override void CreateChildControls() { this.checkBoxList.DataSource = uQuery.GetNodesByXPath(this.options.XPath).ToNameIds(); this.checkBoxList.DataTextField = "Value"; this.checkBoxList.DataValueField = this.options.UseIds ? "Key" : "Value"; this.checkBoxList.DataBind(); this.Controls.Add(this.checkBoxList); } /// /// Raises the event. /// /// The object that contains the event data. protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.EnsureChildControls(); if (!this.Page.IsPostBack && this.data.Value != null) { string value = this.data.Value.ToString(); List selectedValues = new List(); if (xmlHelper.CouldItBeXml(value)) { // build selected values from XML fragment foreach (XElement nodeXElement in XElement.Parse(value).Elements()) { selectedValues.Add(nodeXElement.Value); } } else { // Assume a CSV source selectedValues = value.Split(',').ToList(); } // Find checkboxes where values match the stored values and set to selected ListItem checkBoxListItem = null; foreach (string selectedValue in selectedValues) { checkBoxListItem = this.checkBoxList.Items.FindByValue(selectedValue); if (checkBoxListItem != null) { checkBoxListItem.Selected = true; } } } } /// /// Called by Umbraco when saving the node /// public void Save() { // Get all checked item values IEnumerable selectedOptions = from ListItem item in this.checkBoxList.Items where item.Selected select item.Value; if (this.options.UseXml) { string elementName = this.options.UseIds ? "nodeId" : "nodeName"; this.data.Value = new XElement("XPathCheckBoxList", selectedOptions.Select(x => new XElement(elementName, x.ToString()))).ToString(); } else { // Save the CSV this.data.Value = string.Join(",", selectedOptions.ToArray()); } } } }