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);
        }
Exemple #2
0
        /// <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);
            }
        }
Exemple #3
0
        /// <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();
            }
        }
Exemple #4
0
        /// <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();
            }
        }