/// <summary> /// Sets the value. /// </summary> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <param name="currentPersonId">The current person id.</param> /// <param name="saveValue">if set to <c>true</c> [save value].</param> public void SetValue( string key, string value, int? currentPersonId, bool saveValue ) { if ( saveValue ) { using (new Rock.Data.UnitOfWorkScope()) { // Save new value var attributeValueService = new AttributeValueService(); var attributeValue = attributeValueService.GetGlobalAttributeValue( key ); if ( attributeValue == null ) { var attributeService = new AttributeService(); var attribute = attributeService.GetGlobalAttribute( key ); if ( attribute == null ) { attribute = new Rock.Model.Attribute(); attribute.FieldTypeId = FieldTypeCache.Read(new Guid(SystemGuid.FieldType.TEXT)).Id; attribute.EntityTypeQualifierColumn = string.Empty; attribute.EntityTypeQualifierValue = string.Empty; attribute.Key = key; attribute.Name = key.SplitCase(); attributeService.Add(attribute, currentPersonId); attributeService.Save(attribute, currentPersonId); Attributes.Add( AttributeCache.Read( attribute.Id ) ); } attributeValue = new AttributeValue(); attributeValueService.Add( attributeValue, currentPersonId ); attributeValue.IsSystem = false; attributeValue.AttributeId = attribute.Id; if ( !AttributeValues.Keys.Contains( key ) ) { AttributeValues.Add( key, new KeyValuePair<string, string>( attribute.Name, value ) ); } } attributeValue.Value = value; attributeValueService.Save( attributeValue, currentPersonId ); } } var attributeCache = Attributes.FirstOrDefault( a => a.Key.Equals( key, StringComparison.OrdinalIgnoreCase ) ); if ( attributeCache != null ) // (Should never be null) { if ( AttributeValues.Keys.Contains( key ) ) { AttributeValues[key] = new KeyValuePair<string, string>( attributeCache.Name, value ); } else { AttributeValues.Add( key, new KeyValuePair<string, string>( attributeCache.Name, value ) ); } } }
/// <summary> /// Handles the SaveClick event of the modalDetails 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 modalDetails_SaveClick( object sender, EventArgs e ) { if ( _displayValueEdit ) { int attributeId = 0; if ( hfIdValues.Value != string.Empty && !int.TryParse( hfIdValues.Value, out attributeId ) ) { attributeId = 0; } if ( attributeId != 0 && phEditControl.Controls.Count > 0 ) { var attribute = Rock.Web.Cache.AttributeCache.Read( attributeId ); AttributeValueService attributeValueService = new AttributeValueService(); var attributeValue = attributeValueService.GetByAttributeIdAndEntityId( attributeId, _entityId ).FirstOrDefault(); if ( attributeValue == null ) { attributeValue = new Rock.Model.AttributeValue(); attributeValue.AttributeId = attributeId; attributeValue.EntityId = _entityId; attributeValueService.Add( attributeValue, CurrentPersonId ); } var fieldType = Rock.Web.Cache.FieldTypeCache.Read( attribute.FieldType.Id ); attributeValue.Value = fieldType.Field.GetEditValue( phEditControl.Controls[0], attribute.QualifierValues ); attributeValueService.Save( attributeValue, CurrentPersonId ); Rock.Web.Cache.AttributeCache.Flush( attributeId ); if ( !_entityTypeId.HasValue && _entityQualifierColumn == string.Empty && _entityQualifierValue == string.Empty && !_entityId.HasValue ) { Rock.Web.Cache.GlobalAttributesCache.Flush(); } } hfIdValues.Value = string.Empty; modalDetails.Hide(); } BindGrid(); }
void lvAttributeValues_ItemInserting( object sender, ListViewInsertEventArgs e ) { PlaceHolder phInsertValue = lvAttributeValues.InsertItem.FindControl( "phInsertValue" ) as PlaceHolder; if ( phInsertValue != null && phInsertValue.Controls.Count == 1 ) { string value = _attribute.FieldType.Field.GetEditValue( phInsertValue.Controls[0], _attribute.QualifierValues ); var attributeValueService = new AttributeValueService(); var attributeValue = new AttributeValue(); attributeValue.AttributeId = _attribute.Id; attributeValue.EntityId = _model.Id; attributeValue.Value = value; int? maxOrder = attributeValueService.Queryable(). Where( a => a.AttributeId == attributeValue.AttributeId && a.EntityId == attributeValue.EntityId). Select( a => ( int? )a.Order ).Max(); attributeValue.Order = maxOrder.HasValue ? maxOrder.Value + 1 : 0; attributeValueService.Add( attributeValue, _currentPersonId); attributeValueService.Save( attributeValue, _currentPersonId ); _model.LoadAttributes(); } lvAttributeValues.EditIndex = -1; BindData(); }
void lvAttributeValues_ItemUpdating( object sender, ListViewUpdateEventArgs e ) { PlaceHolder phEditValue = lvAttributeValues.EditItem.FindControl( "phEditValue" ) as PlaceHolder; if ( phEditValue != null && phEditValue.Controls.Count == 1 ) { string value = _attribute.FieldType.Field.GetEditValue( phEditValue.Controls[0], _attribute.QualifierValues ); var attributeValueService = new AttributeValueService(); var attributeValue = attributeValueService.Get( ( int )e.Keys["Id"] ); if ( attributeValue == null ) { attributeValue = new AttributeValue(); attributeValueService.Add( attributeValue, _currentPersonId ); attributeValue.AttributeId = _attribute.Id; attributeValue.EntityId = _model.Id; } attributeValue.Value = value; attributeValueService.Save( attributeValue, _currentPersonId ); _model.LoadAttributes(); } lvAttributeValues.EditIndex = -1; BindData(); }
void lvAttributeValues_ItemDeleting( object sender, ListViewDeleteEventArgs e ) { var attributeValueService = new AttributeValueService(); var attributeValue = attributeValueService.Get( ( int )e.Keys["Id"] ); if ( attributeValue != null ) { attributeValueService.Delete( attributeValue, _currentPersonId ); attributeValueService.Save( attributeValue, _currentPersonId ); _model.LoadAttributes(); } BindData(); }