Exemple #1
0
        /// <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)
        {
            PageRoute        pageRoute;
            var              rockContext      = new RockContext();
            PageRouteService pageRouteService = new PageRouteService(rockContext);

            int pageRouteId = int.Parse(hfPageRouteId.Value);

            if (pageRouteId == 0)
            {
                pageRoute = new PageRoute();
                pageRouteService.Add(pageRoute);
            }
            else
            {
                pageRoute = pageRouteService.Get(pageRouteId);
            }

            pageRoute.Route    = tbRoute.Text.Trim();
            pageRoute.IsGlobal = cbIsGlobal.Checked;

            int selectedPageId = int.Parse(ppPage.SelectedValue);

            pageRoute.PageId = selectedPageId;

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

            int?siteId    = null;
            var pageCache = PageCache.Get(selectedPageId);

            if (pageCache != null && pageCache.Layout != null)
            {
                siteId = pageCache.Layout.SiteId;
            }

            var duplicateRoutes = pageRouteService
                                  .Queryable().AsNoTracking()
                                  .Where(r =>
                                         r.Route == pageRoute.Route &&
                                         r.Id != pageRoute.Id);

            if (siteId.HasValue)
            {
                duplicateRoutes = duplicateRoutes
                                  .Where(r =>
                                         r.Page != null &&
                                         r.Page.Layout != null &&
                                         r.Page.Layout.SiteId == siteId.Value);
            }

            if (duplicateRoutes.Any())
            {
                // Duplicate
                nbErrorMessage.Title   = "Duplicate Route";
                nbErrorMessage.Text    = "<p>There is already an existing route with this name for the selected page's site. Route names must be unique per site. Please choose a different route name.</p>";
                nbErrorMessage.Visible = true;
            }
            else
            {
                pageRoute.LoadAttributes(rockContext);

                rockContext.WrapTransaction(() =>
                {
                    rockContext.SaveChanges();
                    if (!pageRoute.IsSystem)
                    {
                        Rock.Attribute.Helper.GetEditValues(phAttributes, pageRoute);
                        pageRoute.SaveAttributeValues(rockContext);
                    }
                });

                PageCache.FlushPage(pageCache.Id);

                Rock.Web.RockRouteHandler.ReregisterRoutes();
                NavigateToParentPage();
            }
        }