protected void Page_Load(object sender, EventArgs e) { if (ViewState[szKeyVSAircraftInProgress] != null) { m_ac = (Aircraft)ViewState[szKeyVSAircraftInProgress]; } if (!IsPostBack) { cmbAircraftInstance.DataSource = AircraftInstance.GetInstanceTypes(); cmbAircraftInstance.DataBind(); InitFormForAircraft(); } else { // If it's an existing aircraft and images may have been uploaded, process them if (!m_ac.IsNew) { ProcessImages(); } } SyncTailToCountry(); util.SetValidationGroup(mfbMaintainAircraft, "EditAircraft"); }
protected void Page_Load(object sender, EventArgs e) { if (ViewState[szKeyVSAircraftInProgress] != null) { m_ac = (Aircraft)ViewState[szKeyVSAircraftInProgress]; } if (!IsPostBack) { cmbCountryCode.DataSource = CountryCodePrefix.CountryCodes(); cmbCountryCode.DataBind(); List <AircraftInstance> lst = new List <AircraftInstance>(AircraftInstance.GetInstanceTypes()); lst.RemoveAll(aic => aic.IsRealAircraft); rblTrainingDevices.DataSource = lst; rblTrainingDevices.DataBind(); rblTrainingDevices.SelectedIndex = 0; InitFormForAircraft(); } else { // If it's an existing aircraft and images may have been uploaded, process them if (!m_ac.IsNew) { ProcessImages(); } } SyncTailToCountry(); SetValidationGroup(mfbMaintainAircraft, "EditAircraft"); }
public static string ValidateAircraft(string szTail, int idModel, int instanceType) { if (!HttpContext.Current.User.Identity.IsAuthenticated || String.IsNullOrEmpty(HttpContext.Current.User.Identity.Name)) { throw new MyFlightbookException("Unauthenticated call to ValidateAircraft"); } if (string.IsNullOrEmpty(szTail)) { throw new ArgumentException("Empty tail in ValidateAircraft"); } // Issue #296: allow sims to come through without a sim prefix; we can fix it at AddNewAircraft time. AircraftInstance aic = Array.Find(AircraftInstance.GetInstanceTypes(), it => it.InstanceTypeInt == instanceType); if (!aic.IsRealAircraft && !szTail.ToUpper(CultureInfo.CurrentCulture).StartsWith(CountryCodePrefix.SimCountry.Prefix.ToUpper(CultureInfo.CurrentCulture), StringComparison.CurrentCultureIgnoreCase)) { szTail = CountryCodePrefix.SimCountry.Prefix; } Aircraft ac = new Aircraft() { TailNumber = szTail, ModelID = idModel, InstanceTypeID = instanceType }; ac.FixTailAndValidate(); return(ac.ErrorString); }
protected void gvAircraftCandidates_RowCommand(object sender, CommandEventArgs e) { if (e == null) { throw new ArgumentException("null gridviewcommandeventargs for new aircraft to import", "e"); } int idRow = Convert.ToInt32(e.CommandArgument, CultureInfo.InvariantCulture); if (e.CommandName.CompareOrdinalIgnoreCase("AddNew") == 0) { UserAircraft ua = new UserAircraft(Page.User.Identity.Name); AircraftImportMatchRow mr = CandidatesForImport.FirstOrDefault <AircraftImportMatchRow>(mr2 => mr2.ID == idRow); mr.BestMatchAircraft.Commit(Page.User.Identity.Name); ModelMapping[mr.ModelGiven] = MakeModel.GetModel(mr.BestMatchAircraft.ModelID); // remember the mapping. // hack, but the commit above can result in the instance type being cleared out, so restore it. if (String.IsNullOrEmpty(mr.BestMatchAircraft.InstanceTypeDescription)) { mr.BestMatchAircraft.InstanceTypeDescription = AircraftInstance.GetInstanceTypes()[mr.BestMatchAircraft.InstanceTypeID - 1].DisplayName; } mr.State = AircraftImportMatchRow.MatchState.JustAdded; UpdateGrid(); } }
public static string AddNewAircraft(string szTail, int idModel, int instanceType, string szModelGiven, string szJsonMapping) { if (!HttpContext.Current.User.Identity.IsAuthenticated || String.IsNullOrEmpty(HttpContext.Current.User.Identity.Name)) { throw new MyFlightbookException("Unauthenticated call to add new aircraft"); } if (string.IsNullOrEmpty(szTail)) { throw new ArgumentException("Missing tail in AddNewAircraft"); } Dictionary <string, MakeModel> dModelMapping = String.IsNullOrEmpty(szJsonMapping) ? new Dictionary <string, MakeModel>() : JsonConvert.DeserializeObject <Dictionary <string, MakeModel> >(szJsonMapping); string szCurrentUser = HttpContext.Current.User.Identity.Name; Aircraft ac = new Aircraft() { TailNumber = szTail, ModelID = idModel, InstanceTypeID = instanceType }; // Issue #296: allow sims to come through without a sim prefix; we can fix it at AddNewAircraft time. AircraftInstance aic = Array.Find(AircraftInstance.GetInstanceTypes(), it => it.InstanceTypeInt == instanceType); string szSpecifiedTail = szTail; bool fIsNamedSim = !aic.IsRealAircraft && !szTail.ToUpper(CultureInfo.CurrentCulture).StartsWith(CountryCodePrefix.SimCountry.Prefix.ToUpper(CultureInfo.CurrentCulture), StringComparison.CurrentCultureIgnoreCase); if (fIsNamedSim) { ac.TailNumber = CountryCodePrefix.SimCountry.Prefix; } if (ac.FixTailAndValidate()) { ac.CommitForUser(szCurrentUser); UserAircraft ua = new UserAircraft(szCurrentUser); if (fIsNamedSim) { ac.PrivateNotes = String.Format(CultureInfo.InvariantCulture, "{0} #ALT{1}#", ac.PrivateNotes ?? string.Empty, szSpecifiedTail); } ua.FAddAircraftForUser(ac); ua.InvalidateCache(); if (!String.IsNullOrEmpty(szModelGiven)) { dModelMapping[szModelGiven] = MakeModel.GetModel(idModel); } } else { throw new MyFlightbookValidationException(ac.ErrorString); } return(JsonConvert.SerializeObject(dModelMapping)); }
protected void gvAircraftCandidates_RowDataBound(object sender, GridViewRowEventArgs e) { if (e == null) { throw new ArgumentNullException("e"); } if (e.Row.RowType == DataControlRowType.DataRow) { AircraftImportMatchRow mr = (AircraftImportMatchRow)e.Row.DataItem; GridViewRow gvr = e.Row; // Column 0: Aircraft tail. Link to registration, if necessary HyperLink lnkFAA = (HyperLink)e.Row.FindControl("lnkFAA"); lnkFAA.NavigateUrl = Aircraft.LinkForTailnumberRegistry(mr.TailNumber); ((Label)gvr.FindControl("lblGivenTail")).Visible = !(lnkFAA.Visible = !String.IsNullOrEmpty(lnkFAA.NavigateUrl)); ((Label)gvr.FindControl("lblAircraftVersion")).Text = (mr.BestMatchAircraft != null && mr.BestMatchAircraft.Version > 0) ? Resources.Aircraft.ImportAlternateVersion : string.Empty; // Column 1 - Best match DropDownList cmbInst = (DropDownList)e.Row.FindControl("cmbInstType"); HiddenField hdnContext = (HiddenField)e.Row.FindControl("hdnContext"); if (mr.State == AircraftImportMatchRow.MatchState.UnMatched) { Label lblInstType = (Label)e.Row.FindControl("lblType"); cmbInst.DataSource = AircraftInstance.GetInstanceTypes(); cmbInst.DataBind(); cmbInst.SelectedValue = mr.BestMatchAircraft.InstanceTypeID.ToString(CultureInfo.InvariantCulture); cmbInst.Attributes["onchange"] = String.Format(CultureInfo.InvariantCulture, "javascript:updateInstanceDesc('{0}', '{1}', '{2}');", cmbInst.ClientID, lblInstType.ClientID, hdnContext.ClientID); } // column 2 - "Add this" and status Button btnAddThis = (Button)gvr.FindControl("btnAddThis"); Label lProblem = (Label)e.Row.FindControl("lblACErr"); Label lblAllGood = (Label)e.Row.FindControl("lblAllGood"); lblAllGood.Style["display"] = (mr.State == AircraftImportMatchRow.MatchState.JustAdded) ? "block" : "none"; ((Label)gvr.FindControl("lblAlreadyInProfile")).Visible = mr.State == AircraftImportMatchRow.MatchState.MatchedInProfile; Panel pnlStaticMake = (Panel)e.Row.FindControl("pnlStaticMake"); Panel pnlEditMake = (Panel)e.Row.FindControl("pnlEditMake"); Image imgEdit = (Image)e.Row.FindControl("imgEdit"); imgEdit.Attributes["onclick"] = String.Format(CultureInfo.InvariantCulture, "javascript:toggleModelEdit('{0}', '{1}');", pnlStaticMake.ClientID, pnlEditMake.ClientID); TextBox textBox = (TextBox)e.Row.FindControl("txtSearch"); HiddenField hdnModel = (HiddenField)e.Row.FindControl("hdnSelectedModel"); Label lblModel = (Label)e.Row.FindControl("lblSelectedMake"); Dictionary <string, object> d = new Dictionary <string, object>() { { "lblID", lblModel.ClientID }, // ID of the label to display the selected model { "lblErr", lProblem.ClientID }, // ID of the label for displaying an error { "lblAllGood", lblAllGood.ClientID }, // ID of the label for displaying success { "mdlID", hdnModel.ClientID }, // ID of the hidden control with the selected model ID { "cmbInstance", cmbInst.ClientID }, // ID of the drop-down with the instance type specified { "progressID", popupAddingInProgress.BehaviorID }, // ID of the progress behavior ID { "btnAdd", btnAddThis.ClientID }, // ID of the "Add this" button { "pnlStaticMake", pnlStaticMake.ClientID }, // ID of the static view of the model/instance type to import { "pnlEditMake", pnlEditMake.ClientID }, // ID of the edit view to import { "matchRow", mr } // The match row with any additional context }; AjaxControlToolkit.AutoCompleteExtender autoCompleteExtender = (AjaxControlToolkit.AutoCompleteExtender)e.Row.FindControl("autocompleteModel"); hdnContext.Value = autoCompleteExtender.ContextKey = JsonConvert.SerializeObject(d); switch (mr.State) { case AircraftImportMatchRow.MatchState.JustAdded: case AircraftImportMatchRow.MatchState.MatchedInProfile: btnAddThis.Visible = false; btnAddThis.Attributes["onclick"] = string.Empty; break; case AircraftImportMatchRow.MatchState.MatchedExisting: btnAddThis.Visible = true; btnAddThis.Text = Resources.Aircraft.ImportExistingAircraft; btnAddThis.Attributes["onclick"] = String.Format(CultureInfo.InvariantCulture, "addExistingAircraft(JSON.parse(document.getElementById('{0}').value)); return false;", hdnContext.ClientID); break; case AircraftImportMatchRow.MatchState.UnMatched: imgEdit.Visible = true; e.Row.FindControl("pnlEditMake").Visible = true; btnAddThis.Visible = true; btnAddThis.Text = Resources.Aircraft.ImportAddNewAircraft; hdnModel.Value = mr.BestMatchAircraft.ModelID.ToString(CultureInfo.InvariantCulture); if (mr.SuggestedModel == null || !String.IsNullOrEmpty(mr.BestMatchAircraft.ErrorString)) { textBox.Text = mr.ModelGiven; pnlEditMake.Style["display"] = "block"; pnlStaticMake.Style["display"] = "none"; btnAddThis.Style["display"] = "none"; } else { textBox.Text = string.Empty; pnlEditMake.Style["display"] = "none"; pnlStaticMake.Style["display"] = "block"; btnAddThis.Style["display"] = "block"; } btnAddThis.Attributes["onclick"] = String.Format(CultureInfo.InvariantCulture, "addNewAircraft(JSON.parse(document.getElementById('{0}').value)); return false;", hdnContext.ClientID); break; } if (mr.BestMatchAircraft != null && mr.BestMatchAircraft.ErrorString.Length > 0) { lProblem.Text = mr.BestMatchAircraft.ErrorString; btnAddThis.Style["display"] = "none"; } else { lProblem.Text = string.Empty; if (mr.State == AircraftImportMatchRow.MatchState.JustAdded) { lblAllGood.Style["display"] = "block"; btnAddThis.Style["display"] = "none"; } } } }
protected void gvAircraftCandidates_RowDataBound(object sender, GridViewRowEventArgs e) { if (e == null) { throw new ArgumentNullException("e"); } if (e.Row.RowType == DataControlRowType.DataRow) { AircraftImportMatchRow mr = (AircraftImportMatchRow)e.Row.DataItem; GridView gv = (GridView)sender; GridViewRow gvr = e.Row; // Column 0: select or not LinkButton lnkEdit = (LinkButton)gvr.FindControl("lnkEdit"); lnkEdit.Visible = mr.State == AircraftImportMatchRow.MatchState.UnMatched; lnkEdit.CommandArgument = gvr.RowIndex.ToString(CultureInfo.InvariantCulture); // Column 1: Aircraft tail. Link to registration, if necessary HyperLink lnkFAA = (HyperLink)e.Row.FindControl("lnkFAA"); lnkFAA.NavigateUrl = Aircraft.LinkForTailnumberRegistry(mr.TailNumber); ((Label)gvr.FindControl("lblGivenTail")).Visible = !(lnkFAA.Visible = !String.IsNullOrEmpty(lnkFAA.NavigateUrl)); ((Label)gvr.FindControl("lblAircraftVersion")).Text = (mr.BestMatchAircraft != null && mr.BestMatchAircraft.Version > 0) ? Resources.Aircraft.ImportAlternateVersion : string.Empty; // Column 2 - Best match MultiView mvMatch = (MultiView)e.Row.FindControl("mvMatch"); mvMatch.ActiveViewIndex = 0; DropDownList cmbInst = (DropDownList)e.Row.FindControl("cmbInstType"); DropDownList cmbModel = (DropDownList)e.Row.FindControl("cmbModel"); DropDownList cmbManufacturer = (DropDownList)e.Row.FindControl("cmbManufacturer"); if (e.Row.RowIndex == gv.SelectedIndex && mr.State == AircraftImportMatchRow.MatchState.UnMatched) { cmbInst.DataSource = AircraftInstance.GetInstanceTypes(); cmbInst.DataBind(); cmbInst.SelectedValue = mr.BestMatchAircraft.InstanceTypeID.ToString(CultureInfo.InvariantCulture); cmbManufacturer.DataSource = AllManufacturers; cmbManufacturer.DataBind(); int idMan = (mr.SpecifiedModel == null) ? Manufacturer.UnsavedID : mr.SpecifiedModel.ManufacturerID; int idModel = (mr.SpecifiedModel == null) ? MakeModel.UnknownModel : mr.SpecifiedModel.MakeModelID; cmbManufacturer.SelectedValue = idMan.ToString(CultureInfo.InvariantCulture); cmbModel.DataSource = ModelsForManufacturer(idMan); cmbModel.DataBind(); cmbModel.SelectedValue = idModel.ToString(CultureInfo.InvariantCulture); mvMatch.ActiveViewIndex = 1; e.Row.BackColor = System.Drawing.Color.FromArgb(0xCC, 0xCC, 0xCC); e.Row.BorderColor = System.Drawing.Color.Black; e.Row.BorderStyle = BorderStyle.Solid; e.Row.BorderWidth = Unit.Pixel(1); } else { cmbInst.Items.Clear(); cmbModel.Items.Clear(); cmbManufacturer.Items.Clear(); } // column 3 - "Add this" and status Button btnAddThis = (Button)gvr.FindControl("btnAddThis"); Label lProblem = (Label)e.Row.FindControl("lblACErr"); Label lblAllGood = (Label)e.Row.FindControl("lblAllGood"); lblAllGood.Style["display"] = (mr.State == AircraftImportMatchRow.MatchState.JustAdded) ? "block" : "none"; ((Label)gvr.FindControl("lblAlreadyInProfile")).Visible = mr.State == AircraftImportMatchRow.MatchState.MatchedInProfile; switch (mr.State) { case AircraftImportMatchRow.MatchState.JustAdded: case AircraftImportMatchRow.MatchState.MatchedInProfile: btnAddThis.Visible = false; btnAddThis.Attributes["onclick"] = string.Empty; break; case AircraftImportMatchRow.MatchState.MatchedExisting: btnAddThis.Visible = true; btnAddThis.Text = Resources.Aircraft.ImportExistingAircraft; btnAddThis.Attributes["onclick"] = String.Format(CultureInfo.InvariantCulture, "addExistingAircraft({0}, '{1}', '{2}', '{3}'); return false;", mr.BestMatchAircraft.AircraftID, btnAddThis.ClientID, lblAllGood.ClientID, popupAddingInProgress.BehaviorID); break; case AircraftImportMatchRow.MatchState.UnMatched: btnAddThis.Visible = true; btnAddThis.Text = Resources.Aircraft.ImportAddNewAircraft; if (e.Row.RowIndex != gv.SelectedIndex && mr.State == AircraftImportMatchRow.MatchState.UnMatched && mr.BestMatchAircraft != null) { // Bypass a postback if it is an error-free non-selected row. This should avoid some of the viewstate errors we've been getting for click-click-click down the list of buttons btnAddThis.Attributes["onclick"] = String.Format(CultureInfo.InvariantCulture, "addNewAircraft('{0}', '{1}', {2}, '{3}', {4}, '{5}'); return false;", btnAddThis.ClientID, lblAllGood.ClientID, mr.BestMatchAircraft.ModelID, mr.BestMatchAircraft.TailNumber, mr.BestMatchAircraft.InstanceTypeID, popupAddingInProgress.BehaviorID); } break; } if (mr.BestMatchAircraft != null && mr.BestMatchAircraft.ErrorString.Length > 0) { lProblem.Text = mr.BestMatchAircraft.ErrorString; btnAddThis.Visible = false; } else { lProblem.Text = string.Empty; if (mr.State == AircraftImportMatchRow.MatchState.JustAdded) { lblAllGood.Style["display"] = "block"; btnAddThis.Visible = false; lnkEdit.Visible = false; // disable the Edit button } } } }