private static void CreateCategoryStructure(BusiBlocksTreeNode rootNode, IList <Category> categories, Category category, int maxLevel, int level, bool includeItems, string username)
        {
            if (level > maxLevel)
            {
                return;
            }

            var subCategories = from x in categories where (x.ParentCategory == null) ? 1 == 0 : x.ParentCategory.Equals(category) select x;

            foreach (Category subCategory in subCategories)
            {
                IList <Item> items = NewsManager.GetItems(subCategory, false);

                BusiBlocksTreeNode categoryNode = new BusiBlocksTreeNode {
                    Name = subCategory.Name, Id = subCategory.Id, IsFolder = true, ChildNodes = new List <BusiBlocksTreeNode>()
                };

                if (items.Count > 0 && includeItems)
                {
                    foreach (Item item in items)
                    {
                        if (BusiBlocks.SecurityHelper.CanUserView(username, subCategory.Id))
                        {
                            categoryNode.ChildNodes.Add(new BusiBlocksTreeNode {
                                Id = item.Id, Name = item.Title, IsFolder = false
                            });
                        }
                    }
                }

                if (BusiBlocks.SecurityHelper.CanUserView(username, categoryNode.Id))
                {
                    rootNode.ChildNodes.Add(categoryNode);
                    CreateCategoryStructure(categoryNode, categories, subCategory, maxLevel, level++, includeItems, username);
                }
            }
        }
        public static BusiBlocksTreeView GetCategoriesItemsTree(bool includeItems, string username)
        {
            BusiBlocksTreeView tree = new BusiBlocksTreeView {
                Nodes = new List <BusiBlocksTreeNode>()
            };
            IList <Category> categories = NewsManager.GetAllCategories();

            var rootCategories = from x in categories where x.ParentCategory == null select x;

            int maxLevel = 100;

            foreach (Category category in rootCategories)
            {
                BusiBlocksTreeNode node = new BusiBlocksTreeNode {
                    Name = category.Name, Id = category.Id, IsFolder = true, ChildNodes = new List <BusiBlocksTreeNode>()
                };
                if (BusiBlocks.SecurityHelper.CanUserView(username, node.Id))
                {
                    tree.Nodes.Add(node);
                    IList <Item> items = NewsManager.GetItems(category, false);
                    if (items.Count > 0 && includeItems)
                    {
                        foreach (Item item in items)
                        {
                            if (BusiBlocks.SecurityHelper.CanUserView(username, category.Id))
                            {
                                node.ChildNodes.Add(new BusiBlocksTreeNode {
                                    Id = item.Id, Name = item.Title, IsFolder = false
                                });
                            }
                        }
                    }
                    CreateCategoryStructure(node, categories, category, maxLevel, 0, includeItems, username);
                }
            }
            return(tree);
        }