Ejemplo n.º 1
0
        /// <summary>
        /// Executes the specified request.
        /// </summary>
        /// <param name="request">The page view model.</param>
        /// <returns>Created page</returns>
        public virtual SavePageResponse Execute(AddNewPageViewModel request)
        {
            if (request.CreateMasterPage)
            {
                AccessControlService.DemandAccess(Context.Principal, RootModuleConstants.UserRoles.Administration);
            }
            else
            {
                AccessControlService.DemandAccess(Context.Principal, RootModuleConstants.UserRoles.EditContent);
            }

            if (!request.MasterPageId.HasValue && !request.TemplateId.HasValue)
            {
                var message = RootGlobalization.MasterPage_Or_Layout_ShouldBeSelected_ValidationMessage;
                throw new ValidationException(() => message, message);
            }
            if (request.MasterPageId.HasValue && request.TemplateId.HasValue)
            {
                var logMessage = string.Format("Only one of master page and layout can be selected. LayoutId: {0}, MasterPageId: {1}", request.MasterPageId, request.TemplateId);
                var message    = RootGlobalization.MasterPage_Or_Layout_OnlyOne_ShouldBeSelected_ValidationMessage;
                throw new ValidationException(() => message, logMessage);
            }

            // Create / fix page url.
            var pageUrl       = request.PageUrl;
            var createPageUrl = pageUrl == null;

            if (createPageUrl && !string.IsNullOrWhiteSpace(request.PageTitle))
            {
                pageUrl = pageService.CreatePagePermalink(request.PageTitle, request.ParentPageUrl, null, request.LanguageId);
            }
            else
            {
                pageUrl = urlService.FixUrl(pageUrl);

                // Validate Url
                pageService.ValidatePageUrl(pageUrl);
            }

            var page = new PageProperties
            {
                PageUrl      = pageUrl,
                PageUrlHash  = pageUrl.UrlHash(),
                Title        = request.PageTitle,
                MetaTitle    = request.PageTitle,
                Status       = request.CreateMasterPage ? PageStatus.Published : PageStatus.Unpublished,
                IsMasterPage = request.CreateMasterPage
            };

            if (request.MasterPageId.HasValue)
            {
                page.MasterPage = Repository.AsProxy <Root.Models.Page>(request.MasterPageId.Value);

                masterPageService.SetPageMasterPages(page, request.MasterPageId.Value);
            }
            else
            {
                page.Layout = Repository.AsProxy <Root.Models.Layout>(request.TemplateId.Value);
            }

            if (cmsConfiguration.EnableMultilanguage)
            {
                if (request.LanguageId.HasValue && !request.LanguageId.Value.HasDefaultValue())
                {
                    page.Language = Repository.AsProxy <Language>(request.LanguageId.Value);
                }
            }

            page.Options = optionService.SaveOptionValues(request.OptionValues, null, () => new PageOption {
                Page = page
            });

            Repository.Save(page);

            // Update access control if enabled:
            if (cmsConfiguration.Security.AccessControlEnabled)
            {
                AccessControlService.UpdateAccessControl(page, request.UserAccessList != null ? request.UserAccessList.Cast <IAccessRule>().ToList() : null);
            }

            UnitOfWork.Commit();

            // Notifying, that page is created
            Events.PageEvents.Instance.OnPageCreated(page);

            var response = new SavePageResponse(page)
            {
                IsSitemapActionEnabled = ConfigurationHelper.IsSitemapActionEnabledAfterAddingNewPage(cmsConfiguration)
            };

            return(response);
        }
Ejemplo n.º 2
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));
        }