示例#1
0
        /// <summary>
        /// Binds the grid.
        /// </summary>
        private void BindGrid()
        {
            var rockContext = new RockContext();

            var communications = new CommunicationService(rockContext)
                                 .Queryable().AsNoTracking()
                                 .Where(c => c.Status != CommunicationStatus.Transient);

            string subject = tbSubject.Text;

            if (!string.IsNullOrWhiteSpace(subject))
            {
                communications = communications.Where(c => (string.IsNullOrEmpty(c.Subject) && c.Name.Contains(subject)) || c.Subject.Contains(subject));
            }

            var communicationType = ddlType.SelectedValueAsEnumOrNull <CommunicationType>();

            if (communicationType != null)
            {
                communications = communications.Where(c => c.CommunicationType == communicationType);
            }

            string status = ddlStatus.SelectedValue;

            if (!string.IsNullOrWhiteSpace(status))
            {
                var communicationStatus = ( CommunicationStatus )System.Enum.Parse(typeof(CommunicationStatus), status);
                communications = communications.Where(c => c.Status == communicationStatus);
            }

            if (canApprove)
            {
                if (ppSender.PersonId.HasValue)
                {
                    communications = communications
                                     .Where(c =>
                                            c.SenderPersonAlias != null &&
                                            c.SenderPersonAlias.PersonId == ppSender.PersonId.Value);
                }
            }
            else
            {
                // If can't approve, only show current person's communications
                communications = communications
                                 .Where(c =>
                                        c.SenderPersonAlias != null &&
                                        c.SenderPersonAlias.PersonId == CurrentPersonId);
            }

            if (drpCreatedDates.LowerValue.HasValue)
            {
                communications = communications.Where(a => a.CreatedDateTime.HasValue && a.CreatedDateTime.Value >= drpCreatedDates.LowerValue.Value);
            }

            if (drpCreatedDates.UpperValue.HasValue)
            {
                DateTime upperDate = drpCreatedDates.UpperValue.Value.Date.AddDays(1);
                communications = communications.Where(a => a.CreatedDateTime.HasValue && a.CreatedDateTime.Value < upperDate);
            }

            if (drpSentDates.LowerValue.HasValue)
            {
                communications = communications.Where(a => (a.SendDateTime ?? a.FutureSendDateTime) >= drpSentDates.LowerValue.Value);
            }

            if (drpSentDates.UpperValue.HasValue)
            {
                DateTime upperDate = drpSentDates.UpperValue.Value.Date.AddDays(1);
                communications = communications.Where(a => (a.SendDateTime ?? a.FutureSendDateTime) < upperDate);
            }

            string content = tbContent.Text;

            if (!string.IsNullOrWhiteSpace(content))
            {
                communications = communications.Where(c =>
                                                      c.Message.Contains(content) ||
                                                      c.SMSMessage.Contains(content) ||
                                                      c.PushMessage.Contains(content));
            }

            var recipients = new CommunicationRecipientService(rockContext).Queryable();

            // We want to limit to only communications that they are authorized to view, but if there are a large number of communications, that could be very slow.
            // So, since communication security is based on CommunicationTemplate, take a shortcut and just limit based on authorized communication templates
            var authorizedCommunicationTemplateIds = new CommunicationTemplateService(rockContext).Queryable()
                                                     .Where(a => communications.Where(x => x.CommunicationTemplateId.HasValue).Select(x => x.CommunicationTemplateId.Value).Distinct().Contains(a.Id))
                                                     .ToList().Where(a => a.IsAuthorized(Rock.Security.Authorization.VIEW, this.CurrentPerson)).Select(a => a.Id).ToList();

            var queryable = communications.Where(a => a.CommunicationTemplateId == null || authorizedCommunicationTemplateIds.Contains(a.CommunicationTemplateId.Value))
                            .Select(c => new CommunicationItem
            {
                Id = c.Id,
                CommunicationType   = c.CommunicationType,
                Subject             = string.IsNullOrEmpty(c.Subject) ? (string.IsNullOrEmpty(c.PushTitle) ? c.Name : c.PushTitle) : c.Subject,
                CreatedDateTime     = c.CreatedDateTime,
                SendDateTime        = c.SendDateTime ?? c.FutureSendDateTime,
                SendDateTimePrefix  = c.SendDateTime == null && c.FutureSendDateTime != null ? "<span class='label label-info'>Future</span>&nbsp;" : "",
                Sender              = c.SenderPersonAlias != null ? c.SenderPersonAlias.Person : null,
                ReviewedDateTime    = c.ReviewedDateTime,
                Reviewer            = c.ReviewerPersonAlias != null ? c.ReviewerPersonAlias.Person : null,
                Status              = c.Status,
                Recipients          = recipients.Where(r => r.CommunicationId == c.Id).Count(),
                PendingRecipients   = recipients.Where(r => r.CommunicationId == c.Id && r.Status == CommunicationRecipientStatus.Pending).Count(),
                CancelledRecipients = recipients.Where(r => r.CommunicationId == c.Id && r.Status == CommunicationRecipientStatus.Cancelled).Count(),
                FailedRecipients    = recipients.Where(r => r.CommunicationId == c.Id && r.Status == CommunicationRecipientStatus.Failed).Count(),
                DeliveredRecipients = recipients.Where(r => r.CommunicationId == c.Id && (r.Status == CommunicationRecipientStatus.Delivered || r.Status == CommunicationRecipientStatus.Opened)).Count(),
                OpenedRecipients    = recipients.Where(r => r.CommunicationId == c.Id && r.Status == CommunicationRecipientStatus.Opened).Count()
            });

            var sortProperty = gCommunication.SortProperty;

            if (sortProperty != null)
            {
                queryable = queryable.Sort(sortProperty);
            }
            else
            {
                queryable = queryable.OrderByDescending(c => c.SendDateTime);
            }

            gCommunication.EntityTypeId = EntityTypeCache.Get <Rock.Model.Communication>().Id;
            nbBindError.Text            = string.Empty;

            try
            {
                gCommunication.SetLinqDataSource(queryable);
                gCommunication.DataBind();
            }
            catch (Exception e)
            {
                ExceptionLogService.LogException(e);

                Exception sqlException = e;
                while (sqlException != null && !(sqlException is System.Data.SqlClient.SqlException))
                {
                    sqlException = sqlException.InnerException;
                }

                nbBindError.Text = string.Format("<p>An error occurred trying to retrieve the communication history. Please try adjusting your filter settings and try again.</p><p>Error: {0}</p>",
                                                 sqlException != null ? sqlException.Message : e.Message);

                gCommunication.DataSource = new List <object>();
                gCommunication.DataBind();
            }
        }
        /// <summary>
        /// Binds the grid.
        /// </summary>
        private void BindGrid()
        {
            var rockContext = new RockContext();

            var communications = new CommunicationService(rockContext)
                                 .Queryable().AsNoTracking()
                                 .Where(c => c.Status != CommunicationStatus.Transient);

            string subject = tbSubject.Text;

            if (!string.IsNullOrWhiteSpace(subject))
            {
                communications = communications.Where(c => c.Subject.Contains(subject));
            }

            var communicationType = ddlType.SelectedValueAsEnumOrNull <CommunicationType>();

            if (communicationType != null)
            {
                communications = communications.Where(c => c.CommunicationType == communicationType);
            }

            string status = ddlStatus.SelectedValue;

            if (!string.IsNullOrWhiteSpace(status))
            {
                var communicationStatus = (CommunicationStatus)System.Enum.Parse(typeof(CommunicationStatus), status);
                communications = communications.Where(c => c.Status == communicationStatus);
            }

            if (canApprove)
            {
                if (ppSender.PersonId.HasValue)
                {
                    communications = communications
                                     .Where(c =>
                                            c.SenderPersonAlias != null &&
                                            c.SenderPersonAlias.PersonId == ppSender.PersonId.Value);
                }
            }
            else
            {
                // If can't approve, only show current person's communications
                communications = communications
                                 .Where(c =>
                                        c.SenderPersonAlias != null &&
                                        c.SenderPersonAlias.PersonId == CurrentPersonId);
            }

            if (drpDates.LowerValue.HasValue)
            {
                communications = communications.Where(a => a.CreatedDateTime >= drpDates.LowerValue.Value);
            }

            if (drpDates.UpperValue.HasValue)
            {
                DateTime upperDate = drpDates.UpperValue.Value.Date.AddDays(1);
                communications = communications.Where(a => a.CreatedDateTime < upperDate);
            }

            string content = tbContent.Text;

            if (!string.IsNullOrWhiteSpace(content))
            {
                communications = communications.Where(c =>
                                                      c.Message.Contains(content) ||
                                                      c.SMSMessage.Contains(content) ||
                                                      c.PushMessage.Contains(content));
            }

            var recipients = new CommunicationRecipientService(rockContext).Queryable();

            // We want to limit to only communications that they are authorized to view, but if there are a large number of communications, that could be very slow.
            // So, since communication security is based on CommunicationTemplate, take a shortcut and just limit based on authorized communication templates
            var authorizedCommunicationTemplateIds = new CommunicationTemplateService(rockContext).Queryable()
                                                     .Where(a => communications.Where(x => x.CommunicationTemplateId.HasValue).Select(x => x.CommunicationTemplateId.Value).Distinct().Contains(a.Id))
                                                     .ToList().Where(a => a.IsAuthorized(Rock.Security.Authorization.VIEW, this.CurrentPerson)).Select(a => a.Id).ToList();

            var queryable = communications.Where(a => a.CommunicationTemplateId == null || authorizedCommunicationTemplateIds.Contains(a.CommunicationTemplateId.Value))
                            .Select(c => new CommunicationItem {
                Id = c.Id,
                CommunicationType   = c.CommunicationType,
                Subject             = c.Subject,
                CreatedDateTime     = c.CreatedDateTime,
                Sender              = c.SenderPersonAlias != null ? c.SenderPersonAlias.Person : null,
                ReviewedDateTime    = c.ReviewedDateTime,
                Reviewer            = c.ReviewerPersonAlias != null ? c.ReviewerPersonAlias.Person : null,
                Status              = c.Status,
                Recipients          = recipients.Where(r => r.CommunicationId == c.Id).Count(),
                PendingRecipients   = recipients.Where(r => r.CommunicationId == c.Id && r.Status == CommunicationRecipientStatus.Pending).Count(),
                CancelledRecipients = recipients.Where(r => r.CommunicationId == c.Id && r.Status == CommunicationRecipientStatus.Cancelled).Count(),
                FailedRecipients    = recipients.Where(r => r.CommunicationId == c.Id && r.Status == CommunicationRecipientStatus.Failed).Count(),
                DeliveredRecipients = recipients.Where(r => r.CommunicationId == c.Id && (r.Status == CommunicationRecipientStatus.Delivered || r.Status == CommunicationRecipientStatus.Opened)).Count(),
                OpenedRecipients    = recipients.Where(r => r.CommunicationId == c.Id && r.Status == CommunicationRecipientStatus.Opened).Count()
            });

            var sortProperty = gCommunication.SortProperty;

            if (sortProperty != null)
            {
                queryable = queryable.Sort(sortProperty);
            }
            else
            {
                queryable = queryable.OrderByDescending(c => c.CreatedDateTime);
            }

            gCommunication.EntityTypeId = EntityTypeCache.Read <Rock.Model.Communication>().Id;
            gCommunication.SetLinqDataSource(queryable);
            gCommunication.DataBind();
        }