/// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Site site;

            if (Page.IsValid)
            {
                var               rockContext       = new RockContext();
                SiteService       siteService       = new SiteService(rockContext);
                SiteDomainService siteDomainService = new SiteDomainService(rockContext);
                bool              newSite           = false;

                int siteId = hfSiteId.Value.AsInteger();

                if (siteId == 0)
                {
                    newSite = true;
                    site    = new Rock.Model.Site();
                    siteService.Add(site);
                }
                else
                {
                    site = siteService.Get(siteId);
                }

                site.Name                        = tbSiteName.Text;
                site.Description                 = tbDescription.Text;
                site.Theme                       = ddlTheme.Text;
                site.DefaultPageId               = ppDefaultPage.PageId;
                site.DefaultPageRouteId          = ppDefaultPage.PageRouteId;
                site.LoginPageId                 = ppLoginPage.PageId;
                site.LoginPageRouteId            = ppLoginPage.PageRouteId;
                site.CommunicationPageId         = ppCommunicationPage.PageId;
                site.CommunicationPageRouteId    = ppCommunicationPage.PageRouteId;
                site.RegistrationPageId          = ppRegistrationPage.PageId;
                site.RegistrationPageRouteId     = ppRegistrationPage.PageRouteId;
                site.PageNotFoundPageId          = ppPageNotFoundPage.PageId;
                site.PageNotFoundPageRouteId     = ppPageNotFoundPage.PageRouteId;
                site.ErrorPage                   = tbErrorPage.Text;
                site.GoogleAnalyticsCode         = tbGoogleAnalytics.Text;
                site.EnableMobileRedirect        = cbEnableMobileRedirect.Checked;
                site.MobilePageId                = ppMobilePage.PageId;
                site.ExternalUrl                 = tbExternalURL.Text;
                site.RedirectTablets             = cbRedirectTablets.Checked;
                site.EnablePageViews             = cbEnablePageViews.Checked;
                site.PageViewRetentionPeriodDays = nbPageViewRetentionPeriodDays.Text.AsIntegerOrNull();

                var currentDomains = tbSiteDomains.Text.SplitDelimitedValues().ToList <string>();
                site.SiteDomains = site.SiteDomains ?? new List <SiteDomain>();

                // Remove any deleted domains
                foreach (var domain in site.SiteDomains.Where(w => !currentDomains.Contains(w.Domain)).ToList())
                {
                    site.SiteDomains.Remove(domain);
                    siteDomainService.Delete(domain);
                }

                foreach (string domain in currentDomains)
                {
                    SiteDomain sd = site.SiteDomains.Where(d => d.Domain == domain).FirstOrDefault();
                    if (sd == null)
                    {
                        sd        = new SiteDomain();
                        sd.Domain = domain;
                        sd.Guid   = Guid.NewGuid();
                        site.SiteDomains.Add(sd);
                    }
                }

                if (!site.DefaultPageId.HasValue && !newSite)
                {
                    ppDefaultPage.ShowErrorMessage("Default Page is required.");
                    return;
                }

                if (!site.IsValid)
                {
                    // Controls will render the error messages
                    return;
                }

                rockContext.WrapTransaction(() =>
                {
                    rockContext.SaveChanges();

                    if (newSite)
                    {
                        Rock.Security.Authorization.CopyAuthorization(RockPage.Layout.Site, site, rockContext, Authorization.EDIT);
                        Rock.Security.Authorization.CopyAuthorization(RockPage.Layout.Site, site, rockContext, Authorization.ADMINISTRATE);
                        Rock.Security.Authorization.CopyAuthorization(RockPage.Layout.Site, site, rockContext, Authorization.APPROVE);
                    }
                });

                SiteCache.Flush(site.Id);

                // Create the default page is this is a new site
                if (!site.DefaultPageId.HasValue && newSite)
                {
                    var siteCache = SiteCache.Read(site.Id);

                    // Create the layouts for the site, and find the first one
                    LayoutService.RegisterLayouts(Request.MapPath("~"), siteCache);

                    var    layoutService = new LayoutService(rockContext);
                    var    layouts       = layoutService.GetBySiteId(siteCache.Id);
                    Layout layout        = layouts.FirstOrDefault(l => l.FileName.Equals("FullWidth", StringComparison.OrdinalIgnoreCase));
                    if (layout == null)
                    {
                        layout = layouts.FirstOrDefault();
                    }

                    if (layout != null)
                    {
                        var pageService = new PageService(rockContext);
                        var page        = new Page();
                        page.LayoutId              = layout.Id;
                        page.PageTitle             = siteCache.Name + " Home Page";
                        page.InternalName          = page.PageTitle;
                        page.BrowserTitle          = page.PageTitle;
                        page.EnableViewState       = true;
                        page.IncludeAdminFooter    = true;
                        page.MenuDisplayChildPages = true;

                        var lastPage = pageService.GetByParentPageId(null).OrderByDescending(b => b.Order).FirstOrDefault();

                        page.Order = lastPage != null ? lastPage.Order + 1 : 0;
                        pageService.Add(page);

                        rockContext.SaveChanges();

                        site = siteService.Get(siteCache.Id);
                        site.DefaultPageId = page.Id;

                        rockContext.SaveChanges();

                        SiteCache.Flush(site.Id);
                    }
                }

                var qryParams = new Dictionary <string, string>();
                qryParams["siteId"] = site.Id.ToString();

                NavigateToPage(RockPage.Guid, qryParams);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Handles the Click event of the lbSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void lbSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Rock.Model.Page page;

                var rockContext = new RockContext();
                var pageService = new PageService(rockContext);

                int pageId = hfPageId.Value.AsInteger();
                if (pageId == 0)
                {
                    page = new Rock.Model.Page();

                    if (_page != null)
                    {
                        page.ParentPageId  = _page.Id;
                        page.LayoutId      = _page.LayoutId;
                        page.AllowIndexing = _page.AllowIndexing;
                    }
                    else
                    {
                        page.ParentPageId = null;
                        page.LayoutId     = PageCache.Read(RockPage.PageId).LayoutId;
                    }

                    page.PageTitle             = dtbPageName.Text;
                    page.BrowserTitle          = page.PageTitle;
                    page.EnableViewState       = true;
                    page.IncludeAdminFooter    = true;
                    page.MenuDisplayChildPages = true;

                    Rock.Model.Page lastPage = pageService.GetByParentPageId(_page.Id).OrderByDescending(b => b.Order).FirstOrDefault();

                    if (lastPage != null)
                    {
                        page.Order = lastPage.Order + 1;
                    }
                    else
                    {
                        page.Order = 0;
                    }

                    pageService.Add(page);
                }
                else
                {
                    page = pageService.Get(pageId);
                }

                page.LayoutId     = ddlLayout.SelectedValueAsInt().Value;
                page.InternalName = dtbPageName.Text;

                if (page.IsValid)
                {
                    rockContext.SaveChanges();

                    PageCache.Flush(page.Id);
                    if (_page != null)
                    {
                        Rock.Security.Authorization.CopyAuthorization(_page, page, rockContext);
                        _page.FlushChildPages();
                    }

                    BindGrid();
                }

                rGrid.Visible      = true;
                pnlDetails.Visible = false;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Handles the Click event of the lbSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void lbSave_Click(object sender, EventArgs e)
        {
            var rockContext  = new RockContext();
            var pageService  = new PageService(rockContext);
            int parentPageId = SiteCache.Get(PageParameter("SiteId").AsInteger()).DefaultPageId.Value;

            var page = pageService.Get(PageParameter("Page").AsInteger());

            if (page == null)
            {
                page = new Rock.Model.Page();
                pageService.Add(page);

                var order = pageService.GetByParentPageId(parentPageId)
                            .OrderByDescending(p => p.Order)
                            .Select(p => p.Order)
                            .FirstOrDefault();
                page.Order        = order + 1;
                page.ParentPageId = parentPageId;
            }

            var additionalSettings = page.AdditionalSettings.FromJsonOrNull <Rock.Mobile.AdditionalPageSettings>() ?? new Rock.Mobile.AdditionalPageSettings();

            additionalSettings.LavaEventHandler = ceEventHandler.Text;

            page.InternalName       = tbInternalName.Text;
            page.BrowserTitle       = tbName.Text;
            page.PageTitle          = tbName.Text;
            page.Description        = tbDescription.Text;
            page.LayoutId           = ddlLayout.SelectedValueAsId().Value;
            page.DisplayInNavWhen   = cbDisplayInNavigation.Checked ? DisplayInNavWhen.WhenAllowed : DisplayInNavWhen.Never;
            page.AdditionalSettings = additionalSettings.ToJson();
            int?oldIconId = null;

            if (page.IconBinaryFileId != imgPageIcon.BinaryFileId)
            {
                oldIconId             = page.IconBinaryFileId;
                page.IconBinaryFileId = imgPageIcon.BinaryFileId;
            }

            rockContext.WrapTransaction(() =>
            {
                rockContext.SaveChanges();

                if (oldIconId.HasValue || page.IconBinaryFileId.HasValue)
                {
                    BinaryFileService binaryFileService = new BinaryFileService(rockContext);
                    if (oldIconId.HasValue)
                    {
                        var binaryFile = binaryFileService.Get(oldIconId.Value);
                        if (binaryFile != null)
                        {
                            // marked the old images as IsTemporary so they will get cleaned up later
                            binaryFile.IsTemporary = true;
                            rockContext.SaveChanges();
                        }
                    }

                    if (page.IconBinaryFileId.HasValue)
                    {
                        var binaryFile = binaryFileService.Get(page.IconBinaryFileId.Value);
                        if (binaryFile != null)
                        {
                            // marked the old images as IsTemporary so they will get cleaned up later
                            binaryFile.IsTemporary = false;
                            rockContext.SaveChanges();
                        }
                    }
                }
            });

            NavigateToCurrentPage(new Dictionary <string, string>
            {
                { "SiteId", PageParameter("SiteId") },
                { "Page", page.Id.ToString() }
            });
        }
        /// <summary>
        /// Recursively saves Pages and associated Blocks, PageRoutes and PageContexts.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="page">The current Page to save</param>
        /// <param name="newBlockTypes">List of BlockTypes not currently installed</param>
        /// <param name="parentPageId">Id of the the current Page's parent</param>
        /// <param name="siteId">Id of the site the current Page is being imported into</param>
        private void SavePages(RockContext rockContext, Page page, IEnumerable <BlockType> newBlockTypes, int parentPageId, int siteId)
        {
            rockContext = rockContext ?? new RockContext();

            // find layout
            var    layoutService = new LayoutService(rockContext);
            Layout layout        = new Layout();

            if (page.Layout != null)
            {
                layout = layoutService.GetBySiteId(siteId).Where(l => l.Name == page.Layout.Name && l.FileName == page.Layout.FileName).FirstOrDefault();
                if (layout == null)
                {
                    layout          = new Layout();
                    layout.FileName = page.Layout.FileName;
                    layout.Name     = page.Layout.Name;
                    layout.SiteId   = siteId;
                    layoutService.Add(layout);
                    rockContext.SaveChanges();
                }
            }
            else
            {
                layout = layoutService.GetBySiteId(siteId).Where(l => l.Name.Contains("Full") || l.Name.Contains("Home")).First();
            }
            int layoutId = layout.Id;

            // Force shallow copies on entities so save operations are more atomic and don't get hosed
            // by nested object references.
            var pg         = page.Clone(deepCopy: false);
            var blockTypes = newBlockTypes.ToList();

            pg.ParentPageId = parentPageId;
            pg.LayoutId     = layoutId;

            var pageService = new PageService(rockContext);

            pageService.Add(pg);
            rockContext.SaveChanges();

            var blockService = new BlockService(rockContext);

            foreach (var block in page.Blocks ?? new List <Block>())
            {
                var blockType = blockTypes.FirstOrDefault(bt => block.BlockType.Path == bt.Path);
                var b         = block.Clone(deepCopy: false);
                b.PageId = pg.Id;

                if (blockType != null)
                {
                    b.BlockTypeId = blockType.Id;
                }

                blockService.Add(b);
            }
            rockContext.SaveChanges();

            var pageRouteService = new PageRouteService(rockContext);

            foreach (var pageRoute in page.PageRoutes ?? new List <PageRoute>())
            {
                var pr = pageRoute.Clone(deepCopy: false);
                pr.PageId = pg.Id;
                pageRouteService.Add(pr);
            }
            rockContext.SaveChanges();

            var pageContextService = new PageContextService(rockContext);

            foreach (var pageContext in page.PageContexts ?? new List <PageContext>())
            {
                var pc = pageContext.Clone(deepCopy: false);
                pc.PageId = pg.Id;
                pageContextService.Add(pc);
            }
            rockContext.SaveChanges();

            foreach (var p in page.Pages ?? new List <Page>())
            {
                SavePages(rockContext, p, blockTypes, pg.Id, siteId);
            }
        }
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Site site;

            if (Page.IsValid)
            {
                var               rockContext       = new RockContext();
                PageService       pageService       = new PageService(rockContext);
                SiteService       siteService       = new SiteService(rockContext);
                SiteDomainService siteDomainService = new SiteDomainService(rockContext);
                bool              newSite           = false;

                int siteId = hfSiteId.Value.AsInteger();

                if (siteId == 0)
                {
                    newSite = true;
                    site    = new Rock.Model.Site();
                    siteService.Add(site);
                }
                else
                {
                    site = siteService.Get(siteId);
                }

                site.Name                      = tbSiteName.Text;
                site.Description               = tbDescription.Text;
                site.Theme                     = ddlTheme.Text;
                site.DefaultPageId             = ppDefaultPage.PageId;
                site.DefaultPageRouteId        = ppDefaultPage.PageRouteId;
                site.LoginPageId               = ppLoginPage.PageId;
                site.LoginPageRouteId          = ppLoginPage.PageRouteId;
                site.ChangePasswordPageId      = ppChangePasswordPage.PageId;
                site.ChangePasswordPageRouteId = ppChangePasswordPage.PageRouteId;
                site.CommunicationPageId       = ppCommunicationPage.PageId;
                site.CommunicationPageRouteId  = ppCommunicationPage.PageRouteId;
                site.RegistrationPageId        = ppRegistrationPage.PageId;
                site.RegistrationPageRouteId   = ppRegistrationPage.PageRouteId;
                site.PageNotFoundPageId        = ppPageNotFoundPage.PageId;
                site.PageNotFoundPageRouteId   = ppPageNotFoundPage.PageRouteId;
                site.ErrorPage                 = tbErrorPage.Text;
                site.GoogleAnalyticsCode       = tbGoogleAnalytics.Text;
                site.RequiresEncryption        = cbRequireEncryption.Checked;
                site.EnabledForShortening      = cbEnableForShortening.Checked;
                site.EnableMobileRedirect      = cbEnableMobileRedirect.Checked;
                site.MobilePageId              = ppMobilePage.PageId;
                site.ExternalUrl               = tbExternalURL.Text;
                site.AllowedFrameDomains       = tbAllowedFrameDomains.Text;
                site.RedirectTablets           = cbRedirectTablets.Checked;
                site.EnablePageViews           = cbEnablePageViews.Checked;
                site.IsActive                  = cbIsActive.Checked;
                site.AllowIndexing             = cbAllowIndexing.Checked;
                site.IsIndexEnabled            = cbEnableIndexing.Checked;
                site.IndexStartingLocation     = tbIndexStartingLocation.Text;

                site.PageHeaderContent = cePageHeaderContent.Text;

                int?existingIconId = null;
                if (site.FavIconBinaryFileId != imgSiteIcon.BinaryFileId)
                {
                    existingIconId           = site.FavIconBinaryFileId;
                    site.FavIconBinaryFileId = imgSiteIcon.BinaryFileId;
                }

                int?existingLogoId = null;
                if (site.SiteLogoBinaryFileId != imgSiteLogo.BinaryFileId)
                {
                    existingLogoId            = site.SiteLogoBinaryFileId;
                    site.SiteLogoBinaryFileId = imgSiteLogo.BinaryFileId;
                }

                var currentDomains = tbSiteDomains.Text.SplitDelimitedValues().ToList <string>();
                site.SiteDomains = site.SiteDomains ?? new List <SiteDomain>();

                // Remove any deleted domains
                foreach (var domain in site.SiteDomains.Where(w => !currentDomains.Contains(w.Domain)).ToList())
                {
                    site.SiteDomains.Remove(domain);
                    siteDomainService.Delete(domain);
                }

                int order = 0;
                foreach (string domain in currentDomains)
                {
                    SiteDomain sd = site.SiteDomains.Where(d => d.Domain == domain).FirstOrDefault();
                    if (sd == null)
                    {
                        sd        = new SiteDomain();
                        sd.Domain = domain;
                        sd.Guid   = Guid.NewGuid();
                        site.SiteDomains.Add(sd);
                    }
                    sd.Order = order++;
                }

                if (!site.DefaultPageId.HasValue && !newSite)
                {
                    ppDefaultPage.ShowErrorMessage("Default Page is required.");
                    return;
                }

                if (!site.IsValid)
                {
                    // Controls will render the error messages
                    return;
                }

                rockContext.WrapTransaction(() =>
                {
                    rockContext.SaveChanges();

                    SaveAttributes(new Page().TypeId, "SiteId", site.Id.ToString(), PageAttributesState, rockContext);

                    if (existingIconId.HasValue)
                    {
                        BinaryFileService binaryFileService = new BinaryFileService(rockContext);
                        var binaryFile = binaryFileService.Get(existingIconId.Value);
                        if (binaryFile != null)
                        {
                            // marked the old images as IsTemporary so they will get cleaned up later
                            binaryFile.IsTemporary = true;
                            rockContext.SaveChanges();
                        }
                    }

                    if (existingLogoId.HasValue)
                    {
                        BinaryFileService binaryFileService = new BinaryFileService(rockContext);
                        var binaryFile = binaryFileService.Get(existingLogoId.Value);
                        if (binaryFile != null)
                        {
                            // marked the old images as IsTemporary so they will get cleaned up later
                            binaryFile.IsTemporary = true;
                            rockContext.SaveChanges();
                        }
                    }

                    if (newSite)
                    {
                        Rock.Security.Authorization.CopyAuthorization(RockPage.Layout.Site, site, rockContext, Authorization.EDIT);
                        Rock.Security.Authorization.CopyAuthorization(RockPage.Layout.Site, site, rockContext, Authorization.ADMINISTRATE);
                        Rock.Security.Authorization.CopyAuthorization(RockPage.Layout.Site, site, rockContext, Authorization.APPROVE);
                    }
                });

                // add/update for the InteractionChannel for this site and set the RetentionPeriod
                var interactionChannelService   = new InteractionChannelService(rockContext);
                int channelMediumWebsiteValueId = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.INTERACTIONCHANNELTYPE_WEBSITE.AsGuid()).Id;
                var interactionChannelForSite   = interactionChannelService.Queryable()
                                                  .Where(a => a.ChannelTypeMediumValueId == channelMediumWebsiteValueId && a.ChannelEntityId == site.Id).FirstOrDefault();

                if (interactionChannelForSite == null)
                {
                    interactionChannelForSite = new InteractionChannel();
                    interactionChannelForSite.ChannelTypeMediumValueId = channelMediumWebsiteValueId;
                    interactionChannelForSite.ChannelEntityId          = site.Id;
                    interactionChannelService.Add(interactionChannelForSite);
                }

                interactionChannelForSite.Name = site.Name;
                interactionChannelForSite.RetentionDuration     = nbPageViewRetentionPeriodDays.Text.AsIntegerOrNull();
                interactionChannelForSite.ComponentEntityTypeId = EntityTypeCache.Get <Rock.Model.Page>().Id;

                rockContext.SaveChanges();


                // Create the default page is this is a new site
                if (!site.DefaultPageId.HasValue && newSite)
                {
                    var siteCache = SiteCache.Get(site.Id);

                    // Create the layouts for the site, and find the first one
                    LayoutService.RegisterLayouts(Request.MapPath("~"), siteCache);

                    var    layoutService = new LayoutService(rockContext);
                    var    layouts       = layoutService.GetBySiteId(siteCache.Id);
                    Layout layout        = layouts.FirstOrDefault(l => l.FileName.Equals("FullWidth", StringComparison.OrdinalIgnoreCase));
                    if (layout == null)
                    {
                        layout = layouts.FirstOrDefault();
                    }

                    if (layout != null)
                    {
                        var page = new Page();
                        page.LayoutId              = layout.Id;
                        page.PageTitle             = siteCache.Name + " Home Page";
                        page.InternalName          = page.PageTitle;
                        page.BrowserTitle          = page.PageTitle;
                        page.EnableViewState       = true;
                        page.IncludeAdminFooter    = true;
                        page.MenuDisplayChildPages = true;

                        var lastPage = pageService.GetByParentPageId(null).OrderByDescending(b => b.Order).FirstOrDefault();

                        page.Order = lastPage != null ? lastPage.Order + 1 : 0;
                        pageService.Add(page);

                        rockContext.SaveChanges();

                        site = siteService.Get(siteCache.Id);
                        site.DefaultPageId = page.Id;

                        rockContext.SaveChanges();
                    }
                }

                var qryParams = new Dictionary <string, string>();
                qryParams["siteId"] = site.Id.ToString();

                NavigateToPage(RockPage.Guid, qryParams);
            }
        }