/// <summary> /// Handles the Delete event of the gReport 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 gReport_Delete(object sender, RowEventArgs e) { int id = int.MinValue; if (TagId.HasValue && int.TryParse(e.RowKeyValue.ToString(), out id)) { object obj = InvokeServiceMethod("Get", new Type[] { typeof(int) }, new object[] { id }); if (obj != null) { Rock.Data.IEntity entity = obj as Rock.Data.IEntity; if (entity != null) { var rockContext = new RockContext(); var service = new TaggedItemService(rockContext); var taggedItem = service.Get(TagId.Value, entity.Guid); if (taggedItem != null) { string errorMessage; if (!service.CanDelete(taggedItem, out errorMessage)) { mdGridWarning.Show(errorMessage, ModalAlertType.Information); return; } service.Delete(taggedItem); rockContext.SaveChanges(); } } } } BindGrid(); }
/// <summary> /// Handles the Delete event of the gReport 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 gReport_Delete(object sender, RowEventArgs e) { using (var rockContext = new RockContext()) { var taggedItemService = new TaggedItemService(rockContext); var taggedItem = taggedItemService.Get(e.RowKeyId); if (taggedItem != null && taggedItem.IsAuthorized(Rock.Security.Authorization.TAG, CurrentPerson)) { string errorMessage; if (!taggedItemService.CanDelete(taggedItem, out errorMessage)) { mdGridWarning.Show(errorMessage, ModalAlertType.Information); return; } taggedItemService.Delete(taggedItem); rockContext.SaveChanges(); } BindGrid(); } }
public void Delete(int entityTypeId, int ownerId, Guid entityGuid, string name, string entityQualifier, string entityQualifierValue) { var user = CurrentUser(); if (user != null) { using (new Rock.Data.UnitOfWorkScope()) { var tagService = new TagService(); var taggedItemService = new TaggedItemService(); if (name.Contains('^')) { name = name.Split('^')[0]; } var tag = tagService.Get(entityTypeId, entityQualifier, entityQualifierValue, ownerId, name); if (tag == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } var taggedItem = taggedItemService.Get(tag.Id, entityGuid); if (taggedItem == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } taggedItemService.Delete(taggedItem, user.PersonId); taggedItemService.Save(taggedItem, user.PersonId); } } else { throw new HttpResponseException(HttpStatusCode.Unauthorized); } }
/// <summary> /// Handles the Delete event of the gReport 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 gReport_Delete(object sender, RowEventArgs e) { Guid guid = Guid.Empty; if (TagId.HasValue && Guid.TryParse(e.RowKeyValue.ToString(), out guid)) { var service = new TaggedItemService(); var taggedItem = service.Get(TagId.Value, guid); if (taggedItem != null) { string errorMessage; if (!service.CanDelete(taggedItem, out errorMessage)) { mdGridWarning.Show(errorMessage, ModalAlertType.Information); return; } service.Delete(taggedItem, CurrentPersonId); service.Save(taggedItem, CurrentPersonId); } } BindGrid(); }
/// <summary> /// Saves the tag values that user entered for the entity /// </summary> /// <param name="personAlias">The person alias.</param> public void SaveTagValues(PersonAlias personAlias) { int?currentPersonId = null; if (personAlias != null) { currentPersonId = personAlias.PersonId; } if (EntityGuid != Guid.Empty) { var rockContext = new RockContext(); var tagService = new TagService(rockContext); var taggedItemService = new TaggedItemService(rockContext); var person = currentPersonId.HasValue ? new PersonService(rockContext).Get(currentPersonId.Value) : null; // Get the existing tagged items for this entity var existingTaggedItems = new List <TaggedItem>(); foreach (var taggedItem in taggedItemService.Get(EntityTypeId, EntityQualifierColumn, EntityQualifierValue, currentPersonId, EntityGuid, CategoryGuid, ShowInactiveTags)) { if (taggedItem.IsAuthorized(Authorization.VIEW, person)) { existingTaggedItems.Add(taggedItem); } } // Get tag values after user edit var currentTags = new List <Tag>(); foreach (var serializedTag in Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { var tagName = GetNameFromSerializedTag(serializedTag); if (tagName.IsNullOrWhiteSpace()) { continue; } // Only if this is a new tag, create it var tag = tagService.Get(EntityTypeId, EntityQualifierColumn, EntityQualifierValue, currentPersonId, tagName, CategoryGuid, ShowInactiveTags); if (personAlias != null && tag == null) { tag = new Tag(); tag.EntityTypeId = EntityTypeId; tag.CategoryId = CategoryId; tag.EntityTypeQualifierColumn = EntityQualifierColumn; tag.EntityTypeQualifierValue = EntityQualifierValue; tag.OwnerPersonAliasId = personAlias.Id; tag.Name = tagName; tagService.Add(tag); } if (tag != null) { currentTags.Add(tag); } } rockContext.SaveChanges(); var currentNames = currentTags.Select(t => t.Name).ToList(); var existingNames = existingTaggedItems.Select(t => t.Tag.Name).ToList(); // Delete any tagged items that user removed foreach (var taggedItem in existingTaggedItems) { if (!currentNames.Contains(taggedItem.Tag.Name, StringComparer.OrdinalIgnoreCase) && taggedItem.IsAuthorized(Rock.Security.Authorization.TAG, person)) { existingNames.Remove(taggedItem.Tag.Name); taggedItemService.Delete(taggedItem); } } rockContext.SaveChanges(); // Add any tagged items that user added foreach (var tag in currentTags) { // If the tagged item was not already there, and (it's their personal tag OR they are authorized to use it) then add it. if (!existingNames.Contains(tag.Name, StringComparer.OrdinalIgnoreCase) && ( (tag.OwnerPersonAliasId != null && tag.OwnerPersonAliasId == personAlias?.Id) || tag.IsAuthorized(Rock.Security.Authorization.TAG, person) ) ) { var taggedItem = new TaggedItem(); taggedItem.TagId = tag.Id; taggedItem.EntityTypeId = this.EntityTypeId; taggedItem.EntityGuid = EntityGuid; taggedItemService.Add(taggedItem); } } rockContext.SaveChanges(); } }
/// <summary> /// Saves the tag values that user entered for the entity ( /// </summary> /// <param name="personAlias">The person alias.</param> public void SaveTagValues(PersonAlias personAlias) { int?currentPersonId = null; if (personAlias != null) { currentPersonId = personAlias.PersonId; } if (EntityGuid != Guid.Empty) { var rockContext = new RockContext(); var tagService = new TagService(rockContext); var taggedItemService = new TaggedItemService(rockContext); var person = currentPersonId.HasValue ? new PersonService(rockContext).Get(currentPersonId.Value) : null; // Get the existing tagged items for this entity var existingTaggedItems = new List <TaggedItem>(); foreach (var taggedItem in taggedItemService.Get(EntityTypeId, EntityQualifierColumn, EntityQualifierValue, currentPersonId, EntityGuid, CategoryGuid, ShowInActiveTags)) { if (taggedItem.IsAuthorized(Authorization.VIEW, person)) { existingTaggedItems.Add(taggedItem); } } // Get tag values after user edit var currentTags = new List <Tag>(); foreach (var value in this.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { string tagName = value; if (tagName.Contains('^')) { tagName = tagName.Split(new char[] { '^' }, StringSplitOptions.RemoveEmptyEntries)[0]; } // If this is a new tag, create it Tag tag = tagService.Get(EntityTypeId, EntityQualifierColumn, EntityQualifierValue, currentPersonId, tagName, CategoryGuid, ShowInActiveTags); if ((tag == null || !tag.IsAuthorized("Tag", person)) && personAlias != null) { tag = new Tag(); tag.EntityTypeId = EntityTypeId; tag.CategoryId = CategoryId; tag.EntityTypeQualifierColumn = EntityQualifierColumn; tag.EntityTypeQualifierValue = EntityQualifierValue; tag.OwnerPersonAliasId = personAlias.Id; tag.Name = tagName; tagService.Add(tag); } if (tag != null) { currentTags.Add(tag); } } rockContext.SaveChanges(); var currentNames = currentTags.Select(t => t.Name).ToList(); var existingNames = existingTaggedItems.Select(t => t.Tag.Name).ToList(); // Delete any tagged items that user removed foreach (var taggedItem in existingTaggedItems) { if (!currentNames.Contains(taggedItem.Tag.Name, StringComparer.OrdinalIgnoreCase) && taggedItem.IsAuthorized("Tag", person)) { existingNames.Remove(taggedItem.Tag.Name); taggedItemService.Delete(taggedItem); } } rockContext.SaveChanges(); // Add any tagged items that user added foreach (var tag in currentTags) { if (tag.IsAuthorized("Tag", person) && !existingNames.Contains(tag.Name, StringComparer.OrdinalIgnoreCase)) { var taggedItem = new TaggedItem(); taggedItem.TagId = tag.Id; taggedItem.EntityTypeId = this.EntityTypeId; taggedItem.EntityGuid = EntityGuid; taggedItemService.Add(taggedItem); } } rockContext.SaveChanges(); } }
/// <summary> /// Saves the tag values that user entered for the entity ( /// </summary> /// <param name="personAlias">The person alias.</param> public void SaveTagValues(PersonAlias personAlias) { int?currentPersonId = null; if (personAlias != null) { currentPersonId = personAlias.PersonId; } if (EntityGuid != Guid.Empty) { var rockContext = new RockContext(); var tagService = new TagService(rockContext); var taggedItemService = new TaggedItemService(rockContext); // Get the existing tags for this entity type var existingTags = tagService.Get(EntityTypeId, EntityQualifierColumn, EntityQualifierValue, currentPersonId).ToList(); // Get the existing tagged items for this entity var existingTaggedItems = taggedItemService.Get(EntityTypeId, EntityQualifierColumn, EntityQualifierValue, currentPersonId, EntityGuid); // Get tag values after user edit var currentTags = new List <Tag>(); foreach (var value in this.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { string tagName = value; if (tagName.Contains('^')) { tagName = tagName.Split(new char[] { '^' }, StringSplitOptions.RemoveEmptyEntries)[0]; } // If this is a new tag, create it Tag tag = existingTags.FirstOrDefault(t => t.Name.Equals(tagName, StringComparison.OrdinalIgnoreCase)); if (tag == null && currentPersonId != null) { tag = new Tag(); tag.EntityTypeId = EntityTypeId; tag.EntityTypeQualifierColumn = EntityQualifierColumn; tag.EntityTypeQualifierValue = EntityQualifierValue; tag.OwnerPersonAliasId = personAlias != null ? personAlias.Id : (int?)null; tag.Name = tagName; } if (tag != null) { currentTags.Add(tag); } } rockContext.SaveChanges(); // Delete any tagged items that user removed var names = currentTags.Select(t => t.Name).ToList(); foreach (var taggedItem in existingTaggedItems) { if (!names.Contains(taggedItem.Tag.Name, StringComparer.OrdinalIgnoreCase)) { taggedItemService.Delete(taggedItem); } } rockContext.SaveChanges(); // Add any tagged items that user added names = existingTaggedItems.Select(t => t.Tag.Name).ToList(); foreach (var tag in currentTags) { if (!names.Contains(tag.Name, StringComparer.OrdinalIgnoreCase)) { var taggedItem = new TaggedItem(); taggedItem.TagId = tag.Id; taggedItem.EntityGuid = EntityGuid; taggedItemService.Add(taggedItem); } } rockContext.SaveChanges(); } }
/// <summary> /// Executes the specified workflow. /// </summary> /// <param name="rockContext">The rock context.</param> /// <param name="action">The action.</param> /// <param name="entity">The entity.</param> /// <param name="errorMessages">The error messages.</param> /// <returns></returns> public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List <string> errorMessages) { errorMessages = new List <string>(); // get the tag string tagName = GetAttributeValue(action, "OrganizationTag").ResolveMergeFields(GetMergeFields(action));; if (!string.IsNullOrEmpty(tagName)) { // get person entity type var personEntityType = Rock.Web.Cache.EntityTypeCache.Read("Rock.Model.Person"); // get tag TagService tagService = new TagService(rockContext); Tag orgTag = tagService.Queryable().Where(t => t.Name == tagName && t.EntityTypeId == personEntityType.Id && t.OwnerPersonAlias == null).FirstOrDefault(); if (orgTag != null) { // get person string value = GetAttributeValue(action, "Person"); Guid guidPersonAttribute = value.AsGuid(); if (!guidPersonAttribute.IsEmpty()) { var attributePerson = AttributeCache.Read(guidPersonAttribute, rockContext); if (attributePerson != null) { string attributePersonValue = action.GetWorklowAttributeValue(guidPersonAttribute); if (!string.IsNullOrWhiteSpace(attributePersonValue)) { if (attributePerson.FieldType.Class == "Rock.Field.Types.PersonFieldType") { Guid personAliasGuid = attributePersonValue.AsGuid(); if (!personAliasGuid.IsEmpty()) { var person = new PersonAliasService(rockContext).Queryable() .Where(a => a.Guid.Equals(personAliasGuid)) .Select(a => a.Person) .FirstOrDefault(); if (person != null) { var personAliasGuids = person.Aliases.Select(a => a.AliasPersonGuid).ToList(); TaggedItemService tiService = new TaggedItemService(rockContext); TaggedItem personTag = tiService.Queryable().Where(t => t.TagId == orgTag.Id && personAliasGuids.Contains(t.EntityGuid)).FirstOrDefault(); if (personTag != null) { tiService.Delete(personTag); rockContext.SaveChanges(); } else { action.AddLogEntry(string.Format("{0} was not in the {1} tag.", person.FullName, orgTag.Name)); } } else { errorMessages.Add(string.Format("Person could not be found for selected value ('{0}')!", guidPersonAttribute.ToString())); } } } } } } } else { action.AddLogEntry(string.Format("{0} organization tag does not exist.", orgTag.Name)); } } else { errorMessages.Add("No organization tag was provided"); } errorMessages.ForEach(m => action.AddLogEntry(m, true)); return(true); }