コード例 #1
0
ファイル: PrayerSession.cs プロジェクト: waldo2590/Rock
        /// <summary>
        /// Builds the content to be shown.
        /// </summary>
        /// <param name="context">The session context.</param>
        /// <returns>
        /// A string containing the XAML content.
        /// </returns>
        protected virtual string BuildContent(string context = null)
        {
            var            template    = Template;
            var            mergeFields = RequestContext.GetCommonMergeFields();
            SessionContext sessionContext;
            PrayerRequest  request;
            var            rockContext = new RockContext();

            if (context.IsNotNullOrWhiteSpace())
            {
                var prayerRequestService = new PrayerRequestService(rockContext);

                sessionContext = Security.Encryption.DecryptString(context).FromJsonOrNull <SessionContext>();

                //
                // Update the prayer count on the last prayer request.
                //
                var lastRequest = prayerRequestService.Get(sessionContext.RequestIds[sessionContext.Index]);
                lastRequest.PrayerCount = (lastRequest.PrayerCount ?? 0) + 1;
                rockContext.SaveChanges();

                //
                // Move to the next prayer request, or else indicate that we have
                // finished this session.
                //
                sessionContext.Index += 1;
                if (sessionContext.RequestIds.Count > sessionContext.Index)
                {
                    var requestId = sessionContext.RequestIds[sessionContext.Index];
                    request = prayerRequestService.Get(requestId);
                }
                else
                {
                    request = null;
                }
            }
            else
            {
                var query = GetPrayerRequests(rockContext);

                sessionContext = new SessionContext
                {
                    RequestIds = query.Select(a => a.Id).ToList()
                };

                request = query.FirstOrDefault();
            }

            mergeFields.Add("PrayedButtonText", PrayedButtonText);
            mergeFields.Add("ShowFollowButton", ShowFollowButton);
            mergeFields.Add("ShowInappropriateButton", ShowInappropriateButton);
            mergeFields.Add("SessionContext", Security.Encryption.EncryptString(sessionContext.ToJson()));
            mergeFields.Add("Request", request);

            return(template.ResolveMergeFields(mergeFields));
        }
コード例 #2
0
        /// <summary>
        /// Handles the edit Click event of the lbEdit 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 lbEdit_Click(object sender, EventArgs e)
        {
            PrayerRequestService service = new PrayerRequestService(new RockContext());
            PrayerRequest        item    = service.Get(hfPrayerRequestId.ValueAsInt());

            ShowEditDetails(item);
        }
コード例 #3
0
        /// <summary>
        /// Prays for the specified request and optionally launches a workflow
        /// and/or records an interaction.
        /// </summary>
        /// <param name="prayerRequestGuid">The prayer request unique identifier that is being prayed for.</param>
        /// <param name="currentPerson">The current person whom is performing the action.</param>
        /// <param name="launchWorkflowGuid">The workflow type unique identifier to be launched.</param>
        /// <param name="recordInteraction">If set to <c>true</c> then an interaction will be recorded.</param>
        /// <param name="interactionSummary">The interaction summary text.</param>
        /// <param name="userAgent">The user agent for the interaction.</param>
        /// <param name="clientIpAddress">The client IP address for the interaction.</param>
        /// <param name="sessionGuid">The session unique identifier for the interaction.</param>
        /// <exception cref="System.ArgumentNullException">prayerRequest</exception>
        private static bool PrayForRequest(Guid prayerRequestGuid, Person currentPerson, Guid?launchWorkflowGuid, bool recordInteraction, string interactionSummary, string userAgent, string clientIpAddress, Guid?sessionGuid)
        {
            using (var rockContext = new RockContext())
            {
                var prayerRequestService = new PrayerRequestService(rockContext);
                var prayerRequest        = prayerRequestService.Get(prayerRequestGuid);

                if (prayerRequest == null)
                {
                    return(false);
                }

                prayerRequest.PrayerCount = (prayerRequest.PrayerCount ?? 0) + 1;

                rockContext.SaveChanges();

                if (launchWorkflowGuid.HasValue)
                {
                    PrayerRequestService.LaunchPrayedForWorkflow(prayerRequest, launchWorkflowGuid.Value, currentPerson);
                }

                if (recordInteraction)
                {
                    PrayerRequestService.EnqueuePrayerInteraction(prayerRequest, currentPerson, interactionSummary, userAgent, clientIpAddress, sessionGuid);
                }

                return(true);
            }
        }
コード例 #4
0
        /// <summary>
        /// Binds all comments for the given prayer request Id.
        /// </summary>
        /// <param name="prayerRequestId">the id of a prayer request</param>
        private void ShowComments(int prayerRequestId)
        {
            PrayerRequestService prayerRequestService = new PrayerRequestService();
            var prayerRequest = prayerRequestService.Get(prayerRequestId);

            ShowComments(prayerRequest);
        }
コード例 #5
0
        /// <summary>
        /// Handler that gets the next prayer request and updates its prayer count.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbNext_Click(object sender, EventArgs e)
        {
            int index = hfPrayerIndex.ValueAsInt();

            index++;

            List <int> prayerRequestIds = (List <int>)Session[_sessionKey];
            int        currentNumber    = index + 1;

            if (currentNumber <= prayerRequestIds.Count)
            {
                UpdateSessionCountLabel(currentNumber, prayerRequestIds.Count);

                hfPrayerIndex.Value = index.ToString();
                PrayerRequestService service = new PrayerRequestService();
                PrayerRequest        request = service.Get(prayerRequestIds[index]);
                ShowPrayerRequest(request, service);
            }
            else
            {
                pnlFinished.Visible = true;
                pnlPrayer.Visible   = false;
                lbStartAgain.Focus();
            }
        }
コード例 #6
0
        /// <summary>
        /// Handles the Click event of the lbDelete 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 lbDelete_Click(object sender, EventArgs e)
        {
            int prayerRequestId = hfPrayerRequestId.ValueAsInt();

            if (!IsUserAuthorized(Authorization.EDIT))
            {
                maWarning.Show("You are not authorized to delete this request.", ModalAlertType.Information);
                return;
            }

            var rockContext = new RockContext();
            PrayerRequestService prayerRequestService = new PrayerRequestService(rockContext);
            PrayerRequest        prayerRequest        = prayerRequestService.Get(prayerRequestId);

            if (prayerRequest != null)
            {
                DeleteAllRelatedNotes(prayerRequest, rockContext);

                string errorMessage;
                if (!prayerRequestService.CanDelete(prayerRequest, out errorMessage))
                {
                    maWarning.Show(errorMessage, ModalAlertType.Information);
                    return;
                }

                prayerRequestService.Delete(prayerRequest);
                rockContext.SaveChanges();
                NavigateToParentPage();
            }
        }
コード例 #7
0
        /// <summary>
        /// Handles the Delete event of the gPrayerRequests 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 gPrayerRequests_Delete(object sender, RowEventArgs e)
        {
            RockTransactionScope.WrapTransaction(() =>
            {
                PrayerRequestService prayerRequestService = new PrayerRequestService();
                PrayerRequest prayerRequest = prayerRequestService.Get((int)e.RowKeyValue);

                if (prayerRequest != null)
                {
                    DeleteAllRelatedNotes(prayerRequest);

                    string errorMessage;
                    if (!prayerRequestService.CanDelete(prayerRequest, out errorMessage))
                    {
                        maGridWarning.Show(errorMessage, ModalAlertType.Information);
                        return;
                    }

                    prayerRequestService.Delete(prayerRequest, CurrentPersonId);
                    prayerRequestService.Save(prayerRequest, CurrentPersonId);
                }
            });

            BindGrid();
        }
コード例 #8
0
        /// <summary>
        /// Saves the prayer request.
        /// </summary>
        private void SaveRequest()
        {
            PrayerRequest        prayerRequest;
            PrayerRequestService prayerRequestService = new PrayerRequestService();

            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, CurrentPersonId);
                prayerRequest.EnteredDate = DateTime.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.ApprovedOnDate     = DateTime.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;
                }
            }
            // 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.CategoryId    = cpCategory.SelectedValueAsInt();
            prayerRequest.FirstName     = tbFirstName.Text;
            prayerRequest.LastName      = tbLastName.Text;
            prayerRequest.Text          = tbText.Text;
            prayerRequest.Answer        = tbAnswer.Text;

            if (!Page.IsValid)
            {
                return;
            }

            if (!prayerRequest.IsValid)
            {
                // field controls render error messages
                return;
            }

            prayerRequestService.Save(prayerRequest, CurrentPersonId);

            NavigateToParentPage();
        }
コード例 #9
0
        /// <summary>
        /// Pray Request.
        /// </summary>
        private void PrayRequest(int prayerRequestId)
        {
            var           rockContext = new RockContext();
            var           service     = new PrayerRequestService(rockContext);
            var           flagLimit   = GetAttributeValue(AttributeKey.FlagLimit).AsIntegerOrNull() ?? 1;
            PrayerRequest request     = service.Get(prayerRequestId);

            if (request != null)
            {
                request.PrayerCount = (request.PrayerCount ?? 0) + 1;
                rockContext.SaveChanges();

                StartWorkflow(request, rockContext, AttributeKey.PrayedWorkflow);
                PrayerRequestService.EnqueuePrayerInteraction(request, CurrentPerson, PageCache.Layout.Site.Name, Request.UserAgent, RockPage.GetClientIpAddress(), RockPage.Session["RockSessionId"]?.ToString().AsGuidOrNull());
            }
        }
コード例 #10
0
        /// <summary>
        /// Handles the CheckChanged event of the gPrayerRequests IsApproved field.
        /// </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 gPrayerRequests_CheckChanged(object sender, RowEventArgs e)
        {
            bool failure = true;

            if (e.RowKeyValue != null)
            {
                var rockContext = new RockContext();
                PrayerRequestService prayerRequestService = new PrayerRequestService(rockContext);
                PrayerRequest        prayerRequest        = prayerRequestService.Get(e.RowKeyId);

                if (prayerRequest != null)
                {
                    failure = false;

                    // if it was approved, set it to unapproved... otherwise
                    if (prayerRequest.IsApproved ?? false)
                    {
                        prayerRequest.IsApproved = false;
                    }
                    else
                    {
                        prayerRequest.IsApproved = true;
                        prayerRequest.ApprovedByPersonAliasId = CurrentPersonAliasId;
                        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;
                        }

                        var expireDays = Convert.ToDouble(GetAttributeValue("ExpireDays"));
                        prayerRequest.ExpirationDate = RockDateTime.Now.AddDays(expireDays);
                    }

                    rockContext.SaveChanges();
                }

                BindGrid();
            }

            if (failure)
            {
                maGridWarning.Show("Unable to approve that prayer request", ModalAlertType.Warning);
            }
        }
コード例 #11
0
        /// <summary>
        /// Flag Prayer Request.
        /// </summary>
        private void FlagPrayerRequest(int prayerRequestId)
        {
            var           rockContext = new RockContext();
            var           service     = new PrayerRequestService(rockContext);
            var           flagLimit   = GetAttributeValue(AttributeKey.FlagLimit).AsIntegerOrNull() ?? 1;
            PrayerRequest request     = service.Get(prayerRequestId);

            if (request != null)
            {
                request.FlagCount = (request.FlagCount ?? 0) + 1;
                if (request.FlagCount >= flagLimit)
                {
                    request.IsApproved = false;
                }

                rockContext.SaveChanges();

                StartWorkflow(request, rockContext, AttributeKey.FlaggedWorkflow);
            }
        }
コード例 #12
0
        /// <summary>
        /// Handles the SaveClick event of the mdFlag control and flags the prayer request and moves to the next.
        /// </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 mdFlag_SaveClick(object sender, EventArgs e)
        {
            int prayerRequestId = hfIdValue.ValueAsInt();

            var service = new PrayerRequestService();

            PrayerRequest request = service.Get(prayerRequestId);

            if (request != null)
            {
                request.FlagCount = (request.FlagCount ?? 0) + 1;
                if (request.FlagCount >= _flagLimit)
                {
                    request.IsApproved = false;
                }
                service.Save(request, this.CurrentPersonId);
            }

            mdFlag.Hide();
            lbNext_Click(sender, e);
        }
コード例 #13
0
        /// <summary>
        /// Handles the CheckChanged event of the gPrayerRequests IsApproved field.
        /// </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 gPrayerRequests_CheckChanged(object sender, RowEventArgs e)
        {
            bool failure = true;

            if (e.RowKeyValue != null)
            {
                PrayerRequestService prayerRequestService = new PrayerRequestService();
                PrayerRequest        prayerRequest        = prayerRequestService.Get((int)e.RowKeyValue);

                if (prayerRequest != null)
                {
                    failure = false;
                    // if it was approved, set it to unapproved... otherwise
                    if (prayerRequest.IsApproved ?? false)
                    {
                        prayerRequest.IsApproved = false;
                    }
                    else
                    {
                        prayerRequest.IsApproved         = true;
                        prayerRequest.ApprovedByPersonId = CurrentPerson.Id;
                        prayerRequest.ApprovedOnDate     = DateTime.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;
                        }
                    }

                    prayerRequestService.Save(prayerRequest, CurrentPersonId);
                }

                BindGrid();
            }

            if (failure)
            {
                mdGridWarning.Show("Unable to approve that prayer request", ModalAlertType.Warning);
            }
        }
コード例 #14
0
        /// <summary>
        /// Handles the SaveClick event of the mdFlag control and flags the prayer request and moves to the next.
        /// </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 mdFlag_SaveClick(object sender, EventArgs e)
        {
            int prayerRequestId = hfIdValue.ValueAsInt();

            var rockContext = new RockContext();
            var service     = new PrayerRequestService(rockContext);

            PrayerRequest request = service.Get(prayerRequestId);

            if (request != null)
            {
                request.FlagCount = (request.FlagCount ?? 0) + 1;
                if (request.FlagCount >= _flagLimit)
                {
                    request.IsApproved = false;
                }

                rockContext.SaveChanges();
            }

            mdFlag.Hide();
            lbNext_Click(sender, e);
        }
コード例 #15
0
        /// <summary>
        /// Handles the Delete event of the gPrayerRequests 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 gPrayerRequests_Delete(object sender, RowEventArgs e)
        {
            var rockContext = new RockContext();
            PrayerRequestService prayerRequestService = new PrayerRequestService(rockContext);
            PrayerRequest        prayerRequest        = prayerRequestService.Get(e.RowKeyId);

            if (prayerRequest != null)
            {
                DeleteAllRelatedNotes(prayerRequest, rockContext);

                string errorMessage;
                if (!prayerRequestService.CanDelete(prayerRequest, out errorMessage))
                {
                    maGridWarning.Show(errorMessage, ModalAlertType.Information);
                    return;
                }

                prayerRequestService.Delete(prayerRequest);
                rockContext.SaveChanges();
            }

            BindGrid();
        }
コード例 #16
0
        /// <summary>
        /// Deletes the request and returns a new set of requests.
        /// </summary>
        /// <returns>
        /// The response to send back to the client.
        /// </returns>
        private CallbackResponse DeleteRequest(Guid requestGuid)
        {
            using (var rockContext = new RockContext())
            {
                var prayerRequestService = new PrayerRequestService(rockContext);
                var prayerRequest        = prayerRequestService.Get(requestGuid);

                if (prayerRequest == null)
                {
                    return(new CallbackResponse
                    {
                        Error = "We couldn't find that prayer request."
                    });
                }

                var canDelete = prayerRequest.RequestedByPersonAlias != null && prayerRequest.RequestedByPersonAlias.PersonId == RequestContext.CurrentPerson?.Id;

                if (!canDelete)
                {
                    return(new CallbackResponse
                    {
                        Error = "You do not have permission to delete this prayer request."
                    });
                }

                prayerRequestService.Delete(prayerRequest);

                // Save all changes to database.
                rockContext.SaveChanges();
            }

            return(new CallbackResponse
            {
                Content = BuildContent()
            });
        }
コード例 #17
0
        /// <summary>
        /// Handles the Click event of the lbDelete 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 lbDelete_Click(object sender, EventArgs e)
        {
            int prayerRequestId = hfPrayerRequestId.ValueAsInt();

            if (!IsUserAuthorized(Authorization.EDIT))
            {
                maWarning.Show("You are not authorized to delete this request.", ModalAlertType.Information);
                return;
            }

            var rockContext = new RockContext();
            PrayerRequestService prayerRequestService = new PrayerRequestService(rockContext);
            PrayerRequest        prayerRequest        = prayerRequestService.Get(prayerRequestId);

            if (prayerRequest != null)
            {
                DeleteAllRelatedNotes(prayerRequest, rockContext);

                string errorMessage;
                if (!prayerRequestService.CanDelete(prayerRequest, out errorMessage))
                {
                    maWarning.Show(errorMessage, ModalAlertType.Information);
                    return;
                }

                prayerRequestService.Delete(prayerRequest);
                rockContext.SaveChanges();

                var queryParms = new Dictionary <string, string>();
                if (!string.IsNullOrWhiteSpace(PageParameter("PersonId")))
                {
                    queryParms.Add("PersonId", PageParameter("PersonId"));
                }
                NavigateToParentPage(queryParms);
            }
        }
コード例 #18
0
        /// <summary>
        /// Saves the prayer request.
        /// </summary>
        /// <param name="parameters">The parameters.</param>
        /// <returns>The response to send back to the client.</returns>
        private CallbackResponse SaveRequest(Dictionary <string, object> parameters)
        {
            using (var rockContext = new RockContext())
            {
                var           prayerRequestService = new PrayerRequestService(rockContext);
                PrayerRequest prayerRequest;
                var           requestGuid = RequestContext.GetPageParameter(PageParameterKeys.RequestGuid).AsGuidOrNull();

                if (requestGuid.HasValue)
                {
                    prayerRequest = prayerRequestService.Get(requestGuid.Value);

                    if (prayerRequest == null || !BlockCache.IsAuthorized(Authorization.EDIT, RequestContext.CurrentPerson))
                    {
                        return(new CallbackResponse
                        {
                            Error = "You are not authorized to edit prayer requests."
                        });
                    }
                }
                else
                {
                    int?categoryId = null;

                    if (DefaultCategory.HasValue)
                    {
                        categoryId = CategoryCache.Get(DefaultCategory.Value).Id;
                    }

                    prayerRequest = new PrayerRequest
                    {
                        Id              = 0,
                        IsActive        = true,
                        IsApproved      = EnableAutoApprove,
                        AllowComments   = false,
                        EnteredDateTime = RockDateTime.Now,
                        CategoryId      = categoryId
                    };
                    prayerRequestService.Add(prayerRequest);

                    if (EnableAutoApprove)
                    {
                        prayerRequest.ApprovedByPersonAliasId = RequestContext.CurrentPerson?.PrimaryAliasId;
                        prayerRequest.ApprovedOnDateTime      = RockDateTime.Now;
                        prayerRequest.ExpirationDate          = RockDateTime.Now.AddDays(ExpiresAfterDays);
                    }
                }

                prayerRequest.FirstName = ( string )parameters["firstName"];
                prayerRequest.LastName  = ( string )parameters["lastName"];
                prayerRequest.Email     = ( string )parameters["email"];
                prayerRequest.Text      = ( string )parameters["request"];

                if (ShowCampus)
                {
                    if (parameters.ContainsKey("campus"))
                    {
                        var campusGuid = (( string )parameters["campus"]).AsGuidOrNull();

                        if (campusGuid.HasValue)
                        {
                            prayerRequest.CampusId = CampusCache.Get(campusGuid.Value).Id;
                        }
                    }
                    else
                    {
                        prayerRequest.CampusId = CampusCache.All().FirstOrDefault(a => a.IsActive ?? false)?.Id;
                    }
                }

                if (ShowCategory && parameters.ContainsKey("category"))
                {
                    var categoryGuid = (( string )parameters["category"]).AsGuidOrNull();

                    if (categoryGuid.HasValue)
                    {
                        prayerRequest.CategoryId = CategoryCache.Get(categoryGuid.Value).Id;
                    }
                    else if (prayerRequest.Id > 0)
                    {
                        prayerRequest.CategoryId = null;
                    }
                }

                if (ShowPublicDisplayFlag)
                {
                    prayerRequest.IsPublic = ( bool )parameters["allowPublication"];
                }

                if (ShowUrgentFlag)
                {
                    prayerRequest.IsUrgent = ( bool )parameters["urgent"];
                }

                if (RequestContext.CurrentPerson != null)
                {
                    //
                    // If there is a logged in person and the names still match, meaning they are not
                    // entering a prayer request for somebody else, then set the requested by property.
                    //
                    var person = RequestContext.CurrentPerson;
                    if (prayerRequest.FirstName == person.FirstName && prayerRequest.LastName == person.LastName)
                    {
                        prayerRequest.RequestedByPersonAliasId = person.PrimaryAliasId;
                    }
                }
                else
                {
                    //
                    // If there is not a logged in person, try to match to an existing person.
                    //
                    var person = MatchPerson(prayerRequest, rockContext);

                    if (person != null)
                    {
                        prayerRequest.RequestedByPersonAliasId = person.PrimaryAliasId;
                    }
                }

                //
                // Save all changes to database.
                //
                rockContext.SaveChanges();

                StartWorkflow(prayerRequest, rockContext);
            }

            if (CompletionAction == 0)
            {
                return(new CallbackResponse
                {
                    Content = CompletionXaml ?? string.Empty
                });
            }
            else if (CompletionAction == 1)
            {
                return(new CallbackResponse
                {
                    Command = "PopPage",
                    CommandParameter = "true"
                });
            }
            else
            {
                return(new CallbackResponse
                {
                    Content = AttributeDefaults.CompletionXaml
                });
            }
        }
コード例 #19
0
        /// <summary>
        /// Saves the prayer request.
        /// </summary>
        private void SaveRequest()
        {
            var                  rockContext = new RockContext();
            PrayerRequest        prayerRequest;
            PrayerRequestService prayerRequestService = new PrayerRequestService(rockContext);

            int prayerRequestId = hfPrayerRequestId.Value.AsInteger();

            // 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 (ppRequestor.PersonId.HasValue)
            {
                prayerRequest.RequestedByPersonAliasId = ppRequestor.PersonAliasId;
            }

            // If changing from NOT-approved to approved, record who and when
            if (!(prayerRequest.IsApproved ?? false) && hfApprovedStatus.Value.AsBoolean())
            {
                prayerRequest.ApprovedByPersonAliasId = CurrentPersonAliasId;
                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;
            }

            prayerRequest.CampusId   = cpCampus.SelectedCampusId;
            prayerRequest.CategoryId = catpCategory.SelectedValueAsInt();

            // Now record all the bits...
            prayerRequest.IsApproved    = hfApprovedStatus.Value.AsBoolean();
            prayerRequest.IsActive      = cbIsActive.Checked;
            prayerRequest.IsUrgent      = cbIsUrgent.Checked;
            prayerRequest.AllowComments = cbAllowComments.Checked;
            prayerRequest.IsPublic      = cbIsPublic.Checked;
            prayerRequest.FirstName     = tbFirstName.Text;
            prayerRequest.LastName      = tbLastName.Text;
            prayerRequest.Email         = tbEmail.Text;
            prayerRequest.Text          = dtbText.Text.Trim();
            prayerRequest.Answer        = dtbAnswer.Text.Trim();

            prayerRequest.LoadAttributes(rockContext);
            Rock.Attribute.Helper.GetEditValues(phAttributes, prayerRequest);

            if (!Page.IsValid)
            {
                return;
            }

            if (!prayerRequest.IsValid)
            {
                // field controls render error messages
                return;
            }

            rockContext.SaveChanges();
            prayerRequest.SaveAttributeValues(rockContext);

            var queryParms = new Dictionary <string, string>();

            if (!string.IsNullOrWhiteSpace(PageParameter("PersonId")))
            {
                queryParms.Add("PersonId", PageParameter("PersonId"));
            }

            NavigateToParentPage(queryParms);
        }
コード例 #20
0
        /// <summary>
        /// Handles the Click event of the lbSave 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 lbSave_Click(object sender, EventArgs e)
        {
            var rockContext = new RockContext();

            rockContext.WrapTransaction(() =>
            {
                if (contextEntity is Person)
                {
                    var personService = new PersonService(rockContext);
                    var changes       = new History.HistoryChangeList();
                    var _person       = personService.Get(contextEntity.Id);

                    History.EvaluateChange(changes, "Foreign Key", _person.ForeignKey, tbForeignKey.Text);
                    _person.ForeignKey = tbForeignKey.Text;

                    History.EvaluateChange(changes, "Foreign Guid", _person.ForeignGuid.ToString(), tbForeignGuid.Text);
                    _person.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();

                    History.EvaluateChange(changes, "Foreign Id", _person.ForeignId.ToString(), tbForeignId.Text);
                    _person.ForeignId = tbForeignId.Text.AsType <int?>();

                    if (rockContext.SaveChanges() > 0)
                    {
                        if (changes.Any())
                        {
                            HistoryService.SaveChanges(
                                rockContext,
                                typeof(Person),
                                Rock.SystemGuid.Category.HISTORY_PERSON_DEMOGRAPHIC_CHANGES.AsGuid(),
                                _person.Id,
                                changes);
                        }
                    }
                }
                else if (contextEntity is FinancialAccount)
                {
                    var accountService = new FinancialAccountService(rockContext);
                    var _account       = accountService.Get(contextEntity.Id);

                    _account.ForeignKey  = tbForeignKey.Text;
                    _account.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();
                    _account.ForeignId   = tbForeignId.Text.AsType <int?>();

                    rockContext.SaveChanges();
                }
                else if (contextEntity is FinancialBatch)
                {
                    var batchService = new FinancialBatchService(rockContext);
                    var changes      = new History.HistoryChangeList();
                    var _batch       = batchService.Get(contextEntity.Id);

                    History.EvaluateChange(changes, "Foreign Key", _batch.ForeignKey, tbForeignKey.Text);
                    _batch.ForeignKey = tbForeignKey.Text;

                    History.EvaluateChange(changes, "Foreign Guid", _batch.ForeignGuid.ToString(), tbForeignGuid.Text);
                    _batch.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();

                    History.EvaluateChange(changes, "Foreign Id", _batch.ForeignId.ToString(), tbForeignId.Text);
                    _batch.ForeignId = tbForeignId.Text.AsType <int?>();

                    if (rockContext.SaveChanges() > 0)
                    {
                        if (changes.Any())
                        {
                            HistoryService.SaveChanges(
                                rockContext,
                                typeof(FinancialBatch),
                                Rock.SystemGuid.Category.HISTORY_FINANCIAL_BATCH.AsGuid(),
                                _batch.Id,
                                changes);
                        }
                    }
                }
                else if (contextEntity is FinancialPledge)
                {
                    var pledgeService = new FinancialPledgeService(rockContext);
                    var _pledge       = pledgeService.Get(contextEntity.Id);

                    _pledge.ForeignKey  = tbForeignKey.Text;
                    _pledge.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();
                    _pledge.ForeignId   = tbForeignId.Text.AsType <int?>();

                    rockContext.SaveChanges();
                }
                else if (contextEntity is FinancialTransaction)
                {
                    var transactionService = new FinancialTransactionService(rockContext);
                    var changes            = new History.HistoryChangeList();
                    var _transaction       = transactionService.Get(contextEntity.Id);

                    History.EvaluateChange(changes, "Foreign Key", _transaction.ForeignKey, tbForeignKey.Text);
                    _transaction.ForeignKey = tbForeignKey.Text;

                    History.EvaluateChange(changes, "Foreign Guid", _transaction.ForeignGuid.ToString(), tbForeignGuid.Text);
                    _transaction.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();

                    History.EvaluateChange(changes, "Foreign Id", _transaction.ForeignId.ToString(), tbForeignId.Text);
                    _transaction.ForeignId = tbForeignId.Text.AsType <int?>();

                    if (rockContext.SaveChanges() > 0)
                    {
                        if (changes.Any())
                        {
                            HistoryService.SaveChanges(
                                rockContext,
                                typeof(FinancialTransaction),
                                Rock.SystemGuid.Category.HISTORY_FINANCIAL_TRANSACTION.AsGuid(),
                                _transaction.Id,
                                changes);
                        }
                    }
                }
                else if (contextEntity is FinancialScheduledTransaction)
                {
                    var transactionScheduledService = new FinancialScheduledTransactionService(rockContext);
                    var _scheduledTransaction       = transactionScheduledService.Get(contextEntity.Id);

                    _scheduledTransaction.ForeignKey  = tbForeignKey.Text;
                    _scheduledTransaction.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();
                    _scheduledTransaction.ForeignId   = tbForeignId.Text.AsType <int?>();

                    rockContext.SaveChanges();
                }
                else if (contextEntity is Group)
                {
                    var groupService = new GroupService(rockContext);
                    var _group       = groupService.Get(contextEntity.Id);

                    _group.ForeignKey  = tbForeignKey.Text;
                    _group.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();
                    _group.ForeignId   = tbForeignId.Text.AsType <int?>();

                    rockContext.SaveChanges();
                }
                else if (contextEntity is GroupMember)
                {
                    var groupMemberService = new GroupMemberService(rockContext);
                    var changes            = new History.HistoryChangeList();
                    var _groupMember       = groupMemberService.Get(contextEntity.Id);

                    History.EvaluateChange(changes, "Foreign Key", _groupMember.ForeignKey, tbForeignKey.Text);
                    _groupMember.ForeignKey = tbForeignKey.Text;

                    History.EvaluateChange(changes, "Foreign Guid", _groupMember.ForeignGuid.ToString(), tbForeignGuid.Text);
                    _groupMember.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();

                    History.EvaluateChange(changes, "Foreign Id", _groupMember.ForeignId.ToString(), tbForeignId.Text);
                    _groupMember.ForeignId = tbForeignId.Text.AsType <int?>();

                    if (rockContext.SaveChanges() > 0)
                    {
                        if (changes.Any())
                        {
                            HistoryService.SaveChanges(
                                rockContext,
                                typeof(GroupMember),
                                Rock.SystemGuid.Category.HISTORY_PERSON_GROUP_MEMBERSHIP.AsGuid(),
                                _groupMember.Id,
                                changes);
                        }
                    }
                }
                else if (contextEntity is Metric)
                {
                    var metricService = new MetricService(rockContext);
                    var _metric       = metricService.Get(contextEntity.Id);

                    _metric.ForeignKey  = tbForeignKey.Text;
                    _metric.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();
                    _metric.ForeignId   = tbForeignId.Text.AsType <int?>();

                    rockContext.SaveChanges();
                }
                else if (contextEntity is Location)
                {
                    var locationService = new LocationService(rockContext);
                    var _location       = locationService.Get(contextEntity.Id);

                    _location.ForeignKey  = tbForeignKey.Text;
                    _location.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();
                    _location.ForeignId   = tbForeignId.Text.AsType <int?>();

                    rockContext.SaveChanges();
                }
                else if (contextEntity is PrayerRequest)
                {
                    var prayerRequestService = new PrayerRequestService(rockContext);
                    var _request             = prayerRequestService.Get(contextEntity.Id);

                    _request.ForeignKey  = tbForeignKey.Text;
                    _request.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();
                    _request.ForeignId   = tbForeignId.Text.AsType <int?>();

                    rockContext.SaveChanges();
                }
                else if (contextEntity is ContentChannel)
                {
                    var contentChannelService = new ContentChannelService(rockContext);
                    var _channel = contentChannelService.Get(contextEntity.Id);

                    _channel.ForeignKey  = tbForeignKey.Text;
                    _channel.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();
                    _channel.ForeignId   = tbForeignId.Text.AsType <int?>();

                    rockContext.SaveChanges();
                }
                else if (contextEntity is ContentChannelItem)
                {
                    var contentChannelItemService = new ContentChannelItemService(rockContext);
                    var _item = contentChannelItemService.Get(contextEntity.Id);

                    _item.ForeignKey  = tbForeignKey.Text;
                    _item.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();
                    _item.ForeignId   = tbForeignId.Text.AsType <int?>();

                    rockContext.SaveChanges();
                }
            });

            Page.Response.Redirect(Page.Request.Url.ToString(), true);
        }
コード例 #21
0
        /// <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();
        }