DigitalFormReportGrid.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.IsOpen = true;
  146. }
  147. }
  148. private void AddLayout(DigitalFormLayout layout)
  149. {
  150. var model = DigitalFormUtils.GetDataModel(Form.AppliesTo, GetVariables());
  151. var item = CreateItem();
  152. item.DataModel = model.Name;
  153. item.Name = string.IsNullOrWhiteSpace(layout.Description) ? layout.Code : layout.Description;
  154. item.RDL = DigitalFormUtils.GenerateReport(layout, model)?.SaveToString();
  155. if (EditItems(new[] { item }))
  156. {
  157. SaveItem(item);
  158. Refresh(false, true);
  159. }
  160. }
  161. public void BeforeSave(object item)
  162. {
  163. }
  164. public void AfterSave(object item)
  165. {
  166. // First remove any deleted files
  167. foreach (var original in OriginalItems)
  168. if (!Items.Contains(original))
  169. new Client<ReportTemplate>().Delete(original, typeof(ReportTemplate).Name + " Deleted by User");
  170. foreach (var template in Items)
  171. {
  172. template.Section = Form.ID.ToString();
  173. }
  174. new Client<ReportTemplate>().Save(Items.Where(x => x.IsChanged()), "Updated by User");
  175. }
  176. protected override ReportTemplate CreateItem()
  177. {
  178. var item = base.CreateItem();
  179. if(Form.ID != Guid.Empty)
  180. {
  181. item.Section = Form.ID.ToString();
  182. }
  183. return item;
  184. }
  185. public string Caption() => "Reports";
  186. public Size MinimumSize()
  187. {
  188. return new Size(400, 400);
  189. }
  190. public int Order()
  191. {
  192. return int.MinValue;
  193. }
  194. }
  195. }