JobFormGrid.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 DynamicActionColumn(PRSDesktop.Resources.pencil.AsBitmapImage(), EditAction));
  23. }
  24. public Guid JobID { get; set; }
  25. private bool EditAction(CoreRow? row)
  26. {
  27. if (row == null) return false;
  28. if(DynamicFormEditWindow.EditDigitalForm<JobForm>(row.Get<JobForm, Guid>(x => x.ID), out var dataModel))
  29. {
  30. dataModel.Update(null);
  31. return true;
  32. }
  33. return false;
  34. }
  35. protected override void Reload(Filters<JobForm> criteria, Columns<JobForm> columns, ref SortOrder<JobForm>? sort,
  36. Action<CoreTable?, Exception?> action)
  37. {
  38. criteria.Add(new Filter<JobForm>(x => x.Parent.ID).IsEqualTo(JobID));
  39. base.Reload(criteria, columns, ref sort, action);
  40. }
  41. protected override JobForm CreateItem()
  42. {
  43. var result = base.CreateItem();
  44. result.Parent.ID = JobID;
  45. return result;
  46. }
  47. }
  48. public class JobFormDefinitionGrid : DynamicManyToManyGrid<JobFormDefinition, Job>
  49. {
  50. public JobFormDefinitionGrid()
  51. {
  52. AddButton("Add All", null, AddAllClick);
  53. }
  54. private bool AddAllClick(Button arg1, CoreRow[] arg2)
  55. {
  56. var existingforms = ExtractValues(x => x.Form.ID, Selection.All).ToArray();
  57. var newforms = new Client<DigitalForm>().Query(
  58. new Filter<DigitalForm>(x => x.Active).IsEqualTo(true)
  59. .And(x => x.AppliesTo).IsEqualTo("Job")
  60. .And(x => x.ID).NotInList(existingforms)
  61. );
  62. foreach (var row in newforms.Rows)
  63. {
  64. var newitem = CreateItem();
  65. newitem.Job.ID = Item.ID;
  66. newitem.Form.ID = row.Get<DigitalForm, Guid>(x => x.ID);
  67. newitem.Form.Synchronise(row.ToObject<DigitalForm>());
  68. SaveItem(newitem);
  69. }
  70. return true;
  71. }
  72. }
  73. }