DigitalFormReportGrid.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. private bool _readOnly;
  27. public bool ReadOnly
  28. {
  29. get => _readOnly;
  30. set
  31. {
  32. if (_readOnly != value)
  33. {
  34. _readOnly = value;
  35. Reconfigure();
  36. }
  37. }
  38. }
  39. protected override void Init()
  40. {
  41. base.Init();
  42. if (Security.CanEdit<ReportTemplate>())
  43. {
  44. ActionColumns.Add(new DynamicImageColumn(ScriptImage, ScriptClick));
  45. ActionColumns.Add(new DynamicImageColumn(Wpf.Resources.pencil.AsBitmapImage(), DesignClick));
  46. }
  47. }
  48. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  49. {
  50. base.DoReconfigure(options);
  51. options.BeginUpdate();
  52. options.Add(DynamicGridOption.ShowHelp);
  53. if (Security.CanEdit<ReportTemplate>() && !ReadOnly)
  54. {
  55. options.Add(DynamicGridOption.AddRows);
  56. options.Add(DynamicGridOption.EditRows);
  57. }
  58. if (Security.CanDelete<ReportTemplate>() && !ReadOnly)
  59. options.Add(DynamicGridOption.DeleteRows);
  60. options.EndUpdate();
  61. }
  62. public void Load(object item, Func<Type, CoreTable?>? PageDataHandler)
  63. {
  64. Form = (DigitalForm)item;
  65. CoreTable data;
  66. if (Form.ID == Guid.Empty)
  67. {
  68. data = new CoreTable();
  69. data.LoadColumns(typeof(ReportTemplate));
  70. }
  71. else
  72. {
  73. data = new Client<ReportTemplate>()
  74. .Query(new Filter<ReportTemplate>(x => x.Section).IsEqualTo(Form.ID.ToString()));
  75. }
  76. OriginalItems = data.Rows.Select(x => x.ToObject<ReportTemplate>()).ToList();
  77. Items = OriginalItems.ToList();
  78. Refresh(true, true);
  79. Ready = true;
  80. }
  81. private DynamicVariableGrid? GetVariableGrid()
  82. => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicVariableGrid)
  83. as DynamicVariableGrid;
  84. private List<DigitalFormVariable> GetVariables()
  85. => GetVariableGrid()?.Items.ToList() ?? new List<DigitalFormVariable>();
  86. private DynamicFormLayoutGrid? GetLayoutGrid()
  87. => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicFormLayoutGrid)
  88. as DynamicFormLayoutGrid;
  89. private List<DigitalFormLayout> GetLayouts()
  90. => GetLayoutGrid()?.Items.ToList() ?? new List<DigitalFormLayout>();
  91. private BitmapImage? ScriptImage(CoreRow? arg)
  92. {
  93. return arg == null ? Wpf.Resources.edit.AsBitmapImage() :
  94. arg.Get<ReportTemplate, bool>(x => x.IsRDL) ? null : Wpf.Resources.edit.AsBitmapImage();
  95. }
  96. private bool ScriptClick(CoreRow? arg)
  97. {
  98. if (arg != null && !ReadOnly)
  99. {
  100. if (DigitalFormUtils.GetDataModel(Form.AppliesTo,GetVariables()) is not DataModel model)
  101. {
  102. Logger.Send(LogType.Error, "", "Invalid entity type for data model.");
  103. return false;
  104. }
  105. var template = LoadItem(arg);
  106. var script = template.Script;
  107. if (string.IsNullOrWhiteSpace(script))
  108. script = string.Format(ReportTemplate.DefaultScriptTemplate, model.GetType().Name.Split('.').Last());
  109. var editor = new ScriptEditorWindow(script);
  110. if (editor.ShowDialog() == true)
  111. {
  112. template.Script = editor.Script;
  113. SaveItem(template);
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119. private bool DesignClick(CoreRow? arg)
  120. {
  121. if (arg is null && !ReadOnly) return false;
  122. if (DigitalFormUtils.GetDataModel(Form.AppliesTo,GetVariables()) is not DataModel model)
  123. {
  124. Logger.Send(LogType.Error, "", "Invalid entity type for data model.");
  125. return false;
  126. }
  127. var template = LoadItem(arg);
  128. ReportUtils.DesignReport(template, model, true, saveTemplate: (template) => SaveItem(template));
  129. return false;
  130. }
  131. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  132. {
  133. var layouts = GetLayouts();
  134. if(layouts.Count == 0)
  135. {
  136. base.DoAdd(OpenEditorOnDirectEdit);
  137. }
  138. else
  139. {
  140. var menu = new ContextMenu();
  141. foreach (var layout in layouts)
  142. {
  143. menu.AddItem($"{layout.Code}: {layout.Description}", null, layout, AddLayout);
  144. }
  145. menu.AddSeparatorIfNeeded();
  146. menu.AddItem("Create blank report", null, () => base.DoAdd());
  147. menu.IsOpen = true;
  148. }
  149. }
  150. private void AddLayout(DigitalFormLayout layout)
  151. {
  152. var model = DigitalFormUtils.GetDataModel(Form.AppliesTo, GetVariables());
  153. var item = CreateItem();
  154. item.DataModel = model.Name;
  155. item.Name = string.IsNullOrWhiteSpace(layout.Description) ? layout.Code : layout.Description;
  156. item.RDL = DigitalFormUtils.GenerateReport(layout, model)?.SaveToString();
  157. if (EditItems(new[] { item }))
  158. {
  159. SaveItem(item);
  160. Refresh(false, true);
  161. }
  162. }
  163. public void BeforeSave(object item)
  164. {
  165. }
  166. public void AfterSave(object item)
  167. {
  168. // First remove any deleted files
  169. foreach (var original in OriginalItems)
  170. if (!Items.Contains(original))
  171. new Client<ReportTemplate>().Delete(original, typeof(ReportTemplate).Name + " Deleted by User");
  172. foreach (var template in Items)
  173. {
  174. template.Section = Form.ID.ToString();
  175. }
  176. new Client<ReportTemplate>().Save(Items.Where(x => x.IsChanged()), "Updated by User");
  177. }
  178. protected override ReportTemplate CreateItem()
  179. {
  180. var item = base.CreateItem();
  181. if(Form.ID != Guid.Empty)
  182. {
  183. item.Section = Form.ID.ToString();
  184. }
  185. return item;
  186. }
  187. public string Caption() => "Reports";
  188. public Size MinimumSize()
  189. {
  190. return new Size(400, 400);
  191. }
  192. public int Order()
  193. {
  194. return int.MinValue;
  195. }
  196. }
  197. }