/// <summary>
        /// Sets up a communication model with the user-entered values
        /// </summary>
        /// <returns>The communication for this session.</returns>
        private Rock.Model.Communication SetupCommunication()
        {
            var communication = new Rock.Model.Communication
            {
                Status                = CommunicationStatus.Approved,
                ReviewedDateTime      = RockDateTime.Now,
                ReviewerPersonAliasId = CurrentPersonAliasId,
                SenderPersonAliasId   = CurrentPersonAliasId,
                CommunicationType     = CommunicationType.PushNotification
            };

            communication.EnabledLavaCommands     = GetAttributeValue(AttributeKey.EnabledLavaCommands);
            communication.CommunicationTemplateId = hfSelectedCommunicationTemplateId.Value.AsIntegerOrNull();

            var communicationData       = new CommunicationDetails();
            var pushNotificationControl = phPushControl.Controls[0] as PushNotification;

            if (pushNotificationControl != null)
            {
                pushNotificationControl.UpdateCommunication(communicationData);
            }

            CommunicationDetails.Copy(communicationData, communication);

            return(communication);
        }
Exemple #2
0
        /// <summary>
        /// Gets the communication.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="peopleIds">The people ids.</param>
        /// <returns></returns>
        private Rock.Model.Communication GetCommunication(RockContext rockContext, List <int> peopleIds)
        {
            var communicationService = new CommunicationService(rockContext);
            var recipientService     = new CommunicationRecipientService(rockContext);

            if (GetTemplateData())
            {
                Rock.Model.Communication communication = new Rock.Model.Communication();
                communication.Status = CommunicationStatus.Transient;
                communication.SenderPersonAliasId = CurrentPersonAliasId;
                communicationService.Add(communication);
                communication.IsBulkCommunication = true;
                communication.CommunicationType   = CommunicationType.Email;
                communication.FutureSendDateTime  = null;

                // add each person as a recipient to the communication
                if (peopleIds != null)
                {
                    var emailMediumTypeId = EntityTypeCache.Get(Rock.SystemGuid.EntityType.COMMUNICATION_MEDIUM_EMAIL.AsGuid()).Id;

                    foreach (var personId in peopleIds)
                    {
                        if (!communication.Recipients.Any(r => r.PersonAlias.PersonId == personId))
                        {
                            var communicationRecipient = new CommunicationRecipient();
                            communicationRecipient.PersonAlias        = new PersonAliasService(rockContext).GetPrimaryAlias(personId);
                            communicationRecipient.MediumEntityTypeId = emailMediumTypeId;
                            communication.Recipients.Add(communicationRecipient);
                        }
                    }
                }

                CommunicationDetails.Copy(CommunicationData, communication);

                return(communication);
            }

            return(null);
        }
Exemple #3
0
        /// <summary>
        /// Gets the template data.
        /// </summary>
        /// <exception cref="System.Exception">Missing communication template configuration.</exception>
        private bool GetTemplateData()
        {
            if (string.IsNullOrWhiteSpace(GetAttributeValue("PhotoRequestTemplate")))
            {
                nbError.Title   = "Configuration Error";
                nbError.Text    = "Missing communication template configuration.";
                nbError.Visible = true;
                return(false);
            }

            var template = new CommunicationTemplateService(new RockContext()).Get(GetAttributeValue("PhotoRequestTemplate").AsGuid());

            if (template == null)
            {
                nbError.Title   = "Configuration Error";
                nbError.Text    = "The communication template appears to be missing.";
                nbError.Visible = true;
                return(false);
            }

            CommunicationDetails.Copy(template, CommunicationData);

            return(true);
        }
        /// <summary>
        /// Updates a communication model with the user-entered values
        /// </summary>
        /// <param name="communicationService">The service.</param>
        /// <returns></returns>
        private Rock.Model.Communication UpdateCommunication(RockContext rockContext, Guid templateGuid)
        {
            var communicationService           = new CommunicationService(rockContext);
            var communicationAttachmentService = new CommunicationAttachmentService(rockContext);
            var communicationRecipientService  = new CommunicationRecipientService(rockContext);
            var MediumEntityTypeId             = EntityTypeCache.Read("Rock.Communication.Medium.Email").Id;

            Rock.Model.Communication            communication = null;
            IQueryable <CommunicationRecipient> qryRecipients = null;

            CommunicationDetails CommunicationData = new CommunicationDetails();
            var template = new CommunicationTemplateService(new RockContext()).Get(templateGuid);

            if (template != null)
            {
                CommunicationDetails.Copy(template, CommunicationData);
                CommunicationData.EmailAttachmentBinaryFileIds = template.EmailAttachmentBinaryFileIds;
            }

            if (communication == null)
            {
                communication        = new Rock.Model.Communication();
                communication.Status = CommunicationStatus.Transient;
                communication.SenderPersonAliasId = CurrentPersonAliasId;
                communicationService.Add(communication);
            }

            if (qryRecipients == null)
            {
                qryRecipients = communication.GetRecipientsQry(rockContext);
            }

            communication.IsBulkCommunication = false;
            var medium = MediumContainer.GetComponentByEntityTypeId(MediumEntityTypeId);

            if (medium != null)
            {
                communication.CommunicationType = medium.CommunicationType;
            }

            communication.CommunicationTemplateId = template.Id;

            //GetMediumData();

            foreach (var recipient in communication.Recipients)
            {
                recipient.MediumEntityTypeId = MediumEntityTypeId;
            }

            CommunicationDetails.Copy(CommunicationData, communication);

            // delete any attachments that are no longer included
            foreach (var attachment in communication.Attachments.Where(a => !CommunicationData.EmailAttachmentBinaryFileIds.Contains(a.BinaryFileId)).ToList())
            {
                communication.Attachments.Remove(attachment);
                communicationAttachmentService.Delete(attachment);
            }

            // add any new attachments that were added
            foreach (var attachmentBinaryFileId in CommunicationData.EmailAttachmentBinaryFileIds.Where(a => !communication.Attachments.Any(x => x.BinaryFileId == a)))
            {
                communication.AddAttachment(new CommunicationAttachment {
                    BinaryFileId = attachmentBinaryFileId
                }, CommunicationType.Email);
            }

            communication.FutureSendDateTime = null;

            return(communication);
        }