123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- using FastReport;
- using InABox.Clients;
- using InABox.Core;
- using InABox.Core.Reports;
- using InABox.Wpf.Reports;
- using InABox.WPF;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Windows.Controls;
- using System.Windows.Media.Imaging;
- using FastReport.Table;
- using FastReport.Utils;
- using Border = System.Windows.Controls.Border;
- using Size = System.Windows.Size;
- namespace InABox.DynamicGrid
- {
- public class DigitalFormReportGrid : DynamicItemsListGrid<ReportTemplate>, IDynamicEditorPage
- {
- public DynamicEditorGrid EditorGrid { get; set; }
- public PageType PageType => PageType.Other;
- public bool Ready { get; set; }
- private DigitalForm Form { get; set; }
- private List<ReportTemplate> OriginalItems { get; set; } = new();
- public DigitalFormReportGrid()
- {
- Options.BeginUpdate();
- Options.Add(DynamicGridOption.ShowHelp);
- if (Security.CanEdit<ReportTemplate>())
- {
- Options.Add(DynamicGridOption.AddRows);
- Options.Add(DynamicGridOption.EditRows);
- ActionColumns.Add(new DynamicImageColumn(ScriptImage, ScriptClick));
- ActionColumns.Add(new DynamicImageColumn(Wpf.Resources.pencil.AsBitmapImage(), DesignClick));
- }
- if (Security.CanDelete<ReportTemplate>())
- Options.Add(DynamicGridOption.DeleteRows);
- Options.EndUpdate();
- }
- public void Load(object item, Func<Type, CoreTable>? PageDataHandler)
- {
- Form = (DigitalForm)item;
- CoreTable data;
- if (Form.ID == Guid.Empty)
- {
- data = new CoreTable();
- data.LoadColumns(typeof(ReportTemplate));
- }
- else
- {
- data = new Client<ReportTemplate>()
- .Query(new Filter<ReportTemplate>(x => x.Section).IsEqualTo(Form.ID.ToString()));
- }
- OriginalItems = data.Rows.Select(x => x.ToObject<ReportTemplate>()).ToList();
- Items = OriginalItems.ToList();
- Refresh(true, true);
- Ready = true;
- }
- private DynamicVariableGrid? GetVariableGrid()
- => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicVariableGrid)
- as DynamicVariableGrid;
- private List<DigitalFormVariable> GetVariables()
- => GetVariableGrid()?.Items.ToList() ?? new List<DigitalFormVariable>();
- private DynamicFormLayoutGrid? GetLayoutGrid()
- => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicFormLayoutGrid)
- as DynamicFormLayoutGrid;
- private List<DigitalFormLayout> GetLayouts()
- => GetLayoutGrid()?.Items.ToList() ?? new List<DigitalFormLayout>();
-
- private BitmapImage? ScriptImage(CoreRow? arg)
- {
- return arg == null ? Wpf.Resources.edit.AsBitmapImage() :
- arg.Get<ReportTemplate, bool>(x => x.IsRDL) ? null : Wpf.Resources.edit.AsBitmapImage();
- }
- private bool ScriptClick(CoreRow? arg)
- {
- if (arg != null)
- {
- if (DigitalFormUtils.GetDataModel(Form.AppliesTo,GetVariables()) is not DataModel model)
- {
- Logger.Send(LogType.Error, "", "Invalid entity type for data model.");
- return false;
- }
- var template = LoadItem(arg);
- var script = template.Script;
- if (string.IsNullOrWhiteSpace(script))
- script = string.Format(ReportTemplate.DefaultScriptTemplate, model.GetType().Name.Split('.').Last());
- var editor = new ScriptEditorWindow(script);
- if (editor.ShowDialog() == true)
- {
- template.Script = editor.Script;
- SaveItem(template);
- return true;
- }
- }
- return false;
- }
- private bool DesignClick(CoreRow? arg)
- {
- if (arg is null) return false;
- if (DigitalFormUtils.GetDataModel(Form.AppliesTo,GetVariables()) is not DataModel model)
- {
- Logger.Send(LogType.Error, "", "Invalid entity type for data model.");
- return false;
- }
- var template = LoadItem(arg);
- ReportUtils.DesignReport(template, model, true, saveTemplate: (template) => SaveItem(template));
- return false;
- }
- protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
- {
- var layouts = GetLayouts();
- if(layouts.Count == 0)
- {
- base.DoAdd(OpenEditorOnDirectEdit);
- }
- else
- {
- var menu = new ContextMenu();
- foreach (var layout in layouts)
- {
- menu.AddItem($"{layout.Code}: {layout.Description}", null, layout, AddLayout);
- }
- menu.IsOpen = true;
- }
- }
-
- private void AddLayout(DigitalFormLayout layout)
- {
- var model = DigitalFormUtils.GetDataModel(Form.AppliesTo, GetVariables());
-
- var item = CreateItem();
- item.DataModel = model.Name;
- item.Name = string.IsNullOrWhiteSpace(layout.Description) ? layout.Code : layout.Description;
- item.RDL = DigitalFormUtils.GenerateReport(layout, model)?.SaveToString();
- if (EditItems(new[] { item }))
- {
- SaveItem(item);
- Refresh(false, true);
- }
- }
- public void BeforeSave(object item)
- {
- }
- public void AfterSave(object item)
- {
- // First remove any deleted files
- foreach (var original in OriginalItems)
- if (!Items.Contains(original))
- new Client<ReportTemplate>().Delete(original, typeof(ReportTemplate).Name + " Deleted by User");
- foreach (var template in Items)
- {
- template.Section = Form.ID.ToString();
- }
- new Client<ReportTemplate>().Save(Items.Where(x => x.IsChanged()), "Updated by User");
- }
- protected override ReportTemplate CreateItem()
- {
- var item = base.CreateItem();
- if(Form.ID != Guid.Empty)
- {
- item.Section = Form.ID.ToString();
- }
- return item;
- }
- public string Caption() => "Reports";
- public Size MinimumSize()
- {
- return new Size(400, 400);
- }
- public int Order()
- {
- return int.MinValue;
- }
- }
- }
|