/// <summary> /// Updates the control with the current tags that exist for the current entity /// </summary> /// <param name="currentPersonId">The current person identifier.</param> public void GetTagValues(int?currentPersonId) { var sb = new StringBuilder(); using (var rockContext = new RockContext()) { var service = new TaggedItemService(rockContext); foreach (dynamic item in service.Get( EntityTypeId, EntityQualifierColumn, EntityQualifierValue, currentPersonId, EntityGuid) .Select(i => new { OwnerId = (i.Tag.OwnerPersonAlias != null ? i.Tag.OwnerPersonAlias.PersonId : (int?)null), Name = i.Tag.Name })) { if (sb.Length > 0) { sb.Append(','); } sb.Append(item.Name); if (currentPersonId.HasValue && item.OwnerId == currentPersonId.Value) { sb.Append("^personal"); } } } this.Text = sb.ToString(); }
/// <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> /// Updates the control with the current tags that exist for the current entity /// </summary> /// <param name="currentPersonId">The current person identifier.</param> public void GetTagValues(int?currentPersonId) { var serializedTags = new List <string>(); using (var rockContext = new RockContext()) { var taggedItemService = new TaggedItemService(rockContext); var itemList = taggedItemService.Get(EntityTypeId, EntityQualifierColumn, EntityQualifierValue, currentPersonId, EntityGuid, CategoryGuid, ShowInactiveTags) .Where(ti => ShowInactiveTags || ti.Tag.IsActive) .Select(ti => ti.Tag) .Include(t => t.OwnerPersonAlias) .OrderBy(t => t.Name) .AsNoTracking() .ToList(); var person = GetCurrentPerson(); foreach (var item in itemList) { if (!item.IsAuthorized(Authorization.VIEW, person)) { continue; } var isPersonal = currentPersonId.HasValue && item.OwnerPersonAlias?.PersonId == currentPersonId.Value; var tagCssClass = isPersonal ? "personal" : string.Empty; var serializedTag = SerializeTag(item.Name, tagCssClass, item.IconCssClass, item.BackgroundColor); serializedTags.Add(serializedTag); } } Text = serializedTags.JoinStrings(","); }
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) { 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> /// Add the person to the tag. /// </summary> /// <returns>True if the person was added, false if they already existed in the tag.</returns> private bool AddPerson(int personId) { using (var rockContext = new RockContext()) { var taggedItemService = new TaggedItemService(rockContext); Tag tag = new TagService(rockContext).Get(TagId.Value); var person = new PersonService(rockContext).Get(personId); if (taggedItemService.Get(tag.Id, person.Guid) != null) { return(false); } var taggedItem = new TaggedItem(); taggedItem.TagId = TagId.Value; taggedItem.EntityTypeId = TagEntityType.Id; taggedItem.EntityGuid = person.Guid; taggedItemService.Add(taggedItem); rockContext.SaveChanges(); return(true); } }
/// <summary> /// Updates the control with the current tags that exist for the current entity /// </summary> /// <param name="currentPersonId">The current person identifier.</param> public void GetTagValues(int?currentPersonId) { var sb = new StringBuilder(); using (var rockContext = new RockContext()) { var service = new TaggedItemService(rockContext); var qry = service.Get( EntityTypeId, EntityQualifierColumn, EntityQualifierValue, currentPersonId, EntityGuid, CategoryGuid, ShowInActiveTags) .Where(c => c.Tag.IsActive || (ShowInActiveTags)); var items = qry .Select(a => a.Tag) .OrderBy(a => a.Name); var person = GetCurrentPerson(); foreach (var item in items) { if (item.IsAuthorized(Rock.Security.Authorization.VIEW, person)) { if (sb.Length > 0) { sb.Append(','); } sb.Append(item.Name); if (currentPersonId.HasValue && item?.OwnerPersonAlias?.PersonId == currentPersonId.Value) { sb.Append("^personal"); } } } } this.Text = sb.ToString(); }
/// <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(); } }
protected void Page_Load(object sender, EventArgs e) { string entityQualifierColumn = GetAttributeValue("EntityQualifierColumn"); if (string.IsNullOrWhiteSpace(entityQualifierColumn)) { entityQualifierColumn = PageParameter("EntityQualifierColumn"); } string entityQualifierValue = GetAttributeValue("EntityQualifierValue"); if (string.IsNullOrWhiteSpace(entityQualifierValue)) { entityQualifierValue = PageParameter("EntityQualifierValue"); } var sb = new StringBuilder(); // Get the context entity Rock.Data.IEntity contextEntity = this.ContextEntity(); if (contextEntity != null) { var service = new TaggedItemService(); foreach (dynamic item in service.Get( contextEntity.TypeId, entityQualifierColumn, entityQualifierValue, CurrentPersonId, contextEntity.Guid) .Select(i => new { OwnerId = i.Tag.OwnerId, Name = i.Tag.Name })) { if (sb.Length > 0) { sb.Append(','); } sb.Append(item.Name); if (CurrentPersonId.HasValue && item.OwnerId == CurrentPersonId.Value) { sb.Append("^personal"); } } phTags.Controls.Add(new LiteralControl(string.Format( "<input name=\"person-tags\" id=\"person-tags\" value=\"{0}\" />", sb.ToString()))); string script = string.Format(@" $(document).ready(function () {{ $('ul.ui-autocomplete').css('width', '300px'); $('#person-tags').tagsInput({{ 'autocomplete_url': function( request, response ) {{ $.ajax({{ url: Rock.settings.get('baseUrl') + 'api/tags/availablenames/{0}/{1}/{2}{3}{4}', dataType: 'json', success: function(data, status, xhr){{ response($.map(data, function (item) {{ return {{ value: item.Name, class: item.OwnerId == null || item.OwnerId == '' ? 'system' : 'personal' }} }})) }}, error: function(xhr, status, error) {{ alert('availablenames status: ' + status + ' [' + error + ']: ' + xhr.reponseText); }} }}); }}, autoCompleteAppendTo: 'div.tag-wrap', autoCompleteMessages: {{ noResults: function () {{ }}, results: function () {{ }} }}, 'height': 'auto', 'width': '100%', 'interactive': true, 'defaultText': 'add tag', 'removeWithBackspace': false, 'onAddTag': verifyTag, 'onRemoveTag': RemoveTag, 'enableDelete': true }}); }}); function verifyTag(tagName) {{ $.ajax({{ type: 'GET', url: Rock.settings.get('baseUrl') + 'api/tags/{0}/{1}/' + tagName + '{3}{4}', statusCode: {{ 404: function () {{ var r = confirm(""A tag called '"" + tagName + ""' does not exist. Do you want to create a new personal tag?""); if (r == true) {{ AddTag(tagName); }} else {{ // remove tag $('#person-tags').removeTag(tagName); }} }}, 200: function (data, status, xhr) {{ AddTag(tagName); }} }}, }}); }} function AddTag(tagName) {{ $.ajax({{ type: 'POST', url: Rock.settings.get('baseUrl') + 'api/taggeditems/{0}/{1}/{2}/' + tagName + '{3}{4}', error: function (xhr, status, error) {{ alert('AddTag() status: ' + status + ' [' + error + ']: ' + xhr.responseText); }} }}); }} function RemoveTag(tagName) {{ $.ajax({{ type: 'DELETE', url: Rock.settings.get('baseUrl') + 'api/taggeditems/{0}/{1}/{2}/' + tagName + '{3}{4}', error: function (xhr, status, error) {{ alert('RemoveTag() status: ' + status + ' [' + error + ']: ' + xhr.responseText); }} }}); }} ", contextEntity.TypeId, CurrentPersonId, contextEntity.Guid.ToString(), string.IsNullOrWhiteSpace(entityQualifierColumn) ? "" : "/" + entityQualifierColumn, string.IsNullOrWhiteSpace(entityQualifierValue) ? "" : "/" + entityQualifierValue); this.Page.ClientScript.RegisterStartupScript(this.GetType(), "tags-" + this.BlockId.ToString(), script, true); } }
/// <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(); } }