/// <summary> /// Handles the AddLocationClick event of the groupEditor 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 groupEditor_AddLocationClick(object sender, EventArgs e) { CheckinGroupEditor checkinGroupEditor = sender as CheckinGroupEditor; // set a hidden field value for the Group Guid so we know which Group to add the location to hfAddLocationGroupGuid.Value = checkinGroupEditor.GroupGuid.ToString(); var locationService = new LocationService(); ddlLocation.Items.Clear(); var list = locationService.Queryable().Where(a => a.IsLocation).OrderBy(a => a.Name); foreach (var item in list) { // add locations to dropdownlist if they aren't already a location for this group if (!checkinGroupEditor.Locations.Select(a => a.LocationId).Contains(item.Id)) { ddlLocation.Items.Add(new ListItem(item.Name, item.Id.ToString())); } } // only enable the Add button if there are labels that can be added btnAddLocation.Enabled = ddlLocation.Items.Count > 0; pnlLocationPicker.Visible = true; pnlDetails.Visible = false; }
/// <summary> /// Handles the Click event of the btnCancelAddLocation 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 btnCancelAddLocation_Click(object sender, EventArgs e) { CheckinGroupEditor checkinGroupEditor = phCheckinGroupTypes.ControlsOfTypeRecursive <CheckinGroupEditor>().FirstOrDefault(a => a.GroupGuid == new Guid(hfAddLocationGroupGuid.Value)); checkinGroupEditor.ForceContentVisible = true; (checkinGroupEditor.Parent as CheckinGroupTypeEditor).ForceContentVisible = true; pnlLocationPicker.Visible = false; pnlDetails.Visible = true; }
/// <summary> /// Handles the DeleteLocationClick event of the groupEditor 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 groupEditor_DeleteLocationClick(object sender, RowEventArgs e) { CheckinGroupEditor checkinGroupEditor = sender as CheckinGroupEditor; checkinGroupEditor.ForceContentVisible = true; (checkinGroupEditor.Parent as CheckinGroupTypeEditor).ForceContentVisible = true; var location = checkinGroupEditor.Locations.FirstOrDefault(a => a.LocationId == e.RowKeyId); checkinGroupEditor.Locations.Remove(location); }
/// <summary> /// Handles the AddLocationClick event of the groupEditor 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 groupEditor_AddLocationClick(object sender, EventArgs e) { CheckinGroupEditor checkinGroupEditor = sender as CheckinGroupEditor; // set a hidden field value for the Group Guid so we know which Group to add the location to hfAddLocationGroupGuid.Value = checkinGroupEditor.GroupGuid.ToString(); checkinGroupEditor.ForceContentVisible = true; (checkinGroupEditor.Parent as CheckinGroupTypeEditor).ForceContentVisible = true; mdLocationPicker.Show(); }
/// <summary> /// Handles the Click event of the btnAddLocation 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 btnAddLocation_Click(object sender, EventArgs e) { CheckinGroupEditor checkinGroupEditor = phCheckinGroupTypes.ControlsOfTypeRecursive <CheckinGroupEditor>().FirstOrDefault(a => a.GroupGuid == new Guid(hfAddLocationGroupGuid.Value)); checkinGroupEditor.ForceContentVisible = true; (checkinGroupEditor.Parent as CheckinGroupTypeEditor).ForceContentVisible = true; CheckinGroupEditor.LocationGridItem gridItem = new CheckinGroupEditor.LocationGridItem(); gridItem.LocationId = ddlLocation.SelectedValueAsId() ?? 0; gridItem.Name = ddlLocation.SelectedItem.Text; checkinGroupEditor.Locations.Add(gridItem); pnlLocationPicker.Visible = false; pnlDetails.Visible = true; }
/// <summary> /// Handles the DeleteGroupClick event of the groupEditor 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 groupEditor_DeleteGroupClick(object sender, EventArgs e) { CheckinGroupEditor groupEditor = sender as CheckinGroupEditor; GroupService groupService = new GroupService(new RockContext()); Group groupDB = groupService.Get(groupEditor.GroupGuid); if (groupDB != null) { string errorMessage; if (!groupService.CanDelete(groupDB, out errorMessage)) { nbDeleteWarning.Text = "WARNING - Cannot Delete: " + errorMessage; nbDeleteWarning.Visible = true; return; } } groupEditor.Parent.Controls.Remove(groupEditor); }
/// <summary> /// Handles the Click event of the btnAddLocation 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 btnAddLocation_Click(object sender, EventArgs e) { CheckinGroupEditor checkinGroupEditor = phCheckinGroupTypes.ControlsOfTypeRecursive <CheckinGroupEditor>().FirstOrDefault(a => a.GroupGuid == new Guid(hfAddLocationGroupGuid.Value)); // Add the location (ignore if they didn't pick one, or they picked one that already is selected) if (locationPicker.Location != null) { if (!checkinGroupEditor.Locations.Any(a => a.LocationId == locationPicker.Location.Id)) { CheckinGroupEditor.LocationGridItem gridItem = new CheckinGroupEditor.LocationGridItem(); gridItem.LocationId = locationPicker.Location.Id; gridItem.Name = locationPicker.Location.Name; checkinGroupEditor.Locations.Add(gridItem); } } checkinGroupEditor.ForceContentVisible = true; (checkinGroupEditor.Parent as CheckinGroupTypeEditor).ForceContentVisible = true; mdLocationPicker.Hide(); }
/// <summary> /// Creates the group editor controls. /// </summary> /// <param name="group">The group.</param> /// <param name="parentControl">The parent control.</param> /// <param name="forceContentVisible">if set to <c>true</c> [force content visible].</param> private void CreateGroupEditorControls(Group group, Control parentControl, bool forceContentVisible = false) { CheckinGroupEditor groupEditor = new CheckinGroupEditor(); groupEditor.ID = "GroupEditor_" + group.Guid.ToString("N"); groupEditor.SetGroup(group); groupEditor.Locations = group.GroupLocations .Select(a => new CheckinGroupEditor.LocationGridItem() { LocationId = a.LocationId, Name = a.Location.ToString() }) .OrderBy(o => o.Name) .ToList(); groupEditor.AddLocationClick += groupEditor_AddLocationClick; groupEditor.DeleteLocationClick += groupEditor_DeleteLocationClick; groupEditor.DeleteGroupClick += groupEditor_DeleteGroupClick; parentControl.Controls.Add(groupEditor); }
/// <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 hasValidationErrors = false; var rockContext = new RockContext(); GroupTypeService groupTypeService = new GroupTypeService(rockContext); GroupService groupService = new GroupService(rockContext); AttributeService attributeService = new AttributeService(rockContext); GroupLocationService groupLocationService = new GroupLocationService(rockContext); int parentGroupTypeId = hfParentGroupTypeId.ValueAsInt(); var groupTypeUIList = new List <GroupType>(); foreach (var checkinGroupTypeEditor in phCheckinGroupTypes.Controls.OfType <CheckinGroupTypeEditor>().ToList()) { var groupType = checkinGroupTypeEditor.GetCheckinGroupType(); groupTypeUIList.Add(groupType); } var groupTypeDBList = new List <GroupType>(); var groupTypesToDelete = new List <GroupType>(); var groupsToDelete = new List <Group>(); var groupTypesToAddUpdate = new List <GroupType>(); var groupsToAddUpdate = new List <Group>(); GroupType parentGroupTypeDB = groupTypeService.Get(parentGroupTypeId); GroupType parentGroupTypeUI = parentGroupTypeDB.Clone(false); parentGroupTypeUI.ChildGroupTypes = groupTypeUIList; PopulateDeleteLists(groupTypesToDelete, groupsToDelete, parentGroupTypeDB, parentGroupTypeUI); PopulateAddUpdateLists(groupTypesToAddUpdate, groupsToAddUpdate, parentGroupTypeUI); int binaryFileFieldTypeID = FieldTypeCache.Read(Rock.SystemGuid.FieldType.BINARY_FILE.AsGuid()).Id; int binaryFileTypeId = new BinaryFileTypeService(rockContext).Get(new Guid(Rock.SystemGuid.BinaryFiletype.CHECKIN_LABEL)).Id; RockTransactionScope.WrapTransaction(() => { // delete in reverse order to get deepest child items first groupsToDelete.Reverse(); foreach (var groupToDelete in groupsToDelete) { groupService.Delete(groupToDelete); } // delete in reverse order to get deepest child items first groupTypesToDelete.Reverse(); foreach (var groupTypeToDelete in groupTypesToDelete) { groupTypeService.Delete(groupTypeToDelete); } rockContext.SaveChanges(); // Add/Update grouptypes and groups that are in the UI // Note: We'll have to save all the groupTypes without changing the DB value of ChildGroupTypes, then come around again and save the ChildGroupTypes // since the ChildGroupTypes may not exist in the database yet foreach (GroupType groupTypeUI in groupTypesToAddUpdate) { GroupType groupTypeDB = groupTypeService.Get(groupTypeUI.Guid); if (groupTypeDB == null) { groupTypeDB = new GroupType(); groupTypeDB.Id = 0; groupTypeDB.Guid = groupTypeUI.Guid; } groupTypeDB.Name = groupTypeUI.Name; groupTypeDB.Order = groupTypeUI.Order; groupTypeDB.InheritedGroupTypeId = groupTypeUI.InheritedGroupTypeId; groupTypeDB.Attributes = groupTypeUI.Attributes; groupTypeDB.AttributeValues = groupTypeUI.AttributeValues; if (groupTypeDB.Id == 0) { groupTypeService.Add(groupTypeDB); } if (!groupTypeDB.IsValid) { hasValidationErrors = true; CheckinGroupTypeEditor groupTypeEditor = phCheckinGroupTypes.ControlsOfTypeRecursive <CheckinGroupTypeEditor>().First(a => a.GroupTypeGuid == groupTypeDB.Guid); groupTypeEditor.ForceContentVisible = true; return; } rockContext.SaveChanges(); groupTypeDB.SaveAttributeValues(); // get fresh from database to make sure we have Id so we can update the CheckinLabel Attributes groupTypeDB = groupTypeService.Get(groupTypeDB.Guid); // rebuild the CheckinLabel attributes from the UI (brute-force) foreach (var labelAttributeDB in CheckinGroupTypeEditor.GetCheckinLabelAttributes(groupTypeDB)) { var attribute = attributeService.Get(labelAttributeDB.Value.Guid); Rock.Web.Cache.AttributeCache.Flush(attribute.Id); attributeService.Delete(attribute); } rockContext.SaveChanges(); foreach (var checkinLabelAttributeInfo in GroupTypeCheckinLabelAttributesState[groupTypeUI.Guid]) { var attribute = new Rock.Model.Attribute(); attribute.AttributeQualifiers.Add(new AttributeQualifier { Key = "binaryFileType", Value = binaryFileTypeId.ToString() }); attribute.Guid = Guid.NewGuid(); attribute.FieldTypeId = binaryFileFieldTypeID; attribute.EntityTypeId = EntityTypeCache.GetId(typeof(GroupType)); attribute.EntityTypeQualifierColumn = "Id"; attribute.EntityTypeQualifierValue = groupTypeDB.Id.ToString(); attribute.DefaultValue = checkinLabelAttributeInfo.BinaryFileId.ToString(); attribute.Key = checkinLabelAttributeInfo.AttributeKey; attribute.Name = checkinLabelAttributeInfo.FileName; if (!attribute.IsValid) { hasValidationErrors = true; CheckinGroupTypeEditor groupTypeEditor = phCheckinGroupTypes.ControlsOfTypeRecursive <CheckinGroupTypeEditor>().First(a => a.GroupTypeGuid == groupTypeDB.Guid); groupTypeEditor.ForceContentVisible = true; return; } attributeService.Add(attribute); } rockContext.SaveChanges(); } // Add/Update Groups foreach (var groupUI in groupsToAddUpdate) { Group groupDB = groupService.Get(groupUI.Guid); if (groupDB == null) { groupDB = new Group(); groupDB.Guid = groupUI.Guid; } groupDB.Name = groupUI.Name; // delete any GroupLocations that were removed in the UI foreach (var groupLocationDB in groupDB.GroupLocations.ToList()) { if (!groupUI.GroupLocations.Select(a => a.LocationId).Contains(groupLocationDB.LocationId)) { groupLocationService.Delete(groupLocationDB); } } // add any GroupLocations that were added in the UI foreach (var groupLocationUI in groupUI.GroupLocations) { if (!groupDB.GroupLocations.Select(a => a.LocationId).Contains(groupLocationUI.LocationId)) { GroupLocation groupLocationDB = new GroupLocation { LocationId = groupLocationUI.LocationId }; groupDB.GroupLocations.Add(groupLocationDB); } } groupDB.Order = groupUI.Order; // get GroupTypeId from database in case the groupType is new groupDB.GroupTypeId = groupTypeService.Get(groupUI.GroupType.Guid).Id; groupDB.Attributes = groupUI.Attributes; groupDB.AttributeValues = groupUI.AttributeValues; if (groupDB.Id == 0) { groupService.Add(groupDB); } if (!groupDB.IsValid) { hasValidationErrors = true; hasValidationErrors = true; CheckinGroupEditor groupEditor = phCheckinGroupTypes.ControlsOfTypeRecursive <CheckinGroupEditor>().First(a => a.GroupGuid == groupDB.Guid); groupEditor.ForceContentVisible = true; return; } rockContext.SaveChanges(); groupDB.SaveAttributeValues(); } /* now that we have all the grouptypes saved, now lets go back and save them again with the current UI ChildGroupTypes */ // save main parentGroupType with current UI ChildGroupTypes parentGroupTypeDB.ChildGroupTypes = new List <GroupType>(); parentGroupTypeDB.ChildGroupTypes.Clear(); foreach (var childGroupTypeUI in parentGroupTypeUI.ChildGroupTypes) { var childGroupTypeDB = groupTypeService.Get(childGroupTypeUI.Guid); parentGroupTypeDB.ChildGroupTypes.Add(childGroupTypeDB); } rockContext.SaveChanges(); // loop thru all the other GroupTypes in the UI and save their childgrouptypes foreach (var groupTypeUI in groupTypesToAddUpdate) { var groupTypeDB = groupTypeService.Get(groupTypeUI.Guid); groupTypeDB.ChildGroupTypes = new List <GroupType>(); groupTypeDB.ChildGroupTypes.Clear(); foreach (var childGroupTypeUI in groupTypeUI.ChildGroupTypes) { var childGroupTypeDB = groupTypeService.Get(childGroupTypeUI.Guid); groupTypeDB.ChildGroupTypes.Add(childGroupTypeDB); } } rockContext.SaveChanges(); }); if (!hasValidationErrors) { NavigateToParentPage(); } }
/// <summary> /// Sorts the group type list contents. /// </summary> /// <param name="eventParam">The event parameter.</param> /// <param name="values">The values.</param> private void SortGroupTypeListContents(string eventParam, string[] values) { if (eventParam.Equals("re-order-grouptype")) { var allCheckinGroupTypeEditors = phCheckinGroupTypes.ControlsOfTypeRecursive <CheckinGroupTypeEditor>(); Guid groupTypeGuid = new Guid(values[0]); int newIndex = int.Parse(values[1]); CheckinGroupTypeEditor sortedGroupTypeEditor = allCheckinGroupTypeEditors.FirstOrDefault(a => a.GroupTypeGuid.Equals(groupTypeGuid)); if (sortedGroupTypeEditor != null) { var siblingGroupTypes = allCheckinGroupTypeEditors.Where(a => a.ParentGroupTypeEditor == sortedGroupTypeEditor.ParentGroupTypeEditor).ToList(); Control parentControl = sortedGroupTypeEditor.Parent; parentControl.Controls.Remove(sortedGroupTypeEditor); if (newIndex >= siblingGroupTypes.Count()) { parentControl.Controls.Add(sortedGroupTypeEditor); } else { parentControl.Controls.AddAt(newIndex, sortedGroupTypeEditor); } } } else if (eventParam.Equals("re-order-group")) { var allCheckinGroupEditors = phCheckinGroupTypes.ControlsOfTypeRecursive <CheckinGroupEditor>(); Guid groupGuid = new Guid(values[0]); int newIndex = int.Parse(values[1]); CheckinGroupEditor sortedGroupEditor = allCheckinGroupEditors.FirstOrDefault(a => a.GroupGuid.Equals(groupGuid)); if (sortedGroupEditor != null) { var siblingGroupEditors = allCheckinGroupEditors.Where(a => a.GroupTypeId == sortedGroupEditor.GroupTypeId).ToList(); Control parentControl = sortedGroupEditor.Parent; // parent control has other controls, so just just remove all the checkingroupeditors, sort them, and add them back in the new order foreach (var item in siblingGroupEditors) { parentControl.Controls.Remove(item); } siblingGroupEditors.Remove(sortedGroupEditor); if (newIndex >= siblingGroupEditors.Count()) { siblingGroupEditors.Add(sortedGroupEditor); } else { siblingGroupEditors.Insert(newIndex, sortedGroupEditor); } foreach (var item in siblingGroupEditors) { parentControl.Controls.Add(item); } (sortedGroupEditor.Parent as CheckinGroupTypeEditor).ForceContentVisible = true; } } }