DigitalFormReportGrid.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using FastReport;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.Core.Reports;
  5. using InABox.Wpf.Reports;
  6. using InABox.WPF;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Windows.Controls;
  12. using System.Windows.Media.Imaging;
  13. using FastReport.Table;
  14. using FastReport.Utils;
  15. using Border = System.Windows.Controls.Border;
  16. using Size = System.Windows.Size;
  17. namespace InABox.DynamicGrid
  18. {
  19. public class DigitalFormReportGrid : DynamicItemsListGrid<ReportTemplate>, IDynamicEditorPage
  20. {
  21. public DynamicEditorGrid EditorGrid { get; set; }
  22. public PageType PageType => PageType.Other;
  23. public bool Ready { get; set; }
  24. private DigitalForm Form { get; set; }
  25. private List<ReportTemplate> OriginalItems { get; set; } = new();
  26. public DigitalFormReportGrid()
  27. {
  28. Options.BeginUpdate();
  29. Options.Add(DynamicGridOption.ShowHelp);
  30. if (Security.CanEdit<ReportTemplate>())
  31. {
  32. Options.Add(DynamicGridOption.AddRows);
  33. Options.Add(DynamicGridOption.EditRows);
  34. ActionColumns.Add(new DynamicImageColumn(ScriptImage, ScriptClick));
  35. ActionColumns.Add(new DynamicImageColumn(Wpf.Resources.pencil.AsBitmapImage(), DesignClick));
  36. }
  37. if (Security.CanDelete<ReportTemplate>())
  38. Options.Add(DynamicGridOption.DeleteRows);
  39. Options.EndUpdate();
  40. }
  41. public void Load(object item, Func<Type, CoreTable>? PageDataHandler)
  42. {
  43. Form = (DigitalForm)item;
  44. CoreTable data;
  45. if (Form.ID == Guid.Empty)
  46. {
  47. data = new CoreTable();
  48. data.LoadColumns(typeof(ReportTemplate));
  49. }
  50. else
  51. {
  52. data = new Client<ReportTemplate>()
  53. .Query(new Filter<ReportTemplate>(x => x.Section).IsEqualTo(Form.ID.ToString()));
  54. }
  55. OriginalItems = data.Rows.Select(x => x.ToObject<ReportTemplate>()).ToList();
  56. Items = OriginalItems.ToList();
  57. Refresh(true, true);
  58. Ready = true;
  59. }
  60. private DynamicVariableGrid? GetVariableGrid()
  61. => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicVariableGrid)
  62. as DynamicVariableGrid;
  63. private List<DigitalFormVariable> GetVariables()
  64. => GetVariableGrid()?.Items.ToList() ?? new List<DigitalFormVariable>();
  65. private DynamicFormLayoutGrid? GetLayoutGrid()
  66. => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicFormLayoutGrid)
  67. as DynamicFormLayoutGrid;
  68. private List<DigitalFormLayout> GetLayouts()
  69. => GetLayoutGrid()?.Items.ToList() ?? new List<DigitalFormLayout>();
  70. private BitmapImage? ScriptImage(CoreRow? arg)
  71. {
  72. return arg == null ? Wpf.Resources.edit.AsBitmapImage() :
  73. arg.Get<ReportTemplate, bool>(x => x.IsRDL) ? null : Wpf.Resources.edit.AsBitmapImage();
  74. }
  75. private bool ScriptClick(CoreRow? arg)
  76. {
  77. if (arg != null)
  78. {
  79. if (DigitalFormUtils.GetDataModel(Form.AppliesTo,GetVariables()) is not DataModel model)
  80. {
  81. Logger.Send(LogType.Error, "", "Invalid entity type for data model.");
  82. return false;
  83. }
  84. var template = LoadItem(arg);
  85. var script = template.Script;
  86. if (string.IsNullOrWhiteSpace(script))
  87. script = string.Format(ReportTemplate.DefaultScriptTemplate, model.GetType().Name.Split('.').Last());
  88. var editor = new ScriptEditorWindow(script);
  89. if (editor.ShowDialog() == true)
  90. {
  91. template.Script = editor.Script;
  92. SaveItem(template);
  93. return true;
  94. }
  95. }
  96. return false;
  97. }
  98. private bool DesignClick(CoreRow? arg)
  99. {
  100. if (arg is null) return false;
  101. if (DigitalFormUtils.GetDataModel(Form.AppliesTo,GetVariables()) is not DataModel model)
  102. {
  103. Logger.Send(LogType.Error, "", "Invalid entity type for data model.");
  104. return false;
  105. }
  106. var template = LoadItem(arg);
  107. ReportUtils.DesignReport(template, model, true, saveTemplate: (template) => SaveItem(template));
  108. return false;
  109. }
  110. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  111. {
  112. var layouts = GetLayouts();
  113. if(layouts.Count == 0)
  114. {
  115. base.DoAdd(OpenEditorOnDirectEdit);
  116. }
  117. else
  118. {
  119. var menu = new ContextMenu();
  120. foreach (var layout in layouts)
  121. {
  122. menu.AddItem($"{layout.Code}: {layout.Description}", null, layout, AddLayout);
  123. }
  124. menu.IsOpen = true;
  125. }
  126. }
  127. private void AddLayout(DigitalFormLayout layout)
  128. {
  129. var model = DigitalFormUtils.GetDataModel(Form.AppliesTo,GetVariables());
  130. var item = CreateItem();
  131. item.DataModel = model.Name;
  132. item.Name = string.IsNullOrWhiteSpace(layout.Description) ? layout.Code : layout.Description;
  133. item.RDL = DigitalFormUtils.GenerateReport(layout,model)?.SaveToString();
  134. if (EditItems(new[] { item }))
  135. {
  136. SaveItem(item);
  137. Refresh(false, true);
  138. }
  139. }
  140. public void BeforeSave(object item)
  141. {
  142. }
  143. public void AfterSave(object item)
  144. {
  145. // First remove any deleted files
  146. foreach (var original in OriginalItems)
  147. if (!Items.Contains(original))
  148. new Client<ReportTemplate>().Delete(original, typeof(ReportTemplate).Name + " Deleted by User");
  149. foreach (var template in Items)
  150. {
  151. template.Section = Form.ID.ToString();
  152. }
  153. new Client<ReportTemplate>().Save(Items.Where(x => x.IsChanged()), "Updated by User");
  154. }
  155. protected override ReportTemplate CreateItem()
  156. {
  157. var item = base.CreateItem();
  158. if(Form.ID != Guid.Empty)
  159. {
  160. item.Section = Form.ID.ToString();
  161. }
  162. return item;
  163. }
  164. public string Caption() => "Reports";
  165. public Size MinimumSize()
  166. {
  167. return new Size(400, 400);
  168. }
  169. public int Order()
  170. {
  171. return int.MinValue;
  172. }
  173. }
  174. }