-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMetricValueDetail.ascx.cs
369 lines (323 loc) · 16.5 KB
/
MetricValueDetail.ascx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// <copyright>
// Copyright by the Spark Development Network
//
// Licensed under the Rock Community License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.rockrms.com/license
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using Rock;
using Rock.Constants;
using Rock.Data;
using Rock.Field;
using Rock.Model;
using Rock.Security;
using Rock.Web.Cache;
using Rock.Web.UI;
using Rock.Web.UI.Controls;
namespace RockWeb.Blocks.Reporting
{
[DisplayName( "Metric Value Detail" )]
[Category( "Reporting" )]
[Description( "Displays the details of a particular metric value." )]
public partial class MetricValueDetail : RockBlock, IDetailBlock
{
#region Control Methods
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Load" /> event.
/// </summary>
/// <param name="e">The <see cref="T:System.EventArgs" /> object that contains the event data.</param>
protected override void OnLoad( EventArgs e )
{
base.OnLoad( e );
if ( Page.IsPostBack )
{
// create dynamic controls
var rockContext = new RockContext();
var metricValue = new MetricValueService( rockContext ).Get( hfMetricValueId.Value.AsInteger() );
if ( metricValue == null )
{
metricValue = new MetricValue { MetricId = hfMetricId.Value.AsInteger() };
metricValue.Metric = new MetricService( rockContext ).Get( metricValue.MetricId );
}
CreateDynamicControls( metricValue, false, false );
}
if ( !Page.IsPostBack )
{
int? metricValueId = PageParameter( "MetricValueId" ).AsIntegerOrNull();
// in case called with MetricId as the parent id parameter
int? metricId = PageParameter( "MetricId" ).AsIntegerOrNull();
// in case called with MetricCategoryId as the parent id parameter
int? metricCategoryId = PageParameter( "MetricCategoryId" ).AsIntegerOrNull();
MetricCategory metricCategory = null;
if ( metricCategoryId.HasValue )
{
if ( metricCategoryId.Value > 0 )
{
// editing a metric, but get the metricId from the metricCategory
metricCategory = new MetricCategoryService( new RockContext() ).Get( metricCategoryId.Value );
if ( metricCategory != null )
{
metricId = metricCategory.MetricId;
}
}
else
{
// adding a new metric. Block will (hopefully) not be shown
metricId = 0;
}
}
hfMetricCategoryId.Value = metricCategoryId.ToString();
if ( metricValueId.HasValue )
{
ShowDetail( metricValueId.Value, metricId );
}
else
{
pnlDetails.Visible = false;
}
}
}
/// <summary>
/// Creates the dynamic controls.
/// </summary>
/// <param name="metricValue">The metric value.</param>
/// <param name="setValues">if set to <c>true</c> [set values].</param>
/// <param name="readOnly">if set to <c>true</c> [read only].</param>
private void CreateDynamicControls( MetricValue metricValue, bool setValues, bool readOnly )
{
if ( metricValue != null )
{
foreach ( var metricPartition in metricValue.Metric.MetricPartitions )
{
if ( metricPartition.EntityTypeId.HasValue )
{
var entityTypeCache = EntityTypeCache.Read( metricPartition.EntityTypeId.Value );
if ( entityTypeCache != null && entityTypeCache.SingleValueFieldType != null )
{
var fieldType = entityTypeCache.SingleValueFieldType;
Dictionary<string, Rock.Field.ConfigurationValue> configurationValues;
if ( fieldType.Field is IEntityQualifierFieldType )
{
configurationValues = ( fieldType.Field as IEntityQualifierFieldType ).GetConfigurationValuesFromEntityQualifier( metricPartition.EntityTypeQualifierColumn, metricPartition.EntityTypeQualifierValue );
}
else
{
configurationValues = new Dictionary<string, ConfigurationValue>();
}
var entityTypeEditControl = fieldType.Field.EditControl( configurationValues, string.Format( "metricPartition{0}_entityTypeEditControl", metricPartition.Id ) );
var panelCol4 = new Panel { CssClass = "col-md-4" };
if ( entityTypeEditControl != null && entityTypeEditControl is IRockControl)
{
panelCol4.Controls.Add( entityTypeEditControl );
phMetricValuePartitions.Controls.Add( panelCol4 );
var entityTypeRockControl = ( entityTypeEditControl as IRockControl );
entityTypeRockControl.Label = metricPartition.Label;
if ( entityTypeEditControl is WebControl )
{
( entityTypeEditControl as WebControl ).Enabled = !readOnly;
}
if ( setValues && metricValue.MetricValuePartitions != null )
{
var metricValuePartition = metricValue.MetricValuePartitions.FirstOrDefault( a => a.MetricPartitionId == metricPartition.Id );
if ( metricValuePartition != null )
{
if ( fieldType.Field is IEntityFieldType )
{
( fieldType.Field as IEntityFieldType ).SetEditValueFromEntityId( entityTypeEditControl, new Dictionary<string, ConfigurationValue>(), metricValuePartition.EntityId );
}
}
}
}
else
{
var errorControl = new LiteralControl();
errorControl.Text = string.Format( "<span class='label label-danger'>Unable to create Partition control for {0}. Verify that the metric partition settings are set correctly</span>", metricPartition.Label );
phMetricValuePartitions.Controls.Add( errorControl );
}
}
}
}
}
}
#endregion
#region Edit Events
/// <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 )
{
var qryParams = new Dictionary<string, string>();
qryParams.Add( "MetricId", hfMetricId.Value );
qryParams.Add( "MetricCategoryId", hfMetricCategoryId.Value );
NavigateToParentPage( qryParams );
}
/// <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.Read( 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 );
}
/// <summary>
/// Shows the detail.
/// </summary>
/// <param name="metricValueId">The metric value identifier.</param>
public void ShowDetail( int metricValueId )
{
ShowDetail( metricValueId, null );
}
/// <summary>
/// Shows the detail.
/// </summary>
/// <param name="metricValueId">The metric value identifier.</param>
/// <param name="metricId">The metric identifier.</param>
public void ShowDetail( int metricValueId, int? metricId )
{
pnlDetails.Visible = true;
// Load depending on Add(0) or Edit
MetricValue metricValue = null;
if ( !metricValueId.Equals( 0 ) )
{
metricValue = new MetricValueService( new RockContext() ).Get( metricValueId );
lActionTitle.Text = ActionTitle.Edit( MetricValue.FriendlyTypeName ).FormatAsHtmlTitle();
pdAuditDetails.SetEntity( metricValue, ResolveRockUrl( "~" ) );
}
if ( metricValue == null && metricId.HasValue )
{
metricValue = new MetricValue { Id = 0, MetricId = metricId.Value };
metricValue.Metric = metricValue.Metric ?? new MetricService( new RockContext() ).Get( metricValue.MetricId );
lActionTitle.Text = ActionTitle.Add( MetricValue.FriendlyTypeName ).FormatAsHtmlTitle();
// hide the panel drawer that show created and last modified dates
pdAuditDetails.Visible = false;
}
hfMetricValueId.Value = metricValue.Id.ToString();
LoadDropDowns();
ddlMetricValueType.SelectedValue = metricValue.MetricValueType.ConvertToInt().ToString();
tbXValue.Text = metricValue.XValue;
tbYValue.Text = metricValue.YValue.ToString();
hfMetricId.Value = metricValue.MetricId.ToString();
tbNote.Text = metricValue.Note;
dpMetricValueDateTime.SelectedDate = metricValue.MetricValueDateTime;
// render UI based on Authorized and IsSystem
bool readOnly = false;
nbEditModeMessage.Text = string.Empty;
bool canEdit = UserCanEdit;
if ( !canEdit && metricId.HasValue && metricId.Value > 0 )
{
var metric = new MetricService( new RockContext() ).Get( metricId.Value );
if ( metric != null && metric.IsAuthorized( Authorization.EDIT, CurrentPerson ) )
{
canEdit = true;
}
}
if ( !canEdit )
{
readOnly = true;
nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed( MetricValue.FriendlyTypeName );
}
if ( readOnly )
{
lActionTitle.Text = ActionTitle.View( MetricValue.FriendlyTypeName );
btnCancel.Text = "Close";
}
CreateDynamicControls( metricValue, true, readOnly );
ddlMetricValueType.Enabled = !readOnly;
tbXValue.ReadOnly = readOnly;
tbYValue.ReadOnly = readOnly;
tbNote.ReadOnly = readOnly;
dpMetricValueDateTime.Enabled = !readOnly;
btnSave.Visible = !readOnly;
}
/// <summary>
/// Loads the drop downs.
/// </summary>
private void LoadDropDowns()
{
ddlMetricValueType.Items.Clear();
foreach ( var item in Enum.GetValues( typeof( MetricValueType ) ).OfType<MetricValueType>().OrderBy( a => a.ConvertToString() ) )
{
ddlMetricValueType.Items.Add( new ListItem( item.ConvertToString(), item.ConvertToInt().ToString() ) );
}
}
#endregion
}
}