JobFormGrid.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Linq;
  3. using System.Windows.Controls;
  4. using Comal.Classes;
  5. using InABox.Clients;
  6. using InABox.Core;
  7. using InABox.DynamicGrid;
  8. using InABox.WPF;
  9. namespace PRSDesktop
  10. {
  11. public class JobFormGrid : DynamicDataGrid<JobForm>, IJobControl, IDataModelSource
  12. {
  13. public event DataModelUpdateEvent? OnUpdateDataModel;
  14. public string SectionName => "Job Forms";
  15. public DataModel DataModel(Selection selection)
  16. {
  17. var ids = ExtractValues(x => x.ID, selection).ToArray();
  18. return new AutoDataModel<JobForm>(new Filter<JobForm>(x => x.ID).InList(ids));
  19. }
  20. public JobFormGrid()
  21. {
  22. ActionColumns.Add(new DynamicImageColumn(PRSDesktop.Resources.pencil.AsBitmapImage(), EditAction));
  23. }
  24. public Guid ParentID { get; set; }
  25. public JobPanelSettings Settings { get; set; }
  26. private bool EditAction(CoreRow? row)
  27. {
  28. if (row == null) return false;
  29. if(DynamicFormEditWindow.EditDigitalForm<JobForm>(row.Get<JobForm, Guid>(x => x.ID), out var dataModel))
  30. {
  31. dataModel.Update(null);
  32. return true;
  33. }
  34. return false;
  35. }
  36. protected override void Reload(Filters<JobForm> criteria, Columns<JobForm> columns, ref SortOrder<JobForm>? sort,
  37. Action<CoreTable?, Exception?> action)
  38. {
  39. criteria.Add(new Filter<JobForm>(x => x.Parent.ID).IsEqualTo(ParentID));
  40. base.Reload(criteria, columns, ref sort, action);
  41. }
  42. protected override JobForm CreateItem()
  43. {
  44. var result = base.CreateItem();
  45. result.Parent.ID = ParentID;
  46. return result;
  47. }
  48. }
  49. public class JobFormDefinitionGrid : DynamicManyToManyGrid<JobFormDefinition, Job>
  50. {
  51. public JobFormDefinitionGrid()
  52. {
  53. AddButton("Add All", null, AddAllClick);
  54. }
  55. private bool AddAllClick(Button arg1, CoreRow[] arg2)
  56. {
  57. var existingforms = ExtractValues(x => x.Form.ID, Selection.All).ToArray();
  58. var newforms = new Client<DigitalForm>().Query(
  59. new Filter<DigitalForm>(x => x.Active).IsEqualTo(true)
  60. .And(x => x.AppliesTo).IsEqualTo("Job")
  61. .And(x => x.ID).NotInList(existingforms)
  62. );
  63. foreach (var row in newforms.Rows)
  64. {
  65. var newitem = CreateItem();
  66. newitem.Job.ID = Item.ID;
  67. newitem.Form.ID = row.Get<DigitalForm, Guid>(x => x.ID);
  68. newitem.Form.Synchronise(row.ToObject<DigitalForm>());
  69. SaveItem(newitem);
  70. }
  71. return true;
  72. }
  73. }
  74. }