1- using Newtonsoft . Json ;
2- using PerplexUmbraco . Forms . Code ;
3- using System ;
4- using System . Collections . Generic ;
5- using System . IO ;
1+ using System ;
62using System . Linq ;
73using System . Net . Http . Formatting ;
8- using System . Text ;
9- using System . Threading . Tasks ;
104using System . Web ;
11- using System . Web . Hosting ;
12- using System . Web . Http ;
13- using Umbraco . Forms . Data ;
5+
6+ using PerplexUmbraco . Forms . Code ;
7+ using PerplexUmbraco . Forms . Code . Configuration ;
8+
9+ using umbraco ;
10+ using umbraco . BusinessLogic . Actions ;
1411using Umbraco . Forms . Web . Trees ;
12+ using Umbraco . Web ;
1513using Umbraco . Web . Models . Trees ;
1614using Umbraco . Web . Mvc ;
1715using Umbraco . Web . Trees ;
18- using Umbraco . Web ;
19- using umbraco . BusinessLogic . Actions ;
20- using umbraco ;
2116
2217namespace PerplexUmbraco . Forms . Controllers
2318{
@@ -41,27 +36,61 @@ public class PerplexFormTreeController : FormTreeController
4136 {
4237 // We load our custom menu actions from our own folder
4338 private const string VIEWS_ROOT = "/App_Plugins/PerplexUmbracoForms/views/" ;
39+ private readonly PerplexCacheConfig _cacheConfig ;
4440
45- public PerplexFormTreeController ( ) { }
41+ public PerplexFormTreeController ( )
42+ {
43+ _cacheConfig = PerplexUmbracoFormsConfig . Get . PerplexCacheConfig ;
44+ }
4645
47- protected override Umbraco . Web . Models . Trees . TreeNodeCollection GetTreeNodes ( string id , System . Net . Http . Formatting . FormDataCollection queryStrings )
46+ protected override TreeNodeCollection GetTreeNodes ( string id , FormDataCollection queryStrings )
4847 {
48+ var cacheKey = $ "PerplexFormTreeController_GetTreeNodes_id:{ queryStrings [ "id" ] } ";
49+ var rtCache = ApplicationContext . ApplicationCache . RuntimeCache ;
50+
4951 // If this is a form, use Umbraco's default behavior
5052 var folder = PerplexFolder . Get ( id ) ;
5153 if ( folder == null )
5254 {
53- return base . GetTreeNodes ( id , queryStrings ) ;
55+ var treeNodeCollection = _cacheConfig . EnableCache ? ( TreeNodeCollection ) rtCache . GetCacheItem ( cacheKey ) : ( TreeNodeCollection ) null ;
56+
57+ if ( treeNodeCollection == null )
58+ {
59+ treeNodeCollection = base . GetTreeNodes ( id , queryStrings ) ;
60+
61+ if ( _cacheConfig . EnableCache )
62+ {
63+ if ( rtCache . GetCacheItemsByKeySearch ( cacheKey ) . Any ( ) )
64+ rtCache . ClearCacheByKeySearch ( cacheKey ) ;
65+
66+ rtCache . InsertCacheItem ( cacheKey , ( ) => treeNodeCollection , new TimeSpan ( 0 , _cacheConfig . CacheDurationInMinutes , 0 ) , true ) ;
67+ }
68+ }
69+
70+ return treeNodeCollection ;
5471 }
5572
5673 // This is a folder
74+ var baseTreeNodes = _cacheConfig . EnableCache ? ( TreeNodeCollection ) rtCache . GetCacheItem ( cacheKey ) : ( TreeNodeCollection ) null ;
75+
76+ if ( baseTreeNodes == null )
77+ {
78+ // We require all forms, and apply filtering based on folders later
79+ baseTreeNodes = base . GetTreeNodes ( "-1" , queryStrings ) ;
5780
58- // We require all forms, and apply filtering based on folders later
59- var baseTreeNodes = base . GetTreeNodes ( "-1" , queryStrings ) ;
81+ if ( _cacheConfig . EnableCache )
82+ {
83+ if ( rtCache . GetCacheItemsByKeySearch ( cacheKey ) . Any ( ) )
84+ rtCache . ClearCacheByKeySearch ( cacheKey ) ;
85+
86+ rtCache . InsertCacheItem ( cacheKey , ( ) => baseTreeNodes , new TimeSpan ( 0 , _cacheConfig . CacheDurationInMinutes , 0 ) , true ) ;
87+ }
88+ }
6089
6190 // Sanity check; make sure there are no orphan forms around
6291 // (forms not contained within any folder). If so, move them to the root folder
6392 var orphans = baseTreeNodes . Where ( n => PerplexFolder . Get ( f => f . Forms . Any ( formId => formId == n . Id . ToString ( ) ) ) == null ) . ToList ( ) ;
64- if ( orphans . Count > 0 )
93+ if ( orphans . Count > 0 )
6594 {
6695 foreach ( var orphan in orphans )
6796 {
@@ -74,7 +103,7 @@ protected override Umbraco.Web.Models.Trees.TreeNodeCollection GetTreeNodes(stri
74103 // Hide all Forms that are not contained within this folder
75104 // If this folder itself is disabled (due to the user not having access),
76105 // we also hide all its forms
77- baseTreeNodes . RemoveAll ( n =>
106+ baseTreeNodes . RemoveAll ( n =>
78107 ! folder . Forms . Contains ( n . Id . ToString ( ) ) ||
79108 ( folder . Disabled && folder . Forms . Contains ( n . Id . ToString ( ) ) )
80109 ) ;
@@ -92,7 +121,7 @@ protected override Umbraco.Web.Models.Trees.TreeNodeCollection GetTreeNodes(stri
92121
93122 // Add any subfolders of this node
94123 // We loop through the list in reverse as we add every folder at the start of the list (before forms)
95- foreach ( var subFolder in folder . Folders . Reverse < PerplexFolder > ( ) )
124+ foreach ( var subFolder in folder . Folders . Reverse ( ) )
96125 {
97126 // If this subfolder is disabled, and it is not on a path towards
98127 // a folder that is NOT disabled, it should not be listed at all.
@@ -139,7 +168,7 @@ protected override TreeNode CreateRootNode(FormDataCollection queryStrings)
139168
140169 // If none are set, all folders are allowed so we just use default behavior
141170 // Likewise if the common ancestors of all allowed folders is the root.
142- PerplexFolder commonAncestor = PerplexFolder . GetCommonAncestor ( startFolders ) ;
171+ PerplexFolder commonAncestor = PerplexFolder . GetCommonAncestor ( startFolders ) ;
143172
144173 if ( ! startFolders . Any ( ) || commonAncestor == PerplexFolder . GetRootFolder ( ) )
145174 {
@@ -157,11 +186,11 @@ protected override TreeNode CreateRootNode(FormDataCollection queryStrings)
157186 // if this is the common ancestor of this user's start nodes but not
158187 // a start node itself. In that case it should also show as disabled in
159188 // the UI, and we hide its URL.
160- rootNode . RoutePath = "forms/perplexForms/folder/" + commonAncestor . Id ;
161- if ( commonAncestor . Disabled )
189+ rootNode . RoutePath = "forms/perplexForms/folder/" + commonAncestor . Id ;
190+ if ( commonAncestor . Disabled )
162191 {
163192 rootNode . CssClasses . Add ( "disabled" ) ;
164- }
193+ }
165194
166195 // Folder has children if it has either forms or folders
167196 rootNode . HasChildren = commonAncestor . Forms . Any ( ) || commonAncestor . Folders . Any ( ) ;
@@ -197,10 +226,10 @@ protected override MenuItemCollection GetMenuForNode(string id, FormDataCollecti
197226 {
198227 menu . Items . RemoveAll ( m => m . Alias != ActionRefresh . Instance . Alias ) ;
199228 return menu ;
200- }
229+ }
201230
202231 // Create Form (default Umbraco view, hence alias)
203- AddMenuItem ( menu , "Create Form" , alias : "create" , icon : "icon icon-add" ) ;
232+ AddMenuItem ( menu , "Create Form" , alias : "create" , icon : "icon icon-add" ) ;
204233
205234 // Create Folder
206235 AddMenuItem ( menu , "Create Folder" , view : "createFolder" , icon : "icon icon-folder" ) ;
@@ -239,7 +268,7 @@ protected override MenuItemCollection GetMenuForNode(string id, FormDataCollecti
239268 var root = PerplexFolder . GetRootFolder ( ) ;
240269
241270 // If the root folder is disabled, remove all menu actions except Reload
242- if ( root . Disabled )
271+ if ( root . Disabled )
243272 {
244273 menu . Items . RemoveAll ( m => m . Alias != ActionRefresh . Instance . Alias ) ;
245274 return menu ;
0 commit comments