Пример #1
0
        /// <summary>
        /// Handles the Delete event of the gDefinedTypeAttributes 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 gDefinedTypeAttributes_Delete(object sender, RowEventArgs e)
        {
            Guid             attributeGuid    = (Guid)e.RowKeyValue;
            var              rockContext      = new RockContext();
            AttributeService attributeService = new AttributeService(rockContext);
            Attribute        attribute        = attributeService.Get(attributeGuid);

            if (attribute != null)
            {
                string errorMessage;
                if (!attributeService.CanDelete(attribute, out errorMessage))
                {
                    mdGridWarningAttributes.Show(errorMessage, ModalAlertType.Information);
                    return;
                }

                AttributeCache.Flush(attribute.Id);
                attributeService.Delete(attribute);
                rockContext.SaveChanges();
            }

            AttributeCache.FlushEntityAttributes();

            BindDefinedTypeAttributesGrid();
        }
        /// <summary>
        /// Handles the GridReorder event of the gBlockTypeAttributes control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="GridReorderEventArgs"/> instance containing the event data.</param>
        void gBlockTypeAttributes_GridReorder(object sender, GridReorderEventArgs e)
        {
            var    blockTypeStaticAttributeKeys = GetBlockTypeStaticAttributeKeys();
            string qualifierValue = hfBlockTypeId.Value;

            var rockContext      = new RockContext();
            var attributeService = new AttributeService(rockContext);

            int order      = 0;
            var attributes = attributeService
                             .GetByEntityTypeId(new Rock.Model.Block().TypeId).AsQueryable()
                             .Where(a =>
                                    a.EntityTypeQualifierColumn.Equals("BlockTypeId", StringComparison.OrdinalIgnoreCase) &&
                                    a.EntityTypeQualifierValue.Equals(qualifierValue))
                             .OrderBy(a => a.Order)
                             .ThenBy(a => a.Name)
                             .ToList();

            foreach (var attribute in attributes)
            {
                attribute.Order = order++;
                AttributeCache.Flush(attribute.Id);
            }

            var movedItem = attributes.Where(a => a.Order == e.OldIndex).FirstOrDefault();

            if (movedItem != null)
            {
                if (e.NewIndex < e.OldIndex)
                {
                    // Moved up
                    foreach (var otherItem in attributes.Where(a => a.Order < e.OldIndex && a.Order >= e.NewIndex))
                    {
                        otherItem.Order = otherItem.Order + 1;
                    }
                }
                else
                {
                    // Moved Down
                    foreach (var otherItem in attributes.Where(a => a.Order > e.OldIndex && a.Order <= e.NewIndex))
                    {
                        otherItem.Order = otherItem.Order - 1;
                    }
                }

                movedItem.Order = e.NewIndex;
                rockContext.SaveChanges();
            }

            BindBlockTypeAttributesGrid();
        }
Пример #3
0
        private void RGrid_GridReorder(object sender, GridReorderEventArgs e)
        {
            var rockContext      = new RockContext();
            var attributeService = new AttributeService(rockContext);
            var qry = GetData(rockContext);
            var updatedAttributeIds = attributeService.Reorder(qry.ToList(), e.OldIndex, e.NewIndex);

            rockContext.SaveChanges();

            foreach (int id in updatedAttributeIds)
            {
                AttributeCache.Flush(id);
            }
            AttributeCache.FlushEntityAttributes();

            BindGrid();
        }
Пример #4
0
        /// <summary>
        /// Sets the value.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public static void SetValue(string key, string value)
        {
            var rockContext      = new Rock.Data.RockContext();
            var attributeService = new AttributeService(rockContext);
            var attribute        = attributeService.GetSystemSetting(key);

            bool isNew = false;

            if (attribute == null)
            {
                attribute             = new Rock.Model.Attribute();
                attribute.FieldTypeId = FieldTypeCache.Read(new Guid(SystemGuid.FieldType.TEXT)).Id;
                attribute.EntityTypeQualifierColumn = Rock.Model.Attribute.SYSTEM_SETTING_QUALIFIER;
                attribute.EntityTypeQualifierValue  = string.Empty;
                attribute.Key          = key;
                attribute.Name         = key.SplitCase();
                attribute.DefaultValue = value;
                attributeService.Add(attribute);
                isNew = true;
            }
            else
            {
                attribute.DefaultValue = value;
            }

            rockContext.SaveChanges();

            AttributeCache.Flush(attribute.Id);
            if (isNew)
            {
                AttributeCache.FlushEntityAttributes();
            }

            var settings       = SystemSettings.Read();
            var attributeCache = settings.Attributes.FirstOrDefault(a => a.Key.Equals(key, StringComparison.OrdinalIgnoreCase));

            if (attributeCache != null)
            {
                attributeCache.DefaultValue = value;
            }
            else
            {
                settings.Attributes.Add(AttributeCache.Read(attribute.Id));
            }
        }
Пример #5
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string attributeKey = GetAttributeValue("OAuthConfigAttributeKey");
            Dictionary <string, string> settings = GlobalAttributesCache.Value(attributeKey).AsDictionary();

            settings["OAuthAuthorizePath"]        = "/" + ddlAuthorizeRoute.SelectedValue;
            settings["OAuthLoginPath"]            = "/" + ddlLoginRoute.SelectedValue;
            settings["OAuthLogoutPath"]           = "/" + ddlLogoutRoute.SelectedValue;
            settings["OAuthTokenPath"]            = "/" + ddlTokenRoute.SelectedValue;
            settings["OAuthRequireSsl"]           = cbSSLRequired.Checked.ToString();
            settings["OAuthTokenLifespan"]        = tbTokenLifespan.Text;
            settings["OAuthRefreshTokenLifespan"] = tbRefreshTokenLifespan.Text;

            RockContext      context          = new RockContext();
            AttributeService attributeService = new AttributeService(context);

            Rock.Model.Attribute attribute = attributeService.Queryable().Where(a => a.Key == attributeKey).FirstOrDefault();
            if (attribute == null)
            {
                attribute             = new Rock.Model.Attribute();
                attribute.Name        = "OAuth Settings";
                attribute.Description = "Settings for the OAuth server plugin.";
                attribute.Key         = "OAuthSettings";
                FieldTypeService fieldTypeService = new FieldTypeService(context);
                attribute.FieldType = fieldTypeService.Get(Rock.SystemGuid.FieldType.KEY_VALUE_LIST.AsGuid());
                context.SaveChanges();
            }
            // Update the actual attribute value.
            AttributeValueService attributeValueService = new AttributeValueService(context);
            AttributeValue        attributeValue        = attributeValueService.GetByAttributeIdAndEntityId(attribute.Id, null);

            if (attributeValue == null)
            {
                attributeValue             = new AttributeValue();
                attributeValue.AttributeId = attribute.Id;
                attributeValueService.Add(attributeValue);
            }
            attributeValue.Value = string.Join("|", settings.Select(a => a.Key + "^" + a.Value).ToList());
            context.SaveChanges();

            // Flush the cache(s)
            AttributeCache.Flush(attribute.Id);
            GlobalAttributesCache.Flush();
        }
        /// <summary>
        /// Handles the Click event of the btnSaveDefinedTypeAttribute 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 btnSaveDefinedTypeAttribute_Click(object sender, EventArgs e)
        {
            Attribute attribute = null;

            AttributeService attributeService = new AttributeService();

            if (edtDefinedTypeAttributes.AttributeId.HasValue)
            {
                attribute = attributeService.Get(edtDefinedTypeAttributes.AttributeId.Value);
            }

            if (attribute == null)
            {
                attribute = new Attribute();
            }

            edtDefinedTypeAttributes.GetAttributeProperties(attribute);

            // Controls will show warnings
            if (!attribute.IsValid)
            {
                return;
            }

            RockTransactionScope.WrapTransaction(() =>
            {
                if (attribute.Id.Equals(0))
                {
                    attribute.EntityTypeId = EntityTypeCache.Read(typeof(DefinedValue)).Id;
                    attribute.EntityTypeQualifierColumn = "DefinedTypeId";
                    attribute.EntityTypeQualifierValue  = hfDefinedTypeId.Value;
                    attributeService.Add(attribute, CurrentPersonId);
                }

                AttributeCache.Flush(attribute.Id);
                attributeService.Save(attribute, CurrentPersonId);
            });

            pnlDetails.Visible = true;
            pnlDefinedTypeAttributes.Visible = false;

            BindDefinedTypeAttributesGrid();
        }
        /// <summary>
        /// Handles the Delete event of the gDefinedTypeAttributes 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 gDefinedTypeAttributes_Delete(object sender, RowEventArgs e)
        {
            Guid             attributeGuid    = (Guid)e.RowKeyValue;
            AttributeService attributeService = new AttributeService();
            Attribute        attribute        = attributeService.Get(attributeGuid);

            if (attribute != null)
            {
                string errorMessage;
                if (!attributeService.CanDelete(attribute, out errorMessage))
                {
                    mdGridWarningAttributes.Show(errorMessage, ModalAlertType.Information);
                    return;
                }

                AttributeCache.Flush(attribute.Id);
                attributeService.Delete(attribute, CurrentPersonId);
                attributeService.Save(attribute, CurrentPersonId);
            }

            BindDefinedTypeAttributesGrid();
        }
Пример #8
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string attributeKey = GetAttributeValue("RoomscannerConfigAttributeKey");
            Dictionary <string, string> settings = GlobalAttributesCache.Value(attributeKey).AsDictionary();

            settings["AllowedGroupId"]      = tbAllowedGroupId.Text;
            settings["SubroomLocationType"] = tbSubroomLocationTypeId.Text;

            RockContext      context          = new RockContext();
            AttributeService attributeService = new AttributeService(context);

            Rock.Model.Attribute attribute = attributeService.Queryable().Where(a => a.Key == attributeKey).FirstOrDefault();
            if (attribute == null)
            {
                attribute             = new Rock.Model.Attribute();
                attribute.Name        = "RoomScanner Settings";
                attribute.Description = "Settings for the OAuth server plugin.";
                attribute.Key         = "RoomScannerSettings";
                FieldTypeService fieldTypeService = new FieldTypeService(context);
                attribute.FieldType = fieldTypeService.Get(Rock.SystemGuid.FieldType.KEY_VALUE_LIST.AsGuid());
                context.SaveChanges();
            }
            // Update the actual attribute value.
            AttributeValueService attributeValueService = new AttributeValueService(context);
            AttributeValue        attributeValue        = attributeValueService.GetByAttributeIdAndEntityId(attribute.Id, null);

            if (attributeValue == null)
            {
                attributeValue             = new AttributeValue();
                attributeValue.AttributeId = attribute.Id;
                attributeValueService.Add(attributeValue);
            }
            attributeValue.Value = string.Join("|", settings.Select(a => a.Key + "^" + a.Value).ToList());
            context.SaveChanges();

            // Flush the cache(s)
            AttributeCache.Flush(attribute.Id);
            GlobalAttributesCache.Flush();
        }
Пример #9
0
        /// <summary>
        /// Saves the attributes.
        /// </summary>
        /// <param name="entityTypeId">The entity type identifier.</param>
        /// <param name="qualifierColumn">The qualifier column.</param>
        /// <param name="qualifierValue">The qualifier value.</param>
        /// <param name="attributes">The attributes.</param>
        /// <param name="rockContext">The rock context.</param>
        private void SaveAttributes(int entityTypeId, string qualifierColumn, string qualifierValue, List <Attribute> attributes, RockContext rockContext)
        {
            // Get the existing attributes for this entity type and qualifier value
            var attributeService   = new AttributeService(rockContext);
            var existingAttributes = attributeService.Get(entityTypeId, qualifierColumn, qualifierValue);

            // Delete any of those attributes that were removed in the UI
            var selectedAttributeGuids = attributes.Select(a => a.Guid);

            foreach (var attr in existingAttributes.Where(a => !selectedAttributeGuids.Contains(a.Guid)))
            {
                attributeService.Delete(attr);
                rockContext.SaveChanges();
                AttributeCache.Flush(attr.Id);
            }

            // Update the Attributes that were assigned in the UI
            foreach (var attribute in attributes)
            {
                Helper.SaveAttributeEdits(attribute, entityTypeId, qualifierColumn, qualifierValue, rockContext);
            }

            AttributeCache.FlushEntityAttributes();
        }
Пример #10
0
 public override void Put(int id, [FromBody] Model.Attribute value)
 {
     base.Put(id, value);
     AttributeCache.Flush(id);
     AttributeCache.FlushEntityAttributes();
 }
Пример #11
0
 public override void Delete(int id)
 {
     base.Delete(id);
     AttributeCache.Flush(id);
     AttributeCache.FlushEntityAttributes();
 }
Пример #12
0
        /// <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)
        {
            using (new UnitOfWorkScope())
            {
                var attributeService          = new AttributeService();
                var attributeQualifierService = new AttributeQualifierService();

                Rock.Model.Attribute attribute;

                int attributeId = 0;
                if (hfId.Value != string.Empty && !int.TryParse(hfId.Value, out attributeId))
                {
                    attributeId = 0;
                }

                if (attributeId == 0)
                {
                    attribute              = new Rock.Model.Attribute();
                    attribute.IsSystem     = false;
                    attribute.EntityTypeId = _entityTypeId;
                    attribute.EntityTypeQualifierColumn = _entityQualifierColumn;
                    attribute.EntityTypeQualifierValue  = _entityQualifierValue;
                    attributeService.Add(attribute, CurrentPersonId);
                }
                else
                {
                    AttributeCache.Flush(attributeId);
                    attribute = attributeService.Get(attributeId);
                }

                attribute.Key         = tbKey.Text;
                attribute.Name        = tbName.Text;
                attribute.Category    = tbCategory.Text;
                attribute.Description = tbDescription.Text;
                attribute.FieldTypeId = int.Parse(ddlFieldType.SelectedValue);

                var fieldType = FieldTypeCache.Read(attribute.FieldTypeId);

                foreach (var oldQualifier in attribute.AttributeQualifiers.ToList())
                {
                    attributeQualifierService.Delete(oldQualifier, CurrentPersonId);
                }

                attribute.AttributeQualifiers.Clear();

                List <Control> configControls = new List <Control>();
                foreach (var key in fieldType.Field.ConfigurationKeys())
                {
                    configControls.Add(phFieldTypeQualifiers.FindControl("configControl_" + key));
                }

                foreach (var configValue in fieldType.Field.ConfigurationValues(configControls))
                {
                    AttributeQualifier qualifier = new AttributeQualifier();
                    qualifier.IsSystem = false;
                    qualifier.Key      = configValue.Key;
                    qualifier.Value    = configValue.Value.Value ?? string.Empty;
                    attribute.AttributeQualifiers.Add(qualifier);
                }

                attribute.DefaultValue = tbDefaultValue.Text;
                attribute.IsMultiValue = cbMultiValue.Checked;
                attribute.IsRequired   = cbRequired.Checked;

                attributeService.Save(attribute, CurrentPersonId);
            }

            BindGrid();

            pnlDetails.Visible = false;
            pnlList.Visible    = true;
        }
Пример #13
0
        /// <summary>
        /// Adds or Updates a <see cref="Rock.Model.Attribute" /> item for the attribute.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <param name="entityTypeId">The entity type id.</param>
        /// <param name="entityQualifierColumn">The entity qualifier column.</param>
        /// <param name="entityQualifierValue">The entity qualifier value.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <returns></returns>
        /// <remarks>
        /// If a rockContext value is included, this method will save any previous changes made to the context
        /// </remarks>
        private static bool UpdateAttribute(FieldAttribute property, int?entityTypeId, string entityQualifierColumn, string entityQualifierValue, RockContext rockContext = null)
        {
            bool updated = false;

            rockContext = rockContext ?? new RockContext();

            var attributeService          = new AttributeService(rockContext);
            var attributeQualifierService = new AttributeQualifierService(rockContext);
            var fieldTypeService          = new FieldTypeService(rockContext);
            var categoryService           = new CategoryService(rockContext);

            var propertyCategories = property.Category.SplitDelimitedValues(false).ToList();

            // Look for an existing attribute record based on the entity, entityQualifierColumn and entityQualifierValue
            Model.Attribute attribute = attributeService.Get(entityTypeId, entityQualifierColumn, entityQualifierValue, property.Key);
            if (attribute == null)
            {
                // If an existing attribute record doesn't exist, create a new one
                updated = true;

                attribute = new Model.Attribute();
                attribute.EntityTypeId = entityTypeId;
                attribute.EntityTypeQualifierColumn = entityQualifierColumn;
                attribute.EntityTypeQualifierValue  = entityQualifierValue;
                attribute.Key          = property.Key;
                attribute.IconCssClass = string.Empty;
                attribute.IsGridColumn = false;
            }
            else
            {
                // Check to see if the existing attribute record needs to be updated
                if (attribute.Name != property.Name ||
                    attribute.DefaultValue != property.DefaultValue ||
                    attribute.Description != property.Description ||
                    attribute.Order != property.Order ||
                    attribute.FieldType.Assembly != property.FieldTypeAssembly ||
                    attribute.FieldType.Class != property.FieldTypeClass ||
                    attribute.IsRequired != property.IsRequired)
                {
                    updated = true;
                }

                // Check category
                else if (attribute.Categories.Select(c => c.Name).Except(propertyCategories).Any() ||
                         propertyCategories.Except(attribute.Categories.Select(c => c.Name)).Any())
                {
                    updated = true;
                }

                // Check the qualifier values
                else if (attribute.AttributeQualifiers.Select(q => q.Key).Except(property.FieldConfigurationValues.Select(c => c.Key)).Any() ||
                         property.FieldConfigurationValues.Select(c => c.Key).Except(attribute.AttributeQualifiers.Select(q => q.Key)).Any())
                {
                    updated = true;
                }
                else
                {
                    foreach (var attributeQualifier in attribute.AttributeQualifiers)
                    {
                        if (!property.FieldConfigurationValues.ContainsKey(attributeQualifier.Key) ||
                            property.FieldConfigurationValues[attributeQualifier.Key].Value != attributeQualifier.Value)
                        {
                            updated = true;
                            break;
                        }
                    }
                }
            }

            if (updated)
            {
                // Update the attribute
                attribute.Name         = property.Name;
                attribute.Description  = property.Description;
                attribute.DefaultValue = property.DefaultValue;
                attribute.Order        = property.Order;
                attribute.IsRequired   = property.IsRequired;

                attribute.Categories.Clear();
                if (propertyCategories.Any())
                {
                    foreach (string propertyCategory in propertyCategories)
                    {
                        int attributeEntityTypeId = EntityTypeCache.Read(typeof(Rock.Model.Attribute)).Id;
                        var category = categoryService.Get(propertyCategory, attributeEntityTypeId, "EntityTypeId", entityTypeId.ToString()).FirstOrDefault();
                        if (category == null)
                        {
                            category              = new Category();
                            category.Name         = propertyCategory;
                            category.EntityTypeId = attributeEntityTypeId;
                            category.EntityTypeQualifierColumn = "EntityTypeId";
                            category.EntityTypeQualifierValue  = entityTypeId.ToString();
                            category.Order = 0;
                        }
                        attribute.Categories.Add(category);
                    }
                }

                foreach (var qualifier in attribute.AttributeQualifiers.ToList())
                {
                    attributeQualifierService.Delete(qualifier);
                }
                attribute.AttributeQualifiers.Clear();

                foreach (var configValue in property.FieldConfigurationValues)
                {
                    var qualifier = new Model.AttributeQualifier();
                    qualifier.Key   = configValue.Key;
                    qualifier.Value = configValue.Value.Value;
                    attribute.AttributeQualifiers.Add(qualifier);
                }

                // Try to set the field type by searching for an existing field type with the same assembly and class name
                if (attribute.FieldType == null || attribute.FieldType.Assembly != property.FieldTypeAssembly ||
                    attribute.FieldType.Class != property.FieldTypeClass)
                {
                    attribute.FieldType = fieldTypeService.Queryable().FirstOrDefault(f =>
                                                                                      f.Assembly == property.FieldTypeAssembly &&
                                                                                      f.Class == property.FieldTypeClass);
                }

                // If this is a new attribute, add it, otherwise remove the exiting one from the cache
                if (attribute.Id == 0)
                {
                    attributeService.Add(attribute);
                }
                else
                {
                    AttributeCache.Flush(attribute.Id);
                }

                rockContext.SaveChanges();

                return(true);
            }
            else
            {
                return(false);
            }
        }