/// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init" /> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs" /> object that contains the event data.</param>
        protected override void OnInit( EventArgs e )
        {
            base.OnInit( e );

            try
            {
                string groupMemberKey = PageParameter( "gm" );
                if ( string.IsNullOrWhiteSpace( groupMemberKey ) )
                {
                    ShowError( "Missing Parameter Value" );
                }
                else
                {
                    var groupMemberService = new GroupMemberService();
                    var groupMember = groupMemberService.GetByUrlEncodedKey( PageParameter( "gm" ) );
                    if ( groupMember == null )
                    {
                        ShowError();
                    }
                    else
                    {
                        groupMember.GroupMemberStatus = GroupMemberStatus.Active;
                        groupMemberService.Save( groupMember, CurrentPersonId );

                        nbMessage.NotificationBoxType = NotificationBoxType.Success;
                        nbMessage.Title = "Success";
                        nbMessage.Text = GetAttributeValue( "SuccessMessage" );
                    }
                }
            }
            catch (SystemException ex)
            {
                ShowError( ex.Message );
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Adds the related person to the selected person's known relationships with a role of 'Can check in' which
        /// is typically configured to allow check-in.  If an inverse relationship is configured for 'Can check in'
        /// (i.e. 'Allow check in by'), that relationship will also be created.
        /// </summary>
        /// <param name="personId">A <see cref="System.Int32"/> representing the Id of the Person.</param>
        /// <param name="relatedPersonId">A <see cref="System.Int32"/> representing the Id of the related Person.</param>
        /// <param name="currentPersonId">A <see cref="System.Int32"/> representing the Id of the Person who is logged in.</param>
        public static void CreateCheckinRelationship(int personId, int relatedPersonId, int?currentPersonId)
        {
            using (new UnitOfWorkScope())
            {
                var groupMemberService     = new GroupMemberService();
                var knownRelationshipGroup = groupMemberService.Queryable()
                                             .Where(m =>
                                                    m.PersonId == personId &&
                                                    m.GroupRole.Guid.Equals(new Guid(SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_OWNER)))
                                             .Select(m => m.Group)
                                             .FirstOrDefault();

                if (knownRelationshipGroup != null)
                {
                    int?canCheckInRoleId = new GroupTypeRoleService().Queryable()
                                           .Where(r =>
                                                  r.Guid.Equals(new Guid(SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_CAN_CHECK_IN)))
                                           .Select(r => r.Id)
                                           .FirstOrDefault();
                    if (canCheckInRoleId.HasValue)
                    {
                        var canCheckInMember = groupMemberService.Queryable()
                                               .FirstOrDefault(m =>
                                                               m.GroupId == knownRelationshipGroup.Id &&
                                                               m.PersonId == relatedPersonId &&
                                                               m.GroupRoleId == canCheckInRoleId.Value);

                        if (canCheckInMember == null)
                        {
                            canCheckInMember             = new GroupMember();
                            canCheckInMember.GroupId     = knownRelationshipGroup.Id;
                            canCheckInMember.PersonId    = relatedPersonId;
                            canCheckInMember.GroupRoleId = canCheckInRoleId.Value;
                            groupMemberService.Add(canCheckInMember, currentPersonId);
                            groupMemberService.Save(canCheckInMember, currentPersonId);
                        }

                        var inverseGroupMember = groupMemberService.GetInverseRelationship(canCheckInMember, true, currentPersonId);
                        if (inverseGroupMember != null)
                        {
                            groupMemberService.Save(inverseGroupMember, currentPersonId);
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Handles the Click event of the btnSave 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 btnSave_Click( object sender, EventArgs e )
        {
            // confirmation was disabled by btnSave on client-side.  So if returning without a redirect,
            // it should be enabled.  If returning with a redirect, the control won't be updated to reflect
            // confirmation being enabled, so it's ok to enable it here
            confirmExit.Enabled = true;

            if ( Page.IsValid )
            {
                confirmExit.Enabled = true;

                RockTransactionScope.WrapTransaction( () =>
                {

                    using ( new UnitOfWorkScope() )
                    {
                        var familyService = new GroupService();
                        var familyMemberService = new GroupMemberService();
                        var personService = new PersonService();
                        var historyService = new HistoryService();

                        var familyChanges = new List<string>();

                        // SAVE FAMILY
                        _family = familyService.Get( _family.Id );

                        History.EvaluateChange( familyChanges, "Name", _family.Name, tbFamilyName.Text );
                        _family.Name = tbFamilyName.Text;

                        int? campusId = cpCampus.SelectedValueAsInt();
                        if ( _family.CampusId != campusId )
                        {
                            History.EvaluateChange( familyChanges, "Campus",
                                _family.CampusId.HasValue ? CampusCache.Read( _family.CampusId.Value ).Name : string.Empty,
                                campusId.HasValue ? CampusCache.Read( campusId.Value ).Name : string.Empty );
                            _family.CampusId = campusId;
                        }

                        var familyGroupTypeId = _family.GroupTypeId;

                        familyService.Save( _family, CurrentPersonId );

                        // SAVE FAMILY MEMBERS
                        int? recordStatusValueID = ddlRecordStatus.SelectedValueAsInt();
                        int? reasonValueId = ddlReason.SelectedValueAsInt();
                        var newFamilies = new List<Group>();

                        foreach ( var familyMember in FamilyMembers )
                        {
                            var memberChanges = new List<string>();
                            var demographicChanges = new List<string>();

                            var role = familyRoles.Where( r => r.Guid.Equals( familyMember.RoleGuid ) ).FirstOrDefault();
                            if ( role == null )
                            {
                                role = familyRoles.FirstOrDefault();
                            }

                            bool isChild = role != null && role.Guid.Equals( new Guid( Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_CHILD ) );

                            // People added to family (new or from other family)
                            if ( !familyMember.ExistingFamilyMember )
                            {
                                var groupMember = new GroupMember();

                                if ( familyMember.Id == -1 )
                                {
                                    // added new person
                                    demographicChanges.Add( "Created" );

                                    var person = new Person();
                                    person.FirstName = familyMember.FirstName;
                                    History.EvaluateChange( demographicChanges, "First Name", string.Empty, person.FirstName );

                                    person.LastName = familyMember.LastName;
                                    History.EvaluateChange( demographicChanges, "Last Name", string.Empty, person.LastName );

                                    person.Gender = familyMember.Gender;
                                    History.EvaluateChange( demographicChanges, "Gender", null, person.Gender );

                                    person.BirthDate = familyMember.BirthDate;
                                    History.EvaluateChange( demographicChanges, "Birth Date", null, person.BirthDate );

                                    if ( !isChild )
                                    {
                                        person.GivingGroupId = _family.Id;
                                        History.EvaluateChange( demographicChanges, "Giving Group", string.Empty, _family.Name );
                                    }

                                    groupMember.Person = person;
                                }
                                else
                                {
                                    // added from other family
                                    groupMember.Person = personService.Get( familyMember.Id );
                                }

                                History.EvaluateChange( demographicChanges, "Record Status", DefinedValueCache.GetName( groupMember.Person.RecordStatusValueId ), DefinedValueCache.GetName( recordStatusValueID ) );
                                groupMember.Person.RecordStatusValueId = recordStatusValueID;

                                History.EvaluateChange( demographicChanges, "Record Status Reason", DefinedValueCache.GetName( groupMember.Person.RecordStatusReasonValueId ), DefinedValueCache.GetName( reasonValueId ) );
                                groupMember.Person.RecordStatusReasonValueId = reasonValueId;

                                groupMember.GroupId = _family.Id;
                                if ( role != null )
                                {
                                    History.EvaluateChange( memberChanges, string.Format( "Role", _family.Name ), string.Empty, role.Name );
                                    groupMember.GroupRoleId = role.Id;
                                }

                                if ( groupMember.Person != null )
                                {
                                    familyMemberService.Add( groupMember, CurrentPersonId );
                                    familyMemberService.Save( groupMember, CurrentPersonId );
                                }

                            }
                            else
                            {
                                // existing family members
                                var groupMember = familyMemberService.Queryable( "Person" ).Where( m =>
                                    m.PersonId == familyMember.Id &&
                                    m.Group.GroupTypeId == familyGroupTypeId &&
                                    m.GroupId == _family.Id ).FirstOrDefault();
                                if ( groupMember != null )
                                {

                                    if ( familyMember.Removed )
                                    {
                                        var newFamilyChanges = new List<string>();
                                        newFamilyChanges.Add( "Created" );

                                        // Family member was removed and should be created in their own new family
                                        var newFamily = new Group();
                                        newFamily.Name = familyMember.LastName + " Family";
                                        History.EvaluateChange( newFamilyChanges, "Name", string.Empty, newFamily.Name );

                                        newFamily.GroupTypeId = familyGroupTypeId;

                                        if ( _family.CampusId.HasValue )
                                        {
                                            History.EvaluateChange( newFamilyChanges, "Campus", string.Empty, CampusCache.Read( _family.CampusId.Value ).Name );
                                        }
                                        newFamily.CampusId = _family.CampusId;

                                        familyService.Add( newFamily, CurrentPersonId );
                                        familyService.Save( newFamily, CurrentPersonId );

                                        historyService.SaveChanges( typeof( Group ), Rock.SystemGuid.Category.HISTORY_PERSON_FAMILY_CHANGES.AsGuid(),
                                            newFamily.Id, newFamilyChanges, CurrentPersonId );

                                        // If person's previous giving group was this family, set it to their new family id
                                        if ( groupMember.Person.GivingGroup != null && groupMember.Person.GivingGroupId == _family.Id )
                                        {
                                            History.EvaluateChange( demographicChanges, "Giving Group", groupMember.Person.GivingGroup.Name, _family.Name );
                                            groupMember.Person.GivingGroupId = newFamily.Id;
                                        }

                                        groupMember.Group = newFamily;
                                        familyMemberService.Save( groupMember, CurrentPersonId );

                                        var newMemberChanges = new List<string>();
                                        History.EvaluateChange( newMemberChanges, "Role", string.Empty, groupMember.GroupRole.Name );
                                        historyService.SaveChanges( typeof( Person ), Rock.SystemGuid.Category.HISTORY_PERSON_FAMILY_CHANGES.AsGuid(),
                                            groupMember.Person.Id, newMemberChanges, CurrentPersonId );

                                        History.EvaluateChange( memberChanges, "Role", groupMember.GroupRole.Name, string.Empty );

                                        newFamilies.Add( newFamily );
                                    }
                                    else
                                    {
                                        // Existing member was not remvoved
                                        if ( role != null )
                                        {
                                            History.EvaluateChange( memberChanges, "Role",
                                                groupMember.GroupRole != null ? groupMember.GroupRole.Name : string.Empty, role.Name );
                                            groupMember.GroupRoleId = role.Id;

                                            History.EvaluateChange( demographicChanges, "Record Status", DefinedValueCache.GetName( groupMember.Person.RecordStatusValueId ), DefinedValueCache.GetName( recordStatusValueID ) );
                                            groupMember.Person.RecordStatusValueId = recordStatusValueID;

                                            History.EvaluateChange( demographicChanges, "Record Status Reason", DefinedValueCache.GetName( groupMember.Person.RecordStatusReasonValueId ), DefinedValueCache.GetName( reasonValueId ) );
                                            groupMember.Person.RecordStatusReasonValueId = reasonValueId;

                                            familyMemberService.Save( groupMember, CurrentPersonId );
                                        }
                                    }
                                }
                            }

                            // Remove anyone that was moved from another family
                            if ( familyMember.RemoveFromOtherFamilies )
                            {
                                var otherFamilies = familyMemberService.Queryable()
                                    .Where( m =>
                                        m.PersonId == familyMember.Id &&
                                        m.Group.GroupTypeId == familyGroupTypeId &&
                                        m.GroupId != _family.Id )
                                    .ToList();

                                foreach ( var otherFamilyMember in otherFamilies )
                                {
                                    var fm = familyMemberService.Get( otherFamilyMember.Id );

                                    // If the person's giving group id was the family they are being removed from, update it to this new family's id
                                    if ( fm.Person.GivingGroupId == fm.GroupId )
                                    {
                                        var person = personService.Get( fm.PersonId );

                                        History.EvaluateChange( demographicChanges, "Giving Group", person.GivingGroup.Name, _family.Name );
                                        person.GivingGroupId = _family.Id;

                                        personService.Save( person, CurrentPersonId );
                                    }

                                    var oldMemberChanges = new List<string>();
                                    History.EvaluateChange( oldMemberChanges, "Role", fm.GroupRole.Name, string.Empty );

                                    familyMemberService.Delete( fm, CurrentPersonId );
                                    familyMemberService.Save( fm, CurrentPersonId );

                                    historyService.SaveChanges( typeof( Person ), Rock.SystemGuid.Category.HISTORY_PERSON_FAMILY_CHANGES.AsGuid(),
                                        fm.Person.Id, oldMemberChanges, CurrentPersonId );

                                    var f = familyService.Queryable()
                                        .Where( g =>
                                            g.Id == otherFamilyMember.GroupId &&
                                            !g.Members.Any() )
                                        .FirstOrDefault();

                                    if ( f != null )
                                    {
                                        var oldFamilyChanges = new List<string>();
                                        oldFamilyChanges.Add( "Deleted" );
                                        historyService.SaveChanges( typeof( Group ), Rock.SystemGuid.Category.HISTORY_PERSON_FAMILY_CHANGES.AsGuid(),
                                            f.Id, oldFamilyChanges, CurrentPersonId );

                                        familyService.Delete( f, CurrentPersonId );
                                        familyService.Save( f, CurrentPersonId );
                                    }
                                }
                            }

                            historyService.SaveChanges( typeof( Person ), Rock.SystemGuid.Category.HISTORY_PERSON_DEMOGRAPHIC_CHANGES.AsGuid(),
                                familyMember.Id, demographicChanges, CurrentPersonId );

                            historyService.SaveChanges( typeof( Person ), Rock.SystemGuid.Category.HISTORY_PERSON_FAMILY_CHANGES.AsGuid(),
                                familyMember.Id, memberChanges, _family.Name, typeof( Group ), _family.Id, CurrentPersonId );
                        }

                        // SAVE LOCATIONS
                        var groupLocationService = new GroupLocationService();

                        // delete any group locations that were removed
                        var remainingLocationIds = FamilyAddresses.Where( a => a.Id > 0 ).Select( a => a.Id ).ToList();
                        foreach ( var removedLocation in groupLocationService.Queryable( "GroupLocationTypeValue,Location" )
                            .Where( l => l.GroupId == _family.Id &&
                                !remainingLocationIds.Contains( l.Id ) ) )
                        {
                            History.EvaluateChange( familyChanges, removedLocation.GroupLocationTypeValue.Name + " Location",
                                removedLocation.Location.ToString(), string.Empty );
                            groupLocationService.Delete( removedLocation, CurrentPersonId );
                            groupLocationService.Save( removedLocation, CurrentPersonId );
                        }

                        foreach ( var familyAddress in FamilyAddresses )
                        {
                            Location updatedAddress = null;
                            if ( familyAddress.LocationIsDirty )
                            {
                                updatedAddress = new LocationService().Get(
                                    familyAddress.Street1, familyAddress.Street2, familyAddress.City,
                                    familyAddress.State, familyAddress.Zip );
                            }

                            GroupLocation groupLocation = null;
                            if ( familyAddress.Id > 0 )
                            {
                                groupLocation = groupLocationService.Get( familyAddress.Id );
                            }
                            if ( groupLocation == null )
                            {
                                groupLocation = new GroupLocation();
                                groupLocation.GroupId = _family.Id;
                                groupLocationService.Add( groupLocation, CurrentPersonId );
                            }

                            History.EvaluateChange( familyChanges, "Location Type",
                                groupLocation.GroupLocationTypeValueId.HasValue ? DefinedValueCache.Read( groupLocation.GroupLocationTypeValueId.Value ).Name : string.Empty,
                                familyAddress.LocationTypeName );
                            groupLocation.GroupLocationTypeValueId = familyAddress.LocationTypeId;

                            History.EvaluateChange( familyChanges, familyAddress.LocationTypeName + " Location Is Mailing",
                                groupLocation.IsMailingLocation.ToString(), familyAddress.IsMailing.ToString() );
                            groupLocation.IsMailingLocation = familyAddress.IsMailing;

                            History.EvaluateChange( familyChanges, familyAddress.LocationTypeName + " Location Is Location",
                                groupLocation.IsMappedLocation.ToString(), familyAddress.IsLocation.ToString() );
                            groupLocation.IsMappedLocation = familyAddress.IsLocation;

                            if ( updatedAddress != null )
                            {
                                History.EvaluateChange( familyChanges, familyAddress.LocationTypeName + " Location",
                                    groupLocation.Location.ToString(), updatedAddress.ToString() );
                                groupLocation.Location = updatedAddress;
                            }

                            groupLocationService.Save( groupLocation, CurrentPersonId );

                            // Add the same locations to any new families created by removing an existing family member
                            if ( newFamilies.Any() )
                            {
                                //reload grouplocation for access to child properties
                                groupLocation = groupLocationService.Get( groupLocation.Id );
                                foreach ( var newFamily in newFamilies )
                                {
                                    var newFamilyLocation = new GroupLocation();
                                    newFamilyLocation.GroupId = newFamily.Id;
                                    newFamilyLocation.LocationId = groupLocation.LocationId;
                                    newFamilyLocation.GroupLocationTypeValueId = groupLocation.GroupLocationTypeValueId;
                                    newFamilyLocation.IsMailingLocation = groupLocation.IsMailingLocation;
                                    newFamilyLocation.IsMappedLocation = groupLocation.IsMappedLocation;
                                    groupLocationService.Add( newFamilyLocation, CurrentPersonId );
                                    groupLocationService.Save( newFamilyLocation, CurrentPersonId );
                                }
                            }
                        }

                        historyService.SaveChanges( typeof( Group ), Rock.SystemGuid.Category.HISTORY_PERSON_FAMILY_CHANGES.AsGuid(),
                            _family.Id, familyChanges, CurrentPersonId );

                        _family = familyService.Get( _family.Id );
                        if ( _family.Members.Any( m => m.PersonId == Person.Id ) )
                        {
                            Response.Redirect( string.Format( "~/Person/{0}", Person.Id ), false );
                        }
                        else
                        {
                            var fm = _family.Members
                                .Where( m =>
                                    m.GroupRole.Guid.Equals( new Guid( Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT ) ) &&
                                    m.Person.Gender == Gender.Male )
                                .OrderByDescending( m => m.Person.Age )
                                .FirstOrDefault();
                            if ( fm == null )
                            {
                                fm = _family.Members
                                    .Where( m =>
                                        m.GroupRole.Guid.Equals( new Guid( Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT ) ) )
                                    .OrderByDescending( m => m.Person.Age )
                                    .FirstOrDefault();
                            }
                            if ( fm == null )
                            {
                                fm = _family.Members
                                    .OrderByDescending( m => m.Person.Age )
                                    .FirstOrDefault();
                            }
                            if ( fm != null )
                            {
                                Response.Redirect( string.Format( "~/Person/{0}", fm.PersonId ), false );
                            }
                            else
                            {
                                Response.Redirect( "~", false );
                            }
                        }

                    }
                } );
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Handles the Click event of the DeleteGroupMember control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Rock.Web.UI.Controls.RowEventArgs" /> instance containing the event data.</param>
        protected void DeleteGroupMember_Click( object sender, Rock.Web.UI.Controls.RowEventArgs e )
        {
            RockTransactionScope.WrapTransaction( () =>
            {
                GroupMemberService groupMemberService = new GroupMemberService();
                GroupMember groupMember = groupMemberService.Get( e.RowKeyId );
                if ( groupMember != null )
                {
                    string errorMessage;
                    if ( !groupMemberService.CanDelete( groupMember, out errorMessage ) )
                    {
                        mdGridWarning.Show( errorMessage, ModalAlertType.Information );
                        return;
                    }

                    int groupId = groupMember.GroupId;

                    groupMemberService.Delete( groupMember, CurrentPersonId );
                    groupMemberService.Save( groupMember, CurrentPersonId );

                    Group group = new GroupService().Get( groupId );
                    if ( group.IsSecurityRole || group.GroupType.Guid.Equals(Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid() ))
                    {
                        // person removed from SecurityRole, Flush
                        Rock.Security.Role.Flush( group.Id );
                        Rock.Security.Authorization.Flush();
                    }
                }
            } );

            BindGroupMembersGrid();
        }
Esempio n. 5
0
        void modalAddPerson_SaveClick( object sender, EventArgs e )
        {
            if (!string.IsNullOrWhiteSpace(acPerson.Value))
            {
                int personId = int.MinValue;
                if ( int.TryParse( acPerson.Value, out personId ) )
                {
                    int? roleId = grpRole.GroupRoleId;
                    if ( roleId.HasValue )
                    {
                        using ( new UnitOfWorkScope() )
                        {
                            var memberService = new GroupMemberService();
                            var group = memberService.Queryable()
                                .Where( m =>
                                    m.PersonId == Person.Id &&
                                    m.GroupRole.Guid == ownerRoleGuid
                                )
                                .Select( m => m.Group )
                                .FirstOrDefault();

                            if ( group != null )
                            {
                                var groupMember = memberService.Queryable()
                                    .Where( m =>
                                        m.GroupId == group.Id &&
                                        m.PersonId == personId &&
                                        m.GroupRoleId == roleId.Value )
                                    .FirstOrDefault();

                                if ( groupMember == null )
                                {
                                    groupMember = new GroupMember();
                                    groupMember.GroupId = group.Id;
                                    groupMember.PersonId = personId;
                                    groupMember.GroupRoleId = roleId.Value;
                                    memberService.Add( groupMember, CurrentPersonId );
                                }

                                memberService.Save( groupMember, CurrentPersonId );
                                if ( IsKnownRelationships )
                                {
                                    var inverseGroupMember = memberService.GetInverseRelationship(
                                        groupMember, bool.Parse( GetAttributeValue( "CreateGroup" ) ), CurrentPersonId );
                                    if ( inverseGroupMember != null )
                                    {
                                        memberService.Save( inverseGroupMember, CurrentPersonId );
                                    }
                                }
                            }
                        }
                    }
                }
            }

            BindData();
        }
Esempio n. 6
0
        void rGroupMembers_ItemCommand( object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e )
        {
            int groupMemberId = int.MinValue;
            if ( int.TryParse( e.CommandArgument.ToString(), out groupMemberId ) )
            {
                var service = new GroupMemberService();
                var groupMember = service.Get( groupMemberId );
                if ( groupMember != null )
                {
                    if ( e.CommandName == "EditRole" )
                    {
                        ShowModal(groupMember.Person, groupMember.GroupRoleId);
                    }

                    else if ( e.CommandName == "RemoveRole" )
                    {
                        if ( IsKnownRelationships )
                        {
                            var inverseGroupMember = service.GetInverseRelationship( groupMember, false, CurrentPersonId );
                            if ( inverseGroupMember != null )
                            {
                                service.Delete( inverseGroupMember, CurrentPersonId );
                            }
                        }

                        service.Delete( groupMember, CurrentPersonId );
                        service.Save( groupMember, CurrentPersonId );

                        BindData();
                    }
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="action">The workflow action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public override bool Execute( Model.WorkflowAction action, Object entity, out List<string> errorMessages )
        {
            var checkInState = GetCheckInState( entity, out errorMessages );
            if ( checkInState != null )
            {
                DateTime startDateTime = DateTime.Now;

                int securityCodeLength = 3;
                if ( !int.TryParse( GetAttributeValue( action, "SecurityCodeLength" ), out securityCodeLength ) )
                {
                    securityCodeLength = 3;
                }

                using ( var uow = new Rock.Data.UnitOfWorkScope() )
                {
                    var attendanceCodeService = new AttendanceCodeService();
                    var attendanceService = new AttendanceService();
                    var groupMemberService = new GroupMemberService();

                    foreach ( var family in checkInState.CheckIn.Families.Where( f => f.Selected ) )
                    {
                        foreach ( var person in family.People.Where( p => p.Selected ) )
                        {
                            var attendanceCode = attendanceCodeService.GetNew( securityCodeLength );
                            person.SecurityCode = attendanceCode.Code;

                            foreach ( var groupType in person.GroupTypes.Where( g => g.Selected ) )
                            {
                                foreach ( var group in groupType.Groups.Where( g => g.Selected ) )
                                {
                                    foreach ( var location in group.Locations.Where( l => l.Selected ) )
                                    {
                                        if ( groupType.GroupType.AttendanceRule == AttendanceRule.AddOnCheckIn &&
                                            groupType.GroupType.DefaultGroupRoleId.HasValue &&
                                            !groupMemberService.GetByGroupIdAndPersonId( group.Group.Id, person.Person.Id, true ).Any() )
                                        {
                                            var groupMember = new GroupMember();
                                            groupMember.GroupId = group.Group.Id;
                                            groupMember.PersonId = person.Person.Id;
                                            groupMember.GroupRoleId = groupType.GroupType.DefaultGroupRoleId.Value;
                                            groupMemberService.Add( groupMember, null );
                                            groupMemberService.Save( groupMember, null );
                                        }

                                        foreach ( var schedule in location.Schedules.Where( s => s.Selected ) )
                                        {
                                            // Only create one attendance record per day for each person/schedule/group/location
                                            var attendance = attendanceService.Get( startDateTime, location.Location.Id, schedule.Schedule.Id, group.Group.Id, person.Person.Id );
                                            if ( attendance == null )
                                            {
                                                attendance = ((Rock.Data.RockContext)uow.DbContext).Attendances.Create();
                                                attendance.LocationId = location.Location.Id;
                                                attendance.ScheduleId = schedule.Schedule.Id;
                                                attendance.GroupId = group.Group.Id;
                                                attendance.PersonId = person.Person.Id;
                                                attendance.DeviceId = checkInState.Kiosk.Device.Id;
                                                attendance.SearchTypeValueId = checkInState.CheckIn.SearchType.Id;
                                                attendanceService.Add( attendance, null );
                                            }

                                            attendance.AttendanceCodeId = attendanceCode.Id;
                                            attendance.StartDateTime = startDateTime;
                                            attendance.EndDateTime = null;
                                            attendance.DidAttend = true;
                                            attendanceService.Save( attendance, null );

                                            KioskLocationAttendance.AddAttendance( attendance );
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                return true;

            }

            return false;
        }
Esempio n. 8
0
        /// <summary>
        /// Adds the group member.
        /// </summary>
        /// <param name="familyGroup">The family group.</param>
        /// <param name="person">The person.</param>
        /// <returns></returns>
        protected GroupMember AddGroupMember( int familyGroupId, Person person )
        {
            GroupMember groupMember = new GroupMember().Clone( false );
            GroupMemberService groupMemberService = new GroupMemberService();
            groupMember.IsSystem = false;
            groupMember.GroupId = familyGroupId;
            groupMember.PersonId = person.Id;
            if ( person.Age >= 18 )
            {
                groupMember.GroupRoleId = new GroupTypeRoleService().Get( new Guid( Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT ) ).Id;
            }
            else
            {
                groupMember.GroupRoleId = new GroupTypeRoleService().Get( new Guid( Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_CHILD ) ).Id;
            }

            Rock.Data.RockTransactionScope.WrapTransaction( () =>
            {
                groupMemberService.Add( groupMember, CurrentPersonId );
                groupMemberService.Save( groupMember, CurrentPersonId );
            } );

            return groupMember;
        }
Esempio n. 9
0
        /// <summary>
        /// Handles the RowCommand event of the grdPersonSearchResults control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="GridViewCommandEventArgs"/> instance containing the event data.</param>
        protected void rGridPersonResults_AddExistingPerson( object sender, GridViewCommandEventArgs e )
        {
            if ( e.CommandName == "Add" )
            {
                GroupMemberService groupMemberService = new GroupMemberService();
                int index = int.Parse( e.CommandArgument.ToString() );
                int personId = int.Parse( rGridPersonResults.DataKeys[index].Value.ToString() );

                var family = CurrentCheckInState.CheckIn.Families.Where( f => f.Selected ).FirstOrDefault();
                if ( family != null )
                {
                    var checkInPerson = new CheckInPerson();
                    checkInPerson.Person = new PersonService().Get( personId ).Clone( false );
                    var isPersonInFamily = family.People.Any( p => p.Person.Id == checkInPerson.Person.Id );
                    if ( !isPersonInFamily )
                    {
                        if ( personVisitorType.Value != "Visitor" )
                        {
                            var groupMember = groupMemberService.GetByPersonId( personId ).FirstOrDefault();
                            groupMember.GroupId = family.Group.Id;
                            Rock.Data.RockTransactionScope.WrapTransaction( () =>
                            {
                                groupMemberService.Save( groupMember, CurrentPersonId );
                            } );

                            checkInPerson.FamilyMember = true;
                            hfSelectedPerson.Value += personId + ",";
                        }
                        else
                        {
                            AddVisitorGroupMemberRoles( family, personId );
                            checkInPerson.FamilyMember = false;
                            hfSelectedVisitor.Value += personId + ",";
                        }
                        checkInPerson.Selected = true;
                        family.People.Add( checkInPerson );
                        ProcessFamily();
                    }
                    
                    mpeAddPerson.Hide();
                }
                else
                {
                    string errorMsg = "<ul><li>You have to pick a family to add this person to.</li></ul>";
                    maWarning.Show( errorMsg, Rock.Web.UI.Controls.ModalAlertType.Warning );
                }
            }
            else
            {
                mpeAddPerson.Show();
                BindPersonGrid();
            }
        }
Esempio n. 10
0
        protected void btnSave_Click( object sender, EventArgs e )
        {
            if ( string.IsNullOrWhiteSpace( txtFirstName.Text ) ||
                string.IsNullOrWhiteSpace( txtLastName.Text ) ||
                string.IsNullOrWhiteSpace( txtEmail.Text ) )
            {
                ShowError( "Missing Information", "Please enter a value for First Name, Last Name, and Email" );
            }
            else
            {
                var person = GetPerson();
                if ( person != null )
                {
                    int groupId = int.MinValue;
                    if ( int.TryParse( GetAttributeValue( "Group" ), out groupId ) )
                    {
                        using ( new UnitOfWorkScope() )
                        {
                            var groupService = new GroupService();
                            var groupMemberService = new GroupMemberService();

                            var group = groupService.Get( groupId );
                            if ( group != null && group.GroupType.DefaultGroupRoleId.HasValue )
                            {
                                string linkedPage = GetAttributeValue( "ConfirmationPage" );
                                if ( !string.IsNullOrWhiteSpace( linkedPage ) )
                                {
                                    var member = group.Members.Where( m => m.PersonId == person.Id ).FirstOrDefault();

                                    // If person has not registered or confirmed their registration
                                    if ( member == null || member.GroupMemberStatus != GroupMemberStatus.Active )
                                    {
                                        Email email = null;

                                        Guid guid = Guid.Empty;
                                        if ( Guid.TryParse( GetAttributeValue( "ConfirmationEmail" ), out guid ) )
                                        {
                                            email = new Email( guid );
                                        }

                                        if ( member == null )
                                        {
                                            member = new GroupMember();
                                            member.GroupId = group.Id;
                                            member.PersonId = person.Id;
                                            member.GroupRoleId = group.GroupType.DefaultGroupRoleId.Value;

                                            // If a confirmation email is configured, set status to Pending otherwise set it to active
                                            member.GroupMemberStatus = email != null ? GroupMemberStatus.Pending : GroupMemberStatus.Active;

                                            groupMemberService.Add( member, CurrentPersonId );
                                            groupMemberService.Save( member, CurrentPersonId );
                                            member = groupMemberService.Get( member.Id );
                                        }

                                        // Send the confirmation
                                        if ( email != null )
                                        {
                                            var mergeObjects = new Dictionary<string, object>();
                                            mergeObjects.Add( "Member", member );

                                            var pageParams = new Dictionary<string, string>();
                                            pageParams.Add( "gm", member.UrlEncodedKey );
                                            var pageReference = new Rock.Web.PageReference( linkedPage, pageParams );
                                            mergeObjects.Add( "ConfirmationPage", pageReference.BuildUrl() );

                                            var recipients = new Dictionary<string, Dictionary<string, object>>();
                                            recipients.Add( person.Email, mergeObjects );
                                            email.Send( recipients );
                                        }

                                        ShowSuccess( GetAttributeValue( "SuccessMessage" ) );

                                    }
                                    else
                                    {
                                        var pageParams = new Dictionary<string, string>();
                                        pageParams.Add( "gm", member.UrlEncodedKey );
                                        var pageReference = new Rock.Web.PageReference( linkedPage, pageParams );
                                        Response.Redirect( pageReference.BuildUrl(), false );
                                    }
                                }
                                else
                                {
                                    ShowError( "Configuration Error", "Invalid Confirmation Page setting" );
                                }
                            }
                            else
                            {
                                ShowError( "Configuration Error",
                                    "The configured group does not exist, or it's group type does not have a default role configured." );
                            }
                        }
                    }
                    else
                    {
                        ShowError( "Configuration Error", "Invalid Group setting" );
                    }
                }
            }
        }