コード例 #1
0
        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();
        }
コード例 #2
0
        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();
        }