Files
using.cash/Site/Pages/v5/Admin/CreateOrganization.aspx.cs
T
2017-10-27 11:53:26 +02:00

193 lines
8.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web;
using System.Web.UI.WebControls;
using Resources;
using Swarmops.Common;
using Swarmops.Common.Enums;
using Swarmops.Interface.Support;
using Swarmops.Logic.Financial;
using Swarmops.Logic.Security;
using Swarmops.Logic.Structure;
using Swarmops.Logic.Swarm;
namespace Swarmops.Frontend.Pages.v5.Admin
{
public partial class CreateOrganization : PageV5Base
{
protected void Page_Load (object sender, EventArgs e)
{
this.BoxTitle.Text = PageTitle = Resources.Pages.Admin.CreateOrganization_PageTitle;
PageIcon = "iconshock-organization-add";
InfoBoxLiteral = Resources.Pages.Admin.CreateOrganization_Info;
DbVersionRequired = 2; // for FinancialAccounts.Active field in Create function
PageAccessRequired = new Access(CurrentOrganization, AccessAspect.Administration, AccessType.Write);
if (!Page.IsPostBack)
{
Localize();
}
}
private void Localize()
{
this.LabelOrganizationName.Text = Resources.Pages.Admin.CreateOrganization_NewOrgName;
this.LabelActivistLabel.Text = Resources.Pages.Admin.CreateOrganization_ActivistTitle;
this.LabelCreateAs.Text = Resources.Pages.Admin.CreateOrganization_CreateNewOrgAs;
this.LabelNativeCurrency.Text = Resources.Pages.Admin.CreateOrganization_NewOrgCurrency;
this.LabelPersonLabel.Text = Resources.Pages.Admin.CreateOrganization_RegularTitle;
this.LabelApplicantLabel.Text = Resources.Pages.Admin.CreateOrganization_ApplicantTitle; // this seems to not take effect?
this.LabelPositionLabel.Text = Resources.Pages.Admin.CreateOrganization_Titles;
this.ButtonCreate.Text = Global.Global_Create;
this.DropPersonLabel.Items.Clear();
this.DropActivistLabel.Items.Clear();
this.DropCurrencies.Items.Clear();
this.DropCreateChild.Items.Clear();
this.DropPositionLabel.Items.Clear();
//this.DropPersonLabel.Items.Add (new ListItem (Global.Global_SelectOne, "0"));
//this.DropActivistLabel.Items.Add (new ListItem (Global.Global_SelectOne, "0"));
//this.DropCurrencies.Items.Add (new ListItem (Global.Global_SelectOne, "0"));
//this.DropCreateChild.Items.Add (new ListItem (Global.Global_SelectOne, "0"));
this.DropPositionLabel.Items.Add (new ListItem(Resources.Pages.Admin.CreateOrganization_Titles_Nonprofit, "Nonprofit"));
this.DropPositionLabel.Items.Add(new ListItem(Resources.Pages.Admin.CreateOrganization_Titles_Commercial, "Commercial"));
this.DropPositionLabel.Items.Add(new ListItem(Resources.Pages.Admin.CreateOrganization_Titles_Government, "Government"));
this.DropCreateChild.Items.Add (new ListItem (Resources.Pages.Admin.CreateOrganization_AsRoot, "Root"));
this.DropCreateChild.Items.Add (
new ListItem (
String.Format (Resources.Pages.Admin.CreateOrganization_ChildOfX, CurrentOrganization.Name), "Child"));
List<string> localizedPersonLabels = new List<string>();
foreach (ParticipantTitle title in (ParticipantTitle[]) Enum.GetValues (typeof (ParticipantTitle)))
{
string localizedPersonLabel =
Participant.Localized (title, TitleVariant.Plural);
localizedPersonLabels.Add (localizedPersonLabel + "|" + title);
}
localizedPersonLabels.Sort(); // Sorts _localized_
this.DropActivistLabel.Items.Clear(); // remove default empty option
this.DropApplicantLabel.Items.Clear();
this.DropPersonLabel.Items.Clear();
foreach (string localizedPersonLabel in localizedPersonLabels)
{
string[] parts = localizedPersonLabel.Split ('|');
if (parts[1] == "Unknown")
{
continue; // skip the empty option
}
this.DropApplicantLabel.Items.Add(new ListItem(parts[0], parts[1]));
this.DropPersonLabel.Items.Add(new ListItem(parts[0], parts[1]));
this.DropActivistLabel.Items.Add (new ListItem (parts[0], parts[1]));
}
this.DropApplicantLabel.SelectedValue = "Applicant";
this.DropPersonLabel.SelectedValue = "Member";
this.DropActivistLabel.SelectedValue = "Activist";
Currencies currencies = Currencies.GetAll();
List<string> currencyStrings = new List<string>();
foreach (Currency currency in currencies)
{
currencyStrings.Add (String.Format ("{0} {1}|{0}", currency.Code, currency.Name));
}
this.TextOrganizationName.Attributes["Placeholder"] = Resources.Pages.Admin.CreateOrganization_NewOrganizationName;
this.TextOrganizationName.Focus();
currencyStrings.Sort();
foreach (string currencyString in currencyStrings)
{
string[] parts = currencyString.Split ('|');
this.DropCurrencies.Items.Add (new ListItem (parts[0], parts[1]));
}
this.DropCurrencies.SelectedValue = "BTC";
}
protected void ButtonCreate_Click (object sender, EventArgs e)
{
ParticipantTitle applicantLabel = (ParticipantTitle)(Enum.Parse(typeof(ParticipantTitle), this.DropApplicantLabel.SelectedValue));
ParticipantTitle activistLabel = (ParticipantTitle)(Enum.Parse(typeof(ParticipantTitle), this.DropActivistLabel.SelectedValue));
ParticipantTitle peopleLabel =
(ParticipantTitle) (Enum.Parse (typeof (ParticipantTitle), this.DropPersonLabel.SelectedValue));
string asRoot = this.DropCreateChild.SelectedValue;
string currencyCode = this.DropCurrencies.SelectedValue;
string newOrgName = this.TextOrganizationName.Text;
if (string.IsNullOrEmpty (newOrgName))
{
throw new ArgumentException ("Organization name can't be empty");
}
if (activistLabel == ParticipantTitle.Unknown || peopleLabel == ParticipantTitle.Unknown || asRoot == "0" || currencyCode == "0")
{
throw new ArgumentException ("Necessary argument was not supplied (did client-side validation run?)");
}
Currency newOrgCurrency = Currency.FromCode (currencyCode);
Organization parent = CurrentOrganization;
if (asRoot == "Root")
{
parent = null;
}
Organization newOrganization = Organization.Create (parent == null ? 0 : parent.Identity, newOrgName,
newOrgName, newOrgName, string.Empty, newOrgName, Geography.RootIdentity, true, true, 0);
newOrganization.EnableEconomy (newOrgCurrency);
newOrganization.ApplicantLabel = applicantLabel;
newOrganization.RegularLabel = peopleLabel;
newOrganization.ActivistLabel = activistLabel;
PositionTitle titleType =
(PositionTitle) (Enum.Parse (typeof (PositionTitle), this.DropPositionLabel.SelectedValue));
Positions.CreateOrganizationDefaultPositions (newOrganization, titleType);
Participation.Create(CurrentUser, newOrganization, Constants.DateTimeHigh);
Positions.ForOrganization(newOrganization).AtLevel(PositionLevel.OrganizationExecutive)[0].Assign(
CurrentUser, null /* assignedby */, null /* assigned by position */, "Initial executive", null
/* expires */);
string successMessage = String.Format (Resources.Pages.Admin.CreateOrganization_Success,
Participant.Localized (peopleLabel, TitleVariant.Ship));
DashboardMessage.Set (successMessage);
// Log in to new organization, then redirect to Edit
Response.Redirect (
"/Pages/v5/Security/SetCurrentOrganization.aspx?OrganizationId=" +
newOrganization.Identity.ToString (CultureInfo.InvariantCulture) +
"&ReturnUrl=/Admin/OrgSettings", true);
}
// ReSharper disable InconsistentNaming
public string Localized_Error_OrganizationNameCannotBeEmpty
{
get
{
return
CommonV5.JavascriptEscape(
Resources.Pages.Admin.CreateOrganization_Error_OrganizationNameCannotBeEmpty);
}
}
}
}