internal void DeserializeTabSortOrder(IContentTypeBase item, XElement node) { var tabNode = node.Element("Tabs"); foreach (var tab in tabNode.Elements("Tab")) { var name = tab.Element("Caption").Value; var sortOrder = tab.Element("SortOrder"); if (sortOrder != null) { if (!string.IsNullOrEmpty(sortOrder.Value)) { var itemTab = item.PropertyGroups.FirstOrDefault(x => x.Name == name); if (itemTab != null) { itemTab.SortOrder = int.Parse(sortOrder.Value); } else { LogHelper.Debug <Events>("Adding new Tab? {0}", () => name); // at this point we might have a missing tab. if (item.AddPropertyGroup(name)) { itemTab = item.PropertyGroups.FirstOrDefault(x => x.Name == name); if (itemTab != null) { itemTab.SortOrder = int.Parse(sortOrder.Value); } } } } } } // remove tabs List <string> tabsToRemove = new List <string>(); foreach (var tab in item.PropertyGroups) { if (tabNode.Elements("Tab").FirstOrDefault(x => x.Element("Caption").Value == tab.Name) == null) { // no tab of this name in the import... remove it. tabsToRemove.Add(tab.Name); } } foreach (var name in tabsToRemove) { item.PropertyGroups.Remove(name); } }
/// <summary> /// Maps properties /// </summary> /// <param name="ct"></param> /// <param name="itemType"></param> /// <param name="overwrite"></param> protected void MapProperties(IContentTypeBase ct, Type itemType) { foreach (PropertyInfo propInfo in itemType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { PropertyAttribute propAttr = Attribute.GetCustomAttribute(propInfo, typeof(PropertyAttribute), false) as PropertyAttribute; if (propAttr != null) { var newProp = propAttr.GetPropertyType(); if (ct.PropertyTypeExists(propAttr.Alias)) { var existingProp = ct.PropertyTypes.First(x => x.Alias == propAttr.Alias); existingProp.DataTypeDefinitionId = newProp.DataTypeDefinitionId; existingProp.Name = newProp.Name; existingProp.Description = newProp.Description; existingProp.Mandatory = newProp.Mandatory; existingProp.ValidationRegExp = newProp.ValidationRegExp; if (!String.IsNullOrEmpty(propAttr.TabName)) { ct.MovePropertyType(propAttr.Alias, propAttr.TabName); } } else { if (!String.IsNullOrEmpty(propAttr.TabName)) { ct.AddPropertyGroup(propAttr.TabName); ct.AddPropertyType(newProp, propAttr.TabName); } else { ct.AddPropertyType(newProp); } } } } }
/// <summary> /// Maps properties /// </summary> /// <param name="ct"></param> /// <param name="itemType"></param> /// <param name="overwrite"></param> protected void MapProperties(IContentTypeBase ct, Type itemType) { var propAttrs = ReflectionHelper.GetPropertiesWithAttribute <PropertyAttribute>(itemType) .Select(prop => prop.GetCustomAttribute <PropertyAttribute>(false)); foreach (var propAttr in propAttrs) { var newProp = propAttr.GetPropertyType(); if (ct.PropertyTypeExists(propAttr.Alias)) { var existingProp = ct.PropertyTypes.First(x => x.Alias == propAttr.Alias); existingProp.DataTypeDefinitionId = newProp.DataTypeDefinitionId; existingProp.Name = newProp.Name; existingProp.Description = newProp.Description; existingProp.Mandatory = newProp.Mandatory; existingProp.ValidationRegExp = newProp.ValidationRegExp; if (!string.IsNullOrEmpty(propAttr.TabName)) { ct.MovePropertyType(propAttr.Alias, propAttr.TabName); } } else { if (!string.IsNullOrEmpty(propAttr.TabName)) { ct.AddPropertyGroup(propAttr.TabName); ct.AddPropertyType(newProp, propAttr.TabName); } else { ct.AddPropertyType(newProp); } } } }
/// <summary> /// Scans for properties on the model which have the UmbracoTab attribute /// </summary> /// <param name="newContentType"></param> /// <param name="model"></param> /// <param name="dataTypeService"></param> private void CreateTabs(IContentTypeBase newContentType, List<TabRegistration> tabRegister, Type contentClrType) { var properties = contentClrType.GetProperties().Where(x => x.GetCodeFirstAttribute<ContentTabAttribute>() != null).ToArray(); int length = properties.Length; for (int i = 0; i < length; i++) { var tabAttribute = properties[i].GetCodeFirstAttribute<ContentTabAttribute>(); var reg = new TabRegistration(); var props = new List<PropertyRegistration>(); reg._properties = props; reg.ClrType = properties[i].PropertyType; reg.Name = tabAttribute.Name; reg.OriginalName = tabAttribute.OriginalName; reg.TabAttribute = tabAttribute; reg.PropertyOfParent = properties[i]; CodeFirstManager.Current.Log("Creating tab " + tabAttribute.Name + " on content type " + newContentType.Name, this); newContentType.AddPropertyGroup(tabAttribute.Name); newContentType.PropertyGroups.Where(x => x.Name == tabAttribute.Name).FirstOrDefault().SortOrder = tabAttribute.SortOrder; CreateProperties(properties[i], newContentType, reg, props, contentClrType); tabRegister.Add(reg); } }
/// <summary> /// Scan the properties on tabs /// </summary> /// <param name="propertyTab"></param> /// <param name="contentType"></param> /// <param name="tabName"></param> /// <returns></returns> private IEnumerable<string> VerifyAllPropertiesOnTab(PropertyInfo propertyTab, IContentTypeBase contentType, TabRegistration registration, Type documentType, ref bool modified) { Type type = propertyTab.PropertyType; var properties = type.GetProperties().Where(x => x.GetCustomAttribute<ContentPropertyAttribute>() != null); //Do NOT initialise attribute here, causes initialisation exception var props = new List<PropertyRegistration>(); registration._properties = props; registration.ClrType = type; if (properties.Count() > 0) { if (!contentType.PropertyGroups.Any(x => x.Name == registration.Name)) { contentType.AddPropertyGroup(registration.Name); } List<string> propertyAliases = new List<string>(); foreach (var item in properties) { CodeFirstManager.Current.Log("Syncing property " + item.Name + " on tab " + registration.Name + " of content type " + contentType.Name, this); var propReg = _propertyModule.VerifyExistingProperty(contentType, registration, item, documentType, ref modified); propertyAliases.Add(propReg.Alias); props.Add(propReg); } return propertyAliases; } return new string[0]; }
/// <summary> /// Loop through all properties and remove existing ones if necessary /// </summary> /// <param name="contentType"></param> /// <param name="type"></param> /// <param name="dataTypeService"></param> private static void VerifyProperties(IContentTypeBase contentType, Type type, IDataTypeService dataTypeService) { var properties = type.GetProperties().Where(x => x.GetCustomAttribute<UmbracoTabAttribute>() != null).ToArray(); List<string> propertiesThatShouldExist = new List<string>(); foreach (var propertyTab in properties) { var tabAttribute = propertyTab.GetCustomAttribute<UmbracoTabAttribute>(); if (!contentType.PropertyGroups.Any(x => x.Name == tabAttribute.Name)) { contentType.AddPropertyGroup(tabAttribute.Name); } propertiesThatShouldExist.AddRange(VerifyAllPropertiesOnTab(propertyTab, contentType, tabAttribute.Name, dataTypeService)); } var propertiesOfRoot = type.GetProperties().Where(x => x.GetCustomAttribute<UmbracoPropertyAttribute>() != null); foreach (var item in propertiesOfRoot) { //TODO: check for correct name propertiesThatShouldExist.Add(VerifyExistingProperty(contentType, null, dataTypeService, item, true)); } //loop through all the properties on the ContentType to see if they should be removed; var existingUmbracoProperties = contentType.PropertyTypes.ToArray(); int length = contentType.PropertyTypes.Count(); for (int i = 0; i < length; i++) { if (!propertiesThatShouldExist.Contains(existingUmbracoProperties[i].Alias)) { //remove the property contentType.RemovePropertyType(existingUmbracoProperties[i].Alias); } } }
/// <summary> /// Scans for properties on the model which have the UmbracoTab attribute /// </summary> /// <param name="newContentType"></param> /// <param name="model"></param> /// <param name="dataTypeService"></param> private static void CreateTabs(IContentTypeBase newContentType, Type model, IDataTypeService dataTypeService) { var properties = model.GetProperties().Where(x => x.DeclaringType == model && x.GetCustomAttribute<UmbracoTabAttribute>() != null).ToArray(); int length = properties.Length; for (int i = 0; i < length; i++) { var tabAttribute = properties[i].GetCustomAttribute<UmbracoTabAttribute>(); newContentType.AddPropertyGroup(tabAttribute.Name); newContentType.PropertyGroups.Where(x => x.Name == tabAttribute.Name).FirstOrDefault().SortOrder = tabAttribute.SortOrder; CreateProperties(properties[i], newContentType, tabAttribute.Name, dataTypeService); } }