This commit is contained in:
Rick Falkvinge
2014-02-17 01:26:12 +00:00
parent 989f24c2b1
commit 2f1e2697f1
3 changed files with 106 additions and 4 deletions
@@ -1,5 +1,6 @@
<%@ Page Title="" Language="C#" MasterPageFile="~/Master-v5.master" AutoEventWireup="true" CodeFile="ChangeOrganizations.aspx.cs" Inherits="Swarmops.Frontend.Pages.Security.ChangeOrganizations" %>
<%@ Register TagPrefix="Swarmops5" TagName="ExternalScripts" Src="~/Controls/v5/UI/ExternalScripts.ascx" %>
<%@ Register TagPrefix="Swarmops5" TagName="ComboBudgets" Src="~/Controls/v5/Financial/ComboBudgets.ascx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderHead" Runat="Server">
<Swarmops5:ExternalScripts ID="ExternalScripts1" Package="easyui" Control="tree" runat="server" />
@@ -13,20 +14,22 @@
});
</script>
<link rel="stylesheet" type="text/css" href="/Style/v5-easyui-elements.css">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderMain" Runat="Server">
<div class="entryFields">
<asp:Label runat="server" ID="LabelCurrentOrganizationName" />&#8203;<br/>
<select class="easyui-combotree" url="Json-AccessibleOrganizationsTree.aspx" name="OrganizationId" id="OrganizationId" animate="true" style="width:300px"></select>
<span style="margin-left:12px"><asp:Label runat="server" ID="LabelCurrentOrganizationName" /></span>&nbsp;<br/>
<select class="easyui-combotree" url="Json-AccessibleOrganizationsTree.aspx" name="OrganizationId" id="OrganizationId" animate="true" style="width:300px" height="30"></select>
<asp:Button ID="ButtonSwitch" runat="server" CssClass="buttonAccentColor NoInputFocus" OnClick="ButtonSwitch_Click" Text="Switch"/>
</div>
<div class="entryLabels">
<asp:Label runat="server" ID="LabelCurrentOrganization" /><br/>
<asp:Label runat="server" ID="LabelNewOrganization" /><br/>
<asp:Label runat="server" ID="LabelNewOrganization" />
</div>
<div style="clear:both"></div></asp:Content>
<div style="clear:both"></div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PlaceHolderSide" Runat="Server">
</asp:Content>
@@ -0,0 +1 @@
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Json-AccessibleOrganizationsTree.aspx.cs" Inherits="Swarmops.Frontend.Pages.Security.AccessibleOrganizationsTree" %>
@@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Swarmops.Logic.Financial;
using Swarmops.Logic.Structure;
namespace Swarmops.Frontend.Pages.Security
{
public partial class AccessibleOrganizationsTree : DataV5Base
{
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "application/json";
/*
* ignore cache - reconstruct every time, this is too individual and volatile
*
// Is this stuff in cache already?
string cacheKey = "ExpensableBudgets-Json-" +
this.CurrentOrganization.Identity.ToString((CultureInfo.InvariantCulture));
string accountsJson =
(string) Cache[cacheKey];
if (accountsJson != null)
{
Response.Output.WriteLine(accountsJson);
Response.End();
return;
}
*/
// Not in cache. Construct.
// Get organizations
Organizations allOrganizations = Organizations.GetAll();
// List accessible organizations. That would be those where this person:
// a) has a role of responsibility;
// b) is a member or other form of minion;
// c) has a role of responsibility in a parent organization;
// d) has a system-wide role that makes it necessary to access orgs.
// Build tree (there should be a template for this)
Dictionary<int, Organizations> treeMap = new Dictionary<int, Organizations>();
foreach (Organization organization in allOrganizations)
{
if (!treeMap.ContainsKey(organization.ParentIdentity))
{
treeMap[organization.ParentIdentity] = new Organizations();
}
treeMap[organization.ParentIdentity].Add(organization);
}
Response.Output.WriteLine(RecurseTreeMap(treeMap, 0));
/* TODO: Cache, if master installation grows large
Cache.Insert(cacheKey, accountsJson, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero);
// cache lasts for five minutes, no sliding expiration
Response.Output.WriteLine(accountsJson); */
Response.End();
}
private string RecurseTreeMap(Dictionary<int, Organizations> treeMap, int nodeId)
{
List<string> elements = new List<string>();
foreach (Organization organization in treeMap[nodeId])
{
string element = string.Format("\"id\":{0},\"text\":\"{1}\"", organization.Identity,
JsonSanitize(organization.Name));
if (treeMap.ContainsKey(organization.Identity))
{
element += ",\"state\":\"closed\",\"children\":" + RecurseTreeMap(treeMap, organization.Identity);
}
elements.Add("{" + element + "}");
}
return "[" + String.Join(",", elements.ToArray()) + "]";
}
}
}