using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.datatype;
using umbraco.cms.businesslogic.property;
using umbraco.cms.businesslogic.web;
using umbraco.interfaces;
namespace umbraco.editorControls.XPathDropDownList
{
///
/// XPath configurabale DropDownList Data Type
///
public class XPathDropDownListDataEditor : CompositeControl, IDataEditor
{
///
/// Field for the data.
///
private IData m_Data;
///
/// Field for the options.
///
private XPathDropDownListOptions m_Options;
///
/// Field for the CustomValidator.
///
private CustomValidator m_CustomValidator = new CustomValidator();
///
/// Field for the DropDownList.
///
private DropDownList m_DropDownList = new DropDownList();
///
/// 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 XPathDropDownListDataEditor(IData data, XPathDropDownListOptions options)
{
this.m_Data = data;
this.m_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.m_DropDownList.DataSource = uQuery.GetNodesByXPath(this.m_Options.XPath).ToNameIds();
this.m_DropDownList.DataTextField = "Value";
this.m_DropDownList.DataValueField = this.m_Options.UseId ? "Key" : "Value";
this.m_DropDownList.DataBind();
// Add a default please select value
this.m_DropDownList.Items.Insert(0, new ListItem(string.Empty, "-1"));
this.Controls.Add(this.m_CustomValidator);
this.Controls.Add(this.m_DropDownList);
}
///
/// 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)
{
// Get selected items from Node Name or Node Id
var dropDownListItem = this.m_DropDownList.Items.FindByValue(this.m_Data.Value.ToString());
if (dropDownListItem != null)
{
dropDownListItem.Selected = true;
}
}
}
///
/// Called by Umbraco when saving the node
///
public void Save()
{
Property property = new Property(((DefaultData)this.m_Data).PropertyId);
if (property.PropertyType.Mandatory && this.m_DropDownList.SelectedValue == "-1")
{
// Property is mandatory, but no value selected in the DropDownList
this.m_CustomValidator.IsValid = false;
DocumentType documentType = new DocumentType(property.PropertyType.ContentTypeId);
ContentType.TabI tab = documentType.getVirtualTabs.Where(x => x.Id == property.PropertyType.TabId).FirstOrDefault();
if (tab != null)
{
this.m_CustomValidator.ErrorMessage = ui.Text("errorHandling", "errorMandatory", new string[] { property.PropertyType.Alias, tab.Caption }, User.GetCurrent());
}
}
this.m_Data.Value = this.m_DropDownList.SelectedValue;
}
}
}