From a190e36fe8c15e83653dd87f1fb1199aa4dc9f13 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sat, 2 May 2015 01:33:35 +0200 Subject: [PATCH] Assigning / removing works everywhere in Positions --- Logic/Swarm/PositionAssignment.cs | 2 +- Logic/Swarm/PositionAssignments.cs | 27 +++++++++++++++++++ Site5/App_GlobalResources/Controls.Swarm.resx | 2 +- Site5/Automation/Json-PositionsTree.aspx.cs | 12 +++++++-- Site5/Automation/SwarmFunctions.aspx.cs | 16 +++++++++++ Site5/Controls/v5/Swarm/TreePositions.ascx | 21 ++++++++++++--- 6 files changed, 73 insertions(+), 7 deletions(-) diff --git a/Logic/Swarm/PositionAssignment.cs b/Logic/Swarm/PositionAssignment.cs index 9d61ef9c..c38dd32b 100644 --- a/Logic/Swarm/PositionAssignment.cs +++ b/Logic/Swarm/PositionAssignment.cs @@ -57,7 +57,7 @@ namespace Swarmops.Logic.Swarm { int geographyId = 0; - if (position.PositionLevel == PositionLevel.Geography) + if (position.PositionLevel == PositionLevel.Geography || position.PositionLevel == PositionLevel.GeographyDefault) { geographyId = geography.Identity; } diff --git a/Logic/Swarm/PositionAssignments.cs b/Logic/Swarm/PositionAssignments.cs index 5c5d844d..a9dc06c7 100644 --- a/Logic/Swarm/PositionAssignments.cs +++ b/Logic/Swarm/PositionAssignments.cs @@ -4,7 +4,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Swarmops.Basic.Types.Swarm; +using Swarmops.Common.Enums; using Swarmops.Database; +using Swarmops.Logic.Structure; using Swarmops.Logic.Support; namespace Swarmops.Logic.Swarm @@ -13,6 +15,31 @@ namespace Swarmops.Logic.Swarm { public static PositionAssignments ForPosition (Position position, bool includeTerminated = false) { + if (position.PositionLevel == PositionLevel.GeographyDefault || + position.PositionLevel == PositionLevel.Geography) + { + // a bit complex; we need to ask the database directly for these geographies + + if (!includeTerminated) + { + return + FromArray ( + SwarmDb.GetDatabaseForReading() + .GetPositionAssignments (position, position.Geography, + DatabaseCondition.ActiveTrue)); + } + + // otherwise, if asking for all assignments (even clsoed), remove "DatabaseCondition.ActiveTrue" + + return + FromArray( + SwarmDb.GetDatabaseForReading() + .GetPositionAssignments(position, position.Geography)); + + } + + // otherwise, global (geo-unbound) position + return ForPositions (Positions.FromSingle (position), includeTerminated); } diff --git a/Site5/App_GlobalResources/Controls.Swarm.resx b/Site5/App_GlobalResources/Controls.Swarm.resx index 33eabec7..aa0caa40 100644 --- a/Site5/App_GlobalResources/Controls.Swarm.resx +++ b/Site5/App_GlobalResources/Controls.Swarm.resx @@ -155,7 +155,7 @@ Kind of "hire" but also for volunteers - Assign person as <span id='{0}_modalPositionName'>...</span> + Assign person as <span id='{0}_modalPositionName'>...</span> <span id='{0}_modalGeographyName'> </span> Header for new assignment dialog diff --git a/Site5/Automation/Json-PositionsTree.aspx.cs b/Site5/Automation/Json-PositionsTree.aspx.cs index b9674c0f..ad62cd60 100644 --- a/Site5/Automation/Json-PositionsTree.aspx.cs +++ b/Site5/Automation/Json-PositionsTree.aspx.cs @@ -86,6 +86,7 @@ namespace Swarmops.Frontend.Automation { throw new InvalidOperationException("Positions are not initialized or are missing."); } + _displayAssignments = false; // Suppresses assignment lookup, which would fail for default positions Response.Output.WriteLine(RecursePositionTree(positions.Tree.RootNodes)); // TODO: turn off assignability! @@ -120,7 +121,7 @@ namespace Swarmops.Frontend.Automation { Position position = positionNode.Data; string localizedPositionName = position.Localized (positionNode.Data.MaxCount != 1); - PositionAssignments assignments = position.Assignments; + PositionAssignments assignments = new PositionAssignments(); string nodeState = "open"; if (position.GeographyId != _geographyId) @@ -131,7 +132,13 @@ namespace Swarmops.Frontend.Automation string expires = string.Empty; string action = string.Empty; - string assignedName = Resources.Controls.Swarm.Positions_Vacant; + string assignedName = string.Empty; + + if (_displayAssignments) + { + assignments = position.Assignments; + assignedName = Resources.Controls.Swarm.Positions_Vacant; + } if (_assignable) { @@ -252,6 +259,7 @@ namespace Swarmops.Frontend.Automation private string _customCookieClass; private bool _assignable = false; // TODO: Maybe make this more dynamic later + private bool _displayAssignments = true; private int _geographyId; diff --git a/Site5/Automation/SwarmFunctions.aspx.cs b/Site5/Automation/SwarmFunctions.aspx.cs index c5b0845f..a0a32231 100644 --- a/Site5/Automation/SwarmFunctions.aspx.cs +++ b/Site5/Automation/SwarmFunctions.aspx.cs @@ -6,6 +6,7 @@ using System.Web; using System.Web.Services; using System.Web.UI; using System.Web.UI.WebControls; +using Swarmops.Common.Enums; using Swarmops.Logic.Security; using Swarmops.Logic.Structure; using Swarmops.Logic.Swarm; @@ -28,6 +29,12 @@ namespace Swarmops.Frontend.Automation Person person = Person.FromIdentity (personId); Geography geography = (geographyId == 0 ? null : Geography.FromIdentity (geographyId)); + if (position.PositionLevel == PositionLevel.Geography || + position.PositionLevel == PositionLevel.GeographyDefault) + { + position.AssignGeography (geography); + } + if ((position.OrganizationId > 0 && authData.CurrentOrganization.Identity != position.OrganizationId) || person.Identity < 0) { throw new UnauthorizedAccessException(); @@ -180,5 +187,14 @@ namespace Swarmops.Frontend.Automation Avatar24Url = person.GetSecureAvatarLink (24) }; } + + [WebMethod] + public static AjaxCallResult GetGeographyName (int geographyId) + { + // This is not sensitive, so no access control, just culture setting + + GetAuthenticationDataAndCulture(); + return new AjaxCallResult {Success = true, DisplayMessage = Geography.FromIdentity (geographyId).Name}; + } } } \ No newline at end of file diff --git a/Site5/Controls/v5/Swarm/TreePositions.ascx b/Site5/Controls/v5/Swarm/TreePositions.ascx index e8a883fb..da5e6e63 100644 --- a/Site5/Controls/v5/Swarm/TreePositions.ascx +++ b/Site5/Controls/v5/Swarm/TreePositions.ascx @@ -26,8 +26,21 @@ onLoadSuccess: function () { $('.LocalAssignPerson.LocalPosition<%=this.Cookie%>').click(function () { - current<%=this.Cookie%>PositionId = $(this).attr("positionId"); + current<%=this.ClientID%>PositionId = $(this).attr("positionId"); $('#<%= this.ClientID %>_modalPositionName').text(decodeURIComponent($(this).attr("positionName"))); + $('#<%= this.ClientID %>_modalGeographyName').text(''); + var thisGeographyId = $(this).attr("geographyId"); + current<%=this.ClientID%>GeographyId = '0'; + if (thisGeographyId != '0' && thisGeographyId != '1') { + current<%=this.ClientID%>GeographyId = thisGeographyId; + SwarmopsJS.ajaxCall("/Automation/SwarmFunctions.aspx/GetGeographyName", + { geographyId: thisGeographyId }, + function(result) { + if (result.Success) { + $('#<%= this.ClientID %>_modalGeographyName').text(result.DisplayMessage); + } + }); + } <%= this.DropPerson.ClientID%>_clear(); $('#<%= this.DropDuration.ClientID%>').val("12"); // reset to 12-month default <%= this.DialogAdd.ClientID %>_open(); @@ -120,7 +133,7 @@ SwarmopsJS.ajaxCall( "/Automation/SwarmFunctions.aspx/AssignPosition", - { personId: personId, positionId: current<%=this.Cookie%>PositionId, durationMonths: $('#<%= this.DropDuration.ClientID%>').val(), geographyId: 0 }, + { personId: personId, positionId: current<%=this.ClientID%>PositionId, durationMonths: $('#<%= this.DropDuration.ClientID%>').val(), geographyId: current<%=this.ClientID%>GeographyId }, function (result) { // Close modal and reload grid regardless of success or not // (the only failure here should be a concurrency error, in which case @@ -151,7 +164,9 @@ }); } - var current<%=this.Cookie%>PositionId = ''; + var current<%=this.ClientID%>PositionId = ''; + var current<%=this.ClientID%>GeographyId = ''; + var displayed<%=this.ClientID%>GeographyId = '<%=this.GeographyId%>';