/// <summary>
        /// Saves the node list.
        /// </summary>
        /// <param name="sitemap"></param>
        /// <param name="nodes">The nodes.</param>
        /// <param name="parentNode">The parent node.</param>
        /// <param name="toList"></param>
        private void SaveNodeList(Models.Sitemap sitemap, IEnumerable <SitemapNodeViewModel> nodes, SitemapNode parentNode, List <SitemapNode> nodesToSearchIn)
        {
            if (nodes == null)
            {
                return;
            }

            foreach (var node in nodes)
            {
                var isDeleted = node.IsDeleted || (parentNode != null && parentNode.IsDeleted);
                var create    = node.Id.HasDefaultValue() && !isDeleted;
                var update    = !node.Id.HasDefaultValue() && !isDeleted;
                var delete    = !node.Id.HasDefaultValue() && isDeleted;

                bool updatedInDB;
                var  sitemapNode = SaveNodeIfUpdated(out updatedInDB, sitemap, node.Id, node.Version, node.Url, node.Title, node.PageId, node.UsePageTitleAsNodeTitle, node.DisplayOrder, node.ParentId, isDeleted, parentNode, nodesToSearchIn);

                if (create && updatedInDB)
                {
                    createdNodes.Add(sitemapNode);
                }
                else if (update && updatedInDB)
                {
                    updatedNodes.Add(sitemapNode);
                }
                else if (delete && updatedInDB)
                {
                    deletedNodes.Add(sitemapNode);
                }

                SaveNodeList(sitemap, node.ChildNodes, sitemapNode, nodesToSearchIn);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Sorts the items.
        /// </summary>
        /// <param name="pages">The full page list</param>
        /// <param name="parentId">The current parent id</param>
        /// <param name="level">The level in structure</param>
        /// <returns>The sitemap</returns>
        private Models.Sitemap Sort(IEnumerable <Page> pages, Guid?parentId = null, int level = 0)
        {
            var result = new Models.Sitemap();

            foreach (var page in pages.Where(p => p.ParentId == parentId).OrderBy(p => p.SortOrder))
            {
                var item = Module.Mapper.Map <Page, Models.SitemapItem>(page);

                if (!string.IsNullOrEmpty(page.RedirectUrl))
                {
                    item.Permalink = page.RedirectUrl;
                }

                item.Level        = level;
                item.PageTypeName = App.PageTypes.First(t => t.Id == page.PageTypeId).Title;
                item.Items        = Sort(pages, page.Id, level + 1);

                //use for multilanguage - anhpt
                Guid   siteId   = _db.Pages.SingleOrDefault(x => x.Id == item.Id).SiteId;
                string hostName = _db.Sites.SingleOrDefault(x => x.Id == siteId).Hostnames;
                string urlName  = hostName.Substring(hostName.Length - 3, 3);
                item.Permalink = urlName + item.Permalink;

                result.Add(item);
            }
            return(result);
        }
Esempio n. 3
0
        // rebuild the sitemap json file based on the database content
        public static void Rebuild(ApplicationDbContext context)
        {
            // retrieve existing sitemap and clear the pages section, ready for new content
            Models.Sitemap sitemap = SiteMapGet;
            sitemap.Pages.Clear();

            // retrieve list of active cms pages and add those to the sitemap
            var q = from p in context.CMSPage
                    where p.Status != Enums.CMS.PageStatus.Deleted
                    select new
            {
                Url   = p.Url,
                Title = p.Title
            };

            foreach (var page in q.ToList())
            {
                sitemap.Pages.Add(new Models.Sitemap.Page()
                {
                    Url   = page.Url,
                    Title = page.Title,
                    Type  = "CMS"
                });
            }

            // write the json back to the file
            File.WriteAllText(@"App_Data/sitemap.json", JsonConvert.SerializeObject(sitemap));
        }
Esempio n. 4
0
 private SitemapViewModel GetModelMainData(Models.Sitemap sitemap)
 {
     return(new SitemapViewModel
     {
         Id = sitemap.Id,
         Version = sitemap.Version,
         Title = sitemap.Title,
     });
 }
Esempio n. 5
0
        /// <summary>
        /// Executes the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Result model.</returns>
        public SitemapsGridViewModel <SiteSettingSitemapViewModel> Execute(SitemapsFilter request)
        {
            request.SetDefaultSortingOptions("Title");

            Models.Sitemap alias = null;
            SiteSettingSitemapViewModel modelAlias = null;

            var query = UnitOfWork.Session.QueryOver(() => alias).Where(() => !alias.IsDeleted);

            if (!string.IsNullOrWhiteSpace(request.SearchQuery))
            {
                var searchQuery = string.Format("%{0}%", request.SearchQuery);
                query = query.Where(Restrictions.Disjunction().Add(Restrictions.InsensitiveLike(Projections.Property(() => alias.Title), searchQuery)));
            }

            if (request.Tags != null)
            {
                foreach (var tagKeyValue in request.Tags)
                {
                    var id = tagKeyValue.Key.ToGuidOrDefault();
                    query = query.WithSubquery.WhereExists(QueryOver.Of <PageTag>().Where(tag => tag.Tag.Id == id && tag.Page.Id == alias.Id).Select(tag => 1));
                }
            }

            query =
                query.SelectList(
                    select =>
                    select.Select(() => alias.Id)
                    .WithAlias(() => modelAlias.Id)
                    .Select(() => alias.Version)
                    .WithAlias(() => modelAlias.Version)
                    .Select(() => alias.Title)
                    .WithAlias(() => modelAlias.Title)).TransformUsing(Transformers.AliasToBean <SiteSettingSitemapViewModel>());

            if (configuration.Security.AccessControlEnabled)
            {
                IEnumerable <Guid> deniedPages = GetDeniedSitemaps();
                foreach (var deniedPageId in deniedPages)
                {
                    var id = deniedPageId;
                    query = query.Where(f => f.Id != id);
                }
            }

            var count = query.ToRowCountFutureValue();

            var sitemaps = query.AddSortingAndPaging(request).Future <SiteSettingSitemapViewModel>();

            return(new SitemapsGridViewModel <SiteSettingSitemapViewModel>(sitemaps.ToList(), request, count.Value));
        }
Esempio n. 6
0
        /// <summary>
        /// Sorts the items.
        /// </summary>
        /// <param name="pages">The full page list</param>
        /// <param name="parentId">The current parent id</param>
        /// <returns>The sitemap</returns>
        private Models.Sitemap Sort(IEnumerable <Page> pages, Guid?parentId = null, int level = 0)
        {
            var result = new Models.Sitemap();

            foreach (var page in pages.Where(p => p.ParentId == parentId).OrderBy(p => p.SortOrder))
            {
                var item = App.Mapper.Map <Page, Models.SitemapItem>(page);

                item.Level = level;
                item.Items = Sort(pages, page.Id, level + 1);

                result.Add(item);
            }
            return(result);
        }
Esempio n. 7
0
        /// <summary>
        /// Sorts the items.
        /// </summary>
        /// <param name="pages">The full page list</param>
        /// <param name="parentId">The current parent id</param>
        /// <returns>The sitemap</returns>
        private Models.Sitemap Sort(IEnumerable <Page> pages, IEnumerable <Models.PageType> pageTypes, Guid?parentId = null, int level = 0)
        {
            var result = new Models.Sitemap();

            foreach (var page in pages.Where(p => p.ParentId == parentId).OrderBy(p => p.SortOrder))
            {
                var item = App.Mapper.Map <Page, Models.SitemapItem>(page);

                if (!string.IsNullOrEmpty(page.RedirectUrl))
                {
                    item.Permalink = page.RedirectUrl;
                }

                item.Level        = level;
                item.PageTypeName = pageTypes.First(t => t.Id == page.PageTypeId).Title;
                item.Items        = Sort(pages, pageTypes, page.Id, level + 1);

                result.Add(item);
            }
            return(result);
        }
Esempio n. 8
0
        /// <summary>
        /// Saves the node list.
        /// </summary>
        /// <param name="sitemap">The sitemap.</param>
        /// <param name="nodes">The nodes.</param>
        /// <param name="parentNode">The parent node.</param>
        /// <param name="nodeList"></param>
        /// <param name="translationList"></param>
        private void SaveNodeList(Models.Sitemap sitemap, IEnumerable <SitemapNodeViewModel> nodes, SitemapNode parentNode, List <SitemapNode> nodeList, List <SitemapNodeTranslation> translationList)
        {
            if (nodes == null)
            {
                return;
            }

            foreach (var node in nodes)
            {
                var isDeleted = node.IsDeleted || (parentNode != null && parentNode.IsDeleted);
                var create    = node.Id.HasDefaultValue() && !isDeleted;
                var update    = !node.Id.HasDefaultValue() && !isDeleted;
                var delete    = !node.Id.HasDefaultValue() && isDeleted;

                bool updatedInDB;
                var  translationUpdatedInDB = false;
                var  sitemapNode            = SitemapService.SaveNode(out updatedInDB, sitemap, node.Id, node.Version, node.Url, node.Title, node.Macro, node.PageId, node.UsePageTitleAsNodeTitle, node.DisplayOrder, node.ParentId, isDeleted, parentNode, nodeList);

                if ((create || update) && (node.Translations != null && node.Translations.Count > 0))
                {
                    SaveTranslations(out translationUpdatedInDB, sitemapNode, node, translationList);
                }

                if (create && (updatedInDB || translationUpdatedInDB))
                {
                    createdNodes.Add(sitemapNode);
                }
                else if (update && (updatedInDB || translationUpdatedInDB))
                {
                    updatedNodes.Add(sitemapNode);
                }
                else if (delete && updatedInDB)
                {
                    deletedNodes.Add(sitemapNode);
                    RemoveTranslations(sitemapNode);
                }

                SaveNodeList(sitemap, node.ChildNodes, sitemapNode, nodeList, translationList);
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Executes the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        public SitemapViewModel Execute(SitemapViewModel request)
        {
            createdNodes.Clear();
            updatedNodes.Clear();
            deletedNodes.Clear();

            var createNew = request.Id.HasDefaultValue();

            Models.Sitemap sitemap;

            if (!createNew)
            {
                var sitemapQuery = Repository.AsQueryable <Models.Sitemap>().Where(s => s.Id == request.Id);

                if (CmsConfiguration.Security.AccessControlEnabled)
                {
                    sitemapQuery = sitemapQuery.FetchMany(f => f.AccessRules);
                }

                sitemap = sitemapQuery.ToList().First();

                if (CmsConfiguration.Security.AccessControlEnabled)
                {
                    AccessControlService.DemandAccess(sitemap, Context.Principal, AccessLevel.ReadWrite);
                }
            }
            else
            {
                sitemap = new Models.Sitemap()
                {
                    AccessRules = new List <AccessRule>()
                };
            }

            var nodeList = !createNew?Repository.AsQueryable <SitemapNode>()
                           .Where(node => node.Sitemap.Id == sitemap.Id)
                           .ToFuture()
                               : new List <SitemapNode>();

            var translationList = !createNew
                                      ? Repository.AsQueryable <SitemapNodeTranslation>()
                                  .Where(t => t.Node.Sitemap.Id == sitemap.Id)
                                  .Fetch(t => t.Node)
                                  .ToFuture()
                                      : new List <SitemapNodeTranslation>();

            UnitOfWork.BeginTransaction();

            if (!createNew)
            {
                SitemapService.ArchiveSitemap(request.Id);
            }

            if (CmsConfiguration.Security.AccessControlEnabled)
            {
                sitemap.AccessRules.RemoveDuplicateEntities();

                var accessRules = request.UserAccessList != null?request.UserAccessList.Cast <IAccessRule>().ToList() : null;

                AccessControlService.UpdateAccessControl(sitemap, accessRules);
            }

            sitemap.Title   = request.Title;
            sitemap.Version = request.Version;
            Repository.Save(sitemap);

            SaveNodeList(sitemap, request.RootNodes, null, nodeList.ToList(), translationList.ToList());

            IList <Tag> newTags;

            TagService.SaveTags(sitemap, request.Tags, out newTags);


            UnitOfWork.Commit();

            foreach (var node in createdNodes)
            {
                Events.SitemapEvents.Instance.OnSitemapNodeCreated(node);
            }

            foreach (var node in updatedNodes)
            {
                Events.SitemapEvents.Instance.OnSitemapNodeUpdated(node);
            }

            foreach (var node in deletedNodes)
            {
                Events.SitemapEvents.Instance.OnSitemapNodeDeleted(node);
            }

            if (createNew)
            {
                Events.SitemapEvents.Instance.OnSitemapCreated(sitemap);
            }
            else
            {
                Events.SitemapEvents.Instance.OnSitemapUpdated(sitemap);
            }

            Events.RootEvents.Instance.OnTagCreated(newTags);

            return(GetModelMainData(sitemap));
        }
        private SitemapNode SaveNodeIfUpdated(out bool nodeUpdated, Models.Sitemap sitemap, Guid nodeId, int version, string url, string title, Guid pageId, bool usePageTitleAsNodeTitle, int displayOrder, Guid parentId, bool isDeleted = false, SitemapNode parentNode = null, List <SitemapNode> nodesToSearchIn = null)
        {
            nodeUpdated = false;
            var node = nodeId.HasDefaultValue()
                ? new SitemapNode()
                : nodesToSearchIn != null?nodesToSearchIn.First(n => n.Id == nodeId) : Repository.First <SitemapNode>(nodeId);

            if (isDeleted)
            {
                if (!node.Id.HasDefaultValue())
                {
                    Repository.Delete(node);
                    nodeUpdated = true;
                }
            }
            else
            {
                var updated = false;
                if (node.Sitemap == null)
                {
                    node.Sitemap = sitemap;
                }

                if (node.Title != title)
                {
                    updated    = true;
                    node.Title = title;
                }

                if (node.Page != (!pageId.HasDefaultValue() ? Repository.AsProxy <PageProperties>(pageId) : null))
                {
                    updated   = true;
                    node.Page = !pageId.HasDefaultValue() ? Repository.AsProxy <PageProperties>(pageId) : null;
                }

                if (node.UsePageTitleAsNodeTitle != (!pageId.HasDefaultValue() && usePageTitleAsNodeTitle))
                {
                    updated = true;
                    node.UsePageTitleAsNodeTitle = !pageId.HasDefaultValue() && usePageTitleAsNodeTitle;
                }

                if (node.Url != (node.Page != null ? null : url))
                {
                    updated      = true;
                    node.Url     = node.Page != null ? null : url;
                    node.UrlHash = node.Page != null ? null : url.UrlHash();
                }

                if (node.DisplayOrder != displayOrder)
                {
                    updated           = true;
                    node.DisplayOrder = displayOrder;
                }

                SitemapNode newParent;
                if (parentNode != null && !parentNode.Id.HasDefaultValue())
                {
                    newParent = parentNode;
                }
                else
                {
                    newParent = parentId.HasDefaultValue()
                        ? null
                        : Repository.First <SitemapNode>(parentId);
                }

                if (node.ParentNode != newParent)
                {
                    updated         = true;
                    node.ParentNode = newParent;
                }

                if (updated)
                {
                    node.Version = version;
                    Repository.Save(node);
                    nodeUpdated = true;
                }
            }

            return(node);
        }
        /// <summary>
        /// Executes the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        public void Execute(List <SitemapViewModel> requests)
        {
            createdNodes.Clear();
            updatedNodes.Clear();
            deletedNodes.Clear();

            var sitemapsToSave = new List <SaveModel>();

            foreach (var request in requests)
            {
                var createNew = request.Id.HasDefaultValue();

                Models.Sitemap sitemap;

                if (!createNew)
                {
                    var sitemapQuery = Repository.AsQueryable <Models.Sitemap>()
                                       .Where(s => s.Id == request.Id);

                    if (CmsConfiguration.Security.AccessControlEnabled)
                    {
                        sitemapQuery = sitemapQuery.FetchMany(f => f.AccessRules);
                    }

                    sitemapQuery = sitemapQuery.FetchMany(s => s.Nodes);

                    sitemap = sitemapQuery.ToList().First(); // NOTE: bottleneck. TODO: remake with ToFuture to get all the sitemaps at once.

                    if (CmsConfiguration.Security.AccessControlEnabled)
                    {
                        AccessControlService.DemandAccess(sitemap, Context.Principal, AccessLevel.ReadWrite);
                    }
                }
                else
                {
                    sitemap = new Models.Sitemap()
                    {
                        AccessRules = new List <AccessRule>(), Nodes = new List <SitemapNode>()
                    };
                }

                sitemapsToSave.Add(new SaveModel()
                {
                    Model = request, Entity = sitemap, IsNew = createNew
                });
            }


            UnitOfWork.BeginTransaction();

            var createdTags = new List <Tag>();

            foreach (var item in sitemapsToSave)
            {
                // Save/update only sitemap nodes.
                SaveNodeList(item.Entity, item.Model.RootNodes, null, item.Entity.Nodes.ToList());
            }

            UnitOfWork.Commit();

            foreach (var node in createdNodes)
            {
                Events.SitemapEvents.Instance.OnSitemapNodeCreated(node);
            }

            foreach (var node in updatedNodes)
            {
                Events.SitemapEvents.Instance.OnSitemapNodeUpdated(node);
            }

            foreach (var node in deletedNodes)
            {
                Events.SitemapEvents.Instance.OnSitemapNodeDeleted(node);
            }

            foreach (var item in sitemapsToSave)
            {
                if (item.IsNew)
                {
                    Events.SitemapEvents.Instance.OnSitemapCreated(item.Entity);
                }
                else
                {
                    Events.SitemapEvents.Instance.OnSitemapUpdated(item.Entity);
                }
            }

            Events.RootEvents.Instance.OnTagCreated(createdTags);
        }