/// <summary> /// Saves the tag values that user entered for the entity ( /// </summary> /// <param name="currentPersonId">The current person identifier.</param> public void SaveTagValues(int? currentPersonId) { if ( EntityGuid != Guid.Empty ) { var tagService = new TagService(); var taggedItemService = new TaggedItemService(); // 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.OwnerId = currentPersonId.Value; tag.Name = tagName; tagService.Add( tag, currentPersonId ); tagService.Save( tag, currentPersonId ); } if ( tag != null ) { currentTags.Add( tag ); } } // 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, currentPersonId ); taggedItemService.Save( taggedItem, currentPersonId ); } } // 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, currentPersonId ); taggedItemService.Save( taggedItem, currentPersonId ); } } } }
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 ); }
public HttpResponseMessage Post( 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(); var tag = tagService.Get( entityTypeId, entityQualifier, entityQualifierValue, ownerId, name ); if ( tag == null ) { tag = new Tag(); tag.EntityTypeId = entityTypeId; tag.EntityTypeQualifierColumn = entityQualifier; tag.EntityTypeQualifierValue = entityQualifierValue; tag.OwnerId = ownerId; tag.Name = name; tagService.Add( tag, user.PersonId ); tagService.Save( tag, user.PersonId ); } var taggedItem = taggedItemService.Get( tag.Id, entityGuid ); if ( taggedItem == null ) { taggedItem = new TaggedItem(); taggedItem.TagId = tag.Id; taggedItem.EntityGuid = entityGuid; taggedItemService.Add( taggedItem, user.PersonId ); taggedItemService.Save( taggedItem, user.PersonId ); } } return ControllerContext.Request.CreateResponse( HttpStatusCode.Created ); } 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(); }