-
-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathConnectionRequestBoard.ascx.cs
6311 lines (5425 loc) · 259 KB
/
ConnectionRequestBoard.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// <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.Data.Entity;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSScriptLibrary;
using Newtonsoft.Json;
//using NuGet;
using Rock;
using Rock.Attribute;
using Rock.Data;
using Rock.Lava;
using Rock.Model;
using Rock.Security;
using Rock.SystemKey;
using Rock.Utility;
using Rock.Web;
using Rock.Web.Cache;
using Rock.Web.UI;
using Rock.Web.UI.Controls;
namespace RockWeb.Blocks.Connection
{
/// <summary>
/// Connect Request Board
/// </summary>
[DisplayName( "Connection Request Board" )]
[Category( "Connection" )]
[Description( "Display the Connection Requests for a selected Connection Opportunity as a list or board view." )]
#region Block Attributes
[IntegerField(
"Max Cards per Column",
DefaultIntegerValue = DefaultMaxCards,
Description = "The maximum number of cards to display per column. This is to prevent performance issues caused by rendering too many cards at a time.",
Key = AttributeKey.MaxCards,
IsRequired = true,
Order = 0 )]
[LinkedPage(
"Person Profile Page",
Description = "Page used for viewing a person's profile. If set a view profile button will show for each grid item.",
Order = 2,
Key = AttributeKey.PersonProfilePage,
DefaultValue = Rock.SystemGuid.Page.PERSON_PROFILE_PERSON_PAGES )]
[LinkedPage(
"Workflow Detail Page",
Description = "Page used to display details about a workflow.",
Order = 3,
Key = AttributeKey.WorkflowDetailPage,
DefaultValue = Rock.SystemGuid.Page.WORKFLOW_DETAIL )]
[LinkedPage(
"Workflow Entry Page",
Description = "Page used to launch a new workflow of the selected type.",
Order = 4,
Key = AttributeKey.WorkflowEntryPage,
DefaultValue = Rock.SystemGuid.Page.WORKFLOW_ENTRY )]
[CodeEditorField(
"Status Template",
Description = "Lava Template that can be used to customize what is displayed in the status bar. Includes common merge fields plus ConnectionOpportunities, ConnectionTypes and the default IdleTooltip.",
EditorMode = CodeEditorMode.Lava,
EditorTheme = CodeEditorTheme.Rock,
DefaultValue = StatusTemplateDefaultValue,
Order = 5,
Key = AttributeKey.StatusTemplate )]
[CodeEditorField(
"Connection Request Status Icons Template",
Description = "Lava Template that can be used to customize what is displayed for the status icons in the connection request grid.",
EditorMode = CodeEditorMode.Lava,
EditorTheme = CodeEditorTheme.Rock,
DefaultValue = ConnectionRequestStatusIconsTemplateDefaultValue,
Key = AttributeKey.ConnectionRequestStatusIconsTemplate,
Order = 6 )]
[LinkedPage(
"Group Detail Page",
Description = "Page used to display group details.",
IsRequired = true,
DefaultValue = Rock.SystemGuid.Page.GROUP_VIEWER,
Order = 8,
Key = AttributeKey.GroupDetailPage )]
[LinkedPage(
"SMS Link Page",
Description = "Page that will be linked for SMS enabled phones.",
Order = 9,
IsRequired = true,
DefaultValue = Rock.SystemGuid.Page.NEW_COMMUNICATION,
Key = AttributeKey.SmsLinkPage )]
[BadgesField(
"Badges",
Description = "The badges to display in this block.",
IsRequired = false,
Order = 10,
Key = AttributeKey.Badges )]
[CodeEditorField(
"Lava Heading Template",
IsRequired = false,
Key = AttributeKey.LavaHeadingTemplate,
EditorMode = CodeEditorMode.Lava,
Description = "The HTML Content to render above the person’s name. Includes merge fields ConnectionRequest and Person. <span class='tip tip-lava'></span>",
Order = 11 )]
[CodeEditorField(
"Lava Badge Bar",
IsRequired = false,
Key = AttributeKey.LavaBadgeBar,
EditorMode = CodeEditorMode.Lava,
Description = "The HTML Content intended to be used as a kind of custom badge bar for the connection request. Includes merge fields ConnectionRequest and Person. <span class='tip tip-lava'></span>",
Order = 12 )]
[ConnectionTypesField(
"Connection Types",
Description = "Optional list of connection types to limit the display to (All will be displayed by default).",
IsRequired = false,
Order = 13,
Key = AttributeKey.ConnectionTypes )]
[BooleanField(
"Limit to Assigned Connections",
DefaultBooleanValue = false,
Description = "When enabled, only requests assigned to the current person will be shown.",
Key = AttributeKey.OnlyShowAssigned,
IsRequired = true,
Order = 14 )]
[LinkedPage(
"Connection Request History Page",
Description = "Page used to display history details.",
IsRequired = true,
DefaultValue = Rock.SystemGuid.Page.GROUP_VIEWER,
Order = 15,
Key = AttributeKey.ConnectionRequestHistoryPage )]
[LinkedPage(
"Bulk Update Requests",
Description = "Page used to update selected connection requests",
IsRequired = true,
DefaultValue = Rock.SystemGuid.Page.CONNECTION_REQUESTS_BULK_UPDATE,
Order = 16,
Key = AttributeKey.BulkUpdateRequestsPage )]
#endregion Block Attributes
[ContextAware( typeof( Person ), IsConfigurable = false )]
[Rock.SystemGuid.BlockTypeGuid( "28DBE708-E99B-4879-A64D-656C030D25B5" )]
public partial class ConnectionRequestBoard : ContextEntityBlock
{
/*
Root State Diagram:
- Card mode
- Grid mode
Request Modal State Diagram (Either of the root states allows selecting a request, which opens the request modal):
- View Mode
- Activity grid (default)
- Add activity
- Transfer
- Add/Edit Mode
*/
#region Fields
private List<ConnectionRequestViewModelWithModel> _ConnectionRequestViewModelsWithFullModel;
private bool _isExporting = false;
#endregion
#region Properties
/// <summary>
/// Gets or sets the available attributes.
/// </summary>
/// <value>
/// The available attributes.
/// </value>
public List<AttributeCache> AvailableAttributes { get; set; }
#endregion
#region Defaults
/// <summary>
/// The default maximum cards per status column
/// </summary>
private const int DefaultMaxCards = 100;
/// <summary>
/// The initial count of activities to show in grid. There is a "show more" button for the user to
/// click if the actual count exceeds this.
/// </summary>
private const int InitialActivitiesToShowInGrid = 10;
private const string StatusTemplateDefaultValue = @"
<div class='pull-left badge-legend padding-r-md'>
<span class='pull-left badge badge-info badge-circle js-legend-badge' data-toggle='tooltip' data-original-title='Assigned To You'><span class='sr-only'>Assigned To You</span></span>
<span class='pull-left badge badge-warning badge-circle js-legend-badge' data-toggle='tooltip' data-original-title='Unassigned Item'><span class='sr-only'>Unassigned Item</span></span>
<span class='pull-left badge badge-critical badge-circle js-legend-badge' data-toggle='tooltip' data-original-title='Critical Status'><span class='sr-only'>Critical Status</span></span>
<span class='pull-left badge badge-danger badge-circle js-legend-badge' data-toggle='tooltip' data-original-title='{{ IdleTooltip }}'><span class='sr-only'>{{ IdleTooltip }}</span></span>
</div>";
private const string ConnectionRequestStatusIconsTemplateDefaultValue = @"
<div class='board-card-pills'>
{% if ConnectionRequestStatusIcons.IsAssignedToYou %}
<span class='board-card-pill badge-info js-legend-badge' data-toggle='tooltip' data-original-title='Assigned To You'><span class='sr-only'>Assigned To You</span></span>
{% endif %}
{% if ConnectionRequestStatusIcons.IsUnassigned %}
<span class='board-card-pill badge-warning js-legend-badge' data-toggle='tooltip' data-original-title='Unassigned'><span class='sr-only'>Unassigned</span></span>
{% endif %}
{% if ConnectionRequestStatusIcons.IsCritical %}
<span class='board-card-pill badge-critical js-legend-badge' data-toggle='tooltip' data-original-title='Critical'><span class='sr-only'>Critical</span></span>
{% endif %}
{% if ConnectionRequestStatusIcons.IsIdle %}
<span class='board-card-pill badge-danger js-legend-badge' data-toggle='tooltip' data-original-title='{{ IdleTooltip }}'><span class='sr-only'>{{ IdleTooltip }}</span></span>
{% endif %}
</div>
";
private const string DefaultDelimiter = "|";
#endregion Defaults
#region Keys
/// <summary>
/// Keys to use for Page Parameters
/// </summary>
private static class PageParameterKey
{
public const string WorkflowId = "WorkflowId";
public const string ConnectionRequestId = "ConnectionRequestId";
public const string ConnectionRequestGuid = "ConnectionRequestGuid";
public const string ConnectionOpportunityId = "ConnectionOpportunityId";
public const string CampusId = "CampusId";
public const string ConnectionTypeId = "ConnectionTypeId";
public const string EntitySetId = "EntitySetId";
}
/// <summary>
/// Attribute Key
/// </summary>
private static class AttributeKey
{
public const string OnlyShowAssigned = "OnlyShowAssigned";
public const string ConnectionTypes = "ConnectionTypes";
public const string GroupDetailPage = "GroupDetailPage";
public const string SmsLinkPage = "SmsLinkPage";
public const string Badges = "Badges";
public const string LavaBadgeBar = "LavaBadgeBar";
public const string LavaHeadingTemplate = "LavaHeadingTemplate";
public const string MaxCards = "MaxCards";
public const string ConnectionRequestStatusIconsTemplate = "ConnectionRequestStatusIconsTemplate";
public const string PersonProfilePage = "PersonProfilePage";
public const string WorkflowDetailPage = "WorkflowDetailPage";
public const string WorkflowEntryPage = "WorkflowEntryPage";
public const string StatusTemplate = "StatusTemplate";
public const string ConnectionRequestHistoryPage = "ConnectionRequestHistoryPage";
public const string BulkUpdateRequestsPage = "BulkUpdateRequestsPage";
}
/// <summary>
/// User Preference Key
/// </summary>
private static class UserPreferenceKey
{
/// <summary>
/// The sort by
/// </summary>
public const string SortBy = "SortBy";
/// <summary>
/// The campus filter
/// </summary>
public const string CampusFilter = "CampusFilter";
/// <summary>
/// The view mode
/// </summary>
public const string ViewMode = "ViewMode";
/// <summary>
/// Connector Person Alias Id
/// </summary>
public const string ConnectorPersonAliasId = "ConnectorPersonAliasId";
/// <summary>
/// The connection opportunity identifier
/// </summary>
public const string ConnectionOpportunityId = "ConnectionOpportunityId";
}
/// <summary>
/// Filter Key
/// </summary>
private static class FilterKey
{
/// <summary>
/// Date Range
/// </summary>
public const string DateRange = "DateRange";
/// <summary>
/// Requester
/// </summary>
public const string Requester = "Requester";
/// <summary>
/// The statuses
/// </summary>
public const string Statuses = "Statuses";
/// <summary>
/// The states
/// </summary>
public const string States = "States";
/// <summary>
/// The last activities
/// </summary>
public const string LastActivities = "LastActivities";
/// <summary>
/// Only show future follow-up that are past due
/// </summary>
public const string PastDueOnly = "PastDueOnly";
}
/// <summary>
/// Keys to use for ViewState.
/// </summary>
private class ViewStateKey
{
public const string ConnectionOpportunityId = "ConnectionOpportunityId";
public const string ConnectionRequestId = "ConnectionRequestId";
public const string CurrentActivityId = "CurrentActivityId";
public const string ViewAllActivities = "ViewAllActivities";
public const string IsRequestModalAddEditMode = "IsRequestModalAddEditMode";
public const string ConnectorPersonAliasId = "ConnectorPersonAliasId";
public const string IsCardViewMode = "IsCardViewMode";
public const string RequestModalSubMode = "RequestModalSubMode";
public const string CurrentSortProperty = "CurrentSortProperty";
public const string CampusId = "CampusId";
public const string AvailableAttributeIds = "AvailableAttributeIds";
}
#endregion Keys
#region ViewState Properties
/// <summary>
/// Gets or sets the connection opportunity identifier.
/// </summary>
/// <value>
/// The connection opportunity identifier.
/// </value>
protected int? ConnectionOpportunityId
{
get
{
return ViewState[ViewStateKey.ConnectionOpportunityId].ToStringSafe().AsIntegerOrNull();
}
set
{
var currentValue = ConnectionOpportunityId;
if ( currentValue != value )
{
ViewState[ViewStateKey.ConnectionOpportunityId] = value;
var preferences = GetBlockPersonPreferences();
preferences.SetValue( UserPreferenceKey.ConnectionOpportunityId, value.ToStringSafe() );
preferences.Save();
}
}
}
/// <summary>
/// Gets or sets the connection request identifier.
/// </summary>
private int? ConnectionRequestId
{
get
{
return ViewState[ViewStateKey.ConnectionRequestId].ToStringSafe().AsIntegerOrNull();
}
set
{
ViewState[ViewStateKey.ConnectionRequestId] = value;
}
}
/// <summary>
/// Gets or sets the current activity identifier.
/// </summary>
private int? CurrentActivityId
{
get
{
return ViewState[ViewStateKey.CurrentActivityId].ToStringSafe().AsIntegerOrNull();
}
set
{
ViewState[ViewStateKey.CurrentActivityId] = value;
}
}
/// <summary>
/// Gets or sets the connection request identifier view all activities.
/// </summary>
private bool ViewAllActivities
{
get
{
return ViewState[ViewStateKey.ViewAllActivities].ToStringSafe().AsBoolean();
}
set
{
ViewState[ViewStateKey.ViewAllActivities] = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether this instance is request modal add edit mode.
/// </summary>
private bool IsRequestModalAddEditMode
{
get
{
return ViewState[ViewStateKey.IsRequestModalAddEditMode].ToStringSafe().AsBoolean();
}
set
{
ViewState[ViewStateKey.IsRequestModalAddEditMode] = value;
}
}
/// <summary>
/// Connector Person Alias Id
/// </summary>
private int? ConnectorPersonAliasId
{
get
{
if ( OnlyShowAssigned() )
{
return CurrentPersonAliasId;
}
return ViewState[ViewStateKey.ConnectorPersonAliasId].ToStringSafe().AsIntegerOrNull();
}
set
{
ViewState[ViewStateKey.ConnectorPersonAliasId] = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether this instance is card view mode.
/// </summary>
private bool IsCardViewMode
{
get
{
var valueToReturn = ViewState[ViewStateKey.IsCardViewMode].ToStringSafe().AsBooleanOrNull();
if ( !valueToReturn.HasValue )
{
var connectionType = GetConnectionType();
valueToReturn = connectionType == null ?
true :
connectionType.DefaultView == ConnectionTypeViewMode.Board;
}
return valueToReturn.Value;
}
set
{
ViewState[ViewStateKey.IsCardViewMode] = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether this instance is modal add mode.
/// </summary>
private string RequestModalViewModeSubMode
{
get
{
return ViewState[ViewStateKey.RequestModalSubMode].ToStringSafe();
}
set
{
ViewState[ViewStateKey.RequestModalSubMode] = value;
}
}
private const string RequestModalViewModeSubMode_View = "View";
private const string RequestModalViewModeSubMode_AddEditActivity = "AddEditActivity";
private const string RequestModalViewModeSubMode_Transfer = "Transfer";
private const string RequestModalViewModeSubMode_TransferSearch = "TransferSearch";
/// <summary>
/// Gets or sets the current sort property.
/// </summary>
private ConnectionRequestViewModelSortProperty CurrentSortProperty
{
get
{
var value = ViewState[ViewStateKey.CurrentSortProperty].ToStringSafe();
ConnectionRequestViewModelSortProperty sortProperty;
if ( !value.IsNullOrWhiteSpace() && Enum.TryParse( value, out sortProperty ) )
{
return sortProperty;
}
return ConnectionRequestViewModelSortProperty.Order;
}
set
{
ViewState[ViewStateKey.CurrentSortProperty] = value;
}
}
/// <summary>
/// Gets or sets the campus identifier.
/// </summary>
private int? CampusId
{
get
{
return ViewState[ViewStateKey.CampusId].ToStringSafe().AsIntegerOrNull();
}
set
{
ViewState[ViewStateKey.CampusId] = value;
}
}
#endregion ViewState Properties
#region Base Control Methods
/// <summary>
/// Restores the view-state information from a previous user control request that was saved by the <see cref="M:System.Web.UI.UserControl.SaveViewState" /> method.
/// </summary>
/// <param name="savedState">An <see cref="T:System.Object" /> that represents the user control state to be restored.</param>
protected override void LoadViewState( object savedState )
{
base.LoadViewState( savedState );
if ( ViewState[ViewStateKey.AvailableAttributeIds] != null )
{
AvailableAttributes = ( ViewState[ViewStateKey.AvailableAttributeIds] as int[] ).Select( a => AttributeCache.Get( a ) ).ToList();
}
AddDynamicControls();
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Init" /> event.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs" /> object that contains the event data.</param>
protected override void OnInit( EventArgs e )
{
base.OnInit( e );
RockPage.AddScriptLink( "~/Scripts/dragula.min.js" );
RockPage.AddScriptLink( "~/Scripts/Rock/Controls/ConnectionRequestBoard/connectionRequestBoard.js" );
RockPage.AddCSSLink( "~/Themes/Rock/Styles/connection-request.css", true );
// this event gets fired after block settings are updated. it's nice to repaint the screen if these settings would alter it
BlockUpdated += Block_BlockUpdated;
AddConfigurationUpdateTrigger( upnlRoot );
// Hide default bulk update and replace with custom action.
gRequests.Actions.ShowBulkUpdate = false;
// Add a custom button with an EventHandler for bulk updates.
// Note that the EventHandler is used to handle the navigation event, but the Route is needed
// to determine the security level associated with this action.
var bulkUpdatePageReference = new PageReference( GetAttributeValue( AttributeKey.BulkUpdateRequestsPage ) );
var customActionConfigEventButton = new CustomActionConfigEvent
{
IconCssClass = "fa fa-truck fa-fw",
HelpText = "Update selected requests",
EventHandler = LbUpdateConnections_Click,
Route = bulkUpdatePageReference.Route
};
gRequests.Actions.AddCustomActionBlockButton( customActionConfigEventButton );
// Configure the badge types
var delimitedBadgeGuids = GetAttributeValue( AttributeKey.Badges );
if ( delimitedBadgeGuids.IsNullOrWhiteSpace() )
{
pnlRequestModalViewModeBadges.Visible = false;
return;
}
// The badge bar relies on the Context Entity to render the badges
Entity = GetRequesterPerson();
// Add the badge types to the badge list control
var badgeTypes = delimitedBadgeGuids.SplitDelimitedValues()
.AsGuidList()
.Select( BadgeCache.Get )
.Where( b => b != null ) // exclude the badge in case it has been deleted
.ToList();
blRequestModalViewModeBadges.BadgeTypes.AddRange( badgeTypes );
pnlRequestModalViewModeBadges.Visible = true;
}
private void LbUpdateConnections_Click( object sender, EventArgs e )
{
var selectedItems = new List<int>();
if ( gRequests.SelectedKeys.Count == 0 )
{
foreach ( var dataKeyObject in gRequests.DataKeys )
{
var dataKey = dataKeyObject as DataKey;
var selectedItem = dataKey?.Value?.ToString()?.AsIntegerOrNull();
if ( selectedItem.HasValue )
{
selectedItems.Add( selectedItem.Value );
}
}
}
else
{
gRequests.SelectedKeys.ToList().ForEach( k => selectedItems.Add( k.ToString().AsInteger() ) );
}
if ( selectedItems.Count == 0 )
{
gRequests.ShowModalAlertMessage( "Grid has no Connection Requests", ModalAlertType.Warning );
}
else
{
var connectionType = GetConnectionType();
int entitySetId = GetEntitySetId( selectedItems );
int connectionTypeId = connectionType.Id;
var queryParams = new Dictionary<string, string>()
{
{ PageParameterKey.ConnectionTypeId, connectionTypeId.ToString() },
{ PageParameterKey.EntitySetId, entitySetId.ToString() }
};
NavigateToLinkedPage( AttributeKey.BulkUpdateRequestsPage, queryParams );
}
}
private int GetEntitySetId( List<int> ids )
{
var rockContext = new RockContext();
var entitySet = new EntitySet();
entitySet.EntityTypeId = gRequests.EntityTypeId ?? ConnectionRequestEntityTypeId;
entitySet.ExpireDateTime = RockDateTime.Now.AddDays( 1 );
var service = new EntitySetService( rockContext );
service.Add( entitySet );
rockContext.SaveChanges();
List<EntitySetItem> entitySetItems = new List<EntitySetItem>();
foreach ( var id in ids )
{
var item = new EntitySetItem();
item.EntitySetId = entitySet.Id;
item.EntityId = id;
entitySetItems.Add( item );
}
rockContext.BulkInsert( entitySetItems );
return entitySet.Id;
}
/// <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 )
{
if ( !Page.IsPostBack )
{
LoadSettings();
BindUI();
}
else
{
var causingControlClientId = Request["__EVENTTARGET"].ToStringSafe();
var causingControl = Page.FindControl( causingControlClientId );
if ( causingControlClientId == lbJavaScriptCommand.ClientID )
{
// Handle commands that are sent via JavaScript
ProcessJavaScriptCommand();
}
else if ( causingControl != null &&
( causingControl == ppRequesterFilter || causingControl.Parent == ppRequesterFilter ) )
{
// If the postback comes from the filter drawer, don't modify the filter drawer CSS,
// which dictates if the drawer is open or closed
divFilterDrawer.Style.Clear();
}
}
base.OnLoad( e );
}
/// <summary>
/// Handles the BlockUpdated event of the 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 Block_BlockUpdated( object sender, EventArgs e )
{
// Refresh the page on board attribute settings changes. This is done because of the unique challenges of
// resetting all of the JavaScript events on this block.
NavigateToCurrentPage( QueryParameters().ToDictionary( kvp => kvp.Key, kvp => kvp.Value.ToString() ) );
}
/// <summary>
/// Saves any user control view-state changes that have occurred since the last page postback.
/// </summary>
/// <returns>
/// Returns the user control's current view state. If there is no view state associated with the control, it returns null.
/// </returns>
protected override object SaveViewState()
{
var jsonSetting = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
ContractResolver = new Rock.Utility.IgnoreUrlEncodedKeyContractResolver()
};
ViewState[ViewStateKey.AvailableAttributeIds] = AvailableAttributes == null ? null : AvailableAttributes.Select( a => a.Id ).ToArray();
return base.SaveViewState();
}
#endregion Base Control Methods
#region JavaScript Postbacks
/// <summary>
/// Processes the JavaScript event.
/// </summary>
private void ProcessJavaScriptCommand()
{
var argument = Request["__EVENTARGUMENT"].ToStringSafe();
string action;
int? newStatusId;
int? requestId;
int? newIndex;
/*
4/30/2024 - JPH
This is an unconventional way of handling a grid row's delete action, but this
block has a pretty complex combination of child controls (to make up its "board"
and "list" view modes), which leads to a complicated control lifecycle. This fix
will properly support deletes from list view mode until we rewrite the block
using the new, Obsidian framework.
Reason: [Sometimes] Can't delete requests in list view.
https://github.com/SparkDevNetwork/Rock/issues/5720
*/
ParseDeleteEventArgument( argument, out requestId );
if ( requestId.HasValue )
{
DeleteGridRequest( requestId.Value );
return;
}
ParseDragEventArgument( argument, out action, out newStatusId, out requestId, out newIndex );
if ( action == "on-following-change" )
{
BindFavoriteConnectionOpportunities();
BindConnectionTypesRepeater();
return;
}
if ( !requestId.HasValue )
{
return;
}
ConnectionRequestId = requestId.Value;
if ( action == "view" )
{
ViewAllActivities = false;
IsRequestModalAddEditMode = false;
RequestModalViewModeSubMode = RequestModalViewModeSubMode_View;
ShowRequestModal();
return;
}
if ( action == "connect" )
{
MarkRequestConnected();
RefreshRequestCard();
return;
}
if ( action == "card-drop-confirmed" )
{
if ( newStatusId.HasValue && newIndex.HasValue )
{
ProcessConfirmedDragEvent( newStatusId.Value, newIndex.Value );
}
return;
}
}
/// <summary>
/// Parses the delete event.
/// </summary>
/// <param name="argument">The argument.</param>
/// <param name="requestId">The request identifier.</param>
private void ParseDeleteEventArgument( string argument, out int? requestId )
{
requestId = null;
var segments = argument.SplitDelimitedValues();
if ( segments.Length == 2 && segments[0] == "on-request-delete" )
{
requestId = segments[1].AsIntegerOrNull();
}
}
/// <summary>
/// Processes the confirmed drag event.
/// </summary>
/// <param name="newStatusId">The new status identifier.</param>
/// <param name="newIndex">The new index.</param>
private void ProcessConfirmedDragEvent( int newStatusId, int newIndex )
{
var rockContext = new RockContext();
var service = new ConnectionRequestService( rockContext );
var request = service.Get( ConnectionRequestId.Value );
if ( request == null )
{
return;
}
var oldStatusId = request.ConnectionStatusId;
var oldIndex = request.Order;
// Update the dragged request to the new status.
request.ConnectionStatusId = newStatusId;
// Reordering is only allowed when the cards are sorted by order
if ( CurrentSortProperty == ConnectionRequestViewModelSortProperty.Order )
{
request.Order = newIndex;
var requestsOfStatus = service.Queryable()
.Where( r =>
r.ConnectionStatusId == newStatusId &&
r.ConnectionOpportunityId == request.ConnectionOpportunityId &&
r.Id != ConnectionRequestId.Value );
// A Connection Request is moved to a new status.
if ( oldStatusId != newStatusId)
{
var requestsOfOldStatus = service.Queryable()
.Where( r =>
r.ConnectionStatusId == oldStatusId &&
r.ConnectionOpportunityId == request.ConnectionOpportunityId &&
r.Order > oldIndex );
rockContext.BulkUpdate( requestsOfOldStatus, r => new ConnectionRequest { Order = r.Order - 1, ModifiedDateTime = r.ModifiedDateTime } );
IncrementRequestOrder( requestsOfStatus, newIndex, rockContext );
}
// A Connection Request remained in its original status and was moved up in order.
else if ( newIndex < oldIndex )
{
IncrementRequestOrder( requestsOfStatus, newIndex, rockContext );
}
// A Connection Request remained in its original status and was moved down in order.
else
{
requestsOfStatus = requestsOfStatus.Where( r => r.Order > oldIndex && r.Order <= newIndex );
rockContext.BulkUpdate( requestsOfStatus, r => new ConnectionRequest { Order = r.Order - 1, ModifiedDateTime = r.ModifiedDateTime } );
}
}
else if ( oldStatusId != newStatusId )
{
MaintainRequestOrder( request, service, oldIndex, oldStatusId, newStatusId, rockContext );
}
rockContext.SaveChanges();
}
/// <summary>
/// Increments the order of selected connection requests from a status.
/// </summary>
/// <param name="requestsOfStatus">The selected connection requests from a status.</param>
/// <param name="newIndex">The new index of the dropped connection request.</param>
/// <param name="rockContext">The rock context.</param>
private void IncrementRequestOrder( IQueryable<ConnectionRequest> requestsOfStatus, int newIndex, RockContext rockContext )
{
requestsOfStatus = requestsOfStatus.Where( r => r.Order >= newIndex );
rockContext.BulkUpdate( requestsOfStatus, r => new ConnectionRequest { Order = r.Order + 1, ModifiedDateTime = r.ModifiedDateTime } );
}
/// <summary>
/// Maintains the order of connection requests when they are moved to a new status and are not sorted by order.
/// </summary>
/// <param name="request">The moved connection request.</param>
/// <param name="service">The connection request service.</param>
/// <param name="oldIndex">The old order index of the moved connection request.</param>
/// <param name="oldStatusId">The old connection status id that the connection request was moved from.</param>
/// <param name="newStatusId">The new connection status id that the connection request was moved to.</param>
/// <param name="rockContext">The rock context.</param>
private void MaintainRequestOrder( ConnectionRequest request, ConnectionRequestService service, int oldIndex, int oldStatusId, int newStatusId, RockContext rockContext )
{
request.Order = 0;
var requestsOfOldStatus = service.Queryable()
.Where( r =>
r.ConnectionStatusId == oldStatusId &&
r.ConnectionOpportunityId == request.ConnectionOpportunityId &&
r.Order > oldIndex );
var requestsOfStatus = service.Queryable()
.Where( r =>
r.ConnectionStatusId == newStatusId &&
r.ConnectionOpportunityId == request.ConnectionOpportunityId &&
r.Id != ConnectionRequestId.Value );
rockContext.BulkUpdate( requestsOfOldStatus, r => new ConnectionRequest { Order = r.Order - 1, ModifiedDateTime = r.ModifiedDateTime } );
IncrementRequestOrder( requestsOfStatus, 0, rockContext );
}
/// <summary>
/// Parses the drag event.
/// </summary>
/// <param name="argument">The argument.</param>
/// <param name="action">The action.</param>
/// <param name="newStatusId">The new status identifier.</param>
/// <param name="requestId">The request identifier.</param>
/// <param name="newIndex">The new index.</param>
private void ParseDragEventArgument( string argument, out string action, out int? newStatusId, out int? requestId, out int? newIndex )
{
var segments = argument.SplitDelimitedValues();
action = segments.Length >= 1 ? segments[0].ToLower() : string.Empty;
requestId = segments.Length >= 2 ? segments[1].AsIntegerOrNull() : null;
newStatusId = segments.Length >= 3 ? segments[2].AsIntegerOrNull() : null;
newIndex = segments.Length >= 4 ? segments[3].AsIntegerOrNull() : null;
}
#endregion JavaScript Postbacks