/// <summary> /// Fix for incompatible Umbraco versions /// </summary> /// <param name="contentType">Type of the content.</param> /// <param name="dt">The dt.</param> /// <param name="alias">The alias.</param> /// <param name="name">The name.</param> /// <param name="tabName">Name of the tab.</param> public static void AddPropertyType(IContentTypeBase contentType, DataTypeDefinition dt, string alias, string name, string tabName) { IDataTypeDefinition idt = DataTypeService.GetDataTypeDefinitionById(dt.Id); Umbraco.Core.Models.PropertyType pt = new Umbraco.Core.Models.PropertyType(idt) { Alias = alias, Name = name }; if (String.IsNullOrEmpty(tabName)) { contentType.AddPropertyType(pt); } else { contentType.AddPropertyType(pt, tabName); } }
private void SavePropertyType(SaveClickEventArgs e, IContentTypeComposition contentTypeItem) { this.CreateChildControls(); //The GenericPropertyWrapper control, which contains the details for the PropertyType being added GenericProperty gpData = gp.GenricPropertyControl; if (string.IsNullOrEmpty(gpData.Name.Trim()) == false && string.IsNullOrEmpty(gpData.Alias.Trim()) == false) { var propertyTypeAlias = Casing.SafeAliasWithForcingCheck(gpData.Alias.Trim()); if (contentTypeItem.PropertyTypeExists(propertyTypeAlias) == false) { //Find the DataTypeDefinition that the PropertyType should be based on var dataTypeDefinition = ApplicationContext.Current.Services.DataTypeService.GetDataTypeDefinitionById(gpData.Type); var propertyType = new PropertyType(dataTypeDefinition) { Alias = propertyTypeAlias, Name = gpData.Name.Trim(), Mandatory = gpData.Mandatory, ValidationRegExp = gpData.Validation, Description = gpData.Description }; //gpData.Tab == 0 Generic Properties / No Group if (gpData.Tab == 0) { contentTypeItem.AddPropertyType(propertyType); } else { //Find the PropertyGroup by its Id and then set the PropertyType on that group var exists = contentTypeItem.CompositionPropertyGroups.Any(x => x.Id == gpData.Tab); if (exists) { var propertyGroup = contentTypeItem.CompositionPropertyGroups.First(x => x.Id == gpData.Tab); contentTypeItem.AddPropertyType(propertyType, propertyGroup.Name); } else { var tab = gpData.Tabs.FirstOrDefault(x => x.Id == gpData.Tab); if (tab != null) { var caption = tab.GetRawCaption(); contentTypeItem.AddPropertyType(propertyType, caption); } } } gpData.Clear(); } else { e.Message = ui.Text("contentTypeDublicatePropertyType"); e.IconType = BasePage.speechBubbleIcon.warning; } } }
//#region [Document type properties synchronization] /// <summary> /// Synchronizes content type properties /// </summary> /// <param name="typeContentType">ContentType type</param> /// <param name="contentType">Umbraco content type</param> /// <param name="hadDefaultValues">set to true if some of properties has default values</param> protected void SynchronizeContentTypeProperties(Type typeContentType, IContentType contentType, DocumentTypeAttribute documentTypeAttribute, out bool hadDefaultValues, bool updateMixins) { // sync the mixins first so that any properties are overwritten by the specific properties on the class if ((documentTypeAttribute.Mixins != null) && (updateMixins == false)) { foreach (Type mixinType in documentTypeAttribute.Mixins) { SynchronizeContentTypeProperties(mixinType, contentType, documentTypeAttribute, out hadDefaultValues, true); } } hadDefaultValues = false; int propertySortOrder = 0; foreach (PropertyInfo propInfo in typeContentType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { DocumentTypePropertyAttribute propAttr = Util.GetAttribute <DocumentTypePropertyAttribute>(propInfo); if (propAttr == null) { continue; // skip this property - not part of a document type } // Getting name and alias string propertyName; string propertyAlias; DocumentTypeManager.ReadPropertyNameAndAlias(propInfo, propAttr, out propertyName, out propertyAlias); // Remove property if it has Obsolete attribute if (this.RemoveIfObsolete(contentType, propInfo, propertyAlias)) { continue; } if (propAttr.DefaultValue != null) { hadDefaultValues = true; // at least one property has a default value } DataTypeDefinition dataTypeDefinition = GetDataTypeDefinition(typeContentType, propAttr, propInfo); // getting property if already exists, or creating new if it not exists Umbraco.Core.Models.PropertyType propertyType = contentType.PropertyTypes.FirstOrDefault(p => p.Alias == propertyAlias); if (propertyType == null) // if not exists, create it { Util.AddPropertyType(contentType, dataTypeDefinition, propertyAlias, propertyName, propAttr.TabAsString); propertyType = contentType.PropertyTypes.FirstOrDefault(p => p.Alias == propertyAlias); } else { if (propertyType.DataTypeDefinitionId != dataTypeDefinition.Id) { propertyType.DataTypeDefinitionId = dataTypeDefinition.Id; } } // If propertyType is still null, skip it. It means it cannot be added and is probably present in a parent content type. // Reason for inability to create the propertyType must be resolved manually. if (propertyType != null) { // Setting up the tab of this property. If tab doesn't exists, create it. if (!string.IsNullOrEmpty(propAttr.TabAsString) && propAttr.TabAsString.ToLower() != DocumentTypeDefaultValues.TabGenericProperties.ToLower()) { // try to find this tab PropertyGroup pg = contentType.PropertyGroups.FirstOrDefault(x => x.Name == propAttr.TabAsString); if (pg == null) // if found { contentType.AddPropertyGroup(propAttr.TabAsString); pg = contentType.PropertyGroups.FirstOrDefault(x => x.Name == propAttr.TabAsString); } if (propAttr.TabOrder.HasValue) { pg.SortOrder = propAttr.TabOrder.Value; } if (!pg.PropertyTypes.Any(x => x.Alias == propertyType.Alias)) { contentType.MovePropertyType(propertyType.Alias, propAttr.TabAsString); } } else if ((propAttr.TabAsString == string.Empty) || (propAttr.TabAsString.ToLower() == "generic properties")) { // In case when some property exists and needs to be moved to "Generic Properties" tab contentType.MovePropertyType(propertyType.Alias, null); } propertyType.Name = propertyName; propertyType.Mandatory = propAttr.Mandatory; propertyType.ValidationRegExp = propAttr.ValidationRegExp; propertyType.Description = propAttr.Description; propertyType.SortOrder = propertySortOrder; propertySortOrder++; } } }
private static bool CompareContentTypeProperties(Type uSiteBuilderDocType, IContentType contentType) { // Retrieve tab names and corresponding properties Dictionary <string, string> tabsWithProperties = new Dictionary <string, string>(); string tmpTabName; foreach (var tabItem in contentType.PropertyGroups) { tmpTabName = tabItem.Name; foreach (var propertyItem in tabItem.PropertyTypes) { tabsWithProperties.Add(propertyItem.Alias, tmpTabName); } } foreach (PropertyInfo propInfo in uSiteBuilderDocType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { DocumentTypePropertyAttribute propAttr = Util.GetAttribute <DocumentTypePropertyAttribute>(propInfo); if (propAttr == null) { continue; // skip this property - not part of a document type } if (propAttr.DefaultValue != null) { _hasDefaultValues = true; } // getting name and alias string propertyName; string propertyAlias; DocumentTypeManager.ReadPropertyNameAndAlias(propInfo, propAttr, out propertyName, out propertyAlias); if (Util.GetAttribute <ObsoleteAttribute>(propInfo) != null) { // If it's marked as obsolete, but still exists, there's a change if (contentType.PropertyTypes.Any(p => p.Alias == propertyAlias)) { return(false); } // marked as obsolete and already deleted, skip rest of checks continue; } var dataTypeDefinition = ManagerBase.GetDataTypeDefinition(uSiteBuilderDocType, propAttr, propInfo); Umbraco.Core.Models.PropertyType property = contentType.PropertyTypes.FirstOrDefault(p => p.Alias == propertyAlias); if (property == null) { // new property return(false); } if (property.DataTypeDefinitionId != dataTypeDefinition.Id) { return(false); } string tabName = string.Empty; tabsWithProperties.TryGetValue(property.Alias, out tabName); if (tabName == null) { // For generics properties tab tabName = string.Empty; } if (propAttr.TabAsString != tabName) { return(false); } if (property.Name != propertyName) { return(false); } if (property.Mandatory != propAttr.Mandatory) { return(false); } if (property.ValidationRegExp != propAttr.ValidationRegExp) { return(false); } if (property.Description != propAttr.Description) { return(false); } } return(true); }