示例#1
0
        /// <summary>
        /// Adds a new category.
        /// </summary>
        /// <param name="categoryName">Name of the category.</param>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public CategoryItem Add(string categoryName, Item item)
        {
            Item blogItem = item;
            var  template = GetDatabase().GetTemplate(Settings.BlogTemplateID);


            if (template != null)
            {
                //Check if current item equals blogroot
                while (blogItem != null && !blogItem.TemplateIsOrBasedOn(template))
                {
                    blogItem = blogItem.Parent;
                }

                // Get all categories from current blog
                CategoryItem[] categories = GetCategories(blogItem);

                // If there are categories, check if it already contains the categoryName
                if (categories.Count() > 0)
                {
                    var resultList = categories.Where(x => x.Title.Raw.ToLower() == categoryName.ToLower());
                    var result     = resultList.Count() == 0 ? null : resultList.First();

                    // If category is found return ID
                    if (result != null)
                    {
                        return(result);
                    }
                }

                // Category doesn't exist so create it
                var categoriesFolder = blogItem.Axes.GetChild("Categories");

                CategoryItem newCategory = ItemManager.AddFromTemplate(categoryName, new ID(CategoryItem.TemplateId), categoriesFolder);
                newCategory.BeginEdit();
                newCategory.Title.Field.Value = categoryName;
                newCategory.EndEdit();

                return(newCategory);
            }
            return(null);
        }
示例#2
0
        /// <summary>
        /// Adds a new category.
        /// </summary>
        /// <param name="categoryName">Name of the category.</param>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public virtual CategoryItem Add(string categoryName, Item item)
        {
            if (item == null)
            {
                Logger.Warn("Could not create a new category item because paremater 'item' was null", this);
                return(null);
            }

            // Get all categories from current blog
            var categories = GetCategories(item);

            // If there are categories, check if it already contains the categoryName
            if (categories.Any())
            {
                var existingCategory = categories.Where(x => x.Title.Raw.CompareTo(categoryName) == 0);
                if (existingCategory.Any())
                {
                    return(existingCategory.First());
                }
            }

            // Category doesn't exist so create it
            var categoryRoot = GetCategoryRoot(item) ?? CreateCategoryRoot(item);

            if (categoryRoot == null)
            {
                Logger.Warn("New category item cannot be created. Could not find the category root item.", this);
                return(null);
            }

            CategoryItem newCategory = ItemManager.AddFromTemplate(categoryName, Settings.CategoryTemplateIds.First(), categoryRoot);

            newCategory.BeginEdit();
            newCategory.Title.Field.Value = categoryName;
            newCategory.EndEdit();

            return(newCategory);
        }