/// <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 ) { EventCalendar eventCalendar; using ( var rockContext = new RockContext() ) { EventCalendarService eventCalendarService = new EventCalendarService( rockContext ); EventCalendarContentChannelService eventCalendarContentChannelService = new EventCalendarContentChannelService( rockContext ); ContentChannelService contentChannelService = new ContentChannelService( rockContext ); AttributeService attributeService = new AttributeService( rockContext ); AttributeQualifierService qualifierService = new AttributeQualifierService( rockContext ); int eventCalendarId = int.Parse( hfEventCalendarId.Value ); if ( eventCalendarId == 0 ) { eventCalendar = new EventCalendar(); eventCalendarService.Add( eventCalendar ); } else { eventCalendar = eventCalendarService.Get( eventCalendarId ); } eventCalendar.IsActive = cbActive.Checked; eventCalendar.Name = tbName.Text; eventCalendar.Description = tbDescription.Text; eventCalendar.IconCssClass = tbIconCssClass.Text; if ( !eventCalendar.IsValid ) { // Controls will render the error messages return; } // need WrapTransaction due to Attribute saves rockContext.WrapTransaction( () => { rockContext.SaveChanges(); var dbChannelGuids = eventCalendarContentChannelService.Queryable() .Where( c => c.EventCalendarId == eventCalendar.Id ) .Select( c => c.Guid ) .ToList(); var uiChannelGuids = ContentChannelsState.Select( c => c.Key ).ToList(); var toDelete = eventCalendarContentChannelService .Queryable() .Where( c => dbChannelGuids.Contains( c.Guid ) && !uiChannelGuids.Contains( c.Guid )); eventCalendarContentChannelService.DeleteRange( toDelete ); contentChannelService.Queryable() .Where( c => uiChannelGuids.Contains( c.Guid ) && !dbChannelGuids.Contains( c.Guid ) ) .ToList() .ForEach( c => { var eventCalendarContentChannel = new EventCalendarContentChannel(); eventCalendarContentChannel.EventCalendarId = eventCalendar.Id; eventCalendarContentChannel.ContentChannelId = c.Id; eventCalendarContentChannelService.Add( eventCalendarContentChannel ); } ); rockContext.SaveChanges(); /* Save Attributes */ string qualifierValue = eventCalendar.Id.ToString(); SaveAttributes( new EventCalendarItem().TypeId, "EventCalendarId", qualifierValue, AttributesState, rockContext ); // Reload calendar and make sure that the person who may have just added a calendar has security to view/edit/administrate the calendar eventCalendar = eventCalendarService.Get( eventCalendar.Id ); if ( eventCalendar != null ) { if ( !eventCalendar.IsAuthorized( Authorization.VIEW, CurrentPerson ) ) { eventCalendar.AllowPerson( Authorization.VIEW, CurrentPerson, rockContext ); } if ( !eventCalendar.IsAuthorized( Authorization.EDIT, CurrentPerson ) ) { eventCalendar.AllowPerson( Authorization.EDIT, CurrentPerson, rockContext ); } if ( !eventCalendar.IsAuthorized( Authorization.ADMINISTRATE, CurrentPerson ) ) { eventCalendar.AllowPerson( Authorization.ADMINISTRATE, CurrentPerson, rockContext ); } } } ); } // Redirect back to same page so that item grid will show any attributes that were selected to show on grid var qryParams = new Dictionary<string, string>(); qryParams["EventCalendarId"] = eventCalendar.Id.ToString(); NavigateToPage( RockPage.Guid, qryParams ); }
/// <summary> /// Binds the grid. /// </summary> private void BindGrid() { ContentChannelService contentChannelService = new ContentChannelService( new RockContext() ); SortProperty sortProperty = gContentChannels.SortProperty; var qry = contentChannelService.Queryable( "ContentChannelType,Items" ); int? typeId = gfFilter.GetUserPreference( "Type" ).AsIntegerOrNull(); if ( typeId.HasValue ) { qry = qry.Where( c => c.ContentChannelTypeId == typeId.Value ); } gContentChannels.ObjectList = new Dictionary<string, object>(); var channels = new List<ContentChannel>(); foreach ( var channel in qry.ToList() ) { if ( channel.IsAuthorized(Rock.Security.Authorization.VIEW, CurrentPerson)) { channels.Add( channel ); gContentChannels.ObjectList.Add( channel.Id.ToString(), channel ); } } var now = RockDateTime.Now; var items = channels.Select( c => new { c.Id, c.Name, ContentChannelType = c.ContentChannelType.Name, c.EnableRss, c.ChannelUrl, ItemLastCreated = c.Items.Max( i => i.CreatedDateTime ), TotalItems = c.Items.Count(), ActiveItems = c.Items .Where( i => ( i.StartDateTime.CompareTo( now ) < 0 ) && ( !i.ExpireDateTime.HasValue || i.ExpireDateTime.Value.CompareTo( now ) > 0 ) && ( i.ApprovedByPersonAliasId.HasValue || !c.RequiresApproval ) ).Count() } ).AsQueryable(); if ( sortProperty != null ) { gContentChannels.DataSource = items.Sort( sortProperty ).ToList(); } else { gContentChannels.DataSource = items.OrderBy( p => p.Name ).ToList(); } gContentChannels.DataBind(); }
public void ProcessRequest( HttpContext context ) { request = context.Request; response = context.Response; RockContext rockContext = new RockContext(); if ( request.HttpMethod != "GET" ) { response.Write( "Invalid request type." ); response.StatusCode = 200; return; } if ( request.QueryString["ChannelId"] != null ) { int channelId; int templateDefinedValueId; DefinedValueCache dvRssTemplate; string rssTemplate; if ( !int.TryParse( request.QueryString["ChannelId"] , out channelId )) { response.Write( "Invalid channel id." ); response.StatusCode = 200; return; } if ( request.QueryString["TemplateId"] == null || !int.TryParse( request.QueryString["TemplateId"], out templateDefinedValueId ) ) { dvRssTemplate = DefinedValueCache.Read( Rock.SystemGuid.DefinedValue.DEFAULT_RSS_CHANNEL ); } else { dvRssTemplate = DefinedValueCache.Read( templateDefinedValueId ); } rssTemplate = dvRssTemplate.GetAttributeValue( "Template" ); if ( request.QueryString["EnableDebug"] != null ) { // when in debug mode we need to export as html and linkin styles so that the debug info will be displayed string appPath = HttpContext.Current.Request.ApplicationPath; response.Write( "<html>" ); response.Write( "<head>" ); response.Write( string.Format( "<link rel='stylesheet' type='text/css' href='{0}Themes/Rock/Styles/bootstrap.css'>", appPath ) ); response.Write( string.Format( "<link rel='stylesheet' type='text/css' href='{0}Themes/Rock/Styles/theme.css'>", appPath ) ); response.Write( string.Format( "<script src='{0}Scripts/jquery-1.10.2.min.js'></script>", appPath ) ); response.Write( string.Format( "<script src='{0}Scripts/bootstrap.min.js'></script>", appPath ) ); response.Write( "</head>" ); response.Write( "<body style='padding: 24px;'>" ); } else { if ( string.IsNullOrWhiteSpace( dvRssTemplate.GetAttributeValue( "MimeType" ) ) ) { response.ContentType = "application/rss+xml"; } else { response.ContentType = dvRssTemplate.GetAttributeValue( "MimeType" ); } } ContentChannelService channelService = new ContentChannelService( rockContext ); var channel = channelService.Queryable( "ContentChannelType" ).Where( c => c.Id == channelId ).FirstOrDefault(); if ( channel != null ) { if ( channel.EnableRss ) { // load merge fields var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields( null ); mergeFields.Add( "Channel", channel ); Dictionary<string, object> requestObjects = new Dictionary<string, object>(); requestObjects.Add( "Scheme", request.Url.Scheme ); requestObjects.Add( "Host", request.Url.Host ); requestObjects.Add( "Authority", request.Url.Authority ); requestObjects.Add( "LocalPath", request.Url.LocalPath ); requestObjects.Add( "AbsoluteUri", request.Url.AbsoluteUri ); requestObjects.Add( "AbsolutePath", request.Url.AbsolutePath ); requestObjects.Add( "Port", request.Url.Port ); requestObjects.Add( "Query", request.Url.Query ); requestObjects.Add( "OriginalString", request.Url.OriginalString ); mergeFields.Add( "Request", requestObjects ); // check for new rss item limit if ( request.QueryString["Count"] != null ) { int.TryParse( request.QueryString["Count"], out rssItemLimit ); } // get channel items ContentChannelItemService contentService = new ContentChannelItemService( rockContext ); var content = contentService.Queryable( "ContentChannelType" ) .Where( c => c.ContentChannelId == channel.Id && (c.Status == ContentChannelItemStatus.Approved || c.ContentChannel.RequiresApproval == false) && c.StartDateTime <= RockDateTime.Now ) .OrderByDescending( c => c.StartDateTime ) .Take( rssItemLimit ); if ( channel.ContentChannelType.DateRangeType == ContentChannelDateType.DateRange ) { if ( channel.ContentChannelType.IncludeTime ) { content = content.Where( c => c.ExpireDateTime >= RockDateTime.Now ); } else { content = content.Where( c => c.ExpireDateTime > RockDateTime.Today ); } } foreach ( var item in content ) { item.Content = item.Content.ResolveMergeFields( mergeFields ); // resolve any relative links var globalAttributes = Rock.Web.Cache.GlobalAttributesCache.Read(); string publicAppRoot = globalAttributes.GetValue( "PublicApplicationRoot" ).EnsureTrailingForwardslash(); item.Content = item.Content.Replace( @" src=""/", @" src=""" + publicAppRoot ); item.Content = item.Content.Replace( @" href=""/", @" href=""" + publicAppRoot ); // get item attributes and add them as elements to the feed item.LoadAttributes( rockContext ); foreach ( var attributeValue in item.AttributeValues ) { attributeValue.Value.Value = attributeValue.Value.Value.ResolveMergeFields( mergeFields ); } } mergeFields.Add( "Items", content ); mergeFields.Add( "RockVersion", Rock.VersionInfo.VersionInfo.GetRockProductVersionNumber() ); // show debug info if ( request.QueryString["EnableDebug"] != null ) { response.Write( mergeFields.lavaDebugInfo() ); response.Write( "<pre>" ); response.Write( WebUtility.HtmlEncode(rssTemplate.ResolveMergeFields( mergeFields )) ); response.Write( "</pre>" ); response.Write( "</body>" ); response.Write( "</html" ); } else { response.Write( rssTemplate.ResolveMergeFields( mergeFields ) ); } } else { response.Write( "RSS is not enabled for this channel." ); response.StatusCode = 200; return; } } else { response.StatusCode = 200; response.Write( "Invalid channel id." ); response.StatusCode = 200; return; } } else { response.Write( "A ChannelId is required." ); response.StatusCode = 200; return; } }