/// <summary> /// Uses the filter information in the CalendarProps object to get a list of events /// </summary> /// <param name="calendarProps">The calendar props.</param> /// <returns></returns> private List <EventItem> GetEventItems(CalendarProps calendarProps) { RockContext rockContext = new RockContext(); EventCalendarItemService eventCalendarItemService = new EventCalendarItemService(rockContext); var eventIdsForCalendar = eventCalendarItemService .Queryable() .Where(i => i.EventCalendarId == calendarProps.CalendarId) .Select(i => i.EventItemId) .ToList(); EventItemService eventItemService = new EventItemService(rockContext); var eventQueryable = eventItemService .Queryable("EventItemAudiences, EventItemOccurrences.Schedule") .Where(e => eventIdsForCalendar.Contains(e.Id)) .Where(e => e.EventItemOccurrences.Any(o => o.Schedule.EffectiveStartDate <= calendarProps.EndDate && calendarProps.StartDate <= o.Schedule.EffectiveEndDate)) .Where(e => e.IsActive == true) .Where(e => e.IsApproved); // For Campus if (calendarProps.CampusIds.Any()) { eventQueryable = eventQueryable.Where(e => e.EventItemOccurrences.Any(c => !c.CampusId.HasValue || calendarProps.CampusIds.Contains(c.CampusId.Value))); } // For Audience if (calendarProps.AudienceIds.Any()) { eventQueryable = eventQueryable.Where(e => e.EventItemAudiences.Any(c => calendarProps.AudienceIds.Contains(c.DefinedValueId))); } return(eventQueryable.ToList()); }
private void DisplayDetails() { RockContext rockContext = new RockContext(); EventItemService eventItemService = new EventItemService(rockContext); var qry = eventItemService .Queryable(); // get the eventItem id if the event item is set via block attribute var eventItemAttGuid = GetAttributeValue("EventItem").AsGuid(); int eventItemId = qry.Where(i => i.Guid == eventItemAttGuid).Select(i => i.Id).FirstOrDefault(); // get the eventItem id if the event item block attribute isn't set if (eventItemId == 0 && !string.IsNullOrWhiteSpace(PageParameter("EventItemId"))) { eventItemId = Convert.ToInt32(PageParameter("EventItemId")); } if (eventItemId > 0) { /*var qry = eventItemService * .Queryable() * ;*/ qry = qry.Where(i => i.Id == eventItemId); } else { // Get the Slug Attribute var slugAttribute = AttributeCache.Get(GetAttributeValue("URLSlugAttribute").AsGuid()); // get the slug if (!string.IsNullOrWhiteSpace(PageParameter("URLSlug")) && slugAttribute != null) { int slugAttributeId = slugAttribute.Id; EventCalendarItemService eventCalendarItemService = new EventCalendarItemService(rockContext); AttributeValueService attributeValueService = new AttributeValueService(rockContext); var tmQry = qry.Join(eventCalendarItemService.Queryable(), ei => ei.Id, aci => aci.EventItemId, (ei, aci) => new { EventItem = ei, EventCalendarItem = aci } ) .Join(attributeValueService.Queryable(), ei => new { Id = ei.EventCalendarItem.Id, AttributeId = slugAttributeId }, av => new { Id = av.EntityId ?? 0, AttributeId = av.AttributeId }, (ei, av) => new { EventItem = ei.EventItem, EventCalendarItem = ei.EventCalendarItem, Slug = av }); string urlSlug = PageParameter("URLSlug"); tmQry = tmQry.Where(obj => obj.Slug.Value.Contains(urlSlug)); // The page parameter could contain something like 'camp' while the slug value list contains 'camp-freedom' so we need to double-check // to make sure we have an exact match qry = tmQry.ToList().AsQueryable().Where(obj => obj.Slug.Value.ToLower().Split('|').Contains(urlSlug.ToLower())).Select(obj => obj.EventItem); } else { // If we don't have an eventItemId or slug we shouldn't get the first item in the database. That would be . . . not good qry = null; } } if (qry != null) { var eventItem = qry.FirstOrDefault(); if (eventItem != null) { // removing any occurrences that don't have a start time in the next twelve months var occurrenceList = eventItem.EventItemOccurrences.ToList(); occurrenceList.RemoveAll(o => o.GetStartTimes(RockDateTime.Now, RockDateTime.Now.AddYears(1)).Count() == 0); //Check for Campus Id Parameter var campusId = PageParameter("CampusId").AsIntegerOrNull(); if (campusId.HasValue) { //check if there's a campus with this id. var campus = CampusCache.Get(campusId.Value); if (campus != null) { occurrenceList.RemoveAll(o => o.CampusId != null && o.CampusId != campus.Id); } } //Check for Campus var campusStr = PageParameter("Campus"); if (!string.IsNullOrEmpty(campusStr)) { //check if there's a campus with this name. var campus = CampusCache.All().Where(c => c.Name.ToLower().RemoveSpaces() == campusStr.ToLower().RemoveSpaces()).FirstOrDefault(); if (campus != null) { occurrenceList.RemoveAll(o => o.CampusId != null && o.CampusId != campus.Id); } else { // check one more time to see if there's a campus slug that matches campus = CampusCache.All().Where(c => c.AttributeValues["Slug"].ToString() == campusStr.ToLower()).FirstOrDefault(); if (campus != null) { occurrenceList.RemoveAll(o => o.CampusId != null && o.CampusId != campus.Id); } } } eventItem.EventItemOccurrences = occurrenceList.OrderBy(o => o.NextStartDateTime.HasValue ? o.NextStartDateTime : DateTime.Now).ToList(); var mergeFields = new Dictionary <string, object>(); mergeFields.Add("RegistrationPage", LinkedPageRoute("RegistrationPage")); var campusEntityType = EntityTypeCache.Get("Rock.Model.Campus"); var contextCampus = RockPage.GetCurrentContext(campusEntityType) as Campus; if (contextCampus != null) { mergeFields.Add("CampusContext", contextCampus); } // determine registration status (Register, Full, or Join Wait List) for each unique registration instance Dictionary <int, string> registrationStatusLabels = new Dictionary <int, string>(); foreach (var occurance in eventItem.EventItemOccurrences) { foreach (var registrationInstance in occurance.Linkages.Select(a => a.RegistrationInstance).Distinct().ToList()) { if (registrationStatusLabels.ContainsKey(registrationInstance.Id)) { continue; } int?maxRegistrantCount = 0; var currentRegistrationCount = 0; if (registrationInstance != null) { if (registrationInstance.MaxAttendees != 0) { maxRegistrantCount = registrationInstance.MaxAttendees; } } int?registrationSpotsAvailable = null; if (maxRegistrantCount.HasValue && maxRegistrantCount != 0) { currentRegistrationCount = new RegistrationRegistrantService(rockContext).Queryable().AsNoTracking() .Where(r => r.Registration.RegistrationInstanceId == registrationInstance.Id && r.OnWaitList == false) .Count(); registrationSpotsAvailable = maxRegistrantCount - currentRegistrationCount; } string registrationStatusLabel = "Register"; if (registrationSpotsAvailable.HasValue && registrationSpotsAvailable.Value < 1) { if (registrationInstance.RegistrationTemplate.WaitListEnabled) { registrationStatusLabel = "Join Wait List"; } else { registrationStatusLabel = "Full"; } } registrationStatusLabels.Add(registrationInstance.Id, registrationStatusLabel); } } // Status of each registration instance mergeFields.Add("RegistrationStatusLabels", registrationStatusLabels); mergeFields.Add("Event", eventItem); mergeFields.Add("CurrentPerson", CurrentPerson); lOutput.Text = GetAttributeValue("LavaTemplate").ResolveMergeFields(mergeFields); if (GetAttributeValue("SetPageTitle").AsBoolean()) { string pageTitle = eventItem != null ? eventItem.Name : "Event"; RockPage.PageTitle = pageTitle; RockPage.BrowserTitle = String.Format("{0} | {1}", pageTitle, RockPage.Site.Name); RockPage.Header.Title = String.Format("{0} | {1}", pageTitle, RockPage.Site.Name); } } else { lOutput.Text = EventNotFoundLava(); } } else { lOutput.Text = EventNotFoundLava(); } }
/// <summary> /// Binds the event calendar items grid. /// </summary> protected void BindEventCalendarItemsGrid() { if (_eventCalendar != null) { pnlEventCalendarItems.Visible = true; var rockContext = new RockContext(); EventCalendarItemService eventCalendarItemService = new EventCalendarItemService(rockContext); var qry = eventCalendarItemService .Queryable("EventCalendar,EventItem.EventItemAudiences,EventItem.EventItemOccurrences.Schedule") .Where(m => m.EventItem != null && m.EventCalendarId == _eventCalendar.Id); // Filter by Status string statusFilter = ddlStatus.SelectedValue; if (statusFilter == "Active") { qry = qry .Where(m => m.EventItem.IsActive); } else if (statusFilter == "Inactive") { qry = qry .Where(m => !m.EventItem.IsActive); } // Filter by Approval Status string approvalStatusFilter = ddlApprovalStatus.SelectedValue; if (approvalStatusFilter == "Approved") { qry = qry .Where(m => m.EventItem.IsApproved); } else if (approvalStatusFilter == "Not Approved") { qry = qry .Where(m => !m.EventItem.IsApproved); } // Filter by Campus List <int> campusIds = cblCampus.SelectedValuesAsInt; if (campusIds.Any()) { qry = qry .Where(i => i.EventItem.EventItemOccurrences .Any(c => !c.CampusId.HasValue || campusIds.Contains(c.CampusId.Value))); } // Filter query by any configured attribute filters if (AvailableAttributes != null && AvailableAttributes.Any()) { var attributeValueService = new AttributeValueService(rockContext); var parameterExpression = attributeValueService.ParameterExpression; foreach (var attribute in AvailableAttributes) { var filterControl = phAttributeFilters.FindControl("filter_" + attribute.Id.ToString()); if (filterControl != null) { var filterValues = attribute.FieldType.Field.GetFilterValues(filterControl, attribute.QualifierValues, Rock.Reporting.FilterMode.SimpleFilter); var expression = attribute.FieldType.Field.AttributeFilterExpression(attribute.QualifierValues, filterValues, parameterExpression); if (expression != null) { var attributeValues = attributeValueService .Queryable() .Where(v => v.Attribute.Id == attribute.Id); attributeValues = attributeValues.Where(parameterExpression, expression, null); qry = qry.Where(w => attributeValues.Select(v => v.EntityId).Contains(w.Id)); } } } } // Filter by Audience List <int> audiences = cblAudience.SelectedValuesAsInt; if (audiences.Any()) { qry = qry.Where(i => i.EventItem.EventItemAudiences .Any(c => audiences.Contains(c.DefinedValueId))); } SortProperty sortProperty = gEventCalendarItems.SortProperty; // Sort and query db List <EventCalendarItem> eventCalendarItems = null; if (sortProperty != null) { // If sorting on date, wait until after checking to see if date range was specified if (sortProperty.Property == "Date") { eventCalendarItems = qry.ToList(); } else { eventCalendarItems = qry.Sort(sortProperty).ToList(); } } else { eventCalendarItems = qry.OrderBy(a => a.EventItem.Name).ToList(); } // Now that items have been loaded and ordered from db, calculate the next start date for each item var calendarItemsWithDates = eventCalendarItems .Select(i => new EventCalendarItemWithDates { EventCalendarItem = i, NextStartDateTime = i.EventItem.NextStartDateTime, }) .ToList(); var dateCol = gEventCalendarItems.Columns.OfType <BoundField>().Where(c => c.DataField == "Date").FirstOrDefault(); // if a date range was specified, need to get all dates for items and filter based on any that have an occurrence withing the date range DateTime?lowerDateRange = drpDate.LowerValue; DateTime?upperDateRange = drpDate.UpperValue; if (lowerDateRange.HasValue || upperDateRange.HasValue) { // If only one value was included, default the other to be a years difference lowerDateRange = lowerDateRange ?? upperDateRange.Value.AddYears(-1).AddDays(1); upperDateRange = upperDateRange ?? lowerDateRange.Value.AddYears(1).AddDays(-1); // Get the start datetimes within the selected date range calendarItemsWithDates.ForEach(i => i.StartDateTimes = i.EventCalendarItem.EventItem.GetStartTimes(lowerDateRange.Value, upperDateRange.Value.AddDays(1))); // Filter out calendar items with no dates within range calendarItemsWithDates = calendarItemsWithDates.Where(i => i.StartDateTimes.Any()).ToList(); // Update the Next Start Date to be the next date in range instead dateCol.HeaderText = "Next Date In Range"; calendarItemsWithDates.ForEach(i => i.NextStartDateTime = i.StartDateTimes.Min()); } else { dateCol.HeaderText = "Next Start Date"; } // Now sort on date if that is what was selected if (sortProperty != null && sortProperty.Property == "Date") { if (sortProperty.Direction == SortDirection.Ascending) { calendarItemsWithDates = calendarItemsWithDates.OrderBy(a => a.NextStartDateTime).ToList(); } else { calendarItemsWithDates = calendarItemsWithDates.OrderByDescending(a => a.NextStartDateTime).ToList(); } } // Save the calendar items to the grid's objectlist gEventCalendarItems.ObjectList = new Dictionary <string, object>(); calendarItemsWithDates.ForEach(i => gEventCalendarItems.ObjectList.Add(i.EventCalendarItem.EventItem.Id.ToString(), i.EventCalendarItem)); gEventCalendarItems.EntityTypeId = EntityTypeCache.Read("Rock.Model.EventCalendarItem").Id; gEventCalendarItems.DataSource = calendarItemsWithDates.Select(i => new { Id = i.EventCalendarItem.EventItem.Id, Guid = i.EventCalendarItem.EventItem.Guid, Date = i.NextStartDateTime.HasValue ? i.NextStartDateTime.Value.ToShortDateString() : "N/A", Name = i.EventCalendarItem.EventItem.Name, Occurrences = campusIds.Any() ? i.EventCalendarItem.EventItem.EventItemOccurrences.Where(c => !c.CampusId.HasValue || campusIds.Contains(c.CampusId.Value)).Count() : i.EventCalendarItem.EventItem.EventItemOccurrences.Count(), Calendar = i.EventCalendarItem.EventItem.EventCalendarItems.ToList().Select(c => c.EventCalendar.Name).ToList().AsDelimited("<br>"), Audience = i.EventCalendarItem.EventItem.EventItemAudiences.ToList().Select(a => a.DefinedValue.Value).ToList().AsDelimited("<br>"), Status = i.EventCalendarItem.EventItem.IsActive ? "<span class='label label-success'>Active</span>" : "<span class='label label-default'>Inactive</span>", ApprovalStatus = i.EventCalendarItem.EventItem.IsApproved ? "<span class='label label-info'>Approved</span>" : "<span class='label label-warning'>Not Approved</span>" }).ToList(); gEventCalendarItems.DataBind(); } else { pnlEventCalendarItems.Visible = false; } }
/// <summary> /// Binds the event calendar items grid. /// </summary> protected void BindEventCalendarItemsGrid() { if ( _eventCalendar != null ) { pnlEventCalendarItems.Visible = true; var rockContext = new RockContext(); EventCalendarItemService eventCalendarItemService = new EventCalendarItemService( rockContext ); var qry = eventCalendarItemService .Queryable( "EventCalendar,EventItem.EventItemAudiences,EventItem.EventItemOccurrences.Schedule" ) .Where( m => m.EventItem != null && m.EventCalendarId == _eventCalendar.Id ); // Filter by Status string statusFilter = ddlStatus.SelectedValue; if ( statusFilter == "Active" ) { qry = qry .Where( m => m.EventItem.IsActive ); } else if ( statusFilter == "Inactive" ) { qry = qry .Where( m => !m.EventItem.IsActive ); } // Filter by Approval Status string approvalStatusFilter = ddlApprovalStatus.SelectedValue; if ( approvalStatusFilter == "Approved" ) { qry = qry .Where( m => m.EventItem.IsApproved ); } else if ( approvalStatusFilter == "Not Approved" ) { qry = qry .Where( m => !m.EventItem.IsApproved ); } // Filter by Campus List<int> campusIds = cblCampus.SelectedValuesAsInt; if ( campusIds.Any() ) { qry = qry .Where( i => i.EventItem.EventItemOccurrences .Any( c => !c.CampusId.HasValue || campusIds.Contains( c.CampusId.Value ) ) ); } // Filter query by any configured attribute filters if ( AvailableAttributes != null && AvailableAttributes.Any() ) { var attributeValueService = new AttributeValueService( rockContext ); var parameterExpression = attributeValueService.ParameterExpression; foreach ( var attribute in AvailableAttributes ) { var filterControl = phAttributeFilters.FindControl( "filter_" + attribute.Id.ToString() ); if ( filterControl != null ) { var filterValues = attribute.FieldType.Field.GetFilterValues( filterControl, attribute.QualifierValues, Rock.Reporting.FilterMode.SimpleFilter ); var expression = attribute.FieldType.Field.AttributeFilterExpression( attribute.QualifierValues, filterValues, parameterExpression ); if ( expression != null ) { var attributeValues = attributeValueService .Queryable() .Where( v => v.Attribute.Id == attribute.Id ); attributeValues = attributeValues.Where( parameterExpression, expression, null ); qry = qry.Where( w => attributeValues.Select( v => v.EntityId ).Contains( w.Id ) ); } } } } // Filter by Audience List<int> audiences = cblAudience.SelectedValuesAsInt; if ( audiences.Any() ) { qry = qry.Where( i => i.EventItem.EventItemAudiences .Any( c => audiences.Contains( c.DefinedValueId ) ) ); } SortProperty sortProperty = gEventCalendarItems.SortProperty; // Sort and query db List<EventCalendarItem> eventCalendarItems = null; if ( sortProperty != null ) { // If sorting on date, wait until after checking to see if date range was specified if ( sortProperty.Property == "Date" ) { eventCalendarItems = qry.ToList(); } else { eventCalendarItems = qry.Sort( sortProperty ).ToList(); } } else { eventCalendarItems = qry.OrderBy( a => a.EventItem.Name ).ToList(); } // Now that items have been loaded and ordered from db, calculate the next start date for each item var calendarItemsWithDates = eventCalendarItems .Select( i => new EventCalendarItemWithDates { EventCalendarItem = i, NextStartDateTime = i.EventItem.NextStartDateTime, } ) .ToList(); var dateCol = gEventCalendarItems.Columns.OfType<BoundField>().Where( c => c.DataField == "Date" ).FirstOrDefault(); // if a date range was specified, need to get all dates for items and filter based on any that have an occurrence withing the date range DateTime? lowerDateRange = drpDate.LowerValue; DateTime? upperDateRange = drpDate.UpperValue; if ( lowerDateRange.HasValue || upperDateRange.HasValue ) { // If only one value was included, default the other to be a years difference lowerDateRange = lowerDateRange ?? upperDateRange.Value.AddYears( -1 ).AddDays( 1 ); upperDateRange = upperDateRange ?? lowerDateRange.Value.AddYears( 1 ).AddDays( -1 ); // Get the start datetimes within the selected date range calendarItemsWithDates.ForEach( i => i.StartDateTimes = i.EventCalendarItem.EventItem.GetStartTimes( lowerDateRange.Value, upperDateRange.Value.AddDays( 1 ) ) ); // Filter out calendar items with no dates within range calendarItemsWithDates = calendarItemsWithDates.Where( i => i.StartDateTimes.Any() ).ToList(); // Update the Next Start Date to be the next date in range instead dateCol.HeaderText = "Next Date In Range"; calendarItemsWithDates.ForEach( i => i.NextStartDateTime = i.StartDateTimes.Min() ); } else { dateCol.HeaderText = "Next Start Date"; } // Now sort on date if that is what was selected if ( sortProperty != null && sortProperty.Property == "Date" ) { if ( sortProperty.Direction == SortDirection.Ascending ) { calendarItemsWithDates = calendarItemsWithDates.OrderBy( a => a.NextStartDateTime ).ToList(); } else { calendarItemsWithDates = calendarItemsWithDates.OrderByDescending( a => a.NextStartDateTime ).ToList(); } } // Save the calendar items to the grid's objectlist gEventCalendarItems.ObjectList = new Dictionary<string, object>(); calendarItemsWithDates.ForEach( i => gEventCalendarItems.ObjectList.Add( i.EventCalendarItem.EventItem.Id.ToString(), i.EventCalendarItem ) ); gEventCalendarItems.EntityTypeId = EntityTypeCache.Read( "Rock.Model.EventCalendarItem" ).Id; gEventCalendarItems.DataSource = calendarItemsWithDates.Select( i => new { Id = i.EventCalendarItem.EventItem.Id, Guid = i.EventCalendarItem.EventItem.Guid, Date = i.NextStartDateTime.HasValue ? i.NextStartDateTime.Value.ToShortDateString() : "N/A", Name = i.EventCalendarItem.EventItem.Name, Occurrences = campusIds.Any() ? i.EventCalendarItem.EventItem.EventItemOccurrences.Where( c => !c.CampusId.HasValue || campusIds.Contains( c.CampusId.Value ) ).Count() : i.EventCalendarItem.EventItem.EventItemOccurrences.Count(), Calendar = i.EventCalendarItem.EventItem.EventCalendarItems.ToList().Select( c => c.EventCalendar.Name ).ToList().AsDelimited( "<br>" ), Audience = i.EventCalendarItem.EventItem.EventItemAudiences.ToList().Select( a => a.DefinedValue.Value ).ToList().AsDelimited( "<br>" ), Status = i.EventCalendarItem.EventItem.IsActive ? "<span class='label label-success'>Active</span>" : "<span class='label label-default'>Inactive</span>", ApprovalStatus = i.EventCalendarItem.EventItem.IsApproved ? "<span class='label label-info'>Approved</span>" : "<span class='label label-warning'>Not Approved</span>" } ).ToList(); gEventCalendarItems.DataBind(); } else { pnlEventCalendarItems.Visible = false; } }