コード例 #1
0
        /// <summary>
        /// Update the existing content Type based on the data in the attributes
        /// </summary>
        /// <param name="contentTypeService"></param>
        /// <param name="fileService"></param>
        /// <param name="attribute"></param>
        /// <param name="contentType"></param>
        /// <param name="type"></param>
        /// <param name="dataTypeService"></param>
        private static void UpdateContentType(IContentTypeService contentTypeService, IFileService fileService, UmbracoContentTypeAttribute attribute, IContentType contentType, Type type, IDataTypeService dataTypeService)
        {
            contentType.Name                = attribute.ContentTypeName;
            contentType.Alias               = attribute.ContentTypeAlias;
            contentType.Icon                = attribute.Icon;
            contentType.IsContainer         = attribute.EnableListView;
            contentType.AllowedContentTypes = FetchAllowedContentTypes(attribute.AllowedChildren, contentTypeService);
            contentType.AllowedAsRoot       = attribute.AllowedAtRoot;

            Type parentType = type.BaseType;

            if (parentType != null && parentType != typeof(UmbracoGeneratedBase) && parentType.GetBaseTypes(false).Any(x => x == typeof(UmbracoGeneratedBase)))
            {
                UmbracoContentTypeAttribute parentAttribute = parentType.GetCustomAttribute <UmbracoContentTypeAttribute>();
                if (parentAttribute != null)
                {
                    string       parentAlias       = parentAttribute.ContentTypeAlias;
                    IContentType parentContentType = contentTypeService.GetContentType(parentAlias);
                    contentType.ParentId = parentContentType.Id;
                }
                else
                {
                    throw new Exception("The given base class has no UmbracoContentTypeAttribute");
                }
            }

            if (attribute.CreateMatchingView)
            {
                CreateMatchingView(fileService, attribute, type, contentType);

                //Template currentTemplate = fileService.GetTemplate(attribute.ContentTypeAlias) as Template;
                //if (currentTemplate == null)
                //{
                //    //there should be a template but there isn't so we create one
                //    currentTemplate = new Template("~/Views/" + attribute.ContentTypeAlias + ".cshtml", attribute.ContentTypeName, attribute.ContentTypeAlias);
                //    CreateViewFile(attribute.ContentTypeAlias, attribute.MasterTemplate, currentTemplate, type, fileService);
                //    fileService.SaveTemplate(currentTemplate, 0);
                //}
                //contentType.AllowedTemplates = new ITemplate[] { currentTemplate };
                //contentType.SetDefaultTemplate(currentTemplate);
            }

            VerifyProperties(contentType, type, dataTypeService);

            //verify if a tab has no properties, if so remove
            var propertyGroups = contentType.PropertyGroups.ToArray();
            int length         = propertyGroups.Length;

            for (int i = 0; i < length; i++)
            {
                if (propertyGroups[i].PropertyTypes.Count == 0)
                {
                    //remove
                    contentType.RemovePropertyGroup(propertyGroups[i].Name);
                }
            }

            //persist
            contentTypeService.Save(contentType, 0);
        }
コード例 #2
0
        /// <summary>
        /// Creates a View if specified in the attribute
        /// </summary>
        /// <param name="fileService"></param>
        /// <param name="attribute"></param>
        /// <param name="type"></param>
        /// <param name="newContentType"></param>
        private static void CreateMatchingView(IFileService fileService, UmbracoContentTypeAttribute attribute, Type type, IContentType newContentType)
        {
            var currentTemplate = fileService.GetTemplate(attribute.ContentTypeAlias) as Template;

            if (currentTemplate == null)
            {
                string templatePath;
                if (string.IsNullOrEmpty(attribute.TemplateLocation))
                {
                    templatePath = string.Format(CultureInfo.InvariantCulture, "~/Views/{0}.cshtml", attribute.ContentTypeAlias);
                }
                else
                {
                    templatePath = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}.cshtml",
                                                 attribute.TemplateLocation,                                    // The template location
                                                 attribute.TemplateLocation.EndsWith("/") ? string.Empty : "/", // Ensure the template location ends with a "/"
                                                 attribute.ContentTypeAlias);                                   // The alias
                }

                currentTemplate = new Template(templatePath, attribute.ContentTypeName, attribute.ContentTypeAlias);
                CreateViewFile(attribute.MasterTemplate, currentTemplate, type, fileService);
            }

            newContentType.AllowedTemplates = new ITemplate[] { currentTemplate };
            newContentType.SetDefaultTemplate(currentTemplate);

            //TODO: in Umbraco 7.1 it will be possible to set the master template of the newly created template
            //https://github.com/umbraco/Umbraco-CMS/pull/294
        }
コード例 #3
0
        /// <summary>
        /// This method is called when the Content Type declared in the attribute hasn't been found in Umbraco
        /// </summary>
        /// <param name="contentTypeService"></param>
        /// <param name="fileService"></param>
        /// <param name="attribute"></param>
        /// <param name="type"></param>
        /// <param name="dataTypeService"></param>
        private static void CreateContentType(IContentTypeService contentTypeService, IFileService fileService,
                                              UmbracoContentTypeAttribute attribute, Type type, IDataTypeService dataTypeService)
        {
            IContentType newContentType;
            Type         parentType = type.BaseType;

            if (parentType != null && parentType != typeof(UmbracoGeneratedBase) && parentType.GetBaseTypes(false).Any(x => x == typeof(UmbracoGeneratedBase)))
            {
                UmbracoContentTypeAttribute parentAttribute = parentType.GetCustomAttribute <UmbracoContentTypeAttribute>();
                if (parentAttribute != null)
                {
                    string       parentAlias       = parentAttribute.ContentTypeAlias;
                    IContentType parentContentType = contentTypeService.GetContentType(parentAlias);
                    newContentType = new ContentType(parentContentType);
                }
                else
                {
                    throw new Exception("The given base class has no UmbracoContentTypeAttribute");
                }
            }
            else
            {
                newContentType = new ContentType(-1);
            }

            newContentType.Name  = attribute.ContentTypeName;
            newContentType.Alias = attribute.ContentTypeAlias;
            newContentType.Icon  = attribute.Icon;

            if (attribute.CreateMatchingView)
            {
                CreateMatchingView(fileService, attribute, type, newContentType);
            }

            newContentType.AllowedAsRoot       = attribute.AllowedAtRoot;
            newContentType.IsContainer         = attribute.EnableListView;
            newContentType.AllowedContentTypes = FetchAllowedContentTypes(attribute.AllowedChildren, contentTypeService);

            //create tabs
            CreateTabs(newContentType, type, dataTypeService);

            //create properties on the generic tab
            var propertiesOfRoot = type.GetProperties().Where(x => x.GetCustomAttribute <UmbracoPropertyAttribute>() != null);

            foreach (var item in propertiesOfRoot)
            {
                CreateProperty(newContentType, null, dataTypeService, true, item);
            }

            //Save and persist the content Type
            contentTypeService.Save(newContentType, 0);
        }
コード例 #4
0
        private static List <string> GetAliasesFromTypes(Type[] types)
        {
            List <string> aliases = new List <string>();

            foreach (Type type in types)
            {
                UmbracoContentTypeAttribute attribute = type.GetCustomAttribute <UmbracoContentTypeAttribute>();
                if (attribute != null)
                {
                    aliases.Add(attribute.ContentTypeAlias);
                }
            }

            return(aliases);
        }
コード例 #5
0
        /// <summary>
        /// This method will create or update the Content Type in Umbraco.
        /// It's possible that you need to run this method a few times to create all relations between Content Types.
        /// </summary>
        /// <param name="type">The type of your model that contains an UmbracoContentTypeAttribute</param>
        public static void CreateOrUpdateEntity(Type type)
        {
            var contentTypeService = ApplicationContext.Current.Services.ContentTypeService;
            var fileService        = ApplicationContext.Current.Services.FileService;
            var dataTypeService    = ApplicationContext.Current.Services.DataTypeService;

            UmbracoContentTypeAttribute attribute = type.GetCustomAttribute <UmbracoContentTypeAttribute>();

            if (attribute == null)
            {
                return;
            }

            if (!contentTypeService.GetAllContentTypes().Any(x => x != null && x.Alias == attribute.ContentTypeAlias))
            {
                CreateContentType(contentTypeService, fileService, attribute, type, dataTypeService);
            }
            else
            {
                //update
                IContentType contentType = contentTypeService.GetContentType(attribute.ContentTypeAlias);
                UpdateContentType(contentTypeService, fileService, attribute, contentType, type, dataTypeService);
            }
        }