Example #1
0
        public async Task <ICollection <NavigationNode> > CreateContentNavigation(int?rootId = null, bool includeCurrentPage = false, Type ofType = null)
        {
            var httpContext        = ServiceLocator.Current.GetInstance <IHttpContextAccessor>().HttpContext;
            var includeUnPublished = httpContext.Items["Area"] as string == "EZMS";
            var currentContentId   = httpContext.Items["contentid"] as int?;

            var allContents = (await _contentLoader.GetAll()).FilterForVisitor <IContent>(includeUnPublished: includeUnPublished);

            if (!includeUnPublished)
            {
                allContents = allContents.Where(w => w.Published);
            }

            if (ofType != null)
            {
                allContents = allContents.Where(w => w.ModelType.IsSubclassOfGeneric(ofType));
            }

            var contentList = allContents.Cast <Content>().ToList().AsReadOnly();

            var nav = new List <NavigationNode>();

            if (!rootId.HasValue)
            {
                foreach (var page in contentList.Where(w => !w.ParentId.HasValue).OrderBy(w => w.Order))
                {
                    var node = new NavigationNode {
                        Id          = page.Id,
                        Name        = page.Name,
                        Url         = page.GetContentFullUrlSlug(),
                        IsStartPage = !page.ParentId.HasValue,
                        IsProduct   = page.ModelType.IsSubclassOfGeneric(typeof(ProductContent <>)),
                        Type        = page.ModelType,
                        IsPublished = page.Published,
                        Order       = page.Order,
                        Children    = GetContentChildrenRecursive(page.Id, contentList)
                    };

                    SetIsActiveState(node, currentContentId);
                    nav.Add(node);
                }
            }
            else
            {
                if (includeCurrentPage)
                {
                    var currentPage = contentList.FirstOrDefault(w => w.Id == rootId);
                    if (currentPage == null)
                    {
                        return(nav);
                    }
                    var node = new NavigationNode {
                        Id          = currentPage.Id,
                        Name        = currentPage.Name,
                        Url         = currentPage.GetContentFullUrlSlug(),
                        IsStartPage = !currentPage.ParentId.HasValue,
                        IsProduct   = currentPage.ModelType.IsSubclassOfGeneric(typeof(ProductContent <>)),
                        Type        = currentPage.ModelType,
                        IsPublished = currentPage.Published,
                        Order       = currentPage.Order,
                        Children    = GetContentChildrenRecursive(currentPage.Id, contentList)
                    };

                    SetIsActiveState(node, currentContentId);
                    nav.Add(node);
                }
                else
                {
                    foreach (var page in contentList.Where(w => w.ParentId == rootId).OrderBy(w => w.Order))
                    {
                        var node = new NavigationNode {
                            Id          = page.Id,
                            Name        = page.Name,
                            Url         = page.GetContentFullUrlSlug(),
                            IsStartPage = !page.ParentId.HasValue,
                            IsProduct   = page.ModelType.IsSubclassOfGeneric(typeof(ProductContent <>)),
                            Type        = page.ModelType,
                            IsPublished = page.Published,
                            Order       = page.Order,
                            Children    = GetContentChildrenRecursive(page.Id, contentList)
                        };

                        SetIsActiveState(node, currentContentId);
                        nav.Add(node);
                    }
                }
            }

            return(nav);
        }