/// <summary> /// Extension method for the UmbracoObejctType enum to return the enum friendly name /// </summary> /// <param name="umbracoObjectType">UmbracoObjectType value</param> /// <returns>a string of the FriendlyName</returns> public static string GetFriendlyName(this uQuery.UmbracoObjectType umbracoObjectType) { var fieldInfo = umbracoObjectType.GetType().GetField(umbracoObjectType.ToString()); var friendlyNames = (uQuery.FriendlyNameAttribute[])fieldInfo.GetCustomAttributes(typeof(uQuery.FriendlyNameAttribute), false); return((friendlyNames.Length > 0) ? friendlyNames[0].ToString() : string.Empty); }
/// <summary> /// Extension method for the UmbracoObjectType enum to return the enum GUID /// </summary> /// <param name="umbracoObjectType">UmbracoObjectType Enum value</param> /// <returns>a GUID value of the UmbracoObjectType</returns> public static Guid GetGuid(this uQuery.UmbracoObjectType umbracoObjectType) { var fieldInfo = umbracoObjectType.GetType().GetField(umbracoObjectType.ToString()); var guidAttributes = (uQuery.GuidAttribute[])fieldInfo.GetCustomAttributes(typeof(uQuery.GuidAttribute), false); return((guidAttributes.Length > 0) ? new Guid(guidAttributes[0].ToString()) : Guid.Empty); }
/// <summary> /// /// </summary> /// <param name="source">xPathCustomValidator</param> /// <param name="args"></param> private void XPathCustomValidator_ServerValidate(object source, ServerValidateEventArgs args) { string xpath = args.Value; bool isValid = false; uQuery.UmbracoObjectType umbracoObjectType = uQuery.GetUmbracoObjectType(new Guid(this.typeRadioButtonList.SelectedValue)); try { switch (umbracoObjectType) { case uQuery.UmbracoObjectType.Document: uQuery.GetNodesByXPath(xpath); break; case uQuery.UmbracoObjectType.Media: uQuery.GetMediaByXPath(xpath); break; case uQuery.UmbracoObjectType.Member: uQuery.GetMembersByXPath(xpath); break; } isValid = true; } catch { this.xPathCustomValidator.ErrorMessage = " Error in XPath expression"; } args.IsValid = isValid; }
/// <summary> /// Saves the pre value data to Umbraco /// </summary> public override void Save() { if (this.Page.IsValid) { this.Options.TypeToPick = this.typeToPickRadioButtonList.SelectedValue; this.Options.XPath = this.xPathTextBox.Text; this.SaveAsJson(this.Options); // Serialize to Umbraco database field } }
/// <summary> /// On Load event /// </summary> /// <param name="sender">this aspx page</param> /// <param name="e">EventArgs (expect empty)</param> protected void Page_Load(object sender, EventArgs e) { int id; if (int.TryParse(Request.QueryString["id"], out id)) { this.relationType = new RelationType(id); if (this.relationType != null) { // API doens't allow us to pull the object Type, so sql needed // this.parentObjectType = UmbracoHelper.GetUmbracoObjectType(uQuery.SqlHelper.ExecuteScalar<Guid>("SELECT parentObjectType FROM umbracoRelationType WHERE id = " + this.relationType.Id.ToString())); // this.childObjectType = UmbracoHelper.GetUmbracoObjectType(uQuery.SqlHelper.ExecuteScalar<Guid>("SELECT childObjectType FROM umbracoRelationType WHERE id = " + this.relationType.Id.ToString())); // uQuery has the above in a helper method, so no sql needed now this.parentObjectType = this.relationType.GetParentUmbracoObjectType(); this.childObjectType = this.relationType.GetChildUmbracoObjectType(); // ----------- if (!this.IsPostBack) { this.EnsureChildControls(); this.idLiteral.Text = this.relationType.Id.ToString(); this.nameTextBox.Text = this.relationType.Name; this.aliasTextBox.Text = this.relationType.Alias; if (this.relationType.Dual) { this.dualRadioButtonList.Items.FindByValue("1").Selected = true; } else { this.dualRadioButtonList.Items.FindByValue("0").Selected = true; } this.parentLiteral.Text = this.parentObjectType.GetFriendlyName(); // UmbracoHelper.GetFriendlyName(this.parentObjectType); this.childLiteral.Text = this.childObjectType.GetFriendlyName(); // UmbracoHelper.GetFriendlyName(this.childObjectType); this.relationsCountLiteral.Text = this.Relations.Count.ToString(); this.relationsRepeater.DataSource = this.Relations; this.relationsRepeater.DataBind(); } } else { throw new Exception("Unable to get RelationType where ID = " + id.ToString()); } } else { throw new Exception("Invalid RelationType ID"); } }
/// <summary> /// returns the UmbracoObjectType associated as defined by the supplied relation type, and if reverse indexing has been enabled /// </summary> /// <param name="relationType">associated RealationType</param> /// <returns></returns> public uQuery.UmbracoObjectType GetPickerUmbracoObjectType(RelationType relationType) { uQuery.UmbracoObjectType pickerUmbracoObjectType = uQuery.UmbracoObjectType.Unknown; if (!relationType.Dual && this.options.ReverseIndexing) { pickerUmbracoObjectType = relationType.GetParentUmbracoObjectType(); } else { pickerUmbracoObjectType = relationType.GetChildUmbracoObjectType(); } return(pickerUmbracoObjectType); }
/// <summary> /// temp replacement for built-in uQuery method (as that hits the database) /// </summary> /// <param name="id">the id of a content, media or member item</param> /// <returns>an enum instance of the UmbracoObjectType</returns> internal static uQuery.UmbracoObjectType GetUmbracoObjectType(int id) { // return variable uQuery.UmbracoObjectType umbracoObjectType = uQuery.UmbracoObjectType.Unknown; UmbracoHelper umbracoHelper = new UmbracoHelper(UmbracoContext.Current); // attempt to get content if (umbracoHelper.TypedContent(id) != null) { umbracoObjectType = uQuery.UmbracoObjectType.Document; } else { // attempt to get media if (umbracoHelper.TypedMedia(id) != null) { umbracoObjectType = uQuery.UmbracoObjectType.Media; } else { // attempt to get member try { if (umbracoHelper.TypedMember(id) != null) { umbracoObjectType = uQuery.UmbracoObjectType.Member; } } catch { // HACK: suppress Umbraco error } } } return(umbracoObjectType); }
/// <summary> /// Check to see if the content id side of the relation type is valid (doesn't check the node picker id side) /// </summary> /// <param name="contextUmbracoObjectType">Type of the content object (content/ media or member)</param> /// <param name="relationType">Type of the relation.</param> /// <returns> /// <c>true</c> if [is content object type valid] [the specified content object type]; otherwise, <c>false</c>. /// </returns> public bool IsContextUmbracoObjectTypeValid(uQuery.UmbracoObjectType contextUmbracoObjectType, RelationType relationType) { bool isContextObjectTypeValid = false; if (!relationType.Dual && this.options.ReverseIndexing) { // expects the current context to be the child in the relation if (contextUmbracoObjectType == relationType.GetChildUmbracoObjectType()) { isContextObjectTypeValid = true; } } else { // expects the current context to be the parent in the relation if (contextUmbracoObjectType == relationType.GetParentUmbracoObjectType()) { isContextObjectTypeValid = true; } } return(isContextObjectTypeValid); }
/// <summary> /// Event after all properties have been saved /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AfterSave(Content sender, SaveEventArgs e) { Guid pickerRelationsId = new Guid(DataTypeGuids.PickerRelationsId); // For each PickerRelations datatype foreach (Property pickerRelationsProperty in from property in sender.GenericProperties where property.PropertyType.DataTypeDefinition.DataType.Id == pickerRelationsId select property) { // used to identify this datatype instance - relations created are marked with this in the comment field string instanceIdentifier = "[\"PropertyTypeId\":" + pickerRelationsProperty.PropertyType.Id.ToString() + "]"; // get configuration options for datatype PickerRelationsOptions options = ((PickerRelationsPreValueEditor)pickerRelationsProperty.PropertyType.DataTypeDefinition.DataType.PrevalueEditor).Options; // find Picker source propertyAlias field on sender Property pickerProperty = sender.getProperty(options.PropertyAlias); if (pickerProperty != null) { // get relationType from options RelationType relationType = RelationType.GetById(options.RelationTypeId); if (relationType != null) { // validate: 1) check current type of sender matches that expected by the relationType, validation method is in the DataEditor uQuery.UmbracoObjectType contextObjectType = uQuery.UmbracoObjectType.Unknown; switch (sender.GetType().ToString()) { case "umbraco.cms.businesslogic.web.Document": contextObjectType = uQuery.UmbracoObjectType.Document; break; case "umbraco.cms.businesslogic.media.Media": contextObjectType = uQuery.UmbracoObjectType.Media; break; case "umbraco.cms.businesslogic.member.Member": contextObjectType = uQuery.UmbracoObjectType.Member; break; } if (((PickerRelationsDataEditor)pickerRelationsProperty.PropertyType.DataTypeDefinition.DataType.DataEditor) .IsContextUmbracoObjectTypeValid(contextObjectType, relationType)) { uQuery.UmbracoObjectType pickerUmbracoObjectType = uQuery.UmbracoObjectType.Unknown; // Get the object type expected by the associated relation type and if this datatype has been configures as a rever index pickerUmbracoObjectType = ((PickerRelationsDataEditor)pickerRelationsProperty.PropertyType.DataTypeDefinition.DataType.DataEditor) .GetPickerUmbracoObjectType(relationType); // clear all exisitng relations (or look to see previous verion of sender to delete changes ?) DeleteRelations(relationType, sender.Id, options.ReverseIndexing, instanceIdentifier); string pickerPropertyValue = pickerProperty.Value.ToString(); var pickerStorageFormat = PickerStorageFormat.Csv; // Assume default of csv if (xmlHelper.CouldItBeXml(pickerPropertyValue)) { pickerStorageFormat = PickerStorageFormat.Xml; } // Creating instances of Documents / Media / Members ensures the IDs are of a valid type - be quicker to check with GetUmbracoObjectType(int) Dictionary <int, string> pickerItems = null; switch (pickerUmbracoObjectType) { case uQuery.UmbracoObjectType.Document: switch (pickerStorageFormat) { case PickerStorageFormat.Csv: pickerItems = uQuery.GetDocumentsByCsv(pickerPropertyValue).ToNameIds(); break; case PickerStorageFormat.Xml: pickerItems = uQuery.GetDocumentsByXml(pickerPropertyValue).ToNameIds(); break; } break; case uQuery.UmbracoObjectType.Media: switch (pickerStorageFormat) { case PickerStorageFormat.Csv: pickerItems = uQuery.GetMediaByCsv(pickerPropertyValue).ToNameIds(); break; case PickerStorageFormat.Xml: pickerItems = uQuery.GetMediaByXml(pickerPropertyValue).ToNameIds(); break; } break; case uQuery.UmbracoObjectType.Member: switch (pickerStorageFormat) { case PickerStorageFormat.Csv: pickerItems = uQuery.GetMembersByCsv(pickerPropertyValue).ToNameIds(); break; case PickerStorageFormat.Xml: pickerItems = uQuery.GetMembersByXml(pickerPropertyValue).ToNameIds(); break; } break; } if (pickerItems != null) { foreach (KeyValuePair <int, string> pickerItem in pickerItems) { CreateRelation(relationType, sender.Id, pickerItem.Key, options.ReverseIndexing, instanceIdentifier); } } } else { // Error: content object type invalid with relation type } } else { // Error: relation type is null } } else { // Error: pickerProperty alias not found } } }
/// <summary> /// Extension method for the UmbracoObjectType enum to return the enum name /// </summary> /// <param name="umbracoObjectType">UmbracoObjectType value</param> /// <returns>The enum name of the UmbracoObjectType value</returns> public static string GetName(this uQuery.UmbracoObjectType umbracoObjectType) { return(Enum.GetName(typeof(uQuery.UmbracoObjectType), umbracoObjectType)); }