Exemplo n.º 1
0
        /// <summary>
        /// Handles the Delete event of the gLocations control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param>
        protected void gLocations_Delete(object sender, RowEventArgs e)
        {
            Guid rowGuid = (Guid)e.RowKeyValue;

            GroupLocationsState.RemoveEntity(rowGuid);

            BindLocationsGrid();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Binds the locations grid.
        /// </summary>
        private void BindLocationsGrid()
        {
            gLocations.Actions.ShowAdd = AllowMultipleLocations || !GroupLocationsState.Any();

            gLocations.DataSource = GroupLocationsState
                                    .Select(gl => new
            {
                gl.Guid,
                gl.Location,
                Type      = gl.GroupLocationTypeValue != null ? gl.GroupLocationTypeValue.Value : string.Empty,
                Order     = gl.GroupLocationTypeValue != null ? gl.GroupLocationTypeValue.Order : 0,
                Schedules = gl.Schedules != null ? gl.Schedules.Select(s => s.Name).ToList().AsDelimited(", ") : string.Empty
            })
                                    .OrderBy(i => i.Order)
                                    .ToList();
            gLocations.DataBind();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Handles the SaveClick event of the dlgLocations 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 dlgLocations_SaveClick(object sender, EventArgs e)
        {
            Location    location            = null;
            int?        memberPersonAliasId = null;
            RockContext rockContext         = new RockContext();

            if (LocationTypeTab.Equals(MEMBER_LOCATION_TAB_TITLE))
            {
                if (ddlMember.SelectedValue != null)
                {
                    var ids = ddlMember.SelectedValue.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries).AsIntegerList().ToArray();
                    if (ids.Length == 2)
                    {
                        int locationId     = ids[0];
                        int primaryAliasId = ids[1];
                        var dbLocation     = new LocationService(rockContext).Get(locationId);
                        if (dbLocation != null)
                        {
                            location = new Location();
                            location.CopyPropertiesFrom(dbLocation);
                        }

                        memberPersonAliasId = new PersonAliasService(rockContext).GetPrimaryAliasId(primaryAliasId);
                    }
                }
            }
            else
            {
                if (locpGroupLocation.Location != null)
                {
                    location = new Location();
                    location.CopyPropertiesFrom(locpGroupLocation.Location);
                }
            }

            if (location != null)
            {
                GroupLocation groupLocation = null;

                Guid guid = hfAddLocationGroupGuid.Value.AsGuid();
                if (!guid.IsEmpty())
                {
                    groupLocation = GroupLocationsState.FirstOrDefault(l => l.Guid.Equals(guid));
                }

                if (groupLocation == null)
                {
                    groupLocation = new GroupLocation();
                    GroupLocationsState.Add(groupLocation);
                }

                groupLocation.GroupMemberPersonAliasId = memberPersonAliasId;
                groupLocation.Location   = location;
                groupLocation.LocationId = groupLocation.Location.Id;
                groupLocation.GroupLocationTypeValueId = ddlLocationType.SelectedValueAsId();

                var selectedIds = spSchedules.SelectedValuesAsInt();
                groupLocation.Schedules = new ScheduleService(rockContext).Queryable()
                                          .Where(s => selectedIds.Contains(s.Id)).ToList();

                if (groupLocation.GroupLocationTypeValueId.HasValue)
                {
                    groupLocation.GroupLocationTypeValue = new DefinedValue();
                    var definedValue = new DefinedValueService(rockContext).Get(groupLocation.GroupLocationTypeValueId.Value);
                    if (definedValue != null)
                    {
                        groupLocation.GroupLocationTypeValue.CopyPropertiesFrom(definedValue);
                    }
                }
            }

            BindLocationsGrid();

            dlgLocations.Hide();
            hfActiveDialog.Value = string.Empty;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gs the locations_ show edit.
        /// </summary>
        /// <param name="locationGuid">The location unique identifier.</param>
        protected void gLocations_ShowEdit(Guid locationGuid)
        {
            var rockContext = new RockContext();

            ddlMember.Items.Clear();
            int?groupTypeId = this.CurrentGroupTypeId;

            if (groupTypeId.HasValue)
            {
                var groupType = GroupTypeCache.Read(groupTypeId.Value);
                if (groupType != null)
                {
                    GroupLocationPickerMode groupTypeModes = groupType.LocationSelectionMode;
                    if (groupTypeModes != GroupLocationPickerMode.None)
                    {
                        // Set the location picker modes allowed based on the group type's allowed modes
                        LocationPickerMode modes = LocationPickerMode.None;
                        if ((groupTypeModes & GroupLocationPickerMode.Named) == GroupLocationPickerMode.Named)
                        {
                            modes = modes | LocationPickerMode.Named;
                        }

                        if ((groupTypeModes & GroupLocationPickerMode.Address) == GroupLocationPickerMode.Address)
                        {
                            modes = modes | LocationPickerMode.Address;
                        }

                        if ((groupTypeModes & GroupLocationPickerMode.Point) == GroupLocationPickerMode.Point)
                        {
                            modes = modes | LocationPickerMode.Point;
                        }

                        if ((groupTypeModes & GroupLocationPickerMode.Polygon) == GroupLocationPickerMode.Polygon)
                        {
                            modes = modes | LocationPickerMode.Polygon;
                        }

                        bool displayMemberTab = (groupTypeModes & GroupLocationPickerMode.GroupMember) == GroupLocationPickerMode.GroupMember;
                        bool displayOtherTab  = modes != LocationPickerMode.None;

                        ulNav.Visible             = displayOtherTab && displayMemberTab;
                        pnlMemberSelect.Visible   = displayMemberTab;
                        pnlLocationSelect.Visible = displayOtherTab && !displayMemberTab;

                        if (displayMemberTab)
                        {
                            int?leaderId = lppLeader.PersonId;
                            if (leaderId != null)
                            {
                                var  personService        = new PersonService(rockContext);
                                Guid previousLocationType = Rock.SystemGuid.DefinedValue.GROUP_LOCATION_TYPE_PREVIOUS.AsGuid();
                                var  leader = personService.Get(lppLeader.PersonId.Value);

                                foreach (Group family in personService.GetFamilies(leaderId.Value))
                                {
                                    foreach (GroupLocation familyGroupLocation in family.GroupLocations
                                             .Where(l => l.IsMappedLocation && !l.GroupLocationTypeValue.Guid.Equals(previousLocationType)))
                                    {
                                        ListItem li = new ListItem(
                                            string.Format("{0} {1} ({2})", leader.FullName, familyGroupLocation.GroupLocationTypeValue.Value, familyGroupLocation.Location.ToString()),
                                            string.Format("{0}|{1}", familyGroupLocation.Location.Id, leader.Id));

                                        ddlMember.Items.Add(li);
                                    }
                                }
                            }
                        }

                        if (displayOtherTab)
                        {
                            locpGroupLocation.AllowedPickerModes = modes;
                            locpGroupLocation.CurrentPickerMode  = locpGroupLocation.GetBestPickerModeForLocation(null);
                        }

                        ddlLocationType.DataSource = groupType.LocationTypeValues.ToList();
                        ddlLocationType.DataBind();

                        var groupLocation = GroupLocationsState.FirstOrDefault(l => l.Guid.Equals(locationGuid));
                        if (groupLocation != null && groupLocation.Location != null)
                        {
                            if (displayOtherTab)
                            {
                                locpGroupLocation.CurrentPickerMode = locpGroupLocation.GetBestPickerModeForLocation(groupLocation.Location);

                                locpGroupLocation.MapStyleValueGuid = GetAttributeValue("MapStyle").AsGuid();

                                if (groupLocation.Location != null)
                                {
                                    locpGroupLocation.Location = new LocationService(rockContext).Get(groupLocation.Location.Id);
                                }
                            }

                            if (displayMemberTab && ddlMember.Items.Count > 0 && groupLocation.GroupMemberPersonAliasId.HasValue)
                            {
                                LocationTypeTab = MEMBER_LOCATION_TAB_TITLE;
                                int?personId = new PersonAliasService(rockContext).GetPersonId(groupLocation.GroupMemberPersonAliasId.Value);
                                if (personId.HasValue)
                                {
                                    ddlMember.SetValue(string.Format("{0}|{1}", groupLocation.LocationId, personId.Value));
                                }
                            }
                            else if (displayOtherTab)
                            {
                                LocationTypeTab = OTHER_LOCATION_TAB_TITLE;
                            }

                            ddlLocationType.SetValue(groupLocation.GroupLocationTypeValueId);

                            spSchedules.SetValues(groupLocation.Schedules);

                            hfAddLocationGroupGuid.Value = locationGuid.ToString();
                        }
                        else
                        {
                            hfAddLocationGroupGuid.Value = string.Empty;
                            LocationTypeTab = (displayMemberTab && ddlMember.Items.Count > 0) ? MEMBER_LOCATION_TAB_TITLE : OTHER_LOCATION_TAB_TITLE;
                        }

                        rptLocationTypes.DataSource = _tabs;
                        rptLocationTypes.DataBind();
                        ShowSelectedPane();

                        ShowDialog("Locations", true);
                    }
                }
            }
        }
Exemplo n.º 5
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)
        {
            bool wasSecurityRole = false;
            bool triggersUpdated = false;

            RockContext rockContext = new RockContext();

            GroupService    groupService    = new GroupService(rockContext);
            ScheduleService scheduleService = new ScheduleService(rockContext);

            var roleGroupType   = GroupTypeCache.Read(Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid());
            int roleGroupTypeId = roleGroupType != null ? roleGroupType.Id : int.MinValue;

            int?parentGroupId = hfParentGroupId.Value.AsIntegerOrNull();

            if (parentGroupId != null)
            {
                var allowedGroupIds = LineQuery.GetCellGroupIdsInLine(CurrentPerson, rockContext);
                if (!allowedGroupIds.Contains(parentGroupId.Value))
                {
                    nbMessage.Text         = "You are not allowed to add a group to this parent group.";
                    nbMessage.Visible      = true;
                    pnlEditDetails.Visible = false;
                    btnSave.Enabled        = false;
                    return;
                }
                var parentGroup = groupService.Get(parentGroupId.Value);
                if (parentGroup != null)
                {
                    CurrentGroupTypeId = parentGroup.GroupTypeId;

                    if (!lppLeader.PersonId.HasValue)
                    {
                        nbMessage.Text    = "A Leader is required.";
                        nbMessage.Visible = true;
                        return;
                    }

                    if (!GroupLocationsState.Any())
                    {
                        nbMessage.Text    = "A location is required.";
                        nbMessage.Visible = true;
                        return;
                    }

                    if (CurrentGroupTypeId == 0)
                    {
                        return;
                    }

                    Group group = new Group();
                    group.IsSystem = false;
                    group.Name     = string.Empty;



                    // add/update any group locations that were added or changed in the UI (we already removed the ones that were removed above)
                    foreach (var groupLocationState in GroupLocationsState)
                    {
                        GroupLocation groupLocation = group.GroupLocations.Where(l => l.Guid == groupLocationState.Guid).FirstOrDefault();
                        if (groupLocation == null)
                        {
                            groupLocation = new GroupLocation();
                            group.GroupLocations.Add(groupLocation);
                        }
                        else
                        {
                            groupLocationState.Id   = groupLocation.Id;
                            groupLocationState.Guid = groupLocation.Guid;

                            var selectedSchedules = groupLocationState.Schedules.Select(s => s.Guid).ToList();
                            foreach (var schedule in groupLocation.Schedules.Where(s => !selectedSchedules.Contains(s.Guid)).ToList())
                            {
                                groupLocation.Schedules.Remove(schedule);
                            }
                        }

                        groupLocation.CopyPropertiesFrom(groupLocationState);

                        var existingSchedules = groupLocation.Schedules.Select(s => s.Guid).ToList();
                        foreach (var scheduleState in groupLocationState.Schedules.Where(s => !existingSchedules.Contains(s.Guid)).ToList())
                        {
                            var schedule = scheduleService.Get(scheduleState.Guid);
                            if (schedule != null)
                            {
                                groupLocation.Schedules.Add(schedule);
                            }
                        }
                    }

                    GroupMember leader = new GroupMember();
                    leader.GroupMemberStatus = GroupMemberStatus.Active;
                    leader.PersonId          = lppLeader.PersonId.Value;
                    leader.Person            = new PersonService(rockContext).Get(lppLeader.PersonId.Value);
                    leader.GroupRole         = parentGroup.GroupType.Roles.Where(r => r.IsLeader).FirstOrDefault() ?? parentGroup.GroupType.DefaultGroupRole;

                    group.Name           = String.Format("{0}, {1}", leader.Person.NickName, leader.Person.LastName);
                    group.Description    = tbDescription.Text;
                    group.CampusId       = parentGroup.CampusId;
                    group.GroupTypeId    = CurrentGroupTypeId;
                    group.ParentGroupId  = parentGroupId;
                    group.IsSecurityRole = false;
                    group.IsActive       = true;
                    group.IsPublic       = true;

                    if (dpStartDate.SelectedDate.HasValue)
                    {
                        group.CreatedDateTime = dpStartDate.SelectedDate.Value;
                    }

                    group.Members.Add(leader);

                    string iCalendarContent = string.Empty;

                    // If unique schedule option was selected, but a schedule was not defined, set option to 'None'
                    var scheduleType = rblScheduleSelect.SelectedValueAsEnum <ScheduleType>(ScheduleType.None);
                    if (scheduleType == ScheduleType.Custom)
                    {
                        iCalendarContent = sbSchedule.iCalendarContent;
                        var calEvent = ScheduleICalHelper.GetCalenderEvent(iCalendarContent);
                        if (calEvent == null || calEvent.DTStart == null)
                        {
                            scheduleType = ScheduleType.None;
                        }
                    }

                    if (scheduleType == ScheduleType.Weekly)
                    {
                        if (!dowWeekly.SelectedDayOfWeek.HasValue)
                        {
                            scheduleType = ScheduleType.None;
                        }
                    }

                    int?oldScheduleId = hfUniqueScheduleId.Value.AsIntegerOrNull();
                    if (scheduleType == ScheduleType.Custom || scheduleType == ScheduleType.Weekly)
                    {
                        if (!oldScheduleId.HasValue || group.Schedule == null)
                        {
                            group.Schedule = new Schedule();
                        }

                        if (scheduleType == ScheduleType.Custom)
                        {
                            group.Schedule.iCalendarContent = iCalendarContent;
                            group.Schedule.WeeklyDayOfWeek  = null;
                            group.Schedule.WeeklyTimeOfDay  = null;
                        }
                        else
                        {
                            group.Schedule.iCalendarContent = null;
                            group.Schedule.WeeklyDayOfWeek  = dowWeekly.SelectedDayOfWeek;
                            group.Schedule.WeeklyTimeOfDay  = timeWeekly.SelectedTime;
                        }
                    }
                    else
                    {
                        // If group did have a unique schedule, delete that schedule
                        if (oldScheduleId.HasValue)
                        {
                            var schedule = scheduleService.Get(oldScheduleId.Value);
                            if (schedule != null && string.IsNullOrEmpty(schedule.Name))
                            {
                                scheduleService.Delete(schedule);
                            }
                        }

                        if (scheduleType == ScheduleType.Named)
                        {
                            group.ScheduleId = spSchedule.SelectedValueAsId();
                        }
                        else
                        {
                            group.ScheduleId = null;
                        }
                    }

                    group.LoadAttributes();
                    Rock.Attribute.Helper.GetEditValues(phGroupAttributes, group);

                    group.GroupType = new GroupTypeService(rockContext).Get(group.GroupTypeId);
                    if (group.ParentGroupId.HasValue)
                    {
                        group.ParentGroup = groupService.Get(group.ParentGroupId.Value);
                    }

                    if (!Page.IsValid)
                    {
                        return;
                    }

                    // if the groupMember IsValid is false, and the UI controls didn't report any errors, it is probably because the custom rules of GroupMember didn't pass.
                    // So, make sure a message is displayed in the validation summary
                    cvGroup.IsValid = group.IsValid;

                    if (!cvGroup.IsValid)
                    {
                        cvGroup.ErrorMessage = group.ValidationResults.Select(a => a.ErrorMessage).ToList().AsDelimited("<br />");
                        return;
                    }

                    // use WrapTransaction since SaveAttributeValues does it's own RockContext.SaveChanges()
                    rockContext.WrapTransaction(() =>
                    {
                        var adding = group.Id.Equals(0);
                        if (adding)
                        {
                            groupService.Add(group);
                        }

                        rockContext.SaveChanges();

                        if (adding)
                        {
                            // add ADMINISTRATE to the person who added the group
                            Rock.Security.Authorization.AllowPerson(group, Authorization.ADMINISTRATE, this.CurrentPerson, rockContext);
                        }

                        group.SaveAttributeValues(rockContext);
                    });

                    bool isNowSecurityRole = group.IsActive && (group.GroupTypeId == roleGroupTypeId);


                    if (isNowSecurityRole)
                    {
                        // new security role, flush
                        Rock.Security.Authorization.Flush();
                    }

                    AttributeCache.FlushEntityAttributes();

                    pnlDetails.Visible = false;
                    pnlSuccess.Visible = true;
                    nbSuccess.Text     = string.Format("Your group ({0}) has been created", group.Name);
                    string linkedPage            = LinkedPageRoute("RedirectPage");
                    int    secondsBeforeRedirect = GetAttributeValue("SecondsBeforeRedirect").AsInteger() * 1000;
                    ScriptManager.RegisterClientScriptBlock(upnlGroupDetail, typeof(UpdatePanel), "Redirect", string.Format("console.log('{0}'); setTimeout(function() {{ window.location.replace('{0}?GroupId={2}') }}, {1});", linkedPage, secondsBeforeRedirect, group.Id), true);
                }
            }
        }