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
///
[Obsolete("IDataType and all other references to the legacy property editors are no longer used this will be removed from the codebase in future versions")]
public class XPathDropDownListDataEditor : CompositeControl, IDataEditor
{
///
/// Field for the data.
///
private IData data;
///
/// Field for the options.
///
private XPathDropDownListOptions options;
///
/// Field for the CustomValidator.
///
private CustomValidator customValidator = new CustomValidator();
///
/// Field for the DropDownList.
///
private DropDownList 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.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()
{
switch (this.options.UmbracoObjectType)
{
case uQuery.UmbracoObjectType.Unknown:
case uQuery.UmbracoObjectType.Document:
this.dropDownList.DataSource = uQuery.GetNodesByXPath(this.options.XPath).Where(x => x.Id != -1).ToNameIds();
break;
case uQuery.UmbracoObjectType.Media:
this.dropDownList.DataSource = uQuery.GetMediaByXPath(this.options.XPath).Where(x => x.Id != -1).ToNameIds();
break;
case uQuery.UmbracoObjectType.Member:
this.dropDownList.DataSource = uQuery.GetMembersByXPath(this.options.XPath).ToNameIds();
break;
}
this.dropDownList.DataTextField = "Value";
this.dropDownList.DataValueField = this.options.UseId ? "Key" : "Value";
this.dropDownList.DataBind();
// Add a default please select value
this.dropDownList.Items.Insert(0, new ListItem(string.Concat(ui.Text("choose"), "..."), "-1"));
this.Controls.Add(this.customValidator);
this.Controls.Add(this.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 && this.data.Value != null)
{
// Get selected items from Node Name or Node Id
var dropDownListItem = this.dropDownList.Items.FindByValue(this.data.Value.ToString());
if (dropDownListItem != null)
{
dropDownListItem.Selected = true;
}
}
}
///
/// Called by Umbraco when saving the node
///
public void Save()
{
Property property = new Property(((umbraco.cms.businesslogic.datatype.DefaultData)this.data).PropertyId);
if (property.PropertyType.Mandatory && this.dropDownList.SelectedValue == "-1")
{
// Property is mandatory, but no value selected in the DropDownList
this.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.customValidator.ErrorMessage = ui.Text("errorHandling", "errorMandatory", new string[] { property.PropertyType.Alias, tab.Caption }, User.GetCurrent());
}
}
this.data.Value = this.dropDownList.SelectedValue;
}
}
}