/// <summary> /// Return the Multi UrlPicker that has been selected. /// </summary> /// <param name="content"></param> /// <param name="alias"></param> /// <returns></returns> public static IEnumerable <UrlPicker> GetMultiUrlPicker(this IPublishedContent content, string alias) { var xml = content.GetPropertyValue <RawXElement>(alias); if (xml != null) { var xmlData = xml.Value; if (xmlData != null) { return ( from pickerXml in xmlData.Elements("url-picker") let urlPickerState = UrlPickerState.Deserialize(pickerXml.ToString()) where (urlPickerState.Mode == UrlPickerMode.Content && urlPickerState.NodeId.HasValue) || !string.IsNullOrEmpty(urlPickerState.Url) select new UrlPicker() { Url = urlPickerState.Mode == UrlPickerMode.Content ? Umbraco.TypedContent(urlPickerState.NodeId.Value).Url : urlPickerState.Url, Title = urlPickerState.Title, NewWindow = urlPickerState.NewWindow } ); } } return(Enumerable.Empty <UrlPicker>()); }
/// <summary> /// Handles the Load event of the m_DataEditor control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> void m_DataEditor_Load(object sender, EventArgs e) { if (!this.ContentEditor.Page.IsPostBack && !string.IsNullOrEmpty((string)this.Data.Value)) { this.ContentEditor.State = UrlPickerState.Deserialize((string)this.Data.Value); } }
/// <summary> /// Inits the specified current node id. /// </summary> /// <param name="CurrentNodeId">The current node id.</param> /// <param name="PropertyData">The property data.</param> /// <param name="instance">The instance.</param> /// <returns></returns> public bool Init(int CurrentNodeId, string PropertyData, out object instance) { if (!Settings.RazorModelBindingEnabled) { if (Helper.Xml.CouldItBeXml(PropertyData)) { #pragma warning disable 0618 instance = new DynamicXml(PropertyData); #pragma warning restore 0618 return(true); } instance = PropertyData; return(true); } UrlPickerState state = null; if (!string.IsNullOrEmpty(PropertyData)) { state = UrlPickerState.Deserialize(PropertyData); } instance = state; return(true); }
public string Import(XElement propertyTag) { var result = string.Empty; if (!string.IsNullOrWhiteSpace(propertyTag.Value)) { var ups = UrlPickerState.Deserialize(propertyTag.Value); if (ups.Mode == UrlPickerMode.Content && propertyTag.Attribute("nodeId") != null) { var content = Services.ContentService.GetById(new Guid(propertyTag.Attribute("nodeId").Value)); if (content != null) { ups.NodeId = content.Id; } } else if (ups.Mode == UrlPickerMode.Media && propertyTag.Attribute("nodeId") != null) { var media = Services.MediaService.GetById(new Guid(propertyTag.Attribute("nodeId").Value)); if (media != null) { ups.NodeId = media.Id; } } UrlPickerDataFormat format; if (Enum.TryParse(propertyTag.Attribute("format").Value, out format)) { result = ups.Serialize(format); } } return(result); }
/// <summary> /// Method for performing special actions while creating the <see cref="IDataType">datatype</see> editor. /// </summary> /// <param name="dataType">The <see cref="IDataType">datatype</see> instance.</param> /// <param name="eventArgs">The <see cref="DataTypeLoadEventArgs"/> instance containing the event data.</param> /// <remarks>Called when the grid creates the editor controls for the specified <see cref="IDataType">datatype</see>.</remarks> public override void Configure(UrlPickerDataType dataType, DataTypeLoadEventArgs eventArgs) { // Deserialize stored value if (dataType.Data.Value != null && !string.IsNullOrEmpty(dataType.Data.Value.ToString()) && dataType.ContentEditor.State == null) { dataType.ContentEditor.State = UrlPickerState.Deserialize((string)dataType.Data.Value) ?? new UrlPickerState(); } }
public object ConvertValueWhenRead(object inputValue) { if (inputValue is string) { return(UrlPickerState.Deserialize(inputValue.ToString())); } return(inputValue); }
/// <summary> /// Attempts to convert the value specified into a useable value on the front-end /// </summary> /// <param name="value"></param> /// <returns></returns> public Attempt <object> ConvertPropertyValue(object value) { if (value != null && value.ToString().Length > 0) { return(new Attempt <object>(true, UrlPickerState.Deserialize(value.ToString()))); } return(Attempt <object> .False); }
public void Export(Property property, XElement propertyTag, Dictionary <int, ObjectTypes> dependantNodes) { if (property.Value != null && !string.IsNullOrWhiteSpace(property.Value.ToString())) { var ups = UrlPickerState.Deserialize(property.Value.ToString()); if (ups != null) { // find the data type definition var dtd = Services.DataTypeService.GetAllDataTypeDefinitions() .FirstOrDefault(x => x.Name == propertyTag.Attribute("dataTypeName").Value); // store how data is saved if (dtd != null) { var values = PreValues.GetPreValues(dtd.Id); if (values.Count >= 1) { var pv = values[1] as PreValue; propertyTag.Add(new XAttribute("format", pv.Value)); } } propertyTag.Add(new XAttribute("mode", ups.Mode.ToString())); propertyTag.Value = property.Value.ToString(); // find the id and convert it to guid if (ups.NodeId != null && ups.NodeId.Value > 0) { if (ups.Mode == UrlPickerMode.Content) { var content = Services.ContentService.GetById(ups.NodeId.Value); if (content != null) { propertyTag.Add(new XAttribute("nodeId", content.Key.ToString())); } } else if (ups.Mode == UrlPickerMode.Media) { var media = Services.MediaService.GetById(ups.NodeId.Value); if (media != null) { propertyTag.Add(new XAttribute("nodeId", media.Key.ToString())); } } } } } }
/// <summary> /// Return the UrlPicker that has been selected. /// </summary> /// <param name="content"></param> /// <param name="alias"></param> /// <returns></returns> public static UrlPicker GetUrlPicker(this IPublishedContent content, string alias) { var urlPickerState = UrlPickerState.Deserialize(content.GetPropertyValue <string>(alias)); if ((urlPickerState.Mode == UrlPickerMode.Content && !urlPickerState.NodeId.HasValue) || string.IsNullOrEmpty(urlPickerState.Url)) { return(new UrlPicker()); } return(new UrlPicker() { Url = urlPickerState.Mode == UrlPickerMode.Content ? Umbraco.NiceUrl(urlPickerState.NodeId.Value) : urlPickerState.Url, Title = urlPickerState.Title, NewWindow = urlPickerState.NewWindow }); }
/// <summary> /// Returns a MultiUrlPickerState based on a serialized string. /// /// Tries to infer the format of the serialized data based on known types. Will throw exceptions /// if it fails to parse. /// </summary> /// <param name="serializedState">An instance of MultiUrlPickerState as a serialized string</param> /// <returns>The state</returns> public static MultiUrlPickerState Deserialize(string serializedState) { // Can't deserialize an empty whatever if (string.IsNullOrEmpty(serializedState)) { return(null); } // Default var state = new MultiUrlPickerState(); var items = new List <UrlPickerState>(); // Imply data format from the formatting of the serialized state UrlPickerDataFormat impliedDataFormat; if (serializedState.StartsWith("<")) { impliedDataFormat = UrlPickerDataFormat.Xml; } else if (serializedState.StartsWith("{")) { impliedDataFormat = UrlPickerDataFormat.Json; } else { impliedDataFormat = UrlPickerDataFormat.Csv; } // Try to deserialize the string try { switch (impliedDataFormat) { case UrlPickerDataFormat.Xml: // Get each url-picker node var dataNode = XElement.Parse(serializedState); var xmlItems = dataNode.Elements("url-picker"); foreach (var xmlItem in xmlItems) { // Deserialize it items.Add(UrlPickerState.Deserialize(xmlItem.ToString())); } state.Items = items; break; case UrlPickerDataFormat.Csv: // Split CSV by lines var csvItems = serializedState.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); // Deserialize each line foreach (var csvItem in csvItems) { items.Add(UrlPickerState.Deserialize(csvItem)); } state.Items = items; break; case UrlPickerDataFormat.Json: var jss = new JavaScriptSerializer(); state = jss.Deserialize <MultiUrlPickerState>(serializedState); break; default: throw new NotImplementedException(); } } catch (Exception) { // Could not be deserialised, return null state = null; } return(state); }
/// <summary> /// Method for customizing the way the <see cref="IDataType">datatype</see> value is displayed in the grid. /// </summary> /// <remarks>Called when the grid displays the cell value for the specified <see cref="IDataType">datatype</see>.</remarks> /// <param name="dataType">The <see cref="IDataType">datatype</see> instance.</param> /// <returns>The display value.</returns> public override string GetDisplayValue(UrlPickerDataType dataType) { UrlPickerState state = null; var displayValue = string.Empty; // Deserialize stored value if (dataType.Data.Value != null && !string.IsNullOrEmpty(dataType.Data.Value.ToString()) && dataType.ContentEditor.State == null) { try { state = UrlPickerState.Deserialize((string)dataType.Data.Value); } catch (Exception ex) { Helper.Log.Error <DataType>(string.Format("DTG: Could not parse '{0}' as URL Picker state", dataType.Data.Value), ex); } } // Generate display value if (state != null) { if (state.Mode == UrlPickerMode.Content && state.NodeId != null) { var node = uQuery.GetNode(state.NodeId.Value); displayValue = string.Format( "<span>{0}: </span><span>{1}</span><br/><span>{2}: </span><a href='{3}' title='{4}'>{5}</a>", Helper.Dictionary.GetDictionaryItem("OpenInNewWindow", "Open in new window"), state.NewWindow, Helper.Dictionary.GetDictionaryItem("Node", "Node"), node.NiceUrl, Helper.Dictionary.GetDictionaryItem("OpenContent", "Open content"), node.Name); if (!string.IsNullOrEmpty(state.Title)) { displayValue += string.Format( "<br/><span>{0}: </span><span>{1}</span>", Helper.Dictionary.GetDictionaryItem("Title", "Title"), state.Title); } } else if (state.Mode == UrlPickerMode.URL) { displayValue = string.Format( "<span>{0}: </span><span>{1}</span><br/><span>{2}: </span><a href='{3}' title='{4}' target='_blank'>{5}</a>", Helper.Dictionary.GetDictionaryItem("OpenInNewWindow", "Open in new window"), state.NewWindow, Helper.Dictionary.GetDictionaryItem("URL", "URL"), state.Url, Helper.Dictionary.GetDictionaryItem("OpenUrl", "Open URL"), state.Url); if (!string.IsNullOrEmpty(state.Title)) { displayValue = string.Format( "<br/><span>{0}: </span><span>{1}</span>", Helper.Dictionary.GetDictionaryItem("Title", "Title"), state.Title); } } else if (state.Mode == UrlPickerMode.Media && state.NodeId != null) { var media = uQuery.GetMedia(state.NodeId.Value); displayValue = string.Format( "<span>{0}: </span><span>{1}</span><br/><span>{2}: </span><a href='{3}' title='{4}'>{5}</a>", Helper.Dictionary.GetDictionaryItem("OpenInNewWindow", "Open in new window"), state.NewWindow, Helper.Dictionary.GetDictionaryItem("Node", "Node"), media.getProperty("umbracoFile"), Helper.Dictionary.GetDictionaryItem("OpenMedia", "Open media"), media.Text); if (!string.IsNullOrEmpty(state.Title)) { displayValue += string.Format( "<br/><span>{0}: </span><span>{1}</span>", Helper.Dictionary.GetDictionaryItem("Title", "Title"), state.Title); } } } return(displayValue); }