private void SaveWidgityAttribute() { var attribute = new Rock.Model.Attribute(); edtWidgityAttributes.GetAttributeProperties(attribute); attribute.EntityTypeId = EntityTypeCache.Get(typeof(Widgity)).Id; attribute.EntityTypeQualifierColumn = "WidgityTypeId"; attribute.EntityTypeQualifierValue = PageParameter(PageParameterKey.WidgityTypeId); var existingAttribute = WidgityAttributes.Where(a => a.Guid == attribute.Guid).FirstOrDefault(); if (existingAttribute == null) //new attribute { attribute.Order = WidgityAttributes.Any() ? WidgityAttributes.Max(a => a.Order) + 1 : 0; WidgityAttributes.Add(attribute); } else //existing attribute { foreach (var att in WidgityAttributes) { if (att.Guid == existingAttribute.Guid) { var index = WidgityAttributes.IndexOf(att); WidgityAttributes.Remove(att); WidgityAttributes.Insert(index, attribute); break; } } } SaveState(); }
protected void gWdigityAttributes_Delete(object sender, Rock.Web.UI.Controls.RowEventArgs e) { var toRemove = WidgityAttributes.Where(a => a.Guid == ( Guid )e.RowKeyValue).FirstOrDefault(); if (toRemove != null) { WidgityAttributes.Remove(toRemove); SaveState(); } }
private void ShowWidgityAttributeEditor(Guid attributeGuid) { var attribute = WidgityAttributes.Where(a => a.Guid == attributeGuid).FirstOrDefault(); if (attribute == null) { attribute = new Rock.Model.Attribute { Guid = attributeGuid, FieldTypeId = 1, EntityTypeId = EntityTypeCache.Get(typeof(Widgity)).Id, EntityTypeQualifierColumn = "WidgityTypeId", EntityTypeQualifierValue = PageParameter(PageParameterKey.WidgityTypeId) }; } edtWidgityAttributes.SetAttributeProperties(attribute, typeof(Widgity)); dlgWidgityTypeAttributes.Show(); }
private void BindAttributeGrids(bool setValues = true) { if (setValues) { RockContext rockContext = new RockContext(); AttributeService attributeService = new AttributeService(rockContext); var widgityTypeId = PageParameter(PageParameterKey.WidgityTypeId); var widgityEntityTypeId = EntityTypeCache.Get(typeof(Widgity)).Id; WidgityAttributes = attributeService.Queryable() .Where(a => a.EntityTypeId == widgityEntityTypeId && a.EntityTypeQualifierValue == widgityTypeId) .OrderBy(a => a.Order) .ToList(); var widgityItemEntityTypeId = EntityTypeCache.Get(typeof(WidgityItem)).Id; WidgityItemAttributes = attributeService.Queryable() .Where(a => a.EntityTypeId == widgityItemEntityTypeId && a.EntityTypeQualifierValue == widgityTypeId) .OrderBy(a => a.Order) .ToList(); SaveState(); } gWidgityAttributes.DataSource = WidgityAttributes.Select(a => new { a.Guid, a.Name, a.Key, a.IsRequired, FieldType = FieldTypeCache.Get(a.FieldTypeId).Name }).ToList(); gWidgityAttributes.DataBind(); gWidgityItemAttributes.DataSource = WidgityItemAttributes.Select(a => new { a.Guid, a.Name, a.Key, a.IsRequired, FieldType = FieldTypeCache.Get(a.FieldTypeId).Name }).ToList(); gWidgityItemAttributes.DataBind(); }
protected void btnSave_Click(object sender, EventArgs e) { RockContext rockContext = new RockContext(); rockContext.WrapTransaction(() => { WidgityTypeService widgityTypeService = new WidgityTypeService(rockContext); AttributeService attributeService = new AttributeService(rockContext); WidgityType widgityType = GetWidgityType(widgityTypeService); if (widgityType.Id == 0) { widgityTypeService.Add(widgityType); } widgityType.Name = tbName.Text; widgityType.Description = tbDescription.Text; widgityType.EnabledLavaCommands = lcCommands.SelectedValue; widgityType.Icon = tbIcon.Text; widgityType.Markdown = ceMarkup.Text; widgityType.HasItems = cbHasItems.Checked; var _ = widgityType.EntityTypes.ToList(); //Attach the entity types to context widgityType.EntityTypes = new EntityTypeService(rockContext).GetByIds(lbEntityTypes.SelectedValuesAsInt).ToList(); widgityType.CategoryId = pCategory.SelectedValueAsId(); rockContext.SaveChanges(); //Widgity Attributes var widgityEntityTypeId = EntityTypeCache.Get(typeof(Widgity)).Id; var actualAttributes = attributeService.Queryable() .Where(a => a.EntityTypeId == widgityEntityTypeId && a.EntityTypeQualifierValue == widgityType.Id.ToString()) .OrderBy(a => a.Order) .ToList(); //Remove deleted attributes foreach (var attribute in actualAttributes) { if (!WidgityAttributes.Where(a => a.Guid == attribute.Guid).Any()) { attributeService.Delete(attribute); } } //Update db from viewstate foreach (var attribute in WidgityAttributes) { if (attribute.Id == 0) { attribute.Order = WidgityAttributes.IndexOf(attribute); attribute.EntityTypeQualifierValue = widgityType.Id.ToString(); attributeService.Add(attribute); } else { var trackedAttribute = actualAttributes.Where(a => a.Guid == attribute.Guid).FirstOrDefault(); trackedAttribute.CopyPropertiesFrom(attribute); foreach (var qualifier in trackedAttribute.AttributeQualifiers) { var value = attribute.AttributeQualifiers.Where(q => q.Key == qualifier.Key).FirstOrDefault(); if (value != null) { qualifier.Value = value.Value; } } trackedAttribute.Order = WidgityAttributes.IndexOf(attribute); } } rockContext.SaveChanges(); //Widgity Item Attributes var widgityItemEntityTypeId = EntityTypeCache.Get(typeof(WidgityItem)).Id; var actualItemAttributes = attributeService.Queryable() .Where(a => a.EntityTypeId == widgityItemEntityTypeId && a.EntityTypeQualifierValue == widgityType.Id.ToString()) .OrderBy(a => a.Order) .ToList(); //Remove deleted attributes foreach (var attribute in actualItemAttributes) { if (!WidgityItemAttributes.Where(a => a.Guid == attribute.Guid).Any()) { attributeService.Delete(attribute); } } //Update db from viewstate foreach (var attribute in WidgityItemAttributes) { if (attribute.Id == 0) { attribute.Order = WidgityItemAttributes.IndexOf(attribute); attribute.EntityTypeQualifierValue = widgityType.Id.ToString(); attributeService.Add(attribute); } else { var trackedAttribute = actualItemAttributes.Where(a => a.Guid == attribute.Guid).FirstOrDefault(); trackedAttribute.CopyPropertiesFrom(attribute); foreach (var qualifier in trackedAttribute.AttributeQualifiers) { var value = attribute.AttributeQualifiers.Where(q => q.Key == qualifier.Key).FirstOrDefault(); if (value != null) { qualifier.Value = value.Value; } } trackedAttribute.Order = WidgityItemAttributes.IndexOf(attribute); } } rockContext.SaveChanges(); }); WidgityTypeCache.Clear(); WidgityCache.Clear(); WidgityItemCache.Clear(); NavigateToParentPage(); }