/// <summary> /// Adds the event to a Calendar. /// </summary> /// <param name="eventItem">The event</param> /// <param name="calendar">The calendar</param> /// <param name="created">If a new calendar item was created</param> /// <returns>The calendar item for the event</returns> public static EventCalendarItem AddToCalendar(this EventItem eventItem, EventCalendarCache calendar, out bool created) { created = false; if (calendar == null) { return(null); } var calendarItem = eventItem.EventCalendarItems.FirstOrDefault(ci => ci.EventCalendarId == calendar.Id); if (calendarItem == null) { created = true; calendarItem = new EventCalendarItem { EventCalendarId = calendar.Id }; eventItem.EventCalendarItems.Add(calendarItem); } return(calendarItem); }
private void BindAttributes() { // Parse the attribute filters AvailableAttributes = new List <AttributeCache>(); if (_eventCalendar != null) { int entityTypeId = new EventCalendarItem().TypeId; foreach (var attributeModel in new AttributeService(new RockContext()).Queryable() .Where(a => a.EntityTypeId == entityTypeId && a.IsGridColumn && a.EntityTypeQualifierColumn.Equals("EventCalendarId", StringComparison.OrdinalIgnoreCase) && a.EntityTypeQualifierValue.Equals(_eventCalendar.Id.ToString())) .OrderBy(a => a.Order) .ThenBy(a => a.Name)) { AvailableAttributes.Add(AttributeCache.Read(attributeModel)); } } }
/// <summary> /// Shows the item attributes. /// </summary> private void ShowItemAttributes() { var eventCalendarList = new List <int> { (_calendarId ?? 0) }; eventCalendarList.AddRange(cblCalendars.SelectedValuesAsInt); wpAttributes.Visible = false; phAttributes.Controls.Clear(); using (var rockContext = new RockContext()) { var eventCalendarService = new EventCalendarService(rockContext); foreach (int eventCalendarId in eventCalendarList.Distinct()) { EventCalendarItem eventCalendarItem = ItemsState.FirstOrDefault(i => i.EventCalendarId == eventCalendarId); if (eventCalendarItem == null) { eventCalendarItem = new EventCalendarItem(); eventCalendarItem.EventCalendarId = eventCalendarId; ItemsState.Add(eventCalendarItem); } eventCalendarItem.LoadAttributes(); if (eventCalendarItem.Attributes.Count > 0) { wpAttributes.Visible = true; phAttributes.Controls.Add(new LiteralControl(String.Format("<h3>{0}</h3>", eventCalendarService.Get(eventCalendarId).Name))); PlaceHolder phcalAttributes = new PlaceHolder(); Rock.Attribute.Helper.AddEditControls(eventCalendarItem, phAttributes, true, BlockValidationGroup); } } } }
/// <summary> /// Shows the detail. /// </summary> /// <param name="eventItemId">The eventItem identifier.</param> public void ShowDetail(int eventItemId) { pnlEditDetails.Visible = false; EventItem eventItem = null; var rockContext = new RockContext(); if (!eventItemId.Equals(0)) { eventItem = GetEventItem(eventItemId, rockContext); } if (eventItem == null) { eventItem = new EventItem { Id = 0, IsActive = true, Name = "" }; eventItem.IsApproved = _canApprove; var calendarItem = new EventCalendarItem { EventCalendarId = (_calendarId ?? 0) }; eventItem.EventCalendarItems.Add(calendarItem); } eventItem.LoadAttributes(rockContext); FollowingsHelper.SetFollowing(eventItem, pnlFollowing, this.CurrentPerson); bool readOnly = false; nbEditModeMessage.Text = string.Empty; if (!_canEdit) { readOnly = true; nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed(EventItem.FriendlyTypeName); } else { if (eventItem.Id != 0 && !eventItem.EventCalendarItems.Any(i => i.EventCalendarId == (_calendarId ?? 0))) { readOnly = true; } } if (readOnly) { btnEdit.Visible = false; btnDelete.Visible = false; ShowReadonlyDetails(eventItem); } else { btnEdit.Visible = true; btnDelete.Visible = true; if (!eventItemId.Equals(0)) { ShowReadonlyDetails(eventItem); } else { ShowEditDetails(eventItem); } } }
/// <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) { EventItem eventItem = null; using (var rockContext = new RockContext()) { var validationMessages = new List <string>(); var eventItemService = new EventItemService(rockContext); var eventCalendarItemService = new EventCalendarItemService(rockContext); var eventItemAudienceService = new EventItemAudienceService(rockContext); int eventItemId = hfEventItemId.ValueAsInt(); if (eventItemId != 0) { eventItem = eventItemService .Queryable("EventItemAudiences,EventItemOccurrences.Linkages,EventItemOccurrences") .Where(i => i.Id == eventItemId) .FirstOrDefault(); } if (eventItem == null) { eventItem = new EventItem(); eventItemService.Add(eventItem); } eventItem.Name = tbName.Text; eventItem.IsActive = cbIsActive.Checked; if (!eventItem.IsApproved && cbIsApproved.Checked) { eventItem.ApprovedByPersonAliasId = CurrentPersonAliasId; eventItem.ApprovedOnDateTime = RockDateTime.Now; } eventItem.IsApproved = cbIsApproved.Checked; if (!eventItem.IsApproved) { eventItem.ApprovedByPersonAliasId = null; eventItem.ApprovedByPersonAlias = null; eventItem.ApprovedOnDateTime = null; } eventItem.Description = htmlDescription.Text; eventItem.Summary = tbSummary.Text; eventItem.DetailsUrl = tbDetailUrl.Text; int?orphanedImageId = null; if (eventItem.PhotoId != imgupPhoto.BinaryFileId) { orphanedImageId = eventItem.PhotoId; eventItem.PhotoId = imgupPhoto.BinaryFileId; } // Remove any audiences that were removed in the UI foreach (var eventItemAudience in eventItem.EventItemAudiences.Where(r => !AudiencesState.Contains(r.DefinedValueId)).ToList()) { eventItem.EventItemAudiences.Remove(eventItemAudience); eventItemAudienceService.Delete(eventItemAudience); } // Add or Update audiences from the UI foreach (int audienceId in AudiencesState) { EventItemAudience eventItemAudience = eventItem.EventItemAudiences.Where(a => a.DefinedValueId == audienceId).FirstOrDefault(); if (eventItemAudience == null) { eventItemAudience = new EventItemAudience(); eventItemAudience.DefinedValueId = audienceId; eventItem.EventItemAudiences.Add(eventItemAudience); } } // remove any calendar items that removed in the UI var calendarIds = new List <int>(); calendarIds.AddRange(cblCalendars.SelectedValuesAsInt); var uiCalendarGuids = ItemsState.Where(i => calendarIds.Contains(i.EventCalendarId)).Select(a => a.Guid); foreach (var eventCalendarItem in eventItem.EventCalendarItems.Where(a => !uiCalendarGuids.Contains(a.Guid)).ToList()) { // Make sure user is authorized to remove calendar (they may not have seen every calendar due to security) if (UserCanEdit || eventCalendarItem.EventCalendar.IsAuthorized(Authorization.EDIT, CurrentPerson)) { eventItem.EventCalendarItems.Remove(eventCalendarItem); eventCalendarItemService.Delete(eventCalendarItem); } } // Add or Update calendar items from the UI foreach (var calendar in ItemsState.Where(i => calendarIds.Contains(i.EventCalendarId))) { var eventCalendarItem = eventItem.EventCalendarItems.Where(a => a.Guid == calendar.Guid).FirstOrDefault(); if (eventCalendarItem == null) { eventCalendarItem = new EventCalendarItem(); eventItem.EventCalendarItems.Add(eventCalendarItem); } eventCalendarItem.CopyPropertiesFrom(calendar); } if (!eventItem.EventCalendarItems.Any()) { validationMessages.Add("At least one calendar is required."); } if (!Page.IsValid) { return; } if (!eventItem.IsValid) { // Controls will render the error messages return; } if (validationMessages.Any()) { nbValidation.Text = "Please Correct the Following<ul><li>" + validationMessages.AsDelimited("</li><li>") + "</li></ul>"; nbValidation.Visible = true; return; } // use WrapTransaction since SaveAttributeValues does it's own RockContext.SaveChanges() rockContext.WrapTransaction(() => { rockContext.SaveChanges(); foreach (EventCalendarItem eventCalendarItem in eventItem.EventCalendarItems) { eventCalendarItem.LoadAttributes(); Rock.Attribute.Helper.GetEditValues(phAttributes, eventCalendarItem); eventCalendarItem.SaveAttributeValues(); } if (orphanedImageId.HasValue) { BinaryFileService binaryFileService = new BinaryFileService(rockContext); var binaryFile = binaryFileService.Get(orphanedImageId.Value); if (binaryFile != null) { // marked the old images as IsTemporary so they will get cleaned up later binaryFile.IsTemporary = true; rockContext.SaveChanges(); } } }); // 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>(); if (_calendarId.HasValue) { qryParams["EventCalendarId"] = _calendarId.Value.ToString(); } qryParams["EventItemId"] = eventItem.Id.ToString(); NavigateToPage(RockPage.Guid, qryParams); } }
/// <summary> /// Shows the item attributes. /// </summary> private void ShowItemAttributes() { var eventCalendarList = new List<int> { ( _calendarId ?? 0 ) }; eventCalendarList.AddRange( cblCalendars.SelectedValuesAsInt ); wpAttributes.Visible = false; phAttributes.Controls.Clear(); using ( var rockContext = new RockContext() ) { var eventCalendarService = new EventCalendarService( rockContext ); foreach ( int eventCalendarId in eventCalendarList.Distinct() ) { EventCalendarItem eventCalendarItem = ItemsState.FirstOrDefault( i => i.EventCalendarId == eventCalendarId ); if ( eventCalendarItem == null ) { eventCalendarItem = new EventCalendarItem(); eventCalendarItem.EventCalendarId = eventCalendarId; ItemsState.Add( eventCalendarItem ); } eventCalendarItem.LoadAttributes(); if ( eventCalendarItem.Attributes.Count > 0 ) { wpAttributes.Visible = true; phAttributes.Controls.Add( new LiteralControl( String.Format( "<h3>{0}</h3>", eventCalendarService.Get( eventCalendarId ).Name ) ) ); PlaceHolder phcalAttributes = new PlaceHolder(); Rock.Attribute.Helper.AddEditControls( eventCalendarItem, phAttributes, true, BlockValidationGroup ); } } } }
/// <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 ) { EventItem eventItem = null; using ( var rockContext = new RockContext() ) { var validationMessages = new List<string>(); var eventItemService = new EventItemService( rockContext ); var eventCalendarItemService = new EventCalendarItemService( rockContext ); var eventItemAudienceService = new EventItemAudienceService( rockContext ); int eventItemId = hfEventItemId.ValueAsInt(); if ( eventItemId != 0 ) { eventItem = eventItemService .Queryable( "EventItemAudiences,EventItemOccurrences.Linkages,EventItemOccurrences" ) .Where( i => i.Id == eventItemId ) .FirstOrDefault(); } if ( eventItem == null ) { eventItem = new EventItem(); eventItemService.Add( eventItem ); } eventItem.Name = tbName.Text; eventItem.IsActive = cbIsActive.Checked; if ( !eventItem.IsApproved && cbIsApproved.Checked ) { eventItem.ApprovedByPersonAliasId = CurrentPersonAliasId; eventItem.ApprovedOnDateTime = RockDateTime.Now; } eventItem.IsApproved = cbIsApproved.Checked; if ( !eventItem.IsApproved ) { eventItem.ApprovedByPersonAliasId = null; eventItem.ApprovedByPersonAlias = null; eventItem.ApprovedOnDateTime = null; } eventItem.Description = htmlDescription.Text; eventItem.Summary = tbSummary.Text; eventItem.DetailsUrl = tbDetailUrl.Text; int? orphanedImageId = null; if ( eventItem.PhotoId != imgupPhoto.BinaryFileId ) { orphanedImageId = eventItem.PhotoId; eventItem.PhotoId = imgupPhoto.BinaryFileId; } // Remove any audiences that were removed in the UI foreach ( var eventItemAudience in eventItem.EventItemAudiences.Where( r => !AudiencesState.Contains( r.DefinedValueId ) ).ToList() ) { eventItem.EventItemAudiences.Remove( eventItemAudience ); eventItemAudienceService.Delete( eventItemAudience ); } // Add or Update audiences from the UI foreach ( int audienceId in AudiencesState ) { EventItemAudience eventItemAudience = eventItem.EventItemAudiences.Where( a => a.DefinedValueId == audienceId ).FirstOrDefault(); if ( eventItemAudience == null ) { eventItemAudience = new EventItemAudience(); eventItemAudience.DefinedValueId = audienceId; eventItem.EventItemAudiences.Add( eventItemAudience ); } } // remove any calendar items that removed in the UI var calendarIds = new List<int>(); calendarIds.AddRange( cblCalendars.SelectedValuesAsInt ); var uiCalendarGuids = ItemsState.Where( i => calendarIds.Contains( i.EventCalendarId ) ).Select( a => a.Guid ); foreach ( var eventCalendarItem in eventItem.EventCalendarItems.Where( a => !uiCalendarGuids.Contains( a.Guid ) ).ToList() ) { // Make sure user is authorized to remove calendar (they may not have seen every calendar due to security) if ( UserCanEdit || eventCalendarItem.EventCalendar.IsAuthorized( Authorization.EDIT, CurrentPerson ) ) { eventItem.EventCalendarItems.Remove( eventCalendarItem ); eventCalendarItemService.Delete( eventCalendarItem ); } } // Add or Update calendar items from the UI foreach ( var calendar in ItemsState.Where( i => calendarIds.Contains( i.EventCalendarId ) ) ) { var eventCalendarItem = eventItem.EventCalendarItems.Where( a => a.Guid == calendar.Guid ).FirstOrDefault(); if ( eventCalendarItem == null ) { eventCalendarItem = new EventCalendarItem(); eventItem.EventCalendarItems.Add( eventCalendarItem ); } eventCalendarItem.CopyPropertiesFrom( calendar ); } if ( !eventItem.EventCalendarItems.Any() ) { validationMessages.Add( "At least one calendar is required." ); } if ( !Page.IsValid ) { return; } if ( !eventItem.IsValid ) { // Controls will render the error messages return; } if ( validationMessages.Any() ) { nbValidation.Text = "Please Correct the Following<ul><li>" + validationMessages.AsDelimited( "</li><li>" ) + "</li></ul>"; nbValidation.Visible = true; return; } // use WrapTransaction since SaveAttributeValues does it's own RockContext.SaveChanges() rockContext.WrapTransaction( () => { rockContext.SaveChanges(); foreach ( EventCalendarItem eventCalendarItem in eventItem.EventCalendarItems ) { eventCalendarItem.LoadAttributes(); Rock.Attribute.Helper.GetEditValues( phAttributes, eventCalendarItem ); eventCalendarItem.SaveAttributeValues(); } if ( orphanedImageId.HasValue ) { BinaryFileService binaryFileService = new BinaryFileService( rockContext ); var binaryFile = binaryFileService.Get( orphanedImageId.Value ); if ( binaryFile != null ) { // marked the old images as IsTemporary so they will get cleaned up later binaryFile.IsTemporary = true; rockContext.SaveChanges(); } } } ); // 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>(); if ( _calendarId.HasValue ) { qryParams["EventCalendarId"] = _calendarId.Value.ToString(); } qryParams["EventItemId"] = eventItem.Id.ToString(); NavigateToPage( RockPage.Guid, qryParams ); } }
/// <summary> /// Shows the detail. /// </summary> /// <param name="eventItemId">The eventItem identifier.</param> public void ShowDetail( int eventItemId ) { pnlEditDetails.Visible = false; EventItem eventItem = null; var rockContext = new RockContext(); if ( !eventItemId.Equals( 0 ) ) { eventItem = GetEventItem( eventItemId, rockContext ); pdAuditDetails.SetEntity( eventItem, ResolveRockUrl( "~" ) ); } if ( eventItem == null ) { eventItem = new EventItem { Id = 0, IsActive = true, Name = "" }; eventItem.IsApproved = _canApprove; var calendarItem = new EventCalendarItem { EventCalendarId = ( _calendarId ?? 0 ) }; eventItem.EventCalendarItems.Add( calendarItem ); // hide the panel drawer that show created and last modified dates pdAuditDetails.Visible = false; } eventItem.LoadAttributes( rockContext ); FollowingsHelper.SetFollowing( eventItem, pnlFollowing, this.CurrentPerson ); bool readOnly = false; nbEditModeMessage.Text = string.Empty; if ( !_canEdit ) { readOnly = true; nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed( EventItem.FriendlyTypeName ); } else { if ( eventItem.Id != 0 && !eventItem.EventCalendarItems.Any( i => i.EventCalendarId == ( _calendarId ?? 0 ) ) ) { readOnly = true; } } if ( readOnly ) { btnEdit.Visible = false; btnDelete.Visible = false; ShowReadonlyDetails( eventItem ); } else { btnEdit.Visible = true; btnDelete.Visible = true; if ( !eventItemId.Equals( 0 ) ) { ShowReadonlyDetails( eventItem ); } else { ShowEditDetails( eventItem ); } } }
private void BindAttributes() { // Parse the attribute filters AvailableAttributes = new List<AttributeCache>(); if ( _eventCalendar != null ) { int entityTypeId = new EventCalendarItem().TypeId; foreach ( var attributeModel in new AttributeService( new RockContext() ).Queryable() .Where( a => a.EntityTypeId == entityTypeId && a.IsGridColumn && a.EntityTypeQualifierColumn.Equals( "EventCalendarId", StringComparison.OrdinalIgnoreCase ) && a.EntityTypeQualifierValue.Equals( _eventCalendar.Id.ToString() ) ) .OrderBy( a => a.Order ) .ThenBy( a => a.Name ) ) { AvailableAttributes.Add( AttributeCache.Read( attributeModel ) ); } } }
/// <summary> /// Adds test data for Events that can be used to test inherited attributes. /// </summary> private static void InitializeInheritedAttributesTestData() { var rockContext = new RockContext(); var EventCalendarPublicId = EventCalendarCache.All().First(x => x.Name == "Public").Id; var EventCalendarInternalId = EventCalendarCache.All().First(x => x.Name == "Internal").Id; // Add Attributes for Calendars. // EventItem Attributes are defined per Calendar, so they are directly associated with the EventCalendarItem entity. // The Attributes collection for the EventItem shows the collection of attributes inherited from the EventCalendarItems associated with the event. var attributeService = new AttributeService(rockContext); var textFieldTypeId = FieldTypeCache.GetId(SystemGuid.FieldType.TEXT.AsGuid()).GetValueOrDefault(); // Add Attribute A for Internal Calendar. var attributeAInternal = attributeService.Get(InternalCalendarAttribute1Guid.AsGuid()); if (attributeAInternal != null) { attributeService.Delete(attributeAInternal); rockContext.SaveChanges(); } attributeAInternal = new Rock.Model.Attribute(); attributeAInternal.EntityTypeId = EntityTypeCache.GetId(typeof(EventCalendarItem)); attributeAInternal.EntityTypeQualifierColumn = "EventCalendarId"; attributeAInternal.EntityTypeQualifierValue = EventCalendarInternalId.ToString(); attributeAInternal.Name = InternalCalendarAttribute1Key; attributeAInternal.Key = InternalCalendarAttribute1Key; attributeAInternal.Guid = InternalCalendarAttribute1Guid.AsGuid(); attributeAInternal.FieldTypeId = textFieldTypeId; attributeService.Add(attributeAInternal); rockContext.SaveChanges(); // Add Attribute A for Public Calendar. var attributeAPublic = attributeService.Get(PublicCalendarAttribute1Guid.AsGuid()); if (attributeAPublic != null) { attributeService.Delete(attributeAPublic); rockContext.SaveChanges(); } attributeAPublic = new Rock.Model.Attribute(); attributeAPublic.EntityTypeId = EntityTypeCache.GetId(typeof(EventCalendarItem)); attributeAPublic.EntityTypeQualifierColumn = "EventCalendarId"; attributeAPublic.EntityTypeQualifierValue = EventCalendarPublicId.ToString(); attributeAPublic.Name = PublicCalendarAttribute1Key; attributeAPublic.Key = PublicCalendarAttribute1Key; attributeAPublic.Guid = PublicCalendarAttribute1Guid.AsGuid(); attributeAPublic.FieldTypeId = textFieldTypeId; attributeService.Add(attributeAPublic); rockContext.SaveChanges(); // Create a new Event: "Event A". // This event exists in the Internal and Public calendars. var eventItemService = new EventItemService(rockContext); var eventA = eventItemService.Get(EventAGuid.AsGuid()); if (eventA != null) { eventItemService.Delete(eventA); rockContext.SaveChanges(); } eventA = new EventItem(); eventA.Name = "Event A"; eventA.Guid = EventAGuid.AsGuid(); var eventACalendarInternal = new EventCalendarItem { EventCalendarId = EventCalendarInternalId, Guid = EventACalendarInternalGuid.AsGuid() }; var eventACalendarPublic = new EventCalendarItem { EventCalendarId = EventCalendarPublicId, Guid = EventACalendarPublicGuid.AsGuid() }; eventA.EventCalendarItems.Add(eventACalendarInternal); eventA.EventCalendarItems.Add(eventACalendarPublic); eventItemService.Add(eventA); rockContext.SaveChanges(); // Create a new Event: "Event B". // This event exists in the Internal calendar only. // This event is created manually, to set the ID of Event B to match an Event Calendar Item associated with Event A. // The purpose is to create an EventItem and an EventCalendarItem that have an identical ID, // so we can verify that Attribute values for parent and child entities are correctly differentiated // by Entity Type when they are collated for inherited attributes. var eventB = eventItemService.Get(EventBGuid.AsGuid()); if (eventB != null) { eventItemService.Delete(eventB); rockContext.SaveChanges(); } var matchedID = eventACalendarInternal.Id; var sql = @" SET IDENTITY_INSERT [EventItem] ON ; INSERT INTO [EventItem] (Id, Name, Guid, IsActive) VALUES (@p0, @p1, @p2, 1) ; SET IDENTITY_INSERT [EventItem] OFF ; "; rockContext.Database.ExecuteSqlCommand(sql, matchedID, "Event B", EventBGuid.AsGuid()); eventB = eventItemService.Get(EventBGuid.AsGuid()); var eventBCalendarInternal = new EventCalendarItem { EventCalendarId = EventCalendarInternalId, Guid = EventBCalendarInternalGuid.AsGuid() }; var eventBCalendarPublic = new EventCalendarItem { EventCalendarId = EventCalendarPublicId, Guid = EventBCalendarPublicGuid.AsGuid() }; eventB.EventCalendarItems.Add(eventBCalendarInternal); eventB.EventCalendarItems.Add(eventBCalendarPublic); rockContext.SaveChanges(); // Set Attribute Values. rockContext = new RockContext(); AttributeCache.Clear(); // Set Attribute Values for Event B. eventBCalendarPublic.LoadAttributes(rockContext); eventBCalendarPublic.SetAttributeValue(PublicCalendarAttribute1Key, "Event B Public"); eventBCalendarPublic.SaveAttributeValues(rockContext); rockContext.SaveChanges(); // Set Attribute Values for Event A eventACalendarInternal.LoadAttributes(rockContext); eventACalendarInternal.SetAttributeValue(InternalCalendarAttribute1Key, "Event A Internal"); eventACalendarInternal.SaveAttributeValues(rockContext); eventACalendarPublic.LoadAttributes(rockContext); eventACalendarPublic.SetAttributeValue(PublicCalendarAttribute1Key, "Event A Public"); eventACalendarPublic.SaveAttributeValues(rockContext); rockContext.SaveChanges(); }