/// <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)
        {
            var rockContext = new RockContext();

            GroupLocationService groupLocationService = new GroupLocationService(rockContext);
            ScheduleService      scheduleService      = new ScheduleService(rockContext);

            var gridViewRows = gGroupLocationSchedule.Rows;

            foreach (GridViewRow row in gridViewRows.OfType <GridViewRow>())
            {
                int           groupLocationId = int.Parse(gGroupLocationSchedule.DataKeys[row.RowIndex].Value as string);
                GroupLocation groupLocation   = groupLocationService.Get(groupLocationId);
                if (groupLocation != null)
                {
                    foreach (var fieldCell in row.Cells.OfType <DataControlFieldCell>())
                    {
                        CheckBoxEditableField checkBoxTemplateField = fieldCell.ContainingField as CheckBoxEditableField;
                        if (checkBoxTemplateField != null)
                        {
                            CheckBox checkBox   = fieldCell.Controls[0] as CheckBox;
                            string   dataField  = (fieldCell.ContainingField as CheckBoxEditableField).DataField;
                            int      scheduleId = int.Parse(dataField.Replace("scheduleField_", string.Empty));

                            // update GroupLocationSchedule depending on if the Schedule is Checked or not
                            if (checkBox.Checked)
                            {
                                // This schedule is selected, so if GroupLocationSchedule doesn't already have this schedule, add it
                                if (!groupLocation.Schedules.Any(a => a.Id == scheduleId))
                                {
                                    var schedule = scheduleService.Get(scheduleId);
                                    groupLocation.Schedules.Add(schedule);
                                }
                            }
                            else
                            {
                                // This schedule is not selected, so if GroupLocationSchedule has this schedule, delete it
                                if (groupLocation.Schedules.Any(a => a.Id == scheduleId))
                                {
                                    groupLocation.Schedules.Remove(groupLocation.Schedules.FirstOrDefault(a => a.Id == scheduleId));
                                }
                            }
                        }
                    }
                }
            }

            rockContext.SaveChanges();

            Rock.CheckIn.KioskDevice.FlushAll();

            if (_groupTypeId.HasValue)
            {
                NavigateToParentPage(new Dictionary <string, string> {
                    { "CheckinTypeId", _groupTypeId.ToString() }
                });
            }
        }
        /// <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)
        {
            using (new UnitOfWorkScope())
            {
                GroupLocationService groupLocationService = new GroupLocationService();
                ScheduleService      scheduleService      = new ScheduleService();

                RockTransactionScope.WrapTransaction(() =>
                {
                    var gridViewRows = gGroupLocationSchedule.Rows;
                    foreach (GridViewRow row in gridViewRows.OfType <GridViewRow>())
                    {
                        int groupLocationId         = int.Parse(gGroupLocationSchedule.DataKeys[row.RowIndex].Value as string);
                        GroupLocation groupLocation = groupLocationService.Get(groupLocationId);
                        if (groupLocation != null)
                        {
                            foreach (var fieldCell in row.Cells.OfType <DataControlFieldCell>())
                            {
                                CheckBoxEditableField checkBoxTemplateField = fieldCell.ContainingField as CheckBoxEditableField;
                                if (checkBoxTemplateField != null)
                                {
                                    CheckBox checkBox = fieldCell.Controls[0] as CheckBox;
                                    string dataField  = (fieldCell.ContainingField as CheckBoxEditableField).DataField;
                                    int scheduleId    = int.Parse(dataField.Replace("scheduleField_", string.Empty));

                                    // update GroupLocationSchedule depending on if the Schedule is Checked or not
                                    if (checkBox.Checked)
                                    {
                                        // This schedule is selected, so if GroupLocationSchedule doesn't already have this schedule, add it
                                        if (!groupLocation.Schedules.Any(a => a.Id == scheduleId))
                                        {
                                            var schedule = scheduleService.Get(scheduleId);
                                            groupLocation.Schedules.Add(schedule);
                                        }
                                    }
                                    else
                                    {
                                        // This schedule is not selected, so if GroupLocationSchedule has this schedule, delete it
                                        if (groupLocation.Schedules.Any(a => a.Id == scheduleId))
                                        {
                                            groupLocation.Schedules.Remove(groupLocation.Schedules.FirstOrDefault(a => a.Id == scheduleId));
                                        }
                                    }
                                }
                            }

                            groupLocationService.Save(groupLocation, this.CurrentPersonId);
                        }
                    }
                });
            }

            NavigateToParentPage();
        }
        /// <summary>
        /// Executes the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Execute(IJobExecutionContext context)
        {
            JobDataMap dataMap = context.JobDetail.JobDataMap;

            var rockContext     = new RockContext();
            var definedTypeGuid = dataMap.GetString("DisabledGroupLocationSchedules").AsGuidOrNull();

            if (definedTypeGuid == null)
            {
                return;
            }
            var definedTypeService   = new DefinedTypeService(rockContext);
            var definedValueService  = new DefinedValueService(rockContext);
            var dtDeactivated        = definedTypeService.Get(definedTypeGuid ?? new Guid());
            var dvDeactivated        = dtDeactivated.DefinedValues.ToList();
            var scheduleService      = new ScheduleService(rockContext);
            var groupLocationService = new GroupLocationService(rockContext);
            var deactivatedGroupLocationSchedules = dvDeactivated.Select(dv => dv.Value.Split('|'))
                                                    .Select(s => new
            {
                GroupLocation = groupLocationService.Get(s[0].AsInteger()),
                Schedule      = scheduleService.Get(s[1].AsInteger()),
            }).ToList();

            //add schedules back
            foreach (var groupLocationSchedule in deactivatedGroupLocationSchedules)
            {
                if (!groupLocationSchedule.GroupLocation.Schedules.Contains(groupLocationSchedule.Schedule))
                {
                    groupLocationSchedule.GroupLocation.Schedules.Add(groupLocationSchedule.Schedule);
                }
            }
            //Remove defined values
            foreach (var value in dvDeactivated)
            {
                definedValueService.Delete(value);
                Rock.Web.Cache.DefinedValueCache.Remove(value.Id);
            }

            //clear defined type cache
            Rock.Web.Cache.DefinedTypeCache.Remove(dtDeactivated.Id);

            rockContext.SaveChanges();

            //flush kiosk cache
            Rock.CheckIn.KioskDevice.Clear();

            context.Result = string.Format("Finished at {0}. Reset {1} GroupScheduleLocations.", Rock.RockDateTime.Now, deactivatedGroupLocationSchedules.Count);
        }
Beispiel #4
0
        /// <summary>
        /// Executes the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Execute(IJobExecutionContext context)
        {
            JobDataMap dataMap = context.JobDetail.JobDataMap;

            var rockContext = new RockContext();

            var definedTypeService   = new DefinedTypeService(rockContext);
            var definedValueService  = new DefinedValueService(rockContext);
            var dtDeactivated        = definedTypeService.Get(Constants.DEFINED_TYPE_DISABLED_GROUPLOCATIONSCHEDULES.AsGuid());
            var dvDeactivated        = dtDeactivated.DefinedValues.ToList();
            var scheduleService      = new ScheduleService(rockContext);
            var groupLocationService = new GroupLocationService(rockContext);
            var deactivatedGroupLocationSchedules = dvDeactivated.Select(dv => dv.Value.Split('|'))
                                                    .Select(s => new
            {
                GroupLocation = groupLocationService.Get(s[0].AsInteger()),
                Schedule      = scheduleService.Get(s[1].AsInteger()),
            }).ToList();

            //add schedules back
            foreach (var groupLocationSchedule in deactivatedGroupLocationSchedules)
            {
                if (!groupLocationSchedule.GroupLocation.Schedules.Contains(groupLocationSchedule.Schedule))
                {
                    groupLocationSchedule.GroupLocation.Schedules.Add(groupLocationSchedule.Schedule);
                }
            }
            //Remove defined values
            foreach (var value in dvDeactivated)
            {
                definedValueService.Delete(value);
                Rock.Web.Cache.DefinedValueCache.Remove(value.Id);
            }

            //clear defined type cache
            Rock.Web.Cache.DefinedTypeCache.Remove(dtDeactivated.Id);

            rockContext.SaveChanges();

            //clear caches
            KioskTypeCache.Clear();
            Rock.CheckIn.KioskDevice.Clear();
            OccurrenceCache.Clear();
            AttendanceCache.Clear();

            context.Result = string.Format("Finished at {0}. Reset {1} GroupScheduleLocations.", Rock.RockDateTime.Now, deactivatedGroupLocationSchedules.Count);
        }
        /// <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;

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

                    // SAVE FAMILY
                    _family          = familyService.Get(_family.Id);
                    _family.Name     = tbFamilyName.Text;
                    _family.CampusId = cpCampus.SelectedValueAsInt();

                    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 role = familyRoles.Where(r => r.Guid.Equals(familyMember.RoleGuid)).FirstOrDefault();
                        if (role == null)
                        {
                            role = familyRoles.FirstOrDefault();
                        }

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

                            if (familyMember.Id == -1)
                            {
                                // added new person
                                groupMember.Person           = new Person();
                                groupMember.Person.GivenName = familyMember.FirstName;
                                groupMember.Person.LastName  = familyMember.LastName;
                                groupMember.Person.Gender    = familyMember.Gender;
                                groupMember.Person.BirthDate = familyMember.BirthDate;
                            }
                            else
                            {
                                // added from other family
                                groupMember.Person = personService.Get(familyMember.Id);
                            }

                            groupMember.Person.RecordStatusValueId       = recordStatusValueID;
                            groupMember.Person.RecordStatusReasonValueId = reasonValueId;

                            groupMember.GroupId = _family.Id;
                            if (role != null)
                            {
                                groupMember.GroupRoleId = role.Id;
                            }

                            if (groupMember.Person != null)
                            {
                                familyMemberService.Add(groupMember, CurrentPersonId);
                                familyMemberService.Save(groupMember, CurrentPersonId);
                            }
                        }
                        else
                        {
                            // existing family members
                            var groupMember = familyMemberService.Queryable().Where(m =>
                                                                                    m.PersonId == familyMember.Id &&
                                                                                    m.Group.GroupTypeId == familyGroupTypeId &&
                                                                                    m.GroupId == _family.Id).FirstOrDefault();
                            if (groupMember != null)
                            {
                                if (familyMember.Removed)
                                {
                                    // Family member was removed and should be created in their own new family
                                    var newFamily = new Group();
                                    newFamily.Name        = familyMember.LastName + " Family";
                                    newFamily.GroupTypeId = familyGroupTypeId;
                                    newFamily.CampusId    = _family.CampusId;
                                    familyService.Add(newFamily, CurrentPersonId);
                                    familyService.Save(newFamily, CurrentPersonId);

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

                                    newFamilies.Add(newFamily);
                                }
                                else
                                {
                                    // Existing member was not remvoved
                                    if (role != null)
                                    {
                                        groupMember.GroupRoleId = role.Id;
                                        groupMember.Person.RecordStatusValueId       = recordStatusValueID;
                                        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);
                                familyMemberService.Delete(fm, CurrentPersonId);
                                familyMemberService.Save(fm, CurrentPersonId);

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

                                if (f != null)
                                {
                                    familyService.Delete(f, CurrentPersonId);
                                    familyService.Save(f, 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()
                             .Where(l => l.GroupId == _family.Id &&
                                    !remainingLocationIds.Contains(l.Id)))
                    {
                        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);
                        }

                        groupLocation.GroupLocationTypeValueId = familyAddress.LocationTypeId;
                        groupLocation.IsMailing  = familyAddress.IsMailing;
                        groupLocation.IsLocation = familyAddress.IsLocation;
                        if (updatedAddress != null)
                        {
                            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.IsMailing  = groupLocation.IsMailing;
                                newFamilyLocation.IsLocation = groupLocation.IsLocation;
                                groupLocationService.Add(newFamilyLocation, CurrentPersonId);
                                groupLocationService.Save(newFamilyLocation, 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);
                        }
                    }
                }
            }
        }