private static DocumentTypeAttribute CreateDefaultDocumentTypeAttribute(Type typeDocType) { var retVal = new DocumentTypeAttribute(); retVal.Name = typeDocType.Name; retVal.IconUrl = DocumentTypeDefaultValues.IconUrl; retVal.Thumbnail = DocumentTypeDefaultValues.Thumbnail; return(retVal); }
internal static List <ITemplate> GetAllowedTemplates(DocumentTypeAttribute docTypeAttr, Type typeDocType) { var allowedTemplates = new List <ITemplate>(); // Use AllowedTemplates if given if (docTypeAttr.AllowedTemplates != null) { foreach (string templateName in docTypeAttr.AllowedTemplates) { ITemplate template = FileService.GetTemplate(templateName); if (template != null) { allowedTemplates.Add(template); } else { throw new Exception(string.Format("Template '{0}' does not exists. That template is set as allowed template for document type '{1}'", templateName, GetDocumentTypeAlias(typeDocType))); } } } else { if (Util.DefaultRenderingEngine == Umbraco.Core.RenderingEngine.WebForms) { // if AllowedTemplates is null, use all generic templates foreach (Type typeTemplate in TemplateManager.GetAllTemplates(typeDocType)) { ITemplate template = FileService.GetTemplate(typeTemplate.Name); if (template != null) { allowedTemplates.Add(template); } } } else if (Util.DefaultRenderingEngine == Umbraco.Core.RenderingEngine.Mvc) { // if AllowedTemplates is null, use all generic templates foreach (ITemplate template in FileService.GetTemplates()) { if (IsViewForDocumentType(typeDocType.Name, template.Content)) { allowedTemplates.Add(template); } } } } return(allowedTemplates); }
internal static ITemplate GetDefaultTemplate(DocumentTypeAttribute docTypeAttr, Type typeDocType, List <ITemplate> allowedTemplates) { if (!String.IsNullOrEmpty(docTypeAttr.DefaultTemplateAsString)) { ITemplate defaultTemplate = allowedTemplates.FirstOrDefault(t => t.Alias == docTypeAttr.DefaultTemplateAsString); if (defaultTemplate == null) { throw new Exception(string.Format("Document type '{0}' has a default template '{1}' but that template does not use this document type", GetDocumentTypeAlias(typeDocType), docTypeAttr.DefaultTemplateAsString)); } return(defaultTemplate); } return(null); }
/// <summary> /// Create list of parents that has allowable children defined via "AllowedChildNodeTypesOf" property /// </summary> /// <param name="typeDocType"></param> /// <param name="parents"></param> private void GetAllowableParents(Type typeDocType, Dictionary <int, List <ContentTypeSort> > parents) { DocumentTypeAttribute docTypeAttr = Util.GetAttribute <DocumentTypeAttribute>(typeDocType); if (docTypeAttr != null) { if (docTypeAttr.AllowedChildNodeTypeOf != null && docTypeAttr.AllowedChildNodeTypeOf.Length > 0) { List <ContentTypeSort> tmpChilds = null; string childDocumentTypeAlias = GetDocumentTypeAlias(typeDocType); int childDocumentTypeId = GetDocumentTypeId(childDocumentTypeAlias); if (childDocumentTypeId != -1) { // enumerates through each one of the parent node type foreach (Type parent in docTypeAttr.AllowedChildNodeTypeOf) { string parentDocumentTypeAlias = GetDocumentTypeAlias(parent); int parentDocumentTypeId = GetDocumentTypeId(parentDocumentTypeAlias); if (parents.TryGetValue(parentDocumentTypeId, out tmpChilds)) { tmpChilds.Add(new ContentTypeSort { Id = new Lazy <int>(() => childDocumentTypeId), Alias = childDocumentTypeAlias }); parents[parentDocumentTypeId] = tmpChilds; } else { tmpChilds = new List <ContentTypeSort>(); tmpChilds.Add(new ContentTypeSort { Id = new Lazy <int>(() => childDocumentTypeId), Alias = childDocumentTypeAlias }); parents.Add(parentDocumentTypeId, tmpChilds); } } } } } }
private void SetAllowedTemplates(IContentType contentType, DocumentTypeAttribute docTypeAttr, Type typeDocType) { List <ITemplate> allowedTemplates = GetAllowedTemplates(docTypeAttr, typeDocType); try { if ((allowedTemplates.Count == 0) && (contentType.AllowedTemplates.Count() != 0)) { // Clear all allowed templates and default template foreach (var templateItem in contentType.AllowedTemplates) { contentType.RemoveTemplate(templateItem); } } else { contentType.AllowedTemplates = allowedTemplates.ToArray(); } } catch (SqlHelperException e) { throw new Exception(string.Format("Sql error setting templates for doc type '{0}' with templates '{1}'", GetDocumentTypeAlias(typeDocType), string.Join(", ", allowedTemplates))); } ITemplate defaultTemplate = GetDefaultTemplate(docTypeAttr, typeDocType, allowedTemplates); if (defaultTemplate != null) { contentType.SetDefaultTemplate(defaultTemplate); } else if (contentType.AllowedTemplates.Count() == 1) // if only one template is defined for this doc type -> make it default template for this doc type { contentType.SetDefaultTemplate(contentType.AllowedTemplates.First()); } }
public static string GetDocumentTypeAlias(Type typeDocType) { string alias; bool aliasUsed = false; DocumentTypeAttribute docTypeAttr = GetDocumentTypeAttribute(typeDocType); if (!String.IsNullOrEmpty(docTypeAttr.Alias)) { alias = docTypeAttr.Alias; aliasUsed = true; } else { alias = typeDocType.Name; } if (alias.ToLower() != alias.ToSafeAlias().ToLower()) { throw new ArgumentException(string.Format("The {0} '{1}', for the document type '{2}', is invalid.", (aliasUsed ? "alias" : "name"), alias, typeDocType.Name), "Alias"); } return(alias); }
private static DocumentTypeAttribute CreateDefaultDocumentTypeAttribute(Type typeDocType) { var retVal = new DocumentTypeAttribute(); retVal.Name = typeDocType.Name; retVal.IconUrl = DocumentTypeDefaultValues.IconUrl; retVal.Thumbnail = DocumentTypeDefaultValues.Thumbnail; return retVal; }
internal static ITemplate GetDefaultTemplate(DocumentTypeAttribute docTypeAttr, Type typeDocType, List<ITemplate> allowedTemplates) { if (!String.IsNullOrEmpty(docTypeAttr.DefaultTemplateAsString)) { ITemplate defaultTemplate = allowedTemplates.FirstOrDefault(t => t.Alias == docTypeAttr.DefaultTemplateAsString); if (defaultTemplate == null) { throw new Exception(string.Format("Document type '{0}' has a default template '{1}' but that template does not use this document type", GetDocumentTypeAlias(typeDocType), docTypeAttr.DefaultTemplateAsString)); } return defaultTemplate; } return null; }
internal static List<ITemplate> GetAllowedTemplates(DocumentTypeAttribute docTypeAttr, Type typeDocType) { var allowedTemplates = new List<ITemplate>(); // Use AllowedTemplates if given if (docTypeAttr.AllowedTemplates != null) { foreach (string templateName in docTypeAttr.AllowedTemplates) { ITemplate template = FileService.GetTemplate(templateName); if (template != null) { allowedTemplates.Add(template); } else { throw new Exception(string.Format("Template '{0}' does not exists. That template is set as allowed template for document type '{1}'", templateName, GetDocumentTypeAlias(typeDocType))); } } } else { if (Util.DefaultRenderingEngine == Umbraco.Core.RenderingEngine.WebForms) { // if AllowedTemplates is null, use all generic templates foreach (Type typeTemplate in TemplateManager.GetAllTemplates(typeDocType)) { ITemplate template = FileService.GetTemplate(typeTemplate.Name); if (template != null) { allowedTemplates.Add(template); } } } else if (Util.DefaultRenderingEngine == Umbraco.Core.RenderingEngine.Mvc) { // if AllowedTemplates is null, use all generic templates foreach (ITemplate template in FileService.GetTemplates()) { if (IsViewForDocumentType(typeDocType.Name, template.Content)) { allowedTemplates.Add(template); break; } } } } return allowedTemplates; }
//#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; } } // 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++; } }
protected void SynchronizeContentTypeProperties(Type typeContentType, IContentType contentType, DocumentTypeAttribute documentTypeAttribute, out bool hadDefaultValues) { SynchronizeContentTypeProperties(typeContentType, contentType, documentTypeAttribute, out hadDefaultValues, false); }
private void SynchronizeDocumentType(Type typeDocType, Type baseTypeDocType) { // Get DocumentTypeAttribute attribute for typeDocType DocumentTypeAttribute docTypeAttr = GetDocumentTypeAttribute(typeDocType); string docTypeName = string.IsNullOrEmpty(docTypeAttr.Name) ? typeDocType.Name : docTypeAttr.Name; string docTypeAlias = string.Empty; if (!String.IsNullOrEmpty(docTypeAttr.Alias)) { docTypeAlias = docTypeAttr.Alias; } else { docTypeAlias = typeDocType.Name; } DocumentTypes.Add(docTypeAlias, typeDocType); // If document type is not changed, skip update if (DocumentTypesComparisonSummary.Exists(dt => (dt.DocumentTypeStatus == Status.Same) && (dt.Alias == docTypeAlias))) { return; } try { AddToSynchronized(typeDocType.Name, docTypeAlias, typeDocType); } catch (ArgumentException exc) { throw new Exception(string.Format("Document type with alias '{0}' already exists! Please use unique class names as class name is used as alias. Document type causing the problem: '{1}' (assembly: '{2}'). Error message: {3}", docTypeAlias, typeDocType.FullName, typeDocType.Assembly.FullName, exc.Message)); } // If parent is some other DT, retrieve parentID; otherwise it's -1 int parentId = -1; if (baseTypeDocType != typeof(DocumentTypeBase)) { parentId = GetDocumentTypeId(baseTypeDocType); } // Get DT with same alias from Umbraco if exists IContentType contentType = ContentTypeService.GetContentType(docTypeAlias); if (contentType == null) { // New DT if (parentId != -1) { IContentType parentType = ContentTypeService.GetContentType(parentId); contentType = new ContentType(parentType); } else { contentType = new ContentType(parentId); } } else { // Existing DT if (contentType.ParentId != parentId) { throw new Exception(string.Format("Document type inheritance for document type with '{0}' alias, cannot be updated.", docTypeAlias)); } } contentType.Name = docTypeName; contentType.Alias = docTypeAlias; contentType.Icon = docTypeAttr.IconUrl; contentType.Thumbnail = docTypeAttr.Thumbnail; contentType.Description = docTypeAttr.Description; contentType.AllowedAsRoot = docTypeAttr.AllowAtRoot; SetAllowedTemplates(contentType, docTypeAttr, typeDocType); SynchronizeDocumentTypeProperties(typeDocType, contentType, docTypeAttr); ContentTypeService.Save(contentType); // store node id for new document types in dictionary (to avoid unnecessary API calls) if (DocumentTypesId.ContainsKey(contentType.Alias) == false) { DocumentTypesId.Add(contentType.Alias, contentType.Id); } }
//#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 void SynchronizeDocumentTypeProperties(Type typeDocType, IContentType docType, DocumentTypeAttribute documentTypeAttribute) { SynchronizeContentTypeProperties(typeDocType, docType, documentTypeAttribute, out _hadDefaultValues); }
private void SetAllowedTemplates(IContentType contentType, DocumentTypeAttribute docTypeAttr, Type typeDocType) { List<ITemplate> allowedTemplates = GetAllowedTemplates(docTypeAttr, typeDocType); try { if ((allowedTemplates.Count == 0) && (contentType.AllowedTemplates.Count() != 0)) { // Clear all allowed templates and default template foreach (var templateItem in contentType.AllowedTemplates) { contentType.RemoveTemplate(templateItem); } } else { contentType.AllowedTemplates = allowedTemplates.ToArray(); } } catch (SqlHelperException e) { throw new Exception(string.Format("Sql error setting templates for doc type '{0}' with templates '{1}'", GetDocumentTypeAlias(typeDocType), string.Join(", ", allowedTemplates))); } ITemplate defaultTemplate = GetDefaultTemplate(docTypeAttr, typeDocType, allowedTemplates); if (defaultTemplate != null) { contentType.SetDefaultTemplate(defaultTemplate); } else if (contentType.AllowedTemplates.Count() == 1) // if only one template is defined for this doc type -> make it default template for this doc type { contentType.SetDefaultTemplate(contentType.AllowedTemplates.First()); } }
/// <summary> /// Updates list of allowable children /// </summary> /// <param name="typeDocType"></param> /// <param name="parents"></param> private void SynchronizeAllowedChildContentType(Type typeDocType, Dictionary <int, List <ContentTypeSort> > parents) { int tmpDocTypeId = GetDocumentTypeId(typeDocType); List <ContentTypeSort> additionalAllowableTypes; parents.TryGetValue(tmpDocTypeId, out additionalAllowableTypes); DocumentTypeAttribute docTypeAttr = Util.GetAttribute <DocumentTypeAttribute>(typeDocType); if (docTypeAttr != null) { IContentType contentType = ContentTypeService.GetContentType(GetDocumentTypeAlias(typeDocType)); List <ContentTypeSort> allowedTypeIds = new List <ContentTypeSort>(); // If "AllowedChildNodeTypes" property has allowable children if (docTypeAttr.AllowedChildNodeTypes != null && docTypeAttr.AllowedChildNodeTypes.Length > 0) { foreach (Type allowedType in docTypeAttr.AllowedChildNodeTypes) { string allowedContentTypeAlias = GetDocumentTypeAlias(allowedType); int allowedContentTypeId = GetDocumentTypeId(allowedContentTypeAlias); // Adds children defined in "AllowedChildNodeTypes" property if (!allowedTypeIds.Any(s => s.Id.Value == allowedContentTypeId)) { allowedTypeIds.Add(new ContentTypeSort { Alias = allowedContentTypeAlias, Id = new Lazy <int>(() => allowedContentTypeId) }); } // Add children defined via "AllowedChildNodeTypesOf" property (Retrieved from method "GetAllowableParents") if (additionalAllowableTypes != null) { foreach (ContentTypeSort item in additionalAllowableTypes) { int childId = item.Id.Value; if (!allowedTypeIds.Any(s => s.Id.Value == childId)) { allowedTypeIds.Add(new ContentTypeSort { Alias = item.Alias, Id = new Lazy <int>(() => childId) }); } } } } contentType.AllowedContentTypes = allowedTypeIds.ToArray(); ContentTypeService.Save(contentType); } // IF children are defined only via "AllowedChildNodeTypesOf" property (Retrieved from method "GetAllowableParents") if (additionalAllowableTypes != null && additionalAllowableTypes.Count > 0) { foreach (ContentTypeSort item in additionalAllowableTypes) { int childId = item.Id.Value; if (!allowedTypeIds.Any(s => s.Id.Value == childId)) { allowedTypeIds.Add(new ContentTypeSort { Alias = item.Alias, Id = new Lazy <int>(() => childId) }); } } } // Update only in case there are allowed type ID's or if the allowed type ID's are cleared for the document type. if (allowedTypeIds.Count > 0 || (contentType.AllowedContentTypes.Count() > 0 && allowedTypeIds.Count == 0)) { contentType.AllowedContentTypes = allowedTypeIds.ToArray(); ContentTypeService.Save(contentType); } } }
internal static List<ITemplate> GetAllowedTemplates(DocumentTypeAttribute docTypeAttr, Type typeDocType) { var allowedTemplates = new List<ITemplate>(); // Use AllowedTemplates if given if (docTypeAttr.AllowedTemplates != null) { foreach (string templateName in docTypeAttr.AllowedTemplates) { ITemplate template = FileService.GetTemplate(templateName); if (template != null) { allowedTemplates.Add(template); } else { throw new Exception(string.Format("Template '{0}' does not exists. That template is set as allowed template for document type '{1}'", templateName, GetDocumentTypeAlias(typeDocType))); } } } else { // if AllowedTemplates if null, use all generic templates foreach (Type typeTemplate in TemplateManager.GetAllTemplates(typeDocType)) { ITemplate template = FileService.GetTemplate(typeTemplate.Name); if (template != null) { allowedTemplates.Add(template); } } } return allowedTemplates; }