This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
/
FormulateTreeController.cs
254 lines (217 loc) · 9.25 KB
/
FormulateTreeController.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
namespace formulate.app.Trees
{
// Namespaces.
using Folders;
using Forms;
using formulate.app.Helpers;
using formulate.app.Trees.Helpers;
using Layouts;
using Persistence;
using Resolvers;
using System;
using System.Linq;
using System.Net.Http.Formatting;
using Umbraco.Core;
using Umbraco.Web.Models.Trees;
using Umbraco.Web.Mvc;
using Umbraco.Web.Trees;
using Validations;
using CoreConstants = Umbraco.Core.Constants;
using DataSourcesConstants = formulate.app.Constants.Trees.DataSources;
using DataValuesConstants = formulate.app.Constants.Trees.DataValues;
using FormsConstants = formulate.app.Constants.Trees.Forms;
using LayoutsConstants = formulate.app.Constants.Trees.Layouts;
using ValidationsConstants = formulate.app.Constants.Trees.Validations;
//TODO: Much to do in this file.
[Tree("formulate", "formulate", "Formulate", "icon-folder",
"icon-folder-open", true, sortOrder: 0)]
[PluginController("formulate")]
public class FormulateTreeController : TreeController
{
private IEntityPersistence Persistence { get; set; }
private FolderHelper FolderHelper { get; set; }
private FormHelper FormHelper { get; set; }
private LayoutHelper LayoutHelper { get; set; }
private ValidationHelper ValidationHelper { get; set; }
public FormulateTreeController()
{
Persistence = EntityPersistence.Current.Manager;
FolderHelper = new FolderHelper(Persistence, this);
FormHelper = new FormHelper(this, FolderHelper);
LayoutHelper = new LayoutHelper(this, FolderHelper);
ValidationHelper = new ValidationHelper(this, FolderHelper);
}
protected override MenuItemCollection GetMenuForNode(string id,
FormDataCollection queryStrings)
{
var menu = new MenuItemCollection();
var rootId = CoreConstants.System.Root.ToInvariantString();
var rootFormsId = GuidHelper.GetGuid(FormsConstants.Id);
var rootLayoutId = GuidHelper.GetGuid(LayoutsConstants.Id);
var rootValidationId = GuidHelper.GetGuid(ValidationsConstants.Id);
if (id.InvariantEquals(rootId))
{
// Do nothing. The root requires no actions.
}
else if (id.InvariantEquals(FormsConstants.Id))
{
// Actions for forms.
FormHelper.AddCreateFormAction(menu);
FolderHelper.AddCreateFolderAction(menu);
}
else if (id.InvariantEquals(LayoutsConstants.Id))
{
// Actions for layouts.
LayoutHelper.AddCreateLayoutAction(menu);
FolderHelper.AddCreateFolderAction(menu);
}
else if (id.InvariantEquals(ValidationsConstants.Id))
{
// Actions for validations.
ValidationHelper.AddCreateValidationAction(menu);
FolderHelper.AddCreateFolderAction(menu);
}
else
{
// Variables.
var entityId = GuidHelper.GetGuid(id);
var entity = Persistence.Retrieve(entityId);
var ancestorId = entity == null
? Guid.Empty
: entity.Path.First();
// What type of entity does the node represent?
if (entity is Form)
{
FormHelper.AddDeleteFormAction(menu);
}
else if (entity is Layout)
{
LayoutHelper.AddDeleteLayoutAction(menu);
}
else if (entity is Validation)
{
ValidationHelper.AddDeleteValidationAction(menu);
}
else if (entity is Folder)
{
FolderHelper.AddCreateFolderAction(menu);
if (ancestorId == rootFormsId)
{
FormHelper.AddCreateFormAction(menu, entityId);
}
else if (ancestorId == rootLayoutId)
{
LayoutHelper.AddCreateLayoutAction(menu, entityId);
}
else if (ancestorId == rootValidationId)
{
ValidationHelper.AddCreateValidationAction(
menu, entityId);
}
}
}
return menu;
}
protected override TreeNodeCollection GetTreeNodes(string id,
FormDataCollection queryStrings)
{
var entityId = GuidHelper.GetGuid(id);
var nodes = new TreeNodeCollection();
var rootId = CoreConstants.System.Root.ToInvariantString();
var rootFormsId = GuidHelper.GetGuid(FormsConstants.Id);
var rootLayoutsId = GuidHelper.GetGuid(LayoutsConstants.Id);
var rootValidationsId = GuidHelper.GetGuid(
ValidationsConstants.Id);
if (id.InvariantEquals(rootId))
{
// Get root nodes.
var formatUrl = "/formulate/formulate/{0}/info";
var hasRootForms = Persistence
.RetrieveChildren(rootFormsId).Any();
var formsNode = this.CreateTreeNode(FormsConstants.Id, id,
queryStrings, FormsConstants.Title,
FormsConstants.TreeIcon, hasRootForms,
string.Format(formatUrl, "forms"));
nodes.Add(formsNode);
var hasRootLayouts = Persistence
.RetrieveChildren(rootLayoutsId).Any();
var layoutsNode = this.CreateTreeNode(LayoutsConstants.Id,
id, queryStrings, LayoutsConstants.Title,
LayoutsConstants.TreeIcon, hasRootLayouts,
string.Format(formatUrl, "layouts"));
nodes.Add(layoutsNode);
var dataSourcesNode = this.CreateTreeNode(
DataSourcesConstants.Id, id, queryStrings,
DataSourcesConstants.Title, DataSourcesConstants.Icon,
false, string.Format(formatUrl, "dataSources"));
nodes.Add(dataSourcesNode);
var dataValuesNode = this.CreateTreeNode(
DataValuesConstants.Id, id, queryStrings,
DataValuesConstants.Title, DataValuesConstants.Icon,
false, string.Format(formatUrl, "dataValues"));
nodes.Add(dataValuesNode);
var hasRootValidations = Persistence
.RetrieveChildren(rootValidationsId).Any();
var validationsNode = this.CreateTreeNode(
ValidationsConstants.Id, id, queryStrings,
ValidationsConstants.Title, ValidationsConstants.TreeIcon,
hasRootValidations,
string.Format(formatUrl, "validationLibrary"));
nodes.Add(validationsNode);
}
else if (id.InvariantEquals(LayoutsConstants.Id))
{
// Get root nodes under layouts.
var entities = Persistence
.RetrieveChildren(rootLayoutsId);
LayoutHelper.AddLayoutChildrenToTree(nodes, queryStrings,
entities);
}
else if (id.InvariantEquals(ValidationsConstants.Id))
{
// Get root nodes under validations.
var entities = Persistence
.RetrieveChildren(rootValidationsId);
ValidationHelper.AddValidationChildrenToTree(
nodes, queryStrings, entities);
}
else if (id.InvariantEquals(FormsConstants.Id))
{
// Get root nodes under forms.
var entities = Persistence
.RetrieveChildren(rootFormsId);
FormHelper.AddFormChildrenToTree(nodes, queryStrings,
entities);
}
else
{
// Variables.
var entity = Persistence.Retrieve(entityId);
var ancestorId = entity.Path.First();
var children = Persistence
.RetrieveChildren(entityId);
// Add children of folder.
if (entity is Folder)
{
// Which subtree?
if (ancestorId == rootFormsId)
{
FormHelper.AddFormChildrenToTree(nodes, queryStrings,
children);
}
else if (ancestorId == rootLayoutsId)
{
LayoutHelper.AddLayoutChildrenToTree(nodes,
queryStrings, children);
}
else if (ancestorId == rootValidationsId)
{
ValidationHelper.AddValidationChildrenToTree(
nodes, queryStrings, children);
}
}
}
return nodes;
}
}
}