The data access/service class for the Rock.Model.Site entity. This inherits from the Service class
        /// <summary>
        /// Binds the group members grid.
        /// </summary>
        protected void BindLayoutsGrid()
        {
            pnlLayouts.Visible = false;

            int siteId = PageParameter( "siteId" ).AsInteger() ?? 0;
            if ( siteId == 0 )
            {
                // quit if the siteId can't be determined
                return;
            }

            hfSiteId.SetValue( siteId );

            pnlLayouts.Visible = true;

            // Add any missing layouts
            LayoutService.RegisterLayouts( Request.MapPath( "~" ), SiteCache.Read( siteId ) );

            LayoutService layoutService = new LayoutService( new RockContext() );
            var qry = layoutService.Queryable().Where( a => a.SiteId.Equals( siteId ) );

            SortProperty sortProperty = gLayouts.SortProperty;

            if ( sortProperty != null )
            {
                gLayouts.DataSource = qry.Sort( sortProperty ).ToList();
            }
            else
            {
                gLayouts.DataSource = qry.OrderBy( l => l.Name ).ToList();
            }

            gLayouts.DataBind();
        }
Example #2
0
        /// <summary>
        /// Registers any layouts in a particular site's theme folder that do not currently have any layouts registered in Rock.
        /// </summary>
        /// <param name="physWebAppPath">A <see cref="System.String" /> containing the physical path to Rock on the server.</param>
        /// <param name="site">The site.</param>
        public static void RegisterLayouts(string physWebAppPath, SiteCache site)
        {
            // Dictionary for block types.  Key is path, value is friendly name
            var list = new Dictionary <string, string>();

            // Find all the layouts in theme layout folder...
            string layoutFolder = Path.Combine(physWebAppPath, string.Format("Themes\\{0}\\Layouts", site.Theme));

            // search for all layouts (aspx files) under the physical path
            var           layoutFiles = new List <string>();
            DirectoryInfo di          = new DirectoryInfo(layoutFolder);

            if (di.Exists)
            {
                foreach (var file in di.GetFiles("*.aspx", SearchOption.AllDirectories))
                {
                    layoutFiles.Add(Path.GetFileNameWithoutExtension(file.Name));
                }
            }

            var rockContext   = new RockContext();
            var layoutService = new LayoutService(rockContext);

            // Get a list of the layout filenames already registered
            var registered = layoutService.GetBySiteId(site.Id).Select(l => l.FileName).Distinct().ToList();

            // for each unregistered layout
            foreach (string layoutFile in layoutFiles.Except(registered, StringComparer.CurrentCultureIgnoreCase))
            {
                // Create new layout record and save it
                Layout layout = new Layout();
                layout.SiteId   = site.Id;
                layout.FileName = layoutFile;
                layout.Name     = layoutFile.SplitCase();
                layout.Guid     = new Guid();

                layoutService.Add(layout);
            }

            rockContext.SaveChanges();
        }
Example #3
0
        /// <summary>
        /// Registers any layouts in a particular site's theme folder that do not currently have any layouts registered in Rock.
        /// </summary>
        /// <param name="physWebAppPath">A <see cref="System.String" /> containing the physical path to Rock on the server.</param>
        /// <param name="site">The site.</param>
        public static void RegisterLayouts( string physWebAppPath, SiteCache site )
        {
            // Dictionary for block types.  Key is path, value is friendly name
            var list = new Dictionary<string, string>();

            // Find all the layouts in theme layout folder...
            string layoutFolder = Path.Combine(physWebAppPath, string.Format("Themes\\{0}\\Layouts", site.Theme));

            // search for all layouts (aspx files) under the physical path
            var layoutFiles = new List<string>();
            DirectoryInfo di = new DirectoryInfo( layoutFolder );
            if ( di.Exists )
            {
                foreach(var file in di.GetFiles( "*.aspx", SearchOption.AllDirectories ))
                {
                    layoutFiles.Add( Path.GetFileNameWithoutExtension( file.Name ) );
                }
            }

            var rockContext = new RockContext();
            var layoutService = new LayoutService( rockContext );

            // Get a list of the layout filenames already registered
            var registered = layoutService.GetBySiteId( site.Id ).Select( l => l.FileName ).Distinct().ToList();

            // for each unregistered layout
            foreach ( string layoutFile in layoutFiles.Except( registered, StringComparer.CurrentCultureIgnoreCase ) )
            {
                // Create new layout record and save it
                Layout layout = new Layout();
                layout.SiteId = site.Id;
                layout.FileName = layoutFile;
                layout.Name = layoutFile.SplitCase();
                layout.Guid = new Guid();

                layoutService.Add( layout );
            }

            rockContext.SaveChanges();
        }
        /// <summary>
        /// Returns breadcrumbs specific to the block that should be added to navigation
        /// based on the current page reference.  This function is called during the page's
        /// oninit to load any initial breadcrumbs
        /// </summary>
        /// <param name="pageReference">The page reference.</param>
        /// <returns></returns>
        public override List<BreadCrumb> GetBreadCrumbs( PageReference pageReference )
        {
            var breadCrumbs = new List<BreadCrumb>();

            int? layoutId = PageParameter(pageReference, "layoutId" ).AsIntegerOrNull();
            if ( layoutId != null )
            {
                Layout layout = new LayoutService( new RockContext() ).Get( layoutId.Value );
                if ( layout != null )
                {
                    breadCrumbs.Add( new BreadCrumb( layout.Name, pageReference ) );
                }
                else
                {
                    breadCrumbs.Add( new BreadCrumb( "New Layout", pageReference ) );
                }
            }
            else
            {
                // don't show a breadcrumb if we don't have a pageparam to work with
            }

            return breadCrumbs;
        }
 /// <summary>
 /// Handles the Click event of the btnEdit 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 btnEdit_Click( object sender, EventArgs e )
 {
     var layout = new LayoutService( new RockContext() ).Get( int.Parse( hfLayoutId.Value ) );
     ShowEditDetails( layout );
 }
 /// <summary>
 /// Handles the Click event of the btnCancel 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 btnCancel_Click( object sender, EventArgs e )
 {
     if ( hfLayoutId.Value.Equals( "0" ) )
     {
         // Cancelling on Add
         Dictionary<string, string> qryString = new Dictionary<string, string>();
         qryString["siteId"] = hfSiteId.Value;
         NavigateToParentPage( qryString );
     }
     else
     {
         // Cancelling on Edit
         Layout layout = new LayoutService( new RockContext() ).Get( int.Parse( hfLayoutId.Value ) );
         ShowReadonlyDetails( layout );
     }
 }
Example #7
0
        /// <summary>
        /// Handles the Click event of the btnDeleteConfirm 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 btnDeleteConfirm_Click( object sender, EventArgs e )
        {
            bool canDelete = false;

            var rockContext = new RockContext();
            SiteService siteService = new SiteService( rockContext );
            Site site = siteService.Get( int.Parse( hfSiteId.Value ) );
            LayoutService layoutService = new LayoutService( rockContext );
            PageService pageService = new PageService( rockContext );
            PageViewService pageViewService = new PageViewService( rockContext );

            if ( site != null )
            {
                var sitePages = new List<int> {
                    site.DefaultPageId ?? -1,
                    site.LoginPageId ?? -1,
                    site.RegistrationPageId ?? -1,
                    site.PageNotFoundPageId ?? -1
                };

                foreach ( var pageView in pageViewService
                    .Queryable()
                    .Where( t =>
                        t.Page != null &&
                        t.Page.Layout != null &&
                        t.Page.Layout.SiteId == site.Id ) )
                {
                    pageView.Page = null;
                    pageView.PageId = null;
                }

                var pageQry = pageService.Queryable( "Layout" )
                    .Where( t =>
                        t.Layout.SiteId == site.Id ||
                        sitePages.Contains( t.Id ) );

                pageService.DeleteRange( pageQry );

                var layoutQry = layoutService.Queryable()
                    .Where( l =>
                        l.SiteId == site.Id );
                layoutService.DeleteRange( layoutQry );
                rockContext.SaveChanges( true );

                string errorMessage;
                canDelete = siteService.CanDelete( site, out errorMessage, includeSecondLvl: true );
                if ( !canDelete )
                {
                    mdDeleteWarning.Show( errorMessage, ModalAlertType.Alert );
                    return;
                }

                siteService.Delete( site );

                rockContext.SaveChanges();

                SiteCache.Flush( site.Id );
            }

            NavigateToParentPage();
        }
Example #8
0
        /// <summary>
        /// Handles the Click event of the DeleteLayout control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Rock.Web.UI.Controls.RowEventArgs" /> instance containing the event data.</param>
        protected void DeleteLayout_Click( object sender, Rock.Web.UI.Controls.RowEventArgs e )
        {
            RockTransactionScope.WrapTransaction( () =>
            {
                LayoutService layoutService = new LayoutService();
                Layout layout = layoutService.Get( e.RowKeyId );
                if ( layout != null )
                {
                    string errorMessage;
                    if ( !layoutService.CanDelete( layout, out errorMessage ) )
                    {
                        mdGridWarning.Show( errorMessage, ModalAlertType.Information );
                        return;
                    }

                    int siteId = layout.SiteId;

                    layoutService.Delete( layout, CurrentPersonId );
                    layoutService.Save( layout, CurrentPersonId );

                    LayoutCache.Flush( e.RowKeyId );
                }
            } );

            BindLayoutsGrid();
        }
        /// <summary>
        /// Loads the layouts.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="site">The site.</param>
        private void LoadLayouts( RockContext rockContext, SiteCache site )
        {
            LayoutService.RegisterLayouts( Request.MapPath( "~" ), site );

            ddlLayout.Items.Clear();
            var layoutService = new LayoutService( rockContext );
            foreach ( var layout in layoutService.GetBySiteId( site.Id ) )
            {
                ddlLayout.Items.Add( new ListItem( layout.Name, layout.Id.ToString() ) );
            }
        }
        /// <summary>
        /// Shows the detail.
        /// </summary>
        /// <param name="layoutId">The layout identifier.</param>
        /// <param name="siteId">The group id.</param>
        public void ShowDetail( int layoutId, int? siteId )
        {
            Layout layout = null;

            if ( !layoutId.Equals( 0 ) )
            {
                layout = new LayoutService( new RockContext() ).Get( layoutId );
            }

            if (layout == null && siteId.HasValue)
            {
                var site = SiteCache.Read( siteId.Value );
                if ( site != null )
                {
                    layout = new Layout { Id = 0 };
                    layout.SiteId = siteId.Value;
                }
            }

            if (layout == null)
            {
                pnlDetails.Visible = false;
                return;
            }

            hfSiteId.Value = layout.SiteId.ToString();
            hfLayoutId.Value = layout.Id.ToString();

            bool readOnly = false;

            nbEditModeMessage.Text = string.Empty;
            if ( !IsUserAuthorized( Authorization.EDIT ) )
            {
                readOnly = true;
                nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed( Rock.Model.Layout.FriendlyTypeName );
            }

            if ( layout.IsSystem )
            {
                nbEditModeMessage.Text = EditModeMessage.System( Rock.Model.Layout.FriendlyTypeName );
            }

            if ( readOnly )
            {
                btnEdit.Visible = false;
                //btnDelete.Visible = false;
                ShowReadonlyDetails( layout );
            }
            else
            {
                btnEdit.Visible = true;
                //btnDelete.Visible = !layout.IsSystem;
                if ( layout.Id > 0 )
                {
                    ShowReadonlyDetails( layout );
                }
                else
                {
                    ShowEditDetails( layout );
                }
            }
        }
Example #11
0
        /// <summary>
        /// Shows the detail.
        /// </summary>
        /// <param name="itemKey">The item key.</param>
        /// <param name="itemKeyValue">The item key value.</param>
        /// <param name="siteId">The group id.</param>
        public void ShowDetail( string itemKey, int itemKeyValue, int? siteId )
        {
            if ( !itemKey.Equals( "layoutId" ) )
            {
                return;
            }

            Layout layout = null;

            if ( !itemKeyValue.Equals( 0 ) )
            {
                layout = new LayoutService().Get( itemKeyValue );
            }
            else
            {
                // only create a new one if parent was specified
                if ( siteId.HasValue )
                {
                    layout = new Layout { Id = 0 };
                    layout.SiteId = siteId.Value;
                }
            }

            if ( layout == null )
            {
                return;
            }

            hfSiteId.Value = layout.SiteId.ToString();
            hfLayoutId.Value = layout.Id.ToString();

            if ( layout.Id.Equals( 0 ) )
            {
                lReadOnlyTitle.Text = ActionTitle.Add( Rock.Model.Layout.FriendlyTypeName ).FormatAsHtmlTitle();
            }
            else
            {
                lReadOnlyTitle.Text = layout.Name.FormatAsHtmlTitle();
            }

            LoadDropDowns();

            tbLayoutName.Text = layout.Name;
            tbDescription.Text = layout.Description;
            ddlLayout.SetValue( layout.FileName );

        }
Example #12
0
        /// <summary>
        /// Handles the Click event of the btnCancel 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 btnCancel_Click( object sender, EventArgs e )
        {
            if ( hfLayoutId.Value.Equals( "0" ) )
            {
                // Cancelling on Add
                Dictionary<string, string> qryString = new Dictionary<string, string>();
                qryString["siteId"] = hfSiteId.Value;
                NavigateToParentPage( qryString );
            }
            else
            {
                // Cancelling on Edit
                LayoutService layoutService = new LayoutService();
                Layout layout = layoutService.Get( int.Parse( hfLayoutId.Value ) );

                Dictionary<string, string> qryString = new Dictionary<string, string>();
                qryString["siteId"] = layout.SiteId.ToString();
                NavigateToParentPage( qryString );
            }
        }
Example #13
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 )
        {
            if ( Page.IsValid )
            {
                LayoutService layoutService = new LayoutService();
                Layout layout;

                int layoutId = int.Parse( hfLayoutId.Value );

                // if adding a new layout 
                if ( layoutId.Equals( 0 ) )
                {
                    layout = new Layout { Id = 0 };
                    layout.SiteId = hfSiteId.ValueAsInt();
                }
                else
                {
                    //load existing group member
                    layout = layoutService.Get( layoutId );
                }

                layout.Name = tbLayoutName.Text;
                layout.Description = tbDescription.Text;
                layout.FileName = ddlLayout.SelectedValue;

                if ( !layout.IsValid )
                {
                    return;
                }

                RockTransactionScope.WrapTransaction( () =>
                {
                    if ( layout.Id.Equals( 0 ) )
                    {
                        layoutService.Add( layout, CurrentPersonId );
                    }

                    layoutService.Save( layout, CurrentPersonId );
                } );

                LayoutCache.Flush( layout.Id );

                Dictionary<string, string> qryString = new Dictionary<string, string>();
                qryString["siteId"] = hfSiteId.Value;
                NavigateToParentPage( qryString );
            }
        }
Example #14
0
 private void LoadLayouts(SiteCache Site)
 {
     ddlLayout.Items.Clear();
     var layoutService = new LayoutService();
     layoutService.RegisterLayouts( Request.MapPath( "~" ), Site, CurrentPersonId );
     foreach ( var layout in layoutService.GetBySiteId( Site.Id ) )
     {
         ddlLayout.Items.Add( new ListItem( layout.Name, layout.Id.ToString() ) );
     }
 }
        /// <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 )
        {
            if ( Page.IsValid )
            {
                var rockContext = new RockContext();
                LayoutService layoutService = new LayoutService( rockContext );
                Layout layout;

                int layoutId = int.Parse( hfLayoutId.Value );

                // if adding a new layout
                if ( layoutId.Equals( 0 ) )
                {
                    layout = new Layout { Id = 0 };
                    layout.SiteId = hfSiteId.ValueAsInt();
                }
                else
                {
                    //load existing group member
                    layout = layoutService.Get( layoutId );
                }

                layout.Name = tbLayoutName.Text;
                layout.Description = tbDescription.Text;
                layout.FileName = ddlLayout.SelectedValue;

                if ( !layout.IsValid )
                {
                    return;
                }

                if ( layout.Id.Equals( 0 ) )
                {
                    layoutService.Add( layout );
                }

                rockContext.SaveChanges();

                LayoutCache.Flush( layout.Id );

                Dictionary<string, string> qryParams = new Dictionary<string, string>();
                qryParams["layoutId"] = layout.Id.ToString();
                NavigateToPage( RockPage.Guid, qryParams );
            }
        }
        /// <summary>
        /// Shows the detail.
        /// </summary>
        /// <param name="itemKey">The item key.</param>
        /// <param name="itemKeyValue">The item key value.</param>
        /// <param name="siteId">The group id.</param>
        public void ShowDetail( string itemKey, int itemKeyValue, int? siteId )
        {
            if ( !itemKey.Equals( "layoutId" ) )
            {
                return;
            }

            Layout layout = null;

            if ( !itemKeyValue.Equals( 0 ) )
            {
                layout = new LayoutService( new RockContext() ).Get( itemKeyValue );
            }
            else
            {
                // only create a new one if parent was specified
                if ( siteId.HasValue )
                {
                    layout = new Layout { Id = 0 };
                    layout.SiteId = siteId.Value;
                }
            }

            if ( layout == null )
            {
                return;
            }

            hfSiteId.Value = layout.SiteId.ToString();
            hfLayoutId.Value = layout.Id.ToString();

            bool readOnly = false;

            nbEditModeMessage.Text = string.Empty;
            if ( !IsUserAuthorized( Authorization.EDIT ) )
            {
                readOnly = true;
                nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed( Rock.Model.Layout.FriendlyTypeName );
            }

            if ( layout.IsSystem )
            {
                nbEditModeMessage.Text = EditModeMessage.System( Rock.Model.Layout.FriendlyTypeName );
            }

            if ( readOnly )
            {
                btnEdit.Visible = false;
                //btnDelete.Visible = false;
                ShowReadonlyDetails( layout );
            }
            else
            {
                btnEdit.Visible = true;
                //btnDelete.Visible = !layout.IsSystem;
                if ( layout.Id > 0 )
                {
                    ShowReadonlyDetails( layout );
                }
                else
                {
                    ShowEditDetails( layout );
                }
            }
        }
Example #17
0
        /// <summary>
        /// Recursively saves Pages and associated Blocks, PageRoutes and PageContexts.
        /// </summary>
        /// <param name="page">The current Page to save</param>
        /// <param name="newBlockTypes">List of BlockTypes not currently installed</param>
        /// <param name="personId">Id of the Person performing the "Import" operation</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( Page page, IEnumerable<BlockType> newBlockTypes, int personId, int parentPageId, int siteId )
        {
            // find layout
            var layoutService = new LayoutService();
            var 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, null );
                layoutService.Save( layout, null );
            }
            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();
            pageService.Add( pg, personId );
            pageService.Save( pg, personId );

            var blockService = new BlockService();

            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, personId );
                blockService.Save( b, personId );
            }

            var pageRouteService = new PageRouteService();

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

            var pageContextService = new PageContextService();

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

            foreach ( var p in page.Pages ?? new List<Page>() )
            {
                SavePages( p, blockTypes, personId, pg.Id, siteId );
            }
        }
Example #18
0
 /// <summary>
 /// Binds the filter.
 /// </summary>
 private void BindFilter()
 {
     int siteId = PageParameter( "siteId" ).AsInteger();
     if ( siteId == 0 )
     {
         // quit if the siteId can't be determined
         return;
     }
     LayoutService.RegisterLayouts( Request.MapPath( "~" ), SiteCache.Read( siteId ) );
     LayoutService layoutService = new LayoutService( new RockContext() );
     var layouts = layoutService.Queryable().Where( a => a.SiteId.Equals( siteId ) ).ToList();
     ddlLayoutFilter.DataSource = layouts;
     ddlLayoutFilter.DataBind();
     ddlLayoutFilter.Items.Insert( 0, Rock.Constants.All.ListItem );
     ddlLayoutFilter.Visible = layouts.Any();
     ddlLayoutFilter.SetValue( gPagesFilter.GetUserPreference( "Layout" ) );
 }
        /// <summary>
        /// Handles the Click event of the DeleteLayout control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Rock.Web.UI.Controls.RowEventArgs" /> instance containing the event data.</param>
        protected void DeleteLayout_Click( object sender, Rock.Web.UI.Controls.RowEventArgs e )
        {
            bool canDelete = false;

            var rockContext = new RockContext();
            LayoutService layoutService = new LayoutService(rockContext);

            Layout layout = layoutService.Get( e.RowKeyId );
            if ( layout != null )
            {
                string errorMessage;
                canDelete = layoutService.CanDelete( layout, out errorMessage );
                if ( !canDelete )
                {
                    mdGridWarning.Show( errorMessage, ModalAlertType.Alert );
                    return;
                }

                int siteId = layout.SiteId;

                layoutService.Delete( layout );
                rockContext.SaveChanges();

                LayoutCache.Flush( e.RowKeyId );
            }

            BindLayoutsGrid();
        }
        /// <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 = int.Parse( hfSiteId.Value );

                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.FacebookAppId = tbFacebookAppId.Text;
                site.FacebookAppSecret = tbFacebookAppSecret.Text;

                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 );
                    }
                } );

                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 );
            }
        }
Example #21
0
        /// <summary>
        /// Binds the pages grid.
        /// </summary>
        protected void BindPagesGrid()
        {
            pnlPages.Visible = false;
            int siteId = PageParameter( "siteId" ).AsInteger() ?? 0;
            if ( siteId == 0 )
            {
                // quit if the siteId can't be determined
                return;
            }

            hfSiteId.SetValue( siteId );
            pnlPages.Visible = true;

            LayoutService layoutService = new LayoutService();
            layoutService.RegisterLayouts( Request.MapPath( "~" ), SiteCache.Read( siteId ), CurrentPersonId );
            var layouts = layoutService.Queryable().Where( a => a.SiteId.Equals( siteId ) ).Select( a => a.Id ).ToList();

            var siteService = new SiteService();
            var pageId = siteService.Get( siteId ).DefaultPageId;

            var pageService = new PageService();
            var qry = pageService.GetAllDescendents( (int)pageId ).AsQueryable().Where( a => layouts.Contains(a.LayoutId) );

            string layoutFilter = gPagesFilter.GetUserPreference( "Layout" );
            if ( !string.IsNullOrWhiteSpace( layoutFilter ) && layoutFilter != Rock.Constants.All.Text )
            {
                qry = qry.Where( a => a.Layout.ToString() == layoutFilter );
            }

            SortProperty sortProperty = gPages.SortProperty;
            if ( sortProperty != null )
            {
                qry = qry.Sort( sortProperty );
            }
            else
            {
                qry = qry.OrderBy( q => q.Id );
            }

            gPages.DataSource = qry.ToList();
            gPages.DataBind();
        }