Exemplo n.º 1
0
        private string GetSlug(PublishGroup publishGroup)
        {
            if (publishGroup.Slug.IsNotNullOrWhiteSpace())
            {
                return(publishGroup.Slug);
            }

            var slug = publishGroup.Title.RemoveAllNonAlphaNumericCharacters().ToLower();

            RockContext         rockContext         = new RockContext();
            PublishGroupService publishGroupService = new PublishGroupService(rockContext);
            var takenSlug = publishGroupService.Queryable()
                            .Where(pg => pg.Slug == slug && pg.GroupId != publishGroup.Group.Id).FirstOrDefault();

            var i = 0;

            while (takenSlug != null)
            {
                i++;
                var testSlug = slug + i.ToString();
                takenSlug = publishGroupService.Queryable().Where(pg => pg.Slug == testSlug).FirstOrDefault();
            }

            if (i != 0)
            {
                slug += i.ToString();
            }

            return(slug);
        }
Exemplo n.º 2
0
        private void BindGrid()
        {
            RockContext         rockContext         = new RockContext();
            PublishGroupService publishGroupService = new PublishGroupService(rockContext);

            var qry = publishGroupService.Queryable();

            if (pContactPerson.SelectedValue.HasValue)
            {
                var contactPersonId = pContactPerson.SelectedValue.Value;
                qry = qry.Where(p => p.ContactPersonAlias.PersonId == contactPersonId);
            }

            if (cblStatus.SelectedValues.Count != 0)
            {
                var selectedItems = cblStatus.SelectedValues.Select(i => ( PublishGroupStatus )i.AsInteger());
                qry = qry.Where(p => selectedItems.Contains(p.PublishGroupStatus));
            }

            gGroups.DataSource = qry
                                 .OrderBy(p => p.PublishGroupStatus)
                                 .ThenByDescending(p => p.StartDateTime)
                                 .ToList();
            gGroups.DataBind();
        }
Exemplo n.º 3
0
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            LinkButton  btn = ( LinkButton )sender;
            GridViewRow gvr = ( GridViewRow )btn.NamingContainer;
            var         hfPublishGroupId = ( HiddenField )gvr.FindControl("hfPublishGroupId");
            var         publishGroupId   = hfPublishGroupId.Value.AsInteger();

            using (RockContext rockContext = new RockContext())
            {
                PublishGroupService publishGroupService = new PublishGroupService(rockContext);
                var publishGroup = publishGroupService.Get(publishGroupId);
                if (publishGroup != null)
                {
                    if (this.IsUserAuthorized(Rock.Security.Authorization.EDIT) ||
                        publishGroup.ContactPersonAlias.Person.Id == CurrentPersonId ||
                        publishGroup.CreatedByPersonId == CurrentPersonId)
                    {
                        publishGroupService.Delete(publishGroup);
                        rockContext.SaveChanges();
                    }
                    else
                    {
                        maAlert.Show("We are sorry, you are not permitted to delete this publish group.", ModalAlertType.Alert);
                    }
                }
            }
            BindGrid();
        }
Exemplo n.º 4
0
        /// <summary>Gets the publish group.</summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="publishGroupService">The publish group service.</param>
        /// <returns></returns>
        private PublishGroup GetPublishGroup(RockContext rockContext = null, PublishGroupService publishGroupService = null)
        {
            rockContext         = rockContext ?? new RockContext();
            publishGroupService = publishGroupService ?? new PublishGroupService(rockContext);
            if (PageParameter(PageParameterKeys.PublishGroupId).IsNotNullOrWhiteSpace())
            {
                var publishGroup = publishGroupService.Get(PageParameter(PageParameterKeys.PublishGroupId).AsInteger());
                if (publishGroup != null)
                {
                    return(publishGroup);
                }
            }

            if (PageParameter(PageParameterKeys.GroupId).IsNotNullOrWhiteSpace())
            {
                int          groupId      = PageParameter(PageParameterKeys.GroupId).AsInteger();
                GroupService groupService = new GroupService(rockContext);
                var          group        = groupService.Get(groupId);
                if (group != null)
                {
                    var publishGroup = publishGroupService.Queryable()
                                       .Where(pg => pg.GroupId == groupId)
                                       .ToList()
                                       .LastOrDefault();

                    if (publishGroup != null)
                    {
                        return(publishGroup);
                    }

                    publishGroup = new PublishGroup
                    {
                        PublishGroupStatus      = PublishGroupStatus.PendingApproval,
                        RegistrationRequirement = RegistrationRequirement.NoRegistration,
                        ChildcareOptions        = ChildcareOptions.NoChildcare,
                        Group                            = group,
                        RequestorAliasId                 = CurrentPersonAliasId.Value,
                        ConfirmationBody                 = GetAttributeValue(AttributeKeys.DefaultEmail),
                        RegistrationDescription          = GetAttributeValue(AttributeKeys.RegistrationDetails),
                        ChildcareRegistrationDescription = GetAttributeValue(AttributeKeys.ChildcareRegistrationDetails)
                    };

                    if (group.Schedule != null)
                    {
                        publishGroup.WeeklyDayOfWeek = group.Schedule.WeeklyDayOfWeek;
                        publishGroup.WeeklyTimeOfDay = group.Schedule.WeeklyTimeOfDay;
                    }

                    if (group.GroupLocations.Any())
                    {
                        publishGroup.MeetingLocation = group.GroupLocations.FirstOrDefault().Location.Name;
                    }

                    return(publishGroup);
                }
            }
            return(null);
        }
Exemplo n.º 5
0
        private void BindGrid()
        {
            RockContext         rockContext         = new RockContext();
            PublishGroupService publishGroupService = new PublishGroupService(rockContext);

            gGroups.DataSource = publishGroupService.Queryable()
                                 .OrderBy(p => p.PublishGroupStatus)
                                 .ThenByDescending(p => p.StartDateTime)
                                 .ToList();
            gGroups.DataBind();
        }
        private PublishGroup GetPublishGroup(RockContext rockContext = null, PublishGroupService publishGroupService = null)
        {
            rockContext         = rockContext ?? new RockContext();
            publishGroupService = publishGroupService ?? new PublishGroupService(rockContext);
            if (PageParameter("PublishGroupId").IsNotNullOrWhiteSpace())
            {
                var publishGroup = publishGroupService.Get(PageParameter("PublishGroupId").AsInteger());
                if (publishGroup != null)
                {
                    return(publishGroup);
                }
            }

            if (PageParameter("GroupId").IsNotNullOrWhiteSpace())
            {
                int          groupId      = PageParameter("GroupId").AsInteger();
                GroupService groupService = new GroupService(rockContext);
                var          group        = groupService.Get(groupId);
                if (group != null)
                {
                    var publishGroup = publishGroupService.Queryable()
                                       .Where(pg => pg.GroupId == groupId)
                                       .ToList()
                                       .LastOrDefault();

                    if (publishGroup != null)
                    {
                        return(publishGroup);
                    }

                    publishGroup = new PublishGroup
                    {
                        PublishGroupStatus = PublishGroupStatus.Pending,
                        Group            = group,
                        RequestorAliasId = CurrentPersonAliasId.Value
                    };

                    if (group.Schedule != null)
                    {
                        publishGroup.WeeklyDayOfWeek = group.Schedule.WeeklyDayOfWeek;
                        publishGroup.WeeklyTimeOfDay = group.Schedule.WeeklyTimeOfDay;
                    }

                    if (group.GroupLocations.Any())
                    {
                        publishGroup.MeetingLocation = group.GroupLocations.FirstOrDefault().Location.Name;
                    }

                    return(publishGroup);
                }
            }
            return(null);
        }
Exemplo n.º 7
0
        protected void tbSlug_TextChanged(object sender, EventArgs e)
        {
            var publishGroup = GetPublishGroup();
            var slug         = tbSlug.Text.ToLower().RemoveAllNonAlphaNumericCharacters();

            tbSlug.Text = slug;
            PublishGroupService publishGroupService = new PublishGroupService(new RockContext());
            var isDuplicateSlug = publishGroupService.Queryable()
                                  .Where(pg => pg.Slug == slug && pg.Id != publishGroup.Id && pg.GroupId != publishGroup.Group.Id)
                                  .Any();

            if (isDuplicateSlug)
            {
                lSlug.Visible  = false;
                nbSlug.Visible = true;
            }
            else
            {
                nbSlug.Visible = false;
                lSlug.Visible  = true;
                lSlug.Text     = GetAttributeValue(AttributeKeys.GroupRegistrationUrl) + slug;
            }
        }
Exemplo n.º 8
0
        protected void BindData()
        {
            List <int> campusIds  = cblCampus.Items.OfType <ListItem>().Where(l => l.Selected).Select(a => a.Value.AsInteger()).ToList();
            List <int> categories = cblCategory.Items.OfType <ListItem>().Where(l => l.Selected).Select(a => a.Value.AsInteger()).ToList();

            // Initialize services
            var rockContext           = new RockContext();
            var publishGroupService   = new PublishGroupService(rockContext);
            var attributeService      = new AttributeService(rockContext);
            var attributeValueService = new AttributeValueService(rockContext);

            //Get the entity types
            var publishGroupEntityTypeId = EntityTypeCache.Get(typeof(PublishGroup)).Id;
            var groupEntityTypeId        = EntityTypeCache.Get(typeof(Group)).Id;

            //Attribute Queryables
            var publishGroupAttributeQry = attributeService.Queryable()
                                           .Where(a => a.EntityTypeId == publishGroupEntityTypeId)
                                           .Select(a => a.Id);

            var groupAttributeQry = attributeService.Queryable()
                                    .Where(a => a.EntityTypeId == groupEntityTypeId)
                                    .Select(a => a.Id);

            //Attribute Value Queryables
            var publishGroupAttributeValueQry = attributeValueService.Queryable()
                                                .Where(av => publishGroupAttributeQry.Contains(av.AttributeId));

            var groupAttributeValueQry = attributeValueService.Queryable()
                                         .Where(av => groupAttributeQry.Contains(av.AttributeId));


            var qry = publishGroupService
                      .Queryable("Group")
                      .Where(pg => !pg.IsHidden)                                                                             //Is not hidden from list
                      .Where(pg => pg.PublishGroupStatus == PublishGroupStatus.Approved)                                     //Approved
                      .Where(pg => pg.StartDateTime <= Rock.RockDateTime.Today && pg.EndDateTime >= Rock.RockDateTime.Today) //Active
                      .Where(pg => pg.Group.GroupType.GroupCapacityRule == GroupCapacityRule.None ||
                             pg.Group.GroupCapacity == null || pg.Group.GroupCapacity == 0 ||
                             pg.Group.Members.Count() < pg.Group.GroupCapacity);      // Not full


            // Filter by campus
            if (campusIds.Any())
            {
                qry = qry
                      .Where(pg =>
                             !pg.Group.CampusId.HasValue || // All
                             campusIds.Contains(pg.Group.CampusId.Value));
            }

            // Filter by Category
            if (categories.Any())
            {
                qry = qry
                      .Where(pg => pg.AudienceValues
                             .Any(dv => categories.Contains(dv.Id)));
            }

            //Group join in attributes
            var mixedQry = qry
                           .GroupJoin(publishGroupAttributeValueQry,
                                      pg => pg.Id,
                                      av => av.EntityId,
                                      (pg, av) => new { pg, av })
                           .GroupJoin(groupAttributeValueQry,
                                      pg => pg.pg.GroupId,
                                      av => av.EntityId,
                                      (pg, av) => new { PublishGroup = pg.pg, PublishGroupAttributes = pg.av, GroupAttributes = av });

            //Add in the attributes in the proper format
            var publishGroups = new List <PublishGroup>();

            foreach (var item in mixedQry.ToList())
            {
                var publishGroup = item.PublishGroup;
                publishGroup.AttributeValues = item.PublishGroupAttributes.ToDictionary(av => av.AttributeKey, av => new AttributeValueCache(av));
                publishGroup.Attributes      = item.PublishGroupAttributes.ToDictionary(av => av.AttributeKey, av => AttributeCache.Get(av.AttributeId));
                if (publishGroup.Group == null)
                {
                    continue;
                }
                publishGroup.Group.AttributeValues = item.GroupAttributes.ToDictionary(av => av.AttributeKey, av => new AttributeValueCache(av));
                publishGroup.Group.Attributes      = item.GroupAttributes.ToDictionary(av => av.AttributeKey, av => AttributeCache.Get(av.AttributeId));
                publishGroups.Add(publishGroup);
            }

            // Output mergefields.
            var mergeFields = LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson);

            mergeFields.Add("PublishGroups", publishGroups);

            lOutput.Text = GetAttributeValue("LavaTemplate").ResolveMergeFields(mergeFields);
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            RockContext         rockContext         = new RockContext();
            PublishGroupService publishGroupService = new PublishGroupService(rockContext);

            publishGroup = GetPublishGroup(rockContext, publishGroupService);

            if (publishGroup.PublishGroupStatus == PublishGroupStatus.Approved)
            {
                var tempGroup = publishGroup.Group;
                publishGroup       = ( PublishGroup )publishGroup.Clone();
                publishGroup.Guid  = Guid.NewGuid();
                publishGroup.Id    = 0;
                publishGroup.Group = tempGroup;
                publishGroupService.Add(publishGroup);
            }

            publishGroup.ImageId                   = iGroupImage.BinaryFileId;
            publishGroup.PublishGroupStatus        = PublishGroupStatus.Pending;
            publishGroup.Description               = tbDescription.Text;
            publishGroup.EndDateTime               = drPublishDates.UpperValue.Value;
            publishGroup.StartDateTime             = drPublishDates.LowerValue.Value;
            publishGroup.WeeklyDayOfWeek           = ddlDayOfWeek.SelectedValueAsEnumOrNull <DayOfWeek>();
            publishGroup.WeeklyTimeOfDay           = tTimeOfDay.SelectedTime;
            publishGroup.StartDate                 = dpStartDate.SelectedDate;
            publishGroup.MeetingLocation           = tbLocationName.Text;
            publishGroup.RequiresRegistration      = cbRequiresRegistration.Checked;
            publishGroup.RegistrationLink          = cbRequiresRegistration.Checked ? tbRegistrationLink.Text : "";
            publishGroup.AllowSpouseRegistration   = cbAllowSpouseRegistration.Checked;
            publishGroup.ChildcareAvailable        = cbChildcareAvailable.Checked;
            publishGroup.ChildcareRegistrationLink = cbChildcareAvailable.Checked ? tbChildcareRegistrationLink.Text : "";
            publishGroup.AudienceValues            = GetSelectedAudiences(rockContext);
            publishGroup.ContactPersonAliasId      = pContactPerson.PersonAliasId.Value;
            publishGroup.RequestorAliasId          = CurrentPersonAliasId.Value;
            publishGroup.ContactEmail              = tbContactEmail.Text;
            publishGroup.ContactPhoneNumber        = tbContactPhoneNumber.Text;
            publishGroup.ConfirmationFromName      = tbConfirmationFromName.Text;
            publishGroup.ConfirmationEmail         = tbConfirmationFromEmail.Text;
            publishGroup.ConfirmationSubject       = tbConfirmationSubject.Text;
            publishGroup.ConfirmationBody          = ceConfirmationBody.Text;

            if (publishGroup.Id == 0)
            {
                publishGroupService.Add(publishGroup);
            }

            if (IsUserAuthorized(Rock.Security.Authorization.EDIT))
            {
                publishGroup.PublishGroupStatus = PublishGroupStatus.Approved;
                publishGroup.Group.IsActive     = true;
                publishGroup.Group.IsPublic     = true;
                //remove all other publish groups for this computer
                publishGroupService.DeleteRange(publishGroupService.Queryable().Where(pg => pg.GroupId == publishGroup.GroupId && pg.Id != publishGroup.Id));
            }
            ;

            //Set the binary file to not be temporary
            if (publishGroup.ImageId.HasValue)
            {
                BinaryFileService binaryFileService = new BinaryFileService(rockContext);
                var binaryFile = binaryFileService.Get(publishGroup.ImageId.Value);
                if (binaryFile != null)
                {
                    binaryFile.IsTemporary = false;
                }
            }

            rockContext.SaveChanges();

            publishGroup.LoadAttributes(rockContext);

            if (publishGroup.Attributes.Any())
            {
                Rock.Attribute.Helper.GetEditValues(phAttributeEdits, publishGroup);
                publishGroup.SaveAttributeValues(rockContext);
            }

            if (IsUserAuthorized(Rock.Security.Authorization.EDIT))
            {
                publishGroup.LaunchWorkflow(GetAttributeValue("CompleteWorkflow").AsGuidOrNull());
            }
            else
            {
                publishGroup.LaunchWorkflow(GetAttributeValue("RequestWorkflow").AsGuidOrNull());
            }
            NavigateToParentPage();
        }
        /// <summary>
        /// Checks the settings.  If false is returned, it's expected that the caller will make
        /// the nbNotice visible to inform the user of the "settings" error.
        /// </summary>
        /// <returns>true if settings are valid; false otherwise</returns>
        private bool CheckSettings()
        {
            _rockContext = _rockContext ?? new RockContext();


            _autoFill = GetAttributeValue("AutoFillForm").AsBoolean();

            string registerButtonText = GetAttributeValue("RegisterButtonAltText");

            if (string.IsNullOrWhiteSpace(registerButtonText))
            {
                registerButtonText = "Register";
            }
            btnRegister.Text = registerButtonText;

            var publishGroupService = new PublishGroupService(_rockContext);

            if (_publishGroup == null)
            {
                var publishGroupGuid = PageParameter("PublishGroup").AsGuidOrNull();
                if (publishGroupGuid.HasValue)
                {
                    _publishGroup = publishGroupService.Get(publishGroupGuid.Value);
                }
                else
                {
                    var slug = PageParameter("PublishGroup").ToLower();
                    _publishGroup = publishGroupService.Queryable().Where(pg => pg.Slug == slug).FirstOrDefault();
                }
            }

            if (_publishGroup == null || _publishGroup.PublishGroupStatus != PublishGroupStatus.Approved)
            {
                nbNotice.Heading = "Unknown Group";
                nbNotice.Text    = "<p>This page requires a valid group identifying parameter and there was not one provided.</p>";
                nbNotice.Visible = true;
                return(false);
            }

            if (_publishGroup.RegistrationLink.IsNotNullOrWhiteSpace())
            {
                Response.Redirect(_publishGroup.RegistrationLink, false);
                Context.ApplicationInstance.CompleteRequest();
                return(false);
            }

            _showSpouse       = _publishGroup.AllowSpouseRegistration;
            _defaultGroupRole = _publishGroup.Group.GroupType.DefaultGroupRole;


            _dvcConnectionStatus = DefinedValueCache.Get(GetAttributeValue("ConnectionStatus").AsGuid());
            if (_dvcConnectionStatus == null)
            {
                nbNotice.Heading = "Invalid Connection Status";
                nbNotice.Text    = "<p>The selected Connection Status setting does not exist.</p>";
                return(false);
            }

            _dvcRecordStatus = DefinedValueCache.Get(GetAttributeValue("RecordStatus").AsGuid());
            if (_dvcRecordStatus == null)
            {
                nbNotice.Heading = "Invalid Record Status";
                nbNotice.Text    = "<p>The selected Record Status setting does not exist.</p>";
                return(false);
            }

            _married         = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.PERSON_MARITAL_STATUS_MARRIED.AsGuid());
            _homeAddressType = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.GROUP_LOCATION_TYPE_HOME.AsGuid());
            _familyType      = GroupTypeCache.Get(Rock.SystemGuid.GroupType.GROUPTYPE_FAMILY.AsGuid());
            _adultRole       = _familyType.Roles.FirstOrDefault(r => r.Guid.Equals(Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT.AsGuid()));

            if (_married == null || _homeAddressType == null || _familyType == null || _adultRole == null)
            {
                nbNotice.Heading = "Missing System Value";
                nbNotice.Text    = "<p>There is a missing or invalid system value. Check the settings for Marital Status of 'Married', Location Type of 'Home', Group Type of 'Family', and Family Group Role of 'Adult'.</p>";
                return(false);
            }

            return(true);
        }
Exemplo n.º 11
0
        /// <summary>Saves the specified publish group status.</summary>
        /// <param name="publishGroupStatus">The publish group status.</param>
        private void Save(PublishGroupStatus publishGroupStatus)
        {
            RockContext         rockContext         = new RockContext();
            PublishGroupService publishGroupService = new PublishGroupService(rockContext);
            PublishGroup        publishGroup        = GetPublishGroup(rockContext, publishGroupService);

            var slug = tbSlug.Text.ToLower().RemoveAllNonAlphaNumericCharacters();

            tbSlug.Text = slug;

            //Test for already taken Slugs
            var isDuplicateSlug = publishGroupService.Queryable()
                                  .Where(pg => pg.Slug == slug && pg.Id != publishGroup.Id && pg.GroupId != publishGroup.Group.Id)
                                  .Any();

            if (isDuplicateSlug)
            {
                nbSlug.Visible = true;
                return;
            }
            else
            {
                nbSlug.Visible    = false;
                publishGroup.Slug = slug;
            }


            bool isApprover = false;

            if (IsUserAuthorized(Rock.Security.Authorization.EDIT))
            {
                isApprover = true;
            }

            if (isApprover)
            {
                publishGroupStatus = ddlStatus.SelectedValueAsEnum <PublishGroupStatus>();
            }
            else if (publishGroupStatus != PublishGroupStatus.Draft)
            {
                if (ddlRegistration.SelectedValue == "4" ||
                    (ddlChildcareOptions.SelectedValue == "2" && ddlChildcareNeedRegistration.SelectedValue == "2"))     //Childcare required && Registration needed.
                {
                    publishGroupStatus = PublishGroupStatus.PendingIT;
                }
            }

            if (publishGroup.PublishGroupStatus == PublishGroupStatus.Approved)
            {
                var tempGroup = publishGroup.Group;
                publishGroup       = ( PublishGroup )publishGroup.Clone();
                publishGroup.Guid  = Guid.NewGuid();
                publishGroup.Id    = 0;
                publishGroup.Group = tempGroup;
                publishGroupService.Add(publishGroup);
            }

            publishGroup.Name                             = tbName.Text;
            publishGroup.ImageId                          = iGroupImage.BinaryFileId;
            publishGroup.PublishGroupStatus               = publishGroupStatus;
            publishGroup.Description                      = tbDescription.Text;
            publishGroup.EndDateTime                      = drPublishDates.UpperValue.Value;
            publishGroup.StartDateTime                    = drPublishDates.LowerValue.Value;
            publishGroup.WeeklyDayOfWeek                  = ddlDayOfWeek.SelectedValueAsEnumOrNull <DayOfWeek>();
            publishGroup.WeeklyTimeOfDay                  = tTimeOfDay.SelectedTime;
            publishGroup.CustomSchedule                   = tbCustomSchedule.Text;
            publishGroup.StartDate                        = dpStartDate.SelectedDate;
            publishGroup.MeetingLocation                  = tbLocationName.Text;
            publishGroup.IsHidden                         = cbIsHidden.Checked;
            publishGroup.RegistrationRequirement          = ( RegistrationRequirement )ddlRegistration.SelectedValue.AsInteger();
            publishGroup.RequiresRegistration             = ddlRegistration.SelectedValue.AsInteger() > 0; //This is obsolete but left in for backward compatability
            publishGroup.RegistrationLink                 = publishGroup.RegistrationRequirement == RegistrationRequirement.CustomRegistration ? tbRegistrationLink.Text : "";
            publishGroup.RegistrationDescription          = tbRegistrationDetails.Text;
            publishGroup.ChildcareRegistrationDescription = ddlChildcareNeedRegistration.SelectedValue == "2" ? tbChildcareRegistrationDetails.Text : "";
            publishGroup.AllowSpouseRegistration          = cbAllowSpouseRegistration.Checked;
            publishGroup.ChildcareOptions                 = ( ChildcareOptions )ddlChildcareOptions.SelectedValue.AsInteger();
            publishGroup.ChildcareAvailable               = ddlChildcareOptions.SelectedValue.AsInteger() > 0;
            publishGroup.ChildcareRegistrationLink        = publishGroup.ChildcareAvailable ? tbChildcareRegistrationLink.Text : "";
            publishGroup.AudienceValues                   = GetSelectedAudiences(rockContext);
            publishGroup.ContactPersonAliasId             = pContactPerson.PersonAliasId.Value;
            publishGroup.RequestorAliasId                 = CurrentPersonAliasId.Value;
            publishGroup.ContactEmail                     = tbContactEmail.Text;
            publishGroup.ContactPhoneNumber               = tbContactPhoneNumber.Text;
            publishGroup.ConfirmationFromName             = tbConfirmationFromName.Text;
            publishGroup.ConfirmationEmail                = tbConfirmationFromEmail.Text;
            publishGroup.ConfirmationSubject              = tbConfirmationSubject.Text;
            publishGroup.ConfirmationBody                 = ceConfirmationBody.Text;

            if (publishGroup.Id == 0)
            {
                publishGroupService.Add(publishGroup);
            }

            if (isApprover && publishGroupStatus == PublishGroupStatus.Approved)
            {
                publishGroup.Group.IsActive = true;
                publishGroup.Group.IsPublic = true;
                //remove all other publish groups for this computer
                publishGroupService.DeleteRange(publishGroupService.Queryable().Where(pg => pg.GroupId == publishGroup.GroupId && pg.Id != publishGroup.Id));
            }
            ;

            //Set the binary file to not be temporary
            if (publishGroup.ImageId.HasValue)
            {
                BinaryFileService binaryFileService = new BinaryFileService(rockContext);
                var binaryFile = binaryFileService.Get(publishGroup.ImageId.Value);
                if (binaryFile != null)
                {
                    binaryFile.IsTemporary = false;
                }
            }

            rockContext.SaveChanges();

            publishGroup.LoadAttributes(rockContext);

            if (publishGroup.Attributes.Any())
            {
                Rock.Attribute.Helper.GetEditValues(phAttributeEdits, publishGroup);
                publishGroup.SaveAttributeValues(rockContext);
            }

            if (publishGroup.PublishGroupStatus != PublishGroupStatus.Draft)
            {
                publishGroup.LaunchWorkflow(GetAttributeValue(AttributeKeys.Workflow).AsGuidOrNull());
            }

            NavigateToParentPage(new Dictionary <string, string> {
                { "GroupId", publishGroup.GroupId.ToString() }
            });
        }
        /// <summary>
        /// Sends the email.
        /// </summary>
        private bool SendEmail()
        {
            var          rockContext         = new RockContext();
            var          personAliasService  = new PersonAliasService(rockContext);
            var          groupService        = new GroupService(rockContext);
            var          publishGroupService = new PublishGroupService(rockContext);
            Group        group        = null;
            PublishGroup publishGroup = null;
            var          mergeFields  = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson);

            List <RecipientData> recipients = new List <RecipientData>();

            // get person from url
            if (PageParameter("PersonGuid").IsNotNullOrWhiteSpace())
            {
                Guid?personGuid = this.PageParameter("PersonGuid").AsGuidOrNull();
                if (personGuid.HasValue)
                {
                    var personAlias = personAliasService.Get(personGuid.Value);
                    if (personAlias != null)
                    {
                        recipients.Add(new RecipientData(new CommunicationRecipient {
                            PersonAlias = personAlias
                        }, mergeFields));
                    }
                }
            }

            // get group members from url
            if (PageParameter("GroupGuid").IsNotNullOrWhiteSpace())
            {
                Guid?groupGuid = PageParameter("GroupGuid").AsGuidOrNull();

                if (groupGuid.HasValue)
                {
                    group = groupService.Get(groupGuid.Value);

                    var leaders = GetGroupLeaders(group);
                    foreach (var leader in leaders)
                    {
                        recipients.Add(new RecipientData(new CommunicationRecipient {
                            PersonAlias = leader.PrimaryAlias
                        }, mergeFields));
                    }
                }
            }

            // get group members from url
            if (PageParameter("PublishGroupGuid").IsNotNullOrWhiteSpace())
            {
                Guid?publishGroupGuid = PageParameter("PublishGroupGuid").AsGuidOrNull();

                if (publishGroupGuid.HasValue)
                {
                    publishGroup = publishGroupService.Get(publishGroupGuid.Value);
                    if (publishGroup != null)
                    {
                        recipients.Add(new RecipientData(publishGroup.ContactEmail, mergeFields));
                    }
                }
            }

            if (!recipients.Any())
            {
                Guid defaultRecipient = this.GetAttributeValue("DefaultRecipient").AsGuid();
                var  defaultPerson    = personAliasService.Get(defaultRecipient);
                if (defaultPerson != null)
                {
                    recipients.Add(new RecipientData(new CommunicationRecipient {
                        PersonAlias = defaultPerson
                    }, mergeFields));
                }
            }

            if (!recipients.Any())
            {
                Guid defaultGroupGuid = GetAttributeValue("LeaderGroup").AsGuid();
                var  defaultGroup     = groupService.Get(defaultGroupGuid);
                var  leaders          = GetGroupLeaders(defaultGroup);
                foreach (var leader in leaders)
                {
                    recipients.Add(new RecipientData(new CommunicationRecipient {
                        PersonAlias = leader.PrimaryAlias
                    }, mergeFields));
                }
            }

            if (!cpCaptcha.IsAvailable || cpCaptcha.IsResponseValid())
            {
                mergeFields.Add("NickName", tbFirstName.Text);
                mergeFields.Add("LastName", tbLastName.Text);
                mergeFields.Add("Email", tbEmail.Text);
                mergeFields.Add("PublishGroup", publishGroup);
                mergeFields.Add("Group", group);
                mergeFields.Add("Message", tbMessage.Text);
                mergeFields.Add("FromEmail", tbEmail.Text);
                mergeFields.Add("FromName", tbFirstName.Text + " " + tbLastName.Text);

                var message = new RockEmailMessage();
                message.EnabledLavaCommands = GetAttributeValue("EnabledLavaCommands");

                // send email
                foreach (var recipient in recipients)
                {
                    message.AddRecipient(recipient);
                }

                message.FromEmail = tbEmail.Text;
                message.FromName  = tbFirstName.Text + " " + tbLastName.Text;
                message.Subject   = GetAttributeValue("Subject");
                message.Message   = GetAttributeValue("MessageBody");
                message.AppRoot   = ResolveRockUrl("~/");
                message.ThemeRoot = ResolveRockUrl("~~/");
                message.CreateCommunicationRecord = GetAttributeValue("SaveCommunicationHistory").AsBoolean();
                message.Send();

                // set response

                // display response message
                lResponse.Visible = true;
                pnlContactGroupLeaders.Visible = false;
                lResponse.Text = GetAttributeValue("ResponseMessage").ResolveMergeFields(mergeFields, GetAttributeValue("EnabledLavaCommands"));
                return(true);
            }
            else
            {
                lResponse.Visible = true;
                pnlContactGroupLeaders.Visible = true;
                lResponse.Text = "<div class='alert alert-danger'>You appear to be a computer. Check the box above and then click Submit.</div>";
                return(false);
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Sends the email.
        /// </summary>
        private bool SendMessage()
        {
            var          rockContext         = new RockContext();
            var          personAliasService  = new PersonAliasService(rockContext);
            var          groupService        = new GroupService(rockContext);
            var          publishGroupService = new PublishGroupService(rockContext);
            Group        group        = null;
            PublishGroup publishGroup = null;
            bool         allowSMS     = GetAttributeValue("EnableSMS").AsBoolean();
            var          mergeFields  = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson);

            List <Person> emailRecipients = new List <Person>();
            List <Person> smsRecipients   = new List <Person>();

            // get person from url
            if (PageParameter("PersonGuid").IsNotNullOrWhiteSpace())
            {
                Guid?personGuid = this.PageParameter("PersonGuid").AsGuidOrNull();
                if (personGuid.HasValue)
                {
                    var person = personAliasService.Queryable()
                                 .Where(pa => pa.Guid == personGuid.Value)
                                 .Select(pa => pa.Person)
                                 .FirstOrDefault();
                    var phoneNumber = person.PhoneNumbers.GetFirstSmsNumber();
                    if (person != null && allowSMS && person.CommunicationPreference == CommunicationType.SMS && phoneNumber.IsNotNullOrWhiteSpace())
                    {
                        smsRecipients.Add(person);
                    }
                    else
                    {
                        emailRecipients.Add(person);
                    }
                }
            }

            // get group members from url
            if (PageParameter("GroupGuid").IsNotNullOrWhiteSpace())
            {
                Guid?groupGuid = PageParameter("GroupGuid").AsGuidOrNull();

                if (groupGuid.HasValue)
                {
                    group = groupService.Get(groupGuid.Value);

                    var leaders = GetGroupLeaders(group);
                    foreach (var leader in leaders)
                    {
                        var phonenumber = leader.PhoneNumbers.GetFirstSmsNumber();
                        if (allowSMS && leader.CommunicationPreference == CommunicationType.SMS && phonenumber.IsNotNullOrWhiteSpace())
                        {
                            smsRecipients.Add(leader);
                        }
                        else
                        {
                            emailRecipients.Add(leader);
                        }
                    }
                }
            }

            // get group members from url
            if (PageParameter("PublishGroupGuid").IsNotNullOrWhiteSpace())
            {
                Guid?publishGroupGuid = PageParameter("PublishGroupGuid").AsGuidOrNull();

                if (publishGroupGuid.HasValue)
                {
                    publishGroup = publishGroupService.Get(publishGroupGuid.Value);
                    if (publishGroup != null)
                    {
                        var person = new Person {
                            Email = publishGroup.ContactEmail
                        };
                        emailRecipients.Add(person);
                    }
                }
            }

            if (!emailRecipients.Any() && !smsRecipients.Any())
            {
                Guid defaultRecipient = this.GetAttributeValue("DefaultRecipient").AsGuid();
                var  defaultPerson    = personAliasService.Queryable()
                                        .Where(pa => pa.Guid == defaultRecipient)
                                        .Select(pa => pa.Person)
                                        .FirstOrDefault();
                if (defaultPerson != null)
                {
                    var phonenumber = defaultPerson.PhoneNumbers.GetFirstSmsNumber();
                    if (allowSMS && defaultPerson.CommunicationPreference == CommunicationType.SMS && phonenumber.IsNotNullOrWhiteSpace())
                    {
                        smsRecipients.Add(defaultPerson);
                    }
                    else
                    {
                        emailRecipients.Add(defaultPerson);
                    }
                }
            }

            if (!emailRecipients.Any() && !smsRecipients.Any())
            {
                Guid defaultGroupGuid = GetAttributeValue("LeaderGroup").AsGuid();
                var  defaultGroup     = groupService.Get(defaultGroupGuid);
                var  leaders          = GetGroupLeaders(defaultGroup);
                foreach (var leader in leaders)
                {
                    var phonenumber = leader.PhoneNumbers.GetFirstSmsNumber();
                    if (allowSMS && leader.CommunicationPreference == CommunicationType.SMS && phonenumber.IsNotNullOrWhiteSpace())
                    {
                        smsRecipients.Add(leader);
                    }
                    else
                    {
                        emailRecipients.Add(leader);
                    }
                }
            }

            if (!cpCaptcha.IsAvailable || cpCaptcha.IsResponseValid())
            {
                mergeFields.Add("NickName", tbFirstName.Text);
                mergeFields.Add("LastName", tbLastName.Text);
                mergeFields.Add("Email", tbEmail.Text);
                mergeFields.Add("PublishGroup", publishGroup);
                mergeFields.Add("Group", group);
                mergeFields.Add("Message", tbMessage.Text);
                mergeFields.Add("FromEmail", tbEmail.Text);
                mergeFields.Add("FromName", tbFirstName.Text + " " + tbLastName.Text);

                if (emailRecipients.Any())
                {
                    SendEmail(emailRecipients, mergeFields);
                }

                if (smsRecipients.Any())
                {
                    SendTextMessage(smsRecipients, mergeFields);
                }

                // display response message
                lResponse.Visible = true;
                pnlContactGroupLeaders.Visible = false;
                lResponse.Text = GetAttributeValue("ResponseMessage").ResolveMergeFields(mergeFields, GetAttributeValue("EnabledLavaCommands"));
                return(true);
            }
            else
            {
                lResponse.Visible = true;
                pnlContactGroupLeaders.Visible = true;
                lResponse.Text = "<div class='alert alert-danger'>You appear to be a computer. Check the box above and then click Submit.</div>";
                return(false);
            }
        }