public Get ( int ParentId, int entityTypeId ) : IQueryable |
||
ParentId | int | A |
entityTypeId | int | A |
return | IQueryable |
/// <summary> /// Returns Category object from cache. If category does not already exist in cache, it /// will be read and added to cache /// </summary> /// <param name="id">The id of the Category to read</param> /// <returns></returns> public static CategoryCache Read(int id) { string cacheKey = CategoryCache.CacheKey(id); ObjectCache cache = MemoryCache.Default; CategoryCache category = cache[cacheKey] as CategoryCache; if (category != null) { return(category); } else { var categoryService = new Rock.Model.CategoryService(); var categoryModel = categoryService.Get(id); if (categoryModel != null) { category = new CategoryCache(categoryModel); var cachePolicy = new CacheItemPolicy(); cache.Set(cacheKey, category, cachePolicy); cache.Set(category.Guid.ToString(), category.Id, cachePolicy); return(category); } else { return(null); } } }
private static CategoryCache LoadById2(int id, RockContext rockContext) { var categoryService = new Rock.Model.CategoryService(rockContext); var categoryModel = categoryService.Get(id); if (categoryModel != null) { return(new CategoryCache(categoryModel)); } return(null); }
/// <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 mdDetails_SaveClick( object sender, EventArgs e ) { int categoryId = 0; if ( hfIdValue.Value != string.Empty && !int.TryParse( hfIdValue.Value, out categoryId ) ) { categoryId = 0; } var service = new CategoryService(); Category category = null; if ( categoryId != 0 ) { category = service.Get( categoryId ); } if ( category == null ) { category = new Category(); category.EntityTypeId = _entityTypeId; var lastCategory = GetUnorderedCategories() .OrderByDescending( c => c.Order ).FirstOrDefault(); category.Order = lastCategory != null ? lastCategory.Order + 1 : 0; service.Add( category, CurrentPersonId ); } category.Name = tbName.Text; category.Description = tbDescription.Text; category.ParentCategoryId = catpParentCategory.SelectedValueAsInt(); category.IconCssClass = tbIconCssClass.Text; List<int> orphanedBinaryFileIdList = new List<int>(); if ( category.IsValid ) { RockTransactionScope.WrapTransaction( () => { service.Save( category, CurrentPersonId ); BinaryFileService binaryFileService = new BinaryFileService(); foreach ( int binaryFileId in orphanedBinaryFileIdList ) { var binaryFile = binaryFileService.Get( binaryFileId ); if ( binaryFile != null ) { // marked the old images as IsTemporary so they will get cleaned up later binaryFile.IsTemporary = true; binaryFileService.Save( binaryFile, CurrentPersonId ); } } } ); hfIdValue.Value = string.Empty; mdDetails.Hide(); BindGrid(); } }
/// <summary> /// Handles the Delete event of the gCategories 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 gCategories_Delete( object sender, RowEventArgs e ) { var service = new CategoryService(); var category = service.Get( (int)gCategories.DataKeys[e.RowIndex]["id"] ); if ( category != null ) { string errorMessage = string.Empty; if ( service.CanDelete( category, out errorMessage ) ) { service.Delete( category, CurrentPersonId ); service.Save( category, CurrentPersonId ); } else { nbMessage.Text = errorMessage; nbMessage.Visible = true; } } BindGrid(); }
/// <summary> /// Shows the detail. /// </summary> /// <param name="categoryId">The category identifier.</param> /// <param name="parentCategoryId">The parent category id.</param> public void ShowDetail( int categoryId, int? parentCategoryId ) { pnlDetails.Visible = false; var categoryService = new CategoryService( new RockContext() ); Category category = null; if ( !categoryId.Equals( 0 ) ) { category = categoryService.Get( categoryId ); } if ( category == null ) { category = new Category { Id = 0, IsSystem = false, ParentCategoryId = parentCategoryId}; // fetch the ParentCategory (if there is one) so that security can check it category.ParentCategory = categoryService.Get( parentCategoryId ?? 0 ); category.EntityTypeId = entityTypeId; category.EntityTypeQualifierColumn = entityTypeQualifierProperty; category.EntityTypeQualifierValue = entityTypeQualifierValue; } if (category.EntityTypeId != entityTypeId || !category.IsAuthorized( Authorization.VIEW, CurrentPerson ) ) { pnlDetails.Visible = false; return; } pnlDetails.Visible = true; hfCategoryId.Value = category.Id.ToString(); // render UI based on Authorized and IsSystem bool readOnly = false; nbEditModeMessage.Text = string.Empty; // if the person is Authorized to EDIT the category, or has EDIT for the block var canEdit = category.IsAuthorized( Authorization.EDIT, CurrentPerson ) || this.IsUserAuthorized(Authorization.EDIT); if ( !canEdit ) { readOnly = true; nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed( Category.FriendlyTypeName ); } if ( category.IsSystem ) { readOnly = true; nbEditModeMessage.Text = EditModeMessage.ReadOnlySystem( Category.FriendlyTypeName ); } btnSecurity.Visible = category.IsAuthorized( Authorization.ADMINISTRATE, CurrentPerson ); btnSecurity.Title = category.Name; btnSecurity.EntityId = category.Id; if ( readOnly ) { btnEdit.Visible = false; btnDelete.Visible = false; ShowReadonlyDetails( category ); } else { btnEdit.Visible = true; string errorMessage = string.Empty; btnDelete.Visible = true; btnDelete.Enabled = categoryService.CanDelete(category, out errorMessage); btnDelete.ToolTip = btnDelete.Enabled ? string.Empty : errorMessage; if ( category.Id > 0 ) { ShowReadonlyDetails( category ); } else { ShowEditDetails( category ); } } if ( btnDelete.Visible && btnDelete.Enabled ) { btnDelete.Attributes["onclick"] = string.Format( "javascript: return Rock.dialogs.confirmDelete(event, '{0}');", Category.FriendlyTypeName.ToLower() ); } }
/// <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 ) { Category category; var rockContext = new RockContext(); CategoryService categoryService = new CategoryService( rockContext ); int categoryId = hfCategoryId.ValueAsInt(); if ( categoryId == 0 ) { category = new Category(); category.IsSystem = false; category.EntityTypeId = entityTypeId; category.EntityTypeQualifierColumn = entityTypeQualifierProperty; category.EntityTypeQualifierValue = entityTypeQualifierValue; category.Order = 0; categoryService.Add( category ); } else { category = categoryService.Get( categoryId ); } category.Name = tbName.Text; category.ParentCategoryId = cpParentCategory.SelectedValueAsInt(); category.IconCssClass = tbIconCssClass.Text; category.HighlightColor = tbHighlightColor.Text; List<int> orphanedBinaryFileIdList = new List<int>(); if ( !Page.IsValid ) { return; } if ( !category.IsValid ) { // Controls will render the error messages return; } BinaryFileService binaryFileService = new BinaryFileService( rockContext ); foreach ( int binaryFileId in orphanedBinaryFileIdList ) { var binaryFile = binaryFileService.Get( binaryFileId ); if ( binaryFile != null ) { // marked the old images as IsTemporary so they will get cleaned up later binaryFile.IsTemporary = true; } } rockContext.SaveChanges(); CategoryCache.Flush( category.Id ); var qryParams = new Dictionary<string, string>(); qryParams["CategoryId"] = category.Id.ToString(); NavigateToPage( RockPage.Guid, qryParams ); }
/// <summary> /// Handles displaying the stored filter values. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The e as DisplayFilterValueArgs (hint: e.Key and e.Value).</param> protected void gfFilter_DisplayFilterValue( object sender, GridFilter.DisplayFilterValueArgs e ) { switch ( e.Key ) { case "Prayer Category": int categoryId = All.Id; if ( int.TryParse( e.Value, out categoryId ) ) { if ( categoryId == All.Id ) { e.Value = "All"; } else { var service = new CategoryService(); var category = service.Get( categoryId ); if ( category != null ) { e.Value = category.Name; } } } break; } }
/// <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 ) { int categoryId = 0; if ( hfIdValue.Value != string.Empty && !int.TryParse( hfIdValue.Value, out categoryId ) ) { categoryId = 0; } var rockContext = new RockContext(); var service = new CategoryService( rockContext ); Category category = null; if ( categoryId != 0 ) { CategoryCache.Flush( categoryId ); category = service.Get( categoryId ); } if ( category == null ) { category = new Category(); category.EntityTypeId = EntityTypeCache.Read( typeof( Rock.Model.Attribute ) ).Id; category.EntityTypeQualifierColumn = "EntityTypeId"; var lastCategory = GetUnorderedCategories() .OrderByDescending( c => c.Order ).FirstOrDefault(); category.Order = lastCategory != null ? lastCategory.Order + 1 : 0; service.Add( category ); } category.Name = tbName.Text; category.Description = tbDescription.Text; string QualifierValue = null; if ( ( entityTypePicker.SelectedEntityTypeId ?? 0 ) != 0 ) { QualifierValue = entityTypePicker.SelectedEntityTypeId.ToString(); } category.EntityTypeQualifierValue = QualifierValue; category.IconCssClass = tbIconCssClass.Text; category.HighlightColor = tbHighlightColor.Text; List<int> orphanedBinaryFileIdList = new List<int>(); if ( category.IsValid ) { BinaryFileService binaryFileService = new BinaryFileService( rockContext ); foreach ( int binaryFileId in orphanedBinaryFileIdList ) { var binaryFile = binaryFileService.Get( binaryFileId ); if ( binaryFile != null ) { // marked the old images as IsTemporary so they will get cleaned up later binaryFile.IsTemporary = true; } } rockContext.SaveChanges(); hfIdValue.Value = string.Empty; modalDetails.Hide(); BindGrid(); } }
/// <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 mdDetails_SaveClick( object sender, EventArgs e ) { int categoryId = 0; if ( hfIdValue.Value != string.Empty && !int.TryParse( hfIdValue.Value, out categoryId ) ) { categoryId = 0; } var rockContext = new RockContext(); var service = new CategoryService( rockContext ); Category category = null; if ( categoryId != 0 ) { category = service.Get( categoryId ); } if ( category == null ) { category = new Category(); category.EntityTypeId = _entityTypeId; var lastCategory = GetUnorderedCategories() .OrderByDescending( c => c.Order ).FirstOrDefault(); category.Order = lastCategory != null ? lastCategory.Order + 1 : 0; service.Add( category ); } category.Name = tbName.Text; category.Description = tbDescription.Text; category.ParentCategoryId = catpParentCategory.SelectedValueAsInt(); category.IconCssClass = tbIconCssClass.Text; category.HighlightColor = tbHighlightColor.Text; category.LoadAttributes( rockContext ); Rock.Attribute.Helper.GetEditValues( phAttributes, category ); List<int> orphanedBinaryFileIdList = new List<int>(); if ( category.IsValid ) { BinaryFileService binaryFileService = new BinaryFileService( rockContext ); foreach ( int binaryFileId in orphanedBinaryFileIdList ) { var binaryFile = binaryFileService.Get( binaryFileId ); if ( binaryFile != null ) { // marked the old images as IsTemporary so they will get cleaned up later binaryFile.IsTemporary = true; } } rockContext.WrapTransaction( () => { rockContext.SaveChanges(); category.SaveAttributeValues( rockContext ); } ); CategoryCache.Flush( category.Id ); hfIdValue.Value = string.Empty; mdDetails.Hide(); BindGrid(); } }
/// <summary> /// Saves any attribute edits made to an attribute /// </summary> /// <param name="newAttribute">The new attribute.</param> /// <param name="entityTypeId">The entity type identifier.</param> /// <param name="entityTypeQualifierColumn">The entity type qualifier column.</param> /// <param name="entityTypeQualifierValue">The entity type 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> public static Rock.Model.Attribute SaveAttributeEdits( Rock.Model.Attribute newAttribute, int? entityTypeId, string entityTypeQualifierColumn, string entityTypeQualifierValue, RockContext rockContext = null ) { rockContext = rockContext ?? new RockContext(); var internalAttributeService = new AttributeService( rockContext ); var attributeQualifierService = new AttributeQualifierService( rockContext ); var categoryService = new CategoryService( rockContext ); // If attribute is not valid, return null if (!newAttribute.IsValid) { return null; } // Create a attribute model that will be saved Rock.Model.Attribute attribute = null; // Check to see if this was an existing or new attribute if (newAttribute.Id > 0) { // If editing an existing attribute, remove all the old qualifiers in case they were changed foreach ( var oldQualifier in attributeQualifierService.GetByAttributeId( newAttribute.Id ).ToList() ) { attributeQualifierService.Delete( oldQualifier ); } rockContext.SaveChanges(); // Then re-load the existing attribute attribute = internalAttributeService.Get( newAttribute.Id ); } if ( attribute == null ) { // If the attribute didn't exist, create it attribute = new Rock.Model.Attribute(); internalAttributeService.Add( attribute ); } else { // If it did exist, set the new attribute ID and GUID since we're copying all properties in the next step newAttribute.Id = attribute.Id; newAttribute.Guid = attribute.Guid; } // Copy all the properties from the new attribute to the attribute model attribute.CopyPropertiesFrom( newAttribute ); // Add any qualifiers foreach ( var qualifier in newAttribute.AttributeQualifiers ) { attribute.AttributeQualifiers.Add( new AttributeQualifier { Key = qualifier.Key, Value = qualifier.Value, IsSystem = qualifier.IsSystem } ); } // Add any categories attribute.Categories.Clear(); foreach ( var category in newAttribute.Categories ) { attribute.Categories.Add( categoryService.Get( category.Id ) ); } attribute.EntityTypeId = entityTypeId; attribute.EntityTypeQualifierColumn = entityTypeQualifierColumn; attribute.EntityTypeQualifierValue = entityTypeQualifierValue; rockContext.SaveChanges(); if ( attribute != null ) { Rock.Web.Cache.AttributeCache.Flush( attribute.Id ); // If this is a global attribute, flush all global attributes if ( !entityTypeId.HasValue && entityTypeQualifierColumn == string.Empty && entityTypeQualifierValue == string.Empty ) { Rock.Web.Cache.GlobalAttributesCache.Flush(); } } return attribute; }
/// <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; } }
/// <summary> /// Shows the detail. /// </summary> /// <param name="itemKey">The item key.</param> /// <param name="itemKeyValue">The item key value.</param> /// <param name="parentCategoryId">The parent category id.</param> public void ShowDetail( string itemKey, int itemKeyValue, int? parentCategoryId ) { pnlDetails.Visible = false; if ( !itemKey.Equals( "categoryId" ) ) { return; } var categoryService = new CategoryService(); Category category = null; if ( !itemKeyValue.Equals( 0 ) ) { category = categoryService.Get( itemKeyValue ); } else { category = new Category { Id = 0, IsSystem = false, ParentCategoryId = parentCategoryId}; category.EntityTypeId = entityTypeId; category.EntityTypeQualifierColumn = entityTypeQualifierProperty; category.EntityTypeQualifierValue = entityTypeQualifierValue; } if ( category == null || !category.IsAuthorized( "View", CurrentPerson ) ) { return; } pnlDetails.Visible = true; hfCategoryId.Value = category.Id.ToString(); // render UI based on Authorized and IsSystem bool readOnly = false; nbEditModeMessage.Text = string.Empty; if ( !category.IsAuthorized( "Edit", CurrentPerson ) ) { readOnly = true; nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed( Category.FriendlyTypeName ); } if ( category.IsSystem ) { readOnly = true; nbEditModeMessage.Text = EditModeMessage.ReadOnlySystem( Category.FriendlyTypeName ); } btnSecurity.Visible = category.IsAuthorized( "Administrate", CurrentPerson ); btnSecurity.Title = category.Name; btnSecurity.EntityId = category.Id; if ( readOnly ) { btnEdit.Visible = false; btnDelete.Visible = false; ShowReadonlyDetails( category ); } else { btnEdit.Visible = true; string errorMessage = string.Empty; btnDelete.Visible = categoryService.CanDelete(category, out errorMessage); if ( category.Id > 0 ) { ShowReadonlyDetails( category ); } else { ShowEditDetails( category ); } } }
/// <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 ) { Category category; using ( new UnitOfWorkScope() ) { CategoryService categoryService = new CategoryService(); int categoryId = hfCategoryId.ValueAsInt(); if ( categoryId == 0 ) { category = new Category(); category.IsSystem = false; category.EntityTypeId = entityTypeId; category.EntityTypeQualifierColumn = entityTypeQualifierProperty; category.EntityTypeQualifierValue = entityTypeQualifierValue; category.Order = 0; } else { category = categoryService.Get( categoryId ); } category.Name = tbName.Text; category.ParentCategoryId = cpParentCategory.SelectedValueAsInt(); category.IconCssClass = tbIconCssClass.Text; List<int> orphanedBinaryFileIdList = new List<int>(); if ( !Page.IsValid ) { return; } if ( !category.IsValid ) { // Controls will render the error messages return; } RockTransactionScope.WrapTransaction( () => { if ( category.Id.Equals( 0 ) ) { categoryService.Add( category, CurrentPersonId ); } categoryService.Save( category, CurrentPersonId ); BinaryFileService binaryFileService = new BinaryFileService(); foreach (int binaryFileId in orphanedBinaryFileIdList) { var binaryFile = binaryFileService.Get(binaryFileId); if ( binaryFile != null ) { // marked the old images as IsTemporary so they will get cleaned up later binaryFile.IsTemporary = true; binaryFileService.Save( binaryFile, CurrentPersonId ); } } } ); } var qryParams = new Dictionary<string, string>(); qryParams["CategoryId"] = category.Id.ToString(); NavigateToPage( RockPage.Guid, qryParams ); }
/// <summary> /// Handles the Click event of the btnCancel 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 btnCancel_Click( object sender, EventArgs e ) { if ( hfCategoryId.Value.Equals( "0" ) ) { // Cancelling on Add. Return to tree view with parent category selected var qryParams = new Dictionary<string, string>(); string parentCategoryId = PageParameter( "parentCategoryId" ); if ( !string.IsNullOrWhiteSpace( parentCategoryId ) ) { qryParams["CategoryId"] = parentCategoryId; } NavigateToPage( RockPage.Guid, qryParams ); } else { // Cancelling on Edit. Return to Details CategoryService service = new CategoryService(); Category category = service.Get( hfCategoryId.ValueAsInt() ); ShowReadonlyDetails( category ); } }
/// <summary> /// Shows the detail. /// </summary> /// <param name="categoryId">The category identifier.</param> /// <param name="parentCategoryId">The parent category id.</param> public void ShowDetail( int categoryId, int? parentCategoryId ) { pnlDetails.Visible = false; var categoryService = new CategoryService( new RockContext() ); Category category = null; if ( !categoryId.Equals( 0 ) ) { category = categoryService.Get( categoryId ); } if ( category == null ) { category = new Category { Id = 0, IsSystem = false, ParentCategoryId = parentCategoryId}; category.EntityTypeId = entityTypeId; category.EntityTypeQualifierColumn = entityTypeQualifierProperty; category.EntityTypeQualifierValue = entityTypeQualifierValue; } if (category.EntityTypeId != entityTypeId || !category.IsAuthorized( Authorization.VIEW, CurrentPerson ) ) { pnlDetails.Visible = false; return; } pnlDetails.Visible = true; hfCategoryId.Value = category.Id.ToString(); // render UI based on Authorized and IsSystem bool readOnly = false; nbEditModeMessage.Text = string.Empty; if ( !category.IsAuthorized( Authorization.EDIT, CurrentPerson ) ) { readOnly = true; nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed( Category.FriendlyTypeName ); } if ( category.IsSystem ) { readOnly = true; nbEditModeMessage.Text = EditModeMessage.ReadOnlySystem( Category.FriendlyTypeName ); } btnSecurity.Visible = category.IsAuthorized( Authorization.ADMINISTRATE, CurrentPerson ); btnSecurity.Title = category.Name; btnSecurity.EntityId = category.Id; if ( readOnly ) { btnEdit.Visible = false; btnDelete.Visible = false; ShowReadonlyDetails( category ); } else { btnEdit.Visible = true; string errorMessage = string.Empty; btnDelete.Visible = categoryService.CanDelete(category, out errorMessage); if ( category.Id > 0 ) { ShowReadonlyDetails( category ); } else { ShowEditDetails( category ); } } }
/// <summary> /// Handles the Click event of the btnCancel 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 btnCancel_Click( object sender, EventArgs e ) { if ( hfCategoryId.Value.Equals( "0" ) ) { int? parentCategoryId = PageParameter( "ParentCategoryId" ).AsIntegerOrNull(); if ( parentCategoryId.HasValue ) { // Cancelling on Add, and we know the parentCategoryId, so we are probably in treeview mode, so navigate to the current page var qryParams = new Dictionary<string, string>(); qryParams["CategoryId"] = parentCategoryId.ToString(); NavigateToPage( RockPage.Guid, qryParams ); } else { // Cancelling on Add. Return to Grid NavigateToParentPage(); } } else { // Cancelling on Edit. Return to Details CategoryService service = new CategoryService( new RockContext() ); Category category = service.Get( hfCategoryId.ValueAsInt() ); ShowReadonlyDetails( category ); } }
/// <summary> /// Handles the Delete event of the gCategories 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 gCategories_Delete( object sender, RowEventArgs e ) { var rockContext = new RockContext(); var service = new CategoryService( rockContext ); var category = service.Get( e.RowKeyId ); if ( category != null ) { string errorMessage = string.Empty; if ( service.CanDelete( category, out errorMessage ) ) { int categoryId = category.Id; service.Delete( category ); rockContext.SaveChanges(); CategoryCache.Flush( categoryId ); } else { nbMessage.Text = errorMessage; nbMessage.Visible = true; } } BindGrid(); }
/// <summary> /// Handles the Click event of the btnDelete 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 btnDelete_Click( object sender, EventArgs e ) { int? parentCategoryId = null; var rockContext = new RockContext(); var categoryService = new CategoryService( rockContext ); var category = categoryService.Get( int.Parse( hfCategoryId.Value ) ); if ( category != null ) { string errorMessage; if ( !categoryService.CanDelete( category, out errorMessage ) ) { ShowReadonlyDetails( category ); mdDeleteWarning.Show( errorMessage, ModalAlertType.Information ); } else { parentCategoryId = category.ParentCategoryId; CategoryCache.Flush( category.Id ); categoryService.Delete( category ); rockContext.SaveChanges(); // reload page, selecting the deleted category's parent var qryParams = new Dictionary<string, string>(); if ( parentCategoryId != null ) { qryParams["CategoryId"] = parentCategoryId.ToString(); } NavigateToPage( RockPage.Guid, qryParams ); } } }
/// <summary> /// Handles the Delete event of the rGrid 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 rGrid_Delete( object sender, RowEventArgs e ) { var rockContext = new RockContext(); var service = new CategoryService( rockContext ); var category = service.Get( (int)rGrid.DataKeys[e.RowIndex]["id"] ); if ( category != null ) { string errorMessage = string.Empty; if ( service.CanDelete( category, out errorMessage ) ) { service.Delete( category ); rockContext.SaveChanges(); } else { nbMessage.Text = errorMessage; nbMessage.Visible = true; } } BindGrid(); }
/// <summary> /// Handles the Click event of the btnEdit 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 btnEdit_Click( object sender, EventArgs e ) { CategoryService service = new CategoryService( new RockContext() ); Category category = service.Get( hfCategoryId.ValueAsInt() ); ShowEditDetails( category ); }
/// <summary> /// Handles displaying the stored filter values. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The e as DisplayFilterValueArgs (hint: e.Key and e.Value).</param> protected void gfFilter_DisplayFilterValue( object sender, GridFilter.DisplayFilterValueArgs e ) { switch ( e.Key ) { case "Prayer Category": int categoryId = e.Value.AsIntegerOrNull() ?? All.Id; if ( categoryId == All.Id ) { e.Value = "All"; } else { var service = new CategoryService( new RockContext() ); var category = service.Get( categoryId ); if ( category != null ) { e.Value = category.Name; } } break; } }
/// <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 ) { Category category; var rockContext = new RockContext(); CategoryService categoryService = new CategoryService( rockContext ); int categoryId = hfCategoryId.ValueAsInt(); if ( categoryId == 0 ) { category = new Category(); category.IsSystem = false; category.EntityTypeId = entityTypeId; category.EntityTypeQualifierColumn = entityTypeQualifierProperty; category.EntityTypeQualifierValue = entityTypeQualifierValue; category.Order = 0; categoryService.Add( category ); } else { category = categoryService.Get( categoryId ); } category.Name = tbName.Text; category.ParentCategoryId = cpParentCategory.SelectedValueAsInt(); category.IconCssClass = tbIconCssClass.Text; category.HighlightColor = tbHighlightColor.Text; List<int> orphanedBinaryFileIdList = new List<int>(); if ( !Page.IsValid ) { return; } // if the category IsValid is false, and the UI controls didn't report any errors, it is probably because the custom rules of category didn't pass. // So, make sure a message is displayed in the validation summary cvCategory.IsValid = category.IsValid; if ( !cvCategory.IsValid ) { cvCategory.ErrorMessage = category.ValidationResults.Select( a => a.ErrorMessage ).ToList().AsDelimited( "<br />" ); return; } BinaryFileService binaryFileService = new BinaryFileService( rockContext ); foreach ( int binaryFileId in orphanedBinaryFileIdList ) { var binaryFile = binaryFileService.Get( binaryFileId ); if ( binaryFile != null ) { // marked the old images as IsTemporary so they will get cleaned up later binaryFile.IsTemporary = true; } } rockContext.SaveChanges(); CategoryCache.Flush( category.Id ); var qryParams = new Dictionary<string, string>(); qryParams["CategoryId"] = category.Id.ToString(); NavigateToPage( RockPage.Guid, qryParams ); }