/// <summary> /// Handles the Delete event of the gSites control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param> protected void gSites_Delete( object sender, RowEventArgs e ) { bool canDelete = false; var rockContext = new RockContext(); SiteService siteService = new SiteService( rockContext ); Site site = siteService.Get( e.RowKeyId ); if ( site != null ) { string errorMessage; canDelete = siteService.CanDelete( site, out errorMessage, includeSecondLvl: true ); if ( !canDelete ) { mdGridWarning.Show( errorMessage, ModalAlertType.Alert ); return; } siteService.Delete( site ); rockContext.SaveChanges(); SiteCache.Flush( site.Id ); } BindGrid(); }
/// <summary> /// Shows the edit. /// </summary> /// <param name="siteId">The site id.</param> public void ShowDetail( int siteId ) { pnlDetails.Visible = false; Site site = null; if ( !siteId.Equals( 0 ) ) { site = new SiteService( new RockContext() ).Get( siteId ); } if (site == null) { site = new Site { Id = 0 }; site.SiteDomains = new List<SiteDomain>(); site.Theme = RockPage.Layout.Site.Theme; } pnlDetails.Visible = true; hfSiteId.Value = site.Id.ToString(); bool readOnly = false; nbEditModeMessage.Text = string.Empty; if ( !IsUserAuthorized( Authorization.EDIT ) ) { readOnly = true; nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed( Rock.Model.Site.FriendlyTypeName ); } if ( site.IsSystem ) { nbEditModeMessage.Text = EditModeMessage.System( Rock.Model.Site.FriendlyTypeName ); } if ( readOnly ) { btnEdit.Visible = false; btnDelete.Visible = false; ShowReadonlyDetails( site ); } else { btnEdit.Visible = true; btnDelete.Visible = !site.IsSystem; if ( site.Id > 0 ) { ShowReadonlyDetails( site ); } else { ShowEditDetails( site ); } } }
/// <summary> /// Creates the control(s) neccessary for prompting user for a new value /// </summary> /// <param name="configurationValues">The configuration values.</param> /// <param name="id"></param> /// <returns> /// The control /// </returns> public override System.Web.UI.Control EditControl( Dictionary<string, ConfigurationValue> configurationValues, string id ) { var editControl = new RockDropDownList { ID = id }; SiteService siteService = new SiteService( new RockContext() ); var siteList = siteService.Queryable().OrderBy( a => a.Name ).ToList(); if ( siteList.Any() ) { foreach ( var site in siteList ) { editControl.Items.Add( new ListItem( site.Name, site.Id.ToString() ) ); } return editControl; } return null; }
/// <summary> /// Handles the Delete event of the gSites control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param> protected void gSites_Delete( object sender, RowEventArgs e ) { SiteService siteService = new SiteService(); Site site = siteService.Get( (int)gSites.DataKeys[e.RowIndex]["id"] ); if ( CurrentBlock != null ) { siteService.Delete( site, CurrentPersonId ); siteService.Save( site, CurrentPersonId ); SiteCache.Flush( site.Id ); } BindGrid(); }
/// <summary> /// Handles the Delete event of the gPages control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param> protected void gPages_Delete( object sender, RowEventArgs e ) { var rockContext = new RockContext(); PageService pageService = new PageService( rockContext ); var pageViewService = new PageViewService(rockContext); var siteService = new SiteService(rockContext); Rock.Model.Page page = pageService.Get( new Guid( e.RowKeyValue.ToString() ) ); if ( page != null ) { string errorMessage; if ( !pageService.CanDelete( page, out errorMessage, includeSecondLvl: true ) ) { mdGridWarning.Show( errorMessage, ModalAlertType.Alert ); return; } foreach (var site in siteService.Queryable()) { if (site.DefaultPageId == page.Id) { site.DefaultPageId = null; site.DefaultPageRouteId = null; } if (site.LoginPageId == page.Id) { site.LoginPageId = null; site.LoginPageRouteId = null; } if (site.RegistrationPageId == page.Id) { site.RegistrationPageId = null; site.RegistrationPageRouteId = null; } } foreach (var pageView in pageViewService.GetByPageId(page.Id)) { pageView.Page = null; pageView.PageId = null; } pageService.Delete( page ); rockContext.SaveChanges(); PageCache.Flush( page.Id ); } BindPagesGrid(); }
/// <summary> /// Binds the pages grid. /// </summary> protected void BindPagesGrid() { pnlPages.Visible = false; int siteId = PageParameter( "siteId" ).AsInteger(); if ( siteId == 0 ) { // quit if the siteId can't be determined return; } hfSiteId.SetValue( siteId ); pnlPages.Visible = true; // Question: Is this RegisterLayouts necessary here? Since if it's a new layout // there would not be any pages on them (which is our concern here). // It seems like it should be the concern of some other part of the puzzle. LayoutService.RegisterLayouts( Request.MapPath( "~" ), SiteCache.Read( siteId ) ); //var layouts = layoutService.Queryable().Where( a => a.SiteId.Equals( siteId ) ).Select( a => a.Id ).ToList(); // Find all the pages that are related to this site... // 1) pages used by one of this site's layouts and // 2) the site's 'special' pages used directly by the site. var rockContext = new RockContext(); var siteService = new SiteService( rockContext ); var pageService = new PageService( rockContext ); var site = siteService.Get( siteId ); if ( site != null ) { var sitePages = new List<int> { site.DefaultPageId ?? -1, site.LoginPageId ?? -1, site.RegistrationPageId ?? -1, site.PageNotFoundPageId ?? -1 }; var qry = pageService.Queryable("Layout") .Where( t => t.Layout.SiteId == siteId || sitePages.Contains( t.Id ) ); 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( t => t.Layout.Name ) .ThenBy( t => t.InternalName ); } gPages.DataSource = qry.ToList(); gPages.DataBind(); } }
/// <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(); }
/// <summary> /// Binds the filter. /// </summary> private void BindFilter() { var rockContext = new RockContext(); var sites = new SiteService( rockContext ).Queryable().OrderBy( s => s.Name ).ToList(); ddlSiteFilter.DataSource = sites; ddlSiteFilter.DataBind(); ddlSiteFilter.Items.Insert( 0, Rock.Constants.All.ListItem ); ddlSiteFilter.Visible = sites.Any(); ddlSiteFilter.SetValue( gContentListFilter.GetUserPreference( "Site" ) ); var item = ddlApprovedFilter.Items.FindByValue( gContentListFilter.GetUserPreference( "Approval Status" ) ); if ( item != null ) { item.Selected = true; } else { ddlApprovedFilter.SelectedIndex = 2; } int? personId = gContentListFilter.GetUserPreference( "Approved By" ).AsIntegerOrNull(); if ( personId.HasValue ) { var personService = new PersonService( rockContext ); var person = personService.Get( personId.Value ); if ( person != null ) { ppApprovedByFilter.SetValue( person ); } } }
protected void rGrid_Delete( object sender, RowEventArgs e ) { var rockContext = new RockContext(); var pageService = new PageService( rockContext ); var pageViewService = new PageViewService( rockContext ); var siteService = new SiteService( rockContext ); var page = pageService.Get( (int)rGrid.DataKeys[e.RowIndex]["id"] ); if ( page != null ) { string errorMessage = string.Empty; if ( !pageService.CanDelete( page, out errorMessage ) ) { //errorMessage = "The page is the parent page of another page."; mdDeleteWarning.Show( errorMessage, ModalAlertType.Alert ); return; } foreach ( var site in siteService.Queryable() ) { if ( site.DefaultPageId == page.Id ) { site.DefaultPageId = null; site.DefaultPageRouteId = null; } if ( site.LoginPageId == page.Id ) { site.LoginPageId = null; site.LoginPageRouteId = null; } if ( site.RegistrationPageId == page.Id ) { site.RegistrationPageId = null; site.RegistrationPageRouteId = null; } } // TODO: Could be thousands of page views. Can we set this up as cascade? foreach( var pageView in pageViewService.GetByPageId(page.Id)) { pageViewService.Delete( pageView ); } pageService.Delete( page ); rockContext.SaveChanges(); Rock.Web.Cache.PageCache.Flush( page.Id ); if ( _page != null ) { _page.FlushChildPages(); } } BindGrid(); }
/// <summary> /// Handles the Delete event of the gSites control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param> protected void gSites_Delete( object sender, RowEventArgs e ) { RockTransactionScope.WrapTransaction( () => { SiteService siteService = new SiteService(); Site site = siteService.Get( (int)e.RowKeyValue ); if ( site != null ) { string errorMessage; if ( !siteService.CanDelete( site, out errorMessage ) ) { mdGridWarning.Show( errorMessage, ModalAlertType.Information ); return; } siteService.Delete( site, CurrentPersonId ); siteService.Save( site, CurrentPersonId ); SiteCache.Flush( site.Id ); } } ); BindGrid(); }
/// <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; SiteDomain sd; bool newSite = false; using ( new UnitOfWorkScope() ) { SiteService siteService = new SiteService(); SiteDomainService siteDomainService = new SiteDomainService(); int siteId = 0; if ( !int.TryParse( hfSiteId.Value, out siteId ) ) { siteId = 0; } if ( siteId == 0 ) { newSite = true; site = new Rock.Model.Site(); siteService.Add( site, CurrentPersonId ); } else { site = siteService.Get( siteId ); foreach ( var domain in site.SiteDomains.ToList() ) { siteDomainService.Delete( domain, CurrentPersonId ); } site.SiteDomains.Clear(); } site.Name = tbSiteName.Text; site.Description = tbDescription.Text; site.Theme = ddlTheme.Text; site.DefaultPageId = int.Parse( ddlDefaultPage.SelectedValue ); foreach ( string domain in tbSiteDomains.Text.SplitDelimitedValues() ) { sd = new SiteDomain(); sd.Domain = domain; sd.Guid = Guid.NewGuid(); site.SiteDomains.Add( sd ); } site.FaviconUrl = tbFaviconUrl.Text; site.AppleTouchIconUrl = tbAppleTouchIconUrl.Text; site.FacebookAppId = tbFacebookAppId.Text; site.FacebookAppSecret = tbFacebookAppSecret.Text; if ( !site.IsValid ) { // Controls will render the error messages return; } siteService.Save( site, CurrentPersonId ); if ( newSite ) { Rock.Security.Authorization.CopyAuthorization( CurrentPage.Site, site, CurrentPersonId ); } SiteCache.Flush( site.Id ); BindGrid(); pnlDetails.Visible = false; pnlList.Visible = true; } }
/// <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 ( hfSiteId.Value.Equals( "0" ) ) { // Cancelling on Add return to site list NavigateToParentPage(); } else { // Cancelling on edit, return to details var site = new SiteService(new RockContext()).Get( int.Parse( hfSiteId.Value ) ); ShowReadonlyDetails( site ); } }
/// <summary> /// Cleans up PagesViews for sites that have a Page View retention period /// </summary> /// <param name="dataMap">The data map.</param> private int CleanupPageViews( JobDataMap dataMap ) { var pageViewRockContext = new Rock.Data.RockContext(); var currentDateTime = RockDateTime.Now; var siteService = new SiteService( pageViewRockContext ); var siteQry = siteService.Queryable().Where( a => a.PageViewRetentionPeriodDays.HasValue ); int totalRowsDeleted = 0; // foreach ( var site in siteQry.ToList() ) { var retentionCutoffDateTime = currentDateTime.AddDays( -site.PageViewRetentionPeriodDays.Value ); if ( retentionCutoffDateTime < System.Data.SqlTypes.SqlDateTime.MinValue.Value ) { retentionCutoffDateTime = System.Data.SqlTypes.SqlDateTime.MinValue.Value; } // delete in chunks (see http://dba.stackexchange.com/questions/1750/methods-of-speeding-up-a-huge-delete-from-table-with-no-clauses) bool keepDeleting = true; while ( keepDeleting ) { var dbTransaction = pageViewRockContext.Database.BeginTransaction(); try { string sqlCommand = @"DELETE TOP (1000) FROM [PageView] WHERE [SiteId] = @siteId AND DateTimeViewed < @retentionCutoffDateTime"; int rowsDeleted = pageViewRockContext.Database.ExecuteSqlCommand( sqlCommand, new SqlParameter( "siteId", site.Id ), new SqlParameter( "retentionCutoffDateTime", retentionCutoffDateTime ) ); keepDeleting = rowsDeleted > 0; totalRowsDeleted += rowsDeleted; } finally { dbTransaction.Commit(); } } } return totalRowsDeleted; }
/// <summary> /// Handles the Delete event of the rGrid control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param> protected void rGrid_Delete( object sender, RowEventArgs e ) { var rockContext = new RockContext(); var pageService = new PageService( rockContext ); var pageViewService = new PageViewService( rockContext ); var siteService = new SiteService( rockContext ); var page = pageService.Get( e.RowKeyId ); if ( page != null ) { string errorMessage = string.Empty; if ( !pageService.CanDelete( page, out errorMessage ) ) { mdDeleteWarning.Show( errorMessage, ModalAlertType.Alert ); return; } foreach ( var site in siteService.Queryable() ) { if ( site.DefaultPageId == page.Id ) { site.DefaultPageId = null; site.DefaultPageRouteId = null; } if ( site.LoginPageId == page.Id ) { site.LoginPageId = null; site.LoginPageRouteId = null; } if ( site.RegistrationPageId == page.Id ) { site.RegistrationPageId = null; site.RegistrationPageRouteId = null; } } foreach ( var pageView in pageViewService.GetByPageId( page.Id ) ) { pageView.Page = null; pageView.PageId = null; } pageService.Delete( page ); rockContext.SaveChanges(); Rock.Web.Cache.PageCache.Flush( page.Id ); if ( _page != null ) { _page.FlushChildPages(); } } BindGrid(); }
/// <summary> /// Binds the grid. /// </summary> private void BindGrid() { var rockContext = new RockContext(); var htmlContentService = new HtmlContentService( rockContext ); var htmlContent = htmlContentService.Queryable(); string pageName = ""; string siteName = ""; var htmlList = new List<HtmlApproval>(); foreach ( var content in htmlContent ) { content.Block.LoadAttributes(); var blah = content.Block.GetAttributeValue( "RequireApproval" ); if ( !string.IsNullOrEmpty( blah ) && blah.ToLower() == "true" ) { var pageService = new PageService( rockContext ); if ( content.Block.PageId != null ) { var page = pageService.Get( (int)content.Block.PageId ); if ( page != null ) { pageName = page.InternalName; while ( page.ParentPageId != null ) { page = pageService.Get( (int)page.ParentPageId ); } var siteService = new SiteService( rockContext ); siteName = siteService.GetByDefaultPageId( page.Id ).Select( s => s.Name ).FirstOrDefault(); } } var htmlApprovalClass = new HtmlApproval(); htmlApprovalClass.SiteName = siteName; htmlApprovalClass.PageName = pageName; htmlApprovalClass.Block = content.Block; htmlApprovalClass.BlockId = content.BlockId; htmlApprovalClass.Content = content.Content; htmlApprovalClass.Id = content.Id; htmlApprovalClass.IsApproved = content.IsApproved; htmlApprovalClass.ApprovedByPerson = content.ApprovedByPerson; htmlApprovalClass.ApprovedByPersonId = content.ApprovedByPersonId; htmlApprovalClass.ApprovedDateTime = content.ApprovedDateTime; htmlList.Add( htmlApprovalClass ); } } // Filter by Site if ( ddlSiteFilter.SelectedIndex > 0 ) { if ( ddlSiteFilter.SelectedValue.ToLower() != "all" ) { htmlList = htmlList.Where( h => h.SiteName == ddlSiteFilter.SelectedValue ).ToList(); } } // Filter by approved/unapproved if ( ddlApprovedFilter.SelectedIndex > -1 ) { if ( ddlApprovedFilter.SelectedValue.ToLower() == "unapproved" ) { htmlList = htmlList.Where( a => a.IsApproved == false ).ToList(); } else if ( ddlApprovedFilter.SelectedValue.ToLower() == "approved" ) { htmlList = htmlList.Where( a => a.IsApproved == true ).ToList(); } } // Filter by the person that approved the content if ( _canApprove ) { int personId = 0; if ( int.TryParse( gContentListFilter.GetUserPreference( "Approved By" ), out personId ) && personId != 0 ) { htmlList = htmlList.Where( a => a.ApprovedByPersonId.HasValue && a.ApprovedByPersonId.Value == personId ).ToList(); } } SortProperty sortProperty = gContentList.SortProperty; if ( sortProperty != null ) { gContentList.DataSource = htmlList.AsQueryable().Sort( sortProperty ).ToList(); } else { gContentList.DataSource = htmlList.OrderBy( h => h.Id ).ToList(); } gContentList.DataBind(); }
/// <summary> /// Build filter values/summary with user friendly data from filters /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The e.</param> protected void fExceptionList_DisplayFilterValue( object sender, GridFilter.DisplayFilterValueArgs e ) { switch ( e.Key ) { case "Site": int siteId; if ( int.TryParse( e.Value, out siteId ) ) { SiteService siteService = new SiteService(); var site = siteService.Get( siteId ); if ( site != null ) { e.Value = site.Name; } } break; case "Page": int pageId; if ( int.TryParse( e.Value, out pageId ) ) { PageService pageService = new PageService(); var page = pageService.Get( pageId ); if ( page != null ) { e.Value = page.InternalName; } } break; case "User": int userPersonId; if ( int.TryParse( e.Value, out userPersonId ) ) { PersonService personService = new PersonService(); var user = personService.Get( userPersonId ); if ( user != null ) { e.Value = user.FullName; } } break; } }
protected void rGrid_Delete( object sender, RowEventArgs e ) { using ( new UnitOfWorkScope() ) { var pageService = new PageService(); var siteService = new SiteService(); var page = pageService.Get( (int)rGrid.DataKeys[e.RowIndex]["id"] ); if ( page != null ) { RockTransactionScope.WrapTransaction( () => { foreach ( var site in siteService.Queryable() ) { bool updateSite = false; if (site.DefaultPageId == page.Id) { site.DefaultPageId = null; site.DefaultPageRouteId = null; updateSite = true; } if (site.LoginPageId == page.Id) { site.LoginPageId = null; site.LoginPageRouteId = null; updateSite = true; } if (site.RegistrationPageId == page.Id) { site.RegistrationPageId = null; site.RegistrationPageRouteId = null; updateSite = true; } if (updateSite) { siteService.Save( site, CurrentPersonId ); } } pageService.Delete( page, CurrentPersonId ); pageService.Save( page, CurrentPersonId ); Rock.Web.Cache.PageCache.Flush( page.Id ); if ( _page != null ) _page.FlushChildPages(); } ); } } BindGrid(); }
/// <summary> /// Shows the edit. /// </summary> /// <param name="siteId">The site id.</param> protected void ShowEdit( int siteId ) { SiteService siteService = new SiteService(); Site site = siteService.Get( siteId ); if ( site != null ) { lAction.Text = "Edit"; hfSiteId.Value = site.Id.ToString(); tbSiteName.Text = site.Name; tbDescription.Text = site.Description; ddlTheme.SetValue( site.Theme ); if ( site.DefaultPageId.HasValue ) { ddlDefaultPage.SelectedValue = site.DefaultPageId.Value.ToString(); } tbSiteDomains.Text = string.Join( "\n", site.SiteDomains.Select( dom => dom.Domain ).ToArray() ); tbFaviconUrl.Text = site.FaviconUrl; tbAppleTouchIconUrl.Text = site.AppleTouchIconUrl; tbFacebookAppId.Text = site.FacebookAppId; tbFacebookAppSecret.Text = site.FacebookAppSecret; } else { lAction.Text = "Add"; tbSiteName.Text = string.Empty; tbDescription.Text = string.Empty; ddlTheme.Text = CurrentPage.Site.Theme; tbSiteDomains.Text = string.Empty; tbFaviconUrl.Text = string.Empty; tbAppleTouchIconUrl.Text = string.Empty; tbFacebookAppId.Text = string.Empty; tbFacebookAppSecret.Text = string.Empty; } pnlList.Visible = false; pnlDetails.Visible = true; }
/// <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 site = new SiteService( new RockContext() ).Get( int.Parse( hfSiteId.Value ) ); ShowEditDetails( site ); }
/// <summary> /// Binds the grid. /// </summary> private void BindGrid() { SiteService siteService = new SiteService(); gSites.DataSource = siteService.Queryable().OrderBy( s => s.Name ).ToList(); gSites.DataBind(); }
/// <summary> /// Binds the grid. /// </summary> private void BindGrid() { SiteService siteService = new SiteService(); SortProperty sortProperty = gSites.SortProperty; var qry = siteService.Queryable(); if ( sortProperty != null ) { gSites.DataSource = qry.Sort(sortProperty).ToList(); } else { gSites.DataSource = qry.OrderBy( s => s.Name ).ToList(); } gSites.DataBind(); }
/// <summary> /// Binds the sites filter. /// </summary> private void BindSitesFilter() { SiteService siteService = new SiteService( new RockContext() ); ddlSite.DataTextField = "Name"; ddlSite.DataValueField = "Id"; ddlSite.DataSource = siteService.Queryable().OrderBy( s => s.Name ).ToList(); ddlSite.DataBind(); ddlSite.Items.Insert( 0, new ListItem( All.Text, All.IdValue ) ); }
/// <summary> /// Handles the Click event of the btnCompileTheme 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 btnCompileTheme_Click( object sender, EventArgs e ) { var rockContext = new RockContext(); SiteService siteService = new SiteService( rockContext ); Site site = siteService.Get( hfSiteId.Value.AsInteger() ); string messages = string.Empty; var theme = new RockTheme( site.Theme ); bool success = theme.Compile( out messages ); if ( success ) { mdThemeCompile.Show( "Theme was successfully compiled.", ModalAlertType.Information ); } else { mdThemeCompile.Show( string.Format("An error occurred compiling the theme {0}. Message: {1}.", site.Theme, messages), ModalAlertType.Warning ); } }
/// <summary> /// Handles the Click event of the btnDelete 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 btnDelete_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 ) ); if ( site != null ) { 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(); }
/// <summary> /// Shows the edit. /// </summary> /// <param name="siteId">The site id.</param> public void ShowDetail( int siteId ) { pnlDetails.Visible = false; Site site = null; if ( !siteId.Equals( 0 ) ) { site = new SiteService( new RockContext() ).Get( siteId ); pdAuditDetails.SetEntity( site, ResolveRockUrl( "~" ) ); } if ( site == null ) { site = new Site { Id = 0 }; site.SiteDomains = new List<SiteDomain>(); site.Theme = RockPage.Layout.Site.Theme; // hide the panel drawer that show created and last modified dates pdAuditDetails.Visible = false; } // set theme compile button if ( ! new RockTheme(site.Theme ).AllowsCompile) { btnCompileTheme.Enabled = false; btnCompileTheme.Text = "Theme Doesn't Support Compiling"; } pnlDetails.Visible = true; hfSiteId.Value = site.Id.ToString(); cePageHeaderContent.Text = site.PageHeaderContent; cbAllowIndexing.Checked = site.AllowIndexing; bool readOnly = false; nbEditModeMessage.Text = string.Empty; if ( !IsUserAuthorized( Authorization.EDIT ) ) { readOnly = true; nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed( Rock.Model.Site.FriendlyTypeName ); } if ( site.IsSystem ) { nbEditModeMessage.Text = EditModeMessage.System( Rock.Model.Site.FriendlyTypeName ); } if ( readOnly ) { btnEdit.Visible = false; btnDelete.Visible = false; ShowReadonlyDetails( site ); } else { btnEdit.Visible = true; btnDelete.Visible = !site.IsSystem; if ( site.Id > 0 ) { ShowReadonlyDetails( site ); } else { ShowEditDetails( site ); } } }
/// <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 ); } }
/// <summary> /// Binds the grid. /// </summary> private void BindGrid() { SiteService siteService = new SiteService( new RockContext() ); SortProperty sortProperty = gSites.SortProperty; var qry = siteService.Queryable(); if ( sortProperty != null ) { gSites.DataSource = qry.Sort(sortProperty).ToList(); } else { gSites.DataSource = qry.OrderBy( s => s.Name ).ToList(); } gSites.EntityTypeId = EntityTypeCache.Read<Site>().Id; gSites.DataBind(); }
// default error handling /// <summary> /// Handles the Error event of the Application 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 Application_Error( object sender, EventArgs e ) { HttpContext context = HttpContext.Current; // If the current context is null, there's nothing that can be done. Just return. if ( context == null ) { return; } // log error var ex = Context.Server.GetLastError(); if ( ex != null ) { bool logException = true; // string to send a message to the error page to prevent infinite loops // of error reporting from incurring if there is an exception on the error page string errorQueryParm = "?type=exception&error=1"; string errorCount = context.Request["error"]; if ( !string.IsNullOrWhiteSpace( errorCount ) ) { if ( errorCount == "1" ) { errorQueryParm = "?type=exception&error=2"; } else if ( errorCount == "2" ) { // something really bad is occurring stop logging errors as we're in an infinate loop logException = false; } } if ( logException ) { string status = "500"; var globalAttributesCache = GlobalAttributesCache.Read(); // determine if 404's should be tracked as exceptions bool track404 = Convert.ToBoolean( globalAttributesCache.GetValue( "Log404AsException" ) ); // set status to 404 if ( ex.Message == "File does not exist." && ex.Source == "System.Web" ) { status = "404"; } if ( status == "500" || track404 ) { LogError( ex, context ); context.Server.ClearError(); string errorPage = string.Empty; // determine error page based on the site SiteService service = new SiteService(); string siteName = string.Empty; if ( context.Items["Rock:SiteId"] != null ) { int siteId; Int32.TryParse( context.Items["Rock:SiteId"].ToString(), out siteId ); // load site Site site = service.Get( siteId ); siteName = site.Name; errorPage = site.ErrorPage; } // Attempt to store exception in session. Session state may not be available // within the context of an HTTP handler or the REST API. try { Session["Exception"] = ex; } catch ( HttpException ) { } // email notifications if 500 error if ( status == "500" ) { // setup merge codes for email var mergeObjects = new Dictionary<string, object>(); mergeObjects.Add( "ExceptionDetails", "An error occurred on the " + siteName + " site on page: <br>" + context.Request.Url.OriginalString + "<p>" + FormatException( ex, "" ) ); // get email addresses to send to string emailAddressesList = globalAttributesCache.GetValue( "EmailExceptionsList" ); if ( !string.IsNullOrWhiteSpace( emailAddressesList ) ) { string[] emailAddresses = emailAddressesList.Split( new[] { ',' }, StringSplitOptions.RemoveEmptyEntries ); var recipients = new Dictionary<string, Dictionary<string, object>>(); foreach ( string emailAddress in emailAddresses ) { recipients.Add( emailAddress, mergeObjects ); } if ( recipients.Count > 0 ) { Email email = new Email( Rock.SystemGuid.EmailTemplate.CONFIG_EXCEPTION_NOTIFICATION ); email.Send( recipients ); } } } // redirect to error page if ( !string.IsNullOrEmpty( errorPage ) ) { Response.Redirect( errorPage + errorQueryParm, false ); Context.ApplicationInstance.CompleteRequest(); } else { Response.Redirect( "~/error.aspx" + errorQueryParm, false ); // default error page Context.ApplicationInstance.CompleteRequest(); } // intentially throw ThreadAbort Response.End(); } } } }
/// <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(); }