/// <summary> /// Handles the Delete event of the gMetricValues 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 gMetricValues_Delete(object sender, RowEventArgs e) { var rockContext = new RockContext(); var metricValueService = new MetricValueService(rockContext); var metricValuePartitionService = new MetricValuePartitionService(rockContext); var metricValue = metricValueService.Get(e.RowKeyId); if (metricValue != null) { string errorMessage; if (!metricValueService.CanDelete(metricValue, out errorMessage)) { mdGridWarning.Show(errorMessage, ModalAlertType.Information); return; } rockContext.WrapTransaction(() => { metricValuePartitionService.DeleteRange(metricValue.MetricValuePartitions); metricValueService.Delete(metricValue); rockContext.SaveChanges(); }); } BindGrid(); }
/// <summary> /// Handles the Click event of the btnSaveValue 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 btnSaveValue_Click(object sender, EventArgs e) { using (new Rock.Data.UnitOfWorkScope()) { int metricValueId = hfMetricValueId.ValueAsInt(); var metricValueService = new MetricValueService(); MetricValue metricValue; if (metricValueId == 0) { metricValue = new MetricValue(); metricValue.IsSystem = false; metricValue.MetricId = hfMetricId.ValueAsInt(); metricValueService.Add(metricValue, CurrentPersonId); } else { metricValue = metricValueService.Get(metricValueId); } metricValue.Value = tbValue.Text; metricValue.Description = tbValueDescription.Text; metricValue.xValue = tbXValue.Text; metricValue.Label = tbLabel.Text; metricValue.isDateBased = cbIsDateBased.Checked; metricValue.MetricId = Int32.Parse(ddlMetricFilter.SelectedValue); metricValueService.Save(metricValue, CurrentPersonId); } BindGrid(); modalValue.Hide(); }
/// <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) { MetricValue metricValue; var rockContext = new RockContext(); MetricValueService metricValueService = new MetricValueService(rockContext); int metricValueId = int.Parse(hfMetricValueId.Value); if (metricValueId == 0) { metricValue = new MetricValue(); metricValueService.Add(metricValue); metricValue.MetricId = hfMetricId.ValueAsInt(); metricValue.Metric = metricValue.Metric ?? new MetricService(rockContext).Get(metricValue.MetricId); } else { metricValue = metricValueService.Get(metricValueId); } metricValue.MetricValueType = ddlMetricValueType.SelectedValueAsEnum <MetricValueType>(); metricValue.XValue = tbXValue.Text; metricValue.YValue = tbYValue.Text.AsDecimalOrNull(); metricValue.Note = tbNote.Text; metricValue.MetricValueDateTime = dpMetricValueDateTime.SelectedDate; // Get EntityId from EntityType UI controls var metricEntityType = EntityTypeCache.Read(metricValue.Metric.EntityTypeId ?? 0); Control entityTypeEditControl = phEntityTypeEntityIdValue.FindControl("entityTypeEditControl"); if (metricEntityType != null && metricEntityType.SingleValueFieldType != null && metricEntityType.SingleValueFieldType.Field is IEntityFieldType) { metricValue.EntityId = (metricEntityType.SingleValueFieldType.Field as IEntityFieldType).GetEditValueAsEntityId(entityTypeEditControl, new Dictionary <string, ConfigurationValue>()); } else { metricValue.EntityId = null; } if (!metricValue.IsValid) { // Controls will render the error messages return; } rockContext.SaveChanges(); var qryParams = new Dictionary <string, string>(); qryParams.Add("MetricId", hfMetricId.Value); qryParams.Add("MetricCategoryId", hfMetricCategoryId.Value); qryParams.Add("ExpandedIds", PageParameter("ExpandedIds")); NavigateToParentPage(qryParams); }
/// <summary> /// Handles the Delete event of the gMetricValues 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 gMetricValues_Delete(object sender, RowEventArgs e) { var metricValueService = new MetricValueService(); MetricValue metricValue = metricValueService.Get((int)e.RowKeyValue); if (metricValue != null) { metricValueService.Delete(metricValue, CurrentPersonId); metricValueService.Save(metricValue, CurrentPersonId); } BindGrid(); }
/// <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) { MetricValue metricValue; var rockContext = new RockContext(); MetricValueService metricValueService = new MetricValueService(rockContext); int metricValueId = int.Parse(hfMetricValueId.Value); if (metricValueId == 0) { metricValue = new MetricValue(); metricValueService.Add(metricValue); metricValue.MetricId = hfMetricId.ValueAsInt(); metricValue.Metric = metricValue.Metric ?? new MetricService(rockContext).Get(metricValue.MetricId); metricValue.MetricValuePartitions = new List <MetricValuePartition>(); } else { metricValue = metricValueService.Get(metricValueId); } metricValue.MetricValueType = ddlMetricValueType.SelectedValueAsEnum <MetricValueType>(); metricValue.XValue = tbXValue.Text; metricValue.YValue = tbYValue.Text.AsDecimalOrNull(); metricValue.Note = tbNote.Text; metricValue.MetricValueDateTime = dpMetricValueDateTime.SelectedDate; // Get EntityId from EntityType UI controls foreach (var metricPartition in metricValue.Metric.MetricPartitions) { var metricPartitionEntityType = EntityTypeCache.Get(metricPartition.EntityTypeId ?? 0); var controlId = string.Format("metricPartition{0}_entityTypeEditControl", metricPartition.Id); Control entityTypeEditControl = phMetricValuePartitions.FindControl(controlId); var metricValuePartition = metricValue.MetricValuePartitions.FirstOrDefault(a => a.MetricPartitionId == metricPartition.Id); if (metricValuePartition == null) { metricValuePartition = new MetricValuePartition(); metricValuePartition.MetricPartitionId = metricPartition.Id; metricValue.MetricValuePartitions.Add(metricValuePartition); } if (metricPartitionEntityType != null && metricPartitionEntityType.SingleValueFieldType != null && metricPartitionEntityType.SingleValueFieldType.Field is IEntityFieldType) { metricValuePartition.EntityId = (metricPartitionEntityType.SingleValueFieldType.Field as IEntityFieldType).GetEditValueAsEntityId(entityTypeEditControl, new Dictionary <string, ConfigurationValue>()); } else { metricValuePartition.EntityId = null; } if (metricPartition.IsRequired && metricPartitionEntityType != null && !metricValuePartition.EntityId.HasValue) { nbValueRequired.Text = string.Format("A value for {0} is required", metricPartition.Label ?? metricPartitionEntityType.FriendlyName); nbValueRequired.Dismissable = true; nbValueRequired.Visible = true; return; } } if (!metricValue.IsValid) { // Controls will render the error messages return; } rockContext.SaveChanges(); var qryParams = new Dictionary <string, string>(); qryParams.Add("MetricId", hfMetricId.Value); qryParams.Add("MetricCategoryId", hfMetricCategoryId.Value); qryParams.Add("ExpandedIds", PageParameter("ExpandedIds")); NavigateToParentPage(qryParams); }