/// <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)
{
var rockContext = new RockContext();
var tagService = new Rock.Model.TagService(rockContext);
Tag tag = null;
int tagId = int.Parse(hfId.Value);
if (tagId != 0)
{
tag = tagService.Get(tagId);
}
if (tag == null)
{
tag = new Tag();
tag.IsSystem = false;
tagService.Add(tag);
}
string name = tbName.Text;
int? ownerId = ppOwner.PersonId;
int entityTypeId = ddlEntityType.SelectedValueAsId().Value;
string qualifierCol = tbEntityTypeQualifierColumn.Text;
string qualifierVal = tbEntityTypeQualifierValue.Text;
// Verify tag with same name does not already exist
if (tagService.Queryable()
.Where(t =>
t.Id != tagId &&
t.Name == name &&
t.OwnerId.Equals(ownerId) &&
t.EntityTypeId == entityTypeId &&
t.EntityTypeQualifierColumn == qualifierCol &&
t.EntityTypeQualifierValue == qualifierVal)
.Any())
{
nbEditError.Heading = "Tag Already Exists";
nbEditError.Text = string.Format("A '{0}' tag already exists for the selected scope, owner, and entity type.", name);
nbEditError.Visible = true;
}
else
{
tag.Name = name;
tag.Description = tbDescription.Text;
tag.OwnerId = ownerId;
tag.EntityTypeId = entityTypeId;
tag.EntityTypeQualifierColumn = qualifierCol;
tag.EntityTypeQualifierValue = qualifierVal;
rockContext.SaveChanges();
var qryParams = new Dictionary <string, string>();
qryParams["tagId"] = tag.Id.ToString();
NavigateToPage(RockPage.Guid, qryParams);
}
}