Procházet zdrojové kódy

Digital form description expression field.

Kenric Nugteren před 1 rokem
rodič
revize
d8ecc23add

+ 24 - 0
InABox.Core/CoreUtils.cs

@@ -2857,6 +2857,30 @@ namespace InABox.Core
                 }
             }
         }
+        public static IEnumerable<TValue> NotNull<T, TValue>(this IEnumerable<T> enumerable, Func<T, TValue?> f)
+            where TValue : struct
+        {
+            foreach (var obj in enumerable)
+            {
+                var v = f(obj);
+                if (v.HasValue)
+                {
+                    yield return v.Value;
+                }
+            }
+        }
+        public static IEnumerable<TValue> NotNull<T, TValue>(this IEnumerable<T> enumerable, Func<T, TValue?> f)
+            where TValue : class
+        {
+            foreach (var obj in enumerable)
+            {
+                var v = f(obj);
+                if (v != null)
+                {
+                    yield return v;
+                }
+            }
+        }
 
         #endregion
 

+ 5 - 1
InABox.Core/DigitalForms/DFUtils.cs

@@ -227,10 +227,14 @@ namespace InABox.Core
         }
 
         public static Type? FormEntityType(DigitalForm form)
+        {
+            return FormEntityType(form.AppliesTo);
+        }
+        public static Type? FormEntityType(string appliesTo)
         {
             return CoreUtils.TypeList(
                 AppDomain.CurrentDomain.GetAssemblies(),
-                x => string.Equals(x.Name, form.AppliesTo)
+                x => string.Equals(x.Name, appliesTo)
             ).FirstOrDefault();
         }
 

+ 6 - 2
InABox.Core/DigitalForms/Forms/DigitalForm.cs

@@ -79,6 +79,10 @@ namespace InABox.Core
         [EditorSequence(7)]
         public DigitalFormGroupLink Group { get; set; }
 
+        [EditorSequence(8)]
+        [ExpressionEditor(null, ToolTip = "Evaluates to a string which becomes the description of the form when saved.")]
+        public string DescriptionExpression { get; set; } = "";
+
         [NullEditor]
         public string Report { get; set; }
         
@@ -93,8 +97,8 @@ namespace InABox.Core
         public IEntityDuplicator GetDuplicator()
         {
             var result = new EntityDuplicator<DigitalForm>();
-            result.AddChild<DigitalForm, DigitalFormVariable>(x => x.Form);
-            result.AddChild<DigitalForm, DigitalFormLayout>(x => x.Form);
+            result.AddChild<DigitalForm, DigitalFormVariable, DigitalFormLink>(x => x.Form);
+            result.AddChild<DigitalForm, DigitalFormLayout, DigitalFormLink>(x => x.Form);
             return result;
         }
 

+ 2 - 2
InABox.Poster.Timberline/TimberlinePostResult.cs

@@ -17,8 +17,8 @@ namespace InABox.Poster.Timberline
         private Dictionary<Type, List<IPostableFragment<TPostable>>> fragments = new Dictionary<Type, List<IPostableFragment<TPostable>>>();
 
         public IEnumerable<TExport> Exports => items
-            .Where(x => x.Item2 is not null && x.Item1.PostedStatus == PostedStatus.Posted)
-            .Select(x => x.Item2!);
+            .Where(x => x.Item1.PostedStatus == PostedStatus.Posted)
+            .NotNull(x => x.Item2);
 
         public IEnumerable<TPostable> PostedEntities => items.Select(x => x.Item1);
 

+ 41 - 0
inabox.wpf/DigitalForms/DigitalFormGrid.cs

@@ -15,6 +15,7 @@ using InABox.Scripting;
 using InABox.Wpf;
 using InABox.WPF;
 using Microsoft.Win32;
+using NPOI.HPSF;
 
 namespace InABox.DynamicGrid
 {
@@ -175,6 +176,8 @@ namespace InABox.DynamicGrid
             {
                 CopyForm.Visibility = Visibility.Collapsed;
             }
+
+            OnCustomiseEditor += DigitalFormGrid_OnCustomiseEditor;
         }
 
         protected override void DoReconfigure(FluentList<DynamicGridOption> options)
@@ -356,6 +359,44 @@ namespace InABox.DynamicGrid
             return pages;
         }
 
+        private DynamicVariableGrid? GetVariableGrid(IDynamicEditorForm sender)
+            => sender.Pages?.FirstOrDefault(x => x is DynamicVariableGrid)
+                as DynamicVariableGrid;
+
+        private List<DigitalFormVariable> GetVariables(IDynamicEditorForm sender)
+            => GetVariableGrid(sender)?.Items.ToList() ?? new List<DigitalFormVariable>();
+
+        // Using the event because it also has the editor form 'sender'.
+        private void DigitalFormGrid_OnCustomiseEditor(IDynamicEditorForm sender, DigitalForm[]? items, DynamicGridColumn column, BaseEditor editor)
+        {
+            if(new Column<DigitalForm>(x => x.DescriptionExpression).IsEqualTo(column.ColumnName) && editor is ExpressionEditor exp)
+            {
+                exp.OnGetVariables += () =>
+                {
+                    var variables = new List<string>();
+                    foreach (var variable in GetVariables(sender))
+                    {
+                        foreach (var col in variable.GetVariableColumns())
+                        {
+                            variables.Add($"Form_Data.{col.ColumnName}");
+                        }
+                    }
+
+                    var appliesTo = items?.Select(x => x.AppliesTo).Distinct().SingleOrDefault();
+                    if (!appliesTo.IsNullOrWhiteSpace() && DFUtils.GetFormInstanceType(appliesTo) is Type formType)
+                    {
+                        foreach(var property in DatabaseSchema.Properties(formType))
+                        {
+                            variables.Add(property.Name);
+                        }
+                    }
+
+                    variables.Sort();
+                    return variables;
+                };
+            }
+        }
+
         public override bool EditItems(DigitalForm[] items, Func<Type, CoreTable?>? PageDataHandler = null, bool PreloadPages = false)
         {
             // Need to do this to make sure that the variables are available to the layouts (and vice versa?)

+ 1 - 0
inabox.wpf/DigitalForms/DigitalFormUtils.cs

@@ -610,6 +610,7 @@ namespace InABox.DynamicGrid
         #region Data Model
         
         private static List<Type>? _entityForms;
+
         public static DataModel? GetDataModel(String appliesto, IEnumerable<DigitalFormVariable> variables)
         {
             _entityForms ??= CoreUtils.Entities