/// <summary> /// Saves the prayer request. /// </summary> private void SaveRequest() { var rockContext = new RockContext(); PrayerRequest prayerRequest; PrayerRequestService prayerRequestService = new PrayerRequestService( rockContext ); int prayerRequestId = int.Parse( hfPrayerRequestId.Value ); // Fetch the prayer request or create a new one if needed if ( prayerRequestId == 0 ) { prayerRequest = new PrayerRequest(); prayerRequestService.Add( prayerRequest ); prayerRequest.EnteredDateTime = RockDateTime.Now; } else { prayerRequest = prayerRequestService.Get( prayerRequestId ); } // If changing from NOT-approved to approved, record who and when if ( !( prayerRequest.IsApproved ?? false ) && cbApproved.Checked ) { prayerRequest.ApprovedByPersonId = CurrentPerson.Id; prayerRequest.ApprovedOnDateTime = RockDateTime.Now; // reset the flag count only to zero ONLY if it had a value previously. if ( prayerRequest.FlagCount.HasValue && prayerRequest.FlagCount > 0 ) { prayerRequest.FlagCount = 0; } } // If no expiration date was manually set, then use the default setting. if ( !dpExpirationDate.SelectedDate.HasValue ) { var expireDays = Convert.ToDouble( GetAttributeValue( "ExpireDays" ) ); prayerRequest.ExpirationDate = RockDateTime.Now.AddDays( expireDays ); } else { prayerRequest.ExpirationDate = dpExpirationDate.SelectedDate; } // If no category was selected, then use the default category if there is one. int? categoryId = catpCategory.SelectedValueAsInt(); Guid defaultCategoryGuid = GetAttributeValue( "DefaultCategory" ).AsGuid(); if ( categoryId == null && !defaultCategoryGuid.IsEmpty() ) { var category = new CategoryService( rockContext ).Get( defaultCategoryGuid ); categoryId = category.Id; } prayerRequest.CategoryId = categoryId; // Now record all the bits... prayerRequest.IsApproved = cbApproved.Checked; prayerRequest.IsActive = cbIsActive.Checked; prayerRequest.IsUrgent = cbIsUrgent.Checked; prayerRequest.AllowComments = cbAllowComments.Checked; prayerRequest.IsPublic = cbIsPublic.Checked; prayerRequest.FirstName = dtbFirstName.Text; prayerRequest.LastName = dtbLastName.Text; prayerRequest.Text = dtbText.Text.Trim(); prayerRequest.Answer = dtbAnswer.Text.Trim(); if ( !Page.IsValid ) { return; } if ( !prayerRequest.IsValid ) { // field controls render error messages return; } rockContext.SaveChanges(); NavigateToParentPage(); }
/// <summary> /// Handles the Click event to save the prayer request. /// </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 ( ! IsValid() ) { return; } bool isAutoApproved = GetAttributeValue( "EnableAutoApprove" ).AsBoolean(); bool defaultAllowComments = GetAttributeValue( "DefaultAllowCommentsSetting" ).AsBoolean(); PrayerRequest prayerRequest = new PrayerRequest { Id = 0, IsActive = true, IsApproved = isAutoApproved, AllowComments = defaultAllowComments }; var rockContext = new RockContext(); PrayerRequestService prayerRequestService = new PrayerRequestService( rockContext ); prayerRequestService.Add( prayerRequest ); prayerRequest.EnteredDateTime = RockDateTime.Now; if ( isAutoApproved ) { prayerRequest.ApprovedByPersonAliasId = CurrentPersonAliasId; prayerRequest.ApprovedOnDateTime = RockDateTime.Now; var expireDays = Convert.ToDouble( GetAttributeValue( "ExpireDays" ) ); prayerRequest.ExpirationDate = RockDateTime.Now.AddDays( expireDays ); } // Now record all the bits... int? categoryId = bddlCategory.SelectedValueAsInt(); Guid defaultCategoryGuid = GetAttributeValue( "DefaultCategory" ).AsGuid(); if ( categoryId == null && !defaultCategoryGuid.IsEmpty() ) { var category = new CategoryService( rockContext ).Get( defaultCategoryGuid ); categoryId = category.Id; } prayerRequest.CategoryId = categoryId; prayerRequest.RequestedByPersonAliasId = CurrentPersonAliasId; prayerRequest.FirstName = dtbFirstName.Text; prayerRequest.LastName = dtbLastName.Text; prayerRequest.Email = dtbEmail.Text; prayerRequest.Text = dtbRequest.Text; if ( this.EnableUrgentFlag ) { prayerRequest.IsUrgent = cbIsUrgent.Checked; } else { prayerRequest.IsUrgent = false; } if ( this.EnableCommentsFlag ) { prayerRequest.AllowComments = cbAllowComments.Checked; } if ( this.EnablePublicDisplayFlag ) { prayerRequest.IsPublic = cbAllowPublicDisplay.Checked; } else { prayerRequest.IsPublic = false; } if ( !Page.IsValid ) { return; } if ( !prayerRequest.IsValid ) { // field controls render error messages return; } rockContext.SaveChanges(); bool isNavigateToParent = GetAttributeValue( "NavigateToParentOnSave" ).AsBoolean(); if ( isNavigateToParent ) { NavigateToParentPage(); } else { pnlForm.Visible = false; pnlReceipt.Visible = true; } }
/// <summary> /// Handles the Click event to save the prayer request. /// </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 ( ! IsValid() ) { return; } bool isAutoApproved = GetAttributeValue( "EnableAutoApprove" ).AsBoolean(); bool defaultAllowComments = GetAttributeValue( "DefaultAllowCommentsSetting" ).AsBoolean(); PrayerRequest prayerRequest = new PrayerRequest { Id = 0, IsActive = true, IsApproved = isAutoApproved, AllowComments = defaultAllowComments }; var rockContext = new RockContext(); PrayerRequestService prayerRequestService = new PrayerRequestService( rockContext ); prayerRequestService.Add( prayerRequest ); prayerRequest.EnteredDateTime = RockDateTime.Now; if ( isAutoApproved ) { prayerRequest.ApprovedByPersonAliasId = CurrentPersonAliasId; prayerRequest.ApprovedOnDateTime = RockDateTime.Now; var expireDays = Convert.ToDouble( GetAttributeValue( "ExpireDays" ) ); prayerRequest.ExpirationDate = RockDateTime.Now.AddDays( expireDays ); } // Now record all the bits... // Make sure the Category is hydrated so it's included for any Lava processing Category category; int? categoryId = bddlCategory.SelectedValueAsInt(); Guid defaultCategoryGuid = GetAttributeValue( "DefaultCategory" ).AsGuid(); if ( categoryId == null && !defaultCategoryGuid.IsEmpty() ) { category = new CategoryService( rockContext ).Get( defaultCategoryGuid ); categoryId = category.Id; } else { category = new CategoryService( rockContext ).Get( categoryId.Value ); } prayerRequest.CategoryId = categoryId; prayerRequest.Category = category; prayerRequest.RequestedByPersonAliasId = CurrentPersonAliasId; prayerRequest.FirstName = tbFirstName.Text; prayerRequest.LastName = tbLastName.Text; prayerRequest.Email = tbEmail.Text; prayerRequest.Text = dtbRequest.Text; if ( this.EnableUrgentFlag ) { prayerRequest.IsUrgent = cbIsUrgent.Checked; } else { prayerRequest.IsUrgent = false; } if ( this.EnableCommentsFlag ) { prayerRequest.AllowComments = cbAllowComments.Checked; } if ( this.EnablePublicDisplayFlag ) { prayerRequest.IsPublic = cbAllowPublicDisplay.Checked; } else { prayerRequest.IsPublic = false; } if ( !Page.IsValid ) { return; } if ( !prayerRequest.IsValid ) { // field controls render error messages return; } rockContext.SaveChanges(); StartWorkflow( prayerRequest, rockContext ); bool isNavigateToParent = GetAttributeValue( "NavigateToParentOnSave" ).AsBoolean(); if ( isNavigateToParent ) { NavigateToParentPage(); } else { pnlForm.Visible = false; pnlReceipt.Visible = true; // Build success text that is Lava capable var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields( this.RockPage, this.CurrentPerson ); mergeFields.Add( "PrayerRequest", prayerRequest ); nbMessage.Text = GetAttributeValue( "SaveSuccessText" ).ResolveMergeFields( mergeFields ); // Resolve any dynamic url references string appRoot = ResolveRockUrl( "~/" ); string themeRoot = ResolveRockUrl( "~~/" ); nbMessage.Text = nbMessage.Text.Replace( "~~/", themeRoot ).Replace( "~/", appRoot ); // show liquid help for debug if ( GetAttributeValue( "EnableDebug" ).AsBoolean() && IsUserAuthorized( Authorization.EDIT ) ) { nbMessage.Text += mergeFields.lavaDebugInfo(); } } }