/// <summary> /// Shows the detail. /// </summary> /// <param name="metricId">The metric identifier.</param> /// <param name="parentCategoryId">The parent category id.</param> public void ShowDetail( int metricId, int? parentCategoryId ) { pnlDetails.Visible = false; var rockContext = new RockContext(); var metricService = new MetricService( rockContext ); Metric metric = null; if ( !metricId.Equals( 0 ) ) { metric = metricService.Get( metricId ); pdAuditDetails.SetEntity( metric, ResolveRockUrl( "~" ) ); } if ( metric == null ) { // hide the panel drawer that show created and last modified dates pdAuditDetails.Visible = false; metric = new Metric { Id = 0, IsSystem = false }; metric.SourceValueTypeId = DefinedValueCache.Read( Rock.SystemGuid.DefinedValue.METRIC_SOURCE_VALUE_TYPE_MANUAL.AsGuid() ).Id; metric.MetricCategories = new List<MetricCategory>(); if ( parentCategoryId.HasValue ) { var metricCategory = new MetricCategory { CategoryId = parentCategoryId.Value }; metricCategory.Category = metricCategory.Category ?? new CategoryService( rockContext ).Get( metricCategory.CategoryId ); metric.MetricCategories.Add( metricCategory ); } metric.MetricPartitions = new List<MetricPartition>(); } if ( !metric.IsAuthorized( Authorization.VIEW, CurrentPerson ) ) { return; } pnlDetails.Visible = true; hfMetricId.Value = metric.Id.ToString(); // render UI based on Authorized and IsSystem bool readOnly = false; nbEditModeMessage.Text = string.Empty; if ( metric.IsSystem ) { readOnly = true; nbEditModeMessage.Text = EditModeMessage.ReadOnlySystem( Metric.FriendlyTypeName ); } if ( !UserCanEdit && !metric.IsAuthorized( Authorization.EDIT, CurrentPerson ) ) { readOnly = true; nbEditModeMessage.Text = EditModeMessage.NotAuthorizedToEdit( Metric.FriendlyTypeName ); } bool canAdministrate = UserCanAdministrate || metric.IsAuthorized( Authorization.ADMINISTRATE, CurrentPerson ); if ( canAdministrate ) { btnSecurity.Visible = true; btnSecurity.Title = metric.Title; btnSecurity.EntityId = metric.Id; } else { btnSecurity.Visible = false; } if ( readOnly ) { btnEdit.Visible = false; btnDelete.Visible = false; ShowReadonlyDetails( metric ); } else { btnEdit.Visible = true; string errorMessage = string.Empty; btnDelete.Visible = metricService.CanDelete( metric, out errorMessage ); if ( metric.Id > 0 ) { ShowReadonlyDetails( metric ); } else { ShowEditDetails( metric ); } } }
/// <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 ) { var rockContext = new RockContext(); MetricService metricService = new MetricService( rockContext ); Metric metric = metricService.Get( hfMetricId.Value.AsInteger() ); // intentionally get metricCategory with new RockContext() so we don't confuse SaveChanges() int? parentCategoryId = null; var metricCategory = new MetricCategoryService( new RockContext() ).Get( hfMetricCategoryId.ValueAsInt() ); if ( metricCategory != null ) { parentCategoryId = metricCategory.CategoryId; } if ( metric != null ) { string errorMessage; if ( !metricService.CanDelete( metric, out errorMessage ) ) { mdDeleteWarning.Show( errorMessage, ModalAlertType.Information ); return; } metricService.Delete( metric ); rockContext.SaveChanges(); } var qryParams = new Dictionary<string, string>(); if ( parentCategoryId != null ) { qryParams["CategoryId"] = parentCategoryId.ToString(); } NavigateToPage( RockPage.Guid, qryParams ); }
/// <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( "MetricId" ) ) { return; } var rockContext = new RockContext(); var metricService = new MetricService( rockContext ); Metric metric = null; if ( !itemKeyValue.Equals( 0 ) ) { metric = metricService.Get( itemKeyValue ); } else { metric = new Metric { Id = 0, IsSystem = false }; metric.MetricCategories = new List<MetricCategory>(); if ( parentCategoryId.HasValue ) { var metricCategory = new MetricCategory { CategoryId = parentCategoryId.Value }; metricCategory.Category = metricCategory.Category ?? new CategoryService( rockContext ).Get( metricCategory.CategoryId ); metric.MetricCategories.Add( metricCategory ); } } if ( metric == null || !metric.IsAuthorized( Authorization.VIEW, CurrentPerson ) ) { return; } pnlDetails.Visible = true; hfMetricId.Value = metric.Id.ToString(); // render UI based on Authorized and IsSystem bool readOnly = false; nbEditModeMessage.Text = string.Empty; if ( metric.IsSystem ) { readOnly = true; nbEditModeMessage.Text = EditModeMessage.ReadOnlySystem( Metric.FriendlyTypeName ); } btnSecurity.Visible = metric.IsAuthorized( Authorization.ADMINISTRATE, CurrentPerson ); btnSecurity.Title = metric.Title; btnSecurity.EntityId = metric.Id; if ( readOnly ) { btnEdit.Visible = false; btnDelete.Visible = false; ShowReadonlyDetails( metric ); } else { btnEdit.Visible = true; string errorMessage = string.Empty; btnDelete.Visible = metricService.CanDelete( metric, out errorMessage ); if ( metric.Id > 0 ) { ShowReadonlyDetails( metric ); } else { ShowEditDetails( metric ); } } }