Exemple #1
0
        /// <summary>
        /// Saves the user preferences.
        /// </summary>
        private void SaveUserPreferences()
        {
            RockBlock rockBlock = this.RockBlock();

            if (rockBlock != null)
            {
                string keyPrefix = string.Format("grid-filter-{0}-", rockBlock.BlockId);

                foreach (var userPreference in _userPreferences)
                {
                    string keyPrefixUserPreferenceKey = string.Format("{0}{1}|", keyPrefix, userPreference.Key);
                    string key = string.Format("{0}{1}", keyPrefixUserPreferenceKey, userPreference.Name);

                    // No duplicate user preference key values before the '|' are allowed.
                    // This search for any keys that match before the '|' but mismatch after '|' and delete it before writing the user preference.
                    int?personEntityTypeId = EntityTypeCache.Get(Person.USER_VALUE_ENTITY).Id;

                    using (var rockContext = new Rock.Data.RockContext())
                    {
                        var attributeService = new Model.AttributeService(rockContext);
                        var attributes       = attributeService
                                               .Queryable()
                                               .Where(a => a.EntityTypeId == personEntityTypeId)
                                               .Where(a => a.Key.StartsWith(keyPrefixUserPreferenceKey))
                                               .Where(a => a.Key != key);

                        if (attributes.Count() != 0)
                        {
                            foreach (var attribute in attributes)
                            {
                                rockBlock.DeleteUserPreference(attribute.Key);
                            }
                        }
                        rockContext.SaveChanges();
                    }

                    rockBlock.SetUserPreference(key, userPreference.Value);
                }
            }
        }