Esempio n. 1
0
        /// <summary>
        /// Render dynamic menu
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public string Render(string[] parameters)
        {
            //Initialize parameter
            ParseParams(parameters);

            var templateList = new List <string>
            {
                ParentTemplateName,
                ChildMobileTemplateName,
                ChildTemplateName
            };

            /*
             * Check storing menu result in cache
             * If all the menus are not updated then get the cache result
             * If not rebuild the data and recache
             */
            CacheMenu cacheMenu;
            var       cacheName = string.Join("_", parameters);

            var lastCreated   = _clientMenuServices.GetAll().Max(t => t.Created);
            var lastUpdated   = _clientMenuServices.GetAll().Max(t => t.Updated) ?? DateTime.MinValue;
            var dataCacheTime = lastCreated > lastUpdated ? lastCreated : lastUpdated;

            lastCreated = _templateServices.Fetch(t => templateList.Contains(t.Name)).Max(t => t.Created);
            lastUpdated = _templateServices.Fetch(t => templateList.Contains(t.Name)).Max(t => t.Updated) ?? DateTime.MinValue;
            var templateCacheTime = lastCreated > lastUpdated ? lastCreated : lastUpdated;

            if (HttpContext.Current.Application[cacheName] != null)
            {
                cacheMenu = (CacheMenu)HttpContext.Current.Application[cacheName];
                if (cacheMenu.DataCacheTime == dataCacheTime && cacheMenu.TemplateCacheTime == templateCacheTime)
                {
                    return(cacheMenu.Content);
                }
            }

            /*
             * Get tree data
             * Recursive to get all the html of dynamic menu
             */
            var items = _clientMenuServices.GetAll()
                        .Where(m => m.IncludeInSiteNavigation &&
                               (!m.StartPublishingDate.HasValue || DateTime.Now > m.StartPublishingDate) &&
                               (!m.EndPublishingDate.HasValue || DateTime.Now < m.StartPublishingDate))
                        .Select(m => new ClientMenuModel
            {
                Id          = m.Id,
                PageId      = m.PageId,
                Name        = m.Name,
                RecordOrder = m.RecordOrder,
                Url         = m.Page.IsHomePage ? string.Empty : m.Url,
                ParentId    = m.ParentId,
            }).ToList();
            var data = GetTree(items, ParentId, 0);

            foreach (var dynamicMenu in data)
            {
                dynamicMenu.ChildMenusString       = RenderMenus(dynamicMenu.ChildMenus, _childTemplate);
                dynamicMenu.ChildMenusMobileString = RenderMobileMenus(dynamicMenu.ChildMenus, _childMobileTemplate);
            }

            /*
             * Create cache version of this curly bracket and store in Application
             */
            cacheMenu = new CacheMenu
            {
                Content           = _templateServices.Parse(_parentTemplate.Content, data, null, _parentTemplate.CacheName),
                DataCacheTime     = dataCacheTime,
                TemplateCacheTime = templateCacheTime
            };
            HttpContext.Current.Application[cacheName] = cacheMenu;

            return(cacheMenu.Content);
        }
Esempio n. 2
0
        /// <summary>
        /// Render dynamic menu
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public string Render(string[] parameters)
        {
            //Initialize parameter
            ParseParams(parameters);

            var templateList = new List<string>
            {
                ParentTemplateName,
                ChildMobileTemplateName,
                ChildTemplateName
            };

            /*
             * Check storing menu result in cache
             * If all the menus are not updated then get the cache result
             * If not rebuild the data and recache
             */
            CacheMenu cacheMenu;
            var cacheName = string.Join("_", parameters);

            var lastCreated = _clientMenuServices.GetAll().Max(t => t.Created);
            var lastUpdated = _clientMenuServices.GetAll().Max(t => t.Updated) ?? DateTime.MinValue;
            var dataCacheTime = lastCreated > lastUpdated ? lastCreated : lastUpdated;

            lastCreated = _templateServices.Fetch(t => templateList.Contains(t.Name)).Max(t => t.Created);
            lastUpdated = _templateServices.Fetch(t => templateList.Contains(t.Name)).Max(t => t.Updated) ?? DateTime.MinValue;
            var templateCacheTime = lastCreated > lastUpdated ? lastCreated : lastUpdated;
            if (HttpContext.Current.Application[cacheName] != null)
            {
                cacheMenu = (CacheMenu)HttpContext.Current.Application[cacheName];
                if (cacheMenu.DataCacheTime == dataCacheTime && cacheMenu.TemplateCacheTime == templateCacheTime)
                {
                    return cacheMenu.Content;
                }
            }

            /*
             * Get tree data
             * Recursive to get all the html of dynamic menu
             */
            var items = _clientMenuServices.GetAll()
                .Where(m => m.IncludeInSiteNavigation
                    && (!m.StartPublishingDate.HasValue || DateTime.Now > m.StartPublishingDate)
                    && (!m.EndPublishingDate.HasValue || DateTime.Now < m.StartPublishingDate))
                .Select(m => new ClientMenuModel
                {
                    Id = m.Id,
                    PageId = m.PageId,
                    Name = m.Name,
                    RecordOrder = m.RecordOrder,
                    Url = m.Page.IsHomePage ? string.Empty : m.Url,
                    ParentId = m.ParentId,
                }).ToList();
            var data = GetTree(items, ParentId, 0);
            foreach (var dynamicMenu in data)
            {
                dynamicMenu.ChildMenusString = RenderMenus(dynamicMenu.ChildMenus, _childTemplate);
                dynamicMenu.ChildMenusMobileString = RenderMobileMenus(dynamicMenu.ChildMenus, _childMobileTemplate);
            }

            /*
             * Create cache version of this curly bracket and store in Application
             */
            cacheMenu = new CacheMenu
            {
                Content = _templateServices.Parse(_parentTemplate.Content, data, null, _parentTemplate.CacheName),
                DataCacheTime = dataCacheTime,
                TemplateCacheTime = templateCacheTime
            };
            HttpContext.Current.Application[cacheName] = cacheMenu;

            return cacheMenu.Content;
        }