Kaynağa Gözat

Added ExportExpression to Digital Forms

frogsoftware 1 yıl önce
ebeveyn
işleme
df51bd8804

+ 25 - 19
InABox.Core/DigitalForms/DataModel/DigitalFormDataModel.cs

@@ -208,30 +208,36 @@ namespace InABox.Core
             Instance.Form.ID = formid;
         }
 
-        private void DoUpdate()
+        public string? EvaluateExpression(string expression)
         {
-            if (!String.IsNullOrWhiteSpace(this.Instance.Form.DescriptionExpression))
-            {
-                Dictionary<string, object?> variables = new Dictionary<string, object?>();
+            if (string.IsNullOrWhiteSpace(expression))
+                return null;
+            
+            Dictionary<string, object?> variables = new Dictionary<string, object?>();
                 
-                var formData = DigitalForm.DeserializeFormData(Instance);
-                foreach (var item in formData.Items())
-                    variables[$"Data.{item.Key}"] = item.Value;
+            var formData = DigitalForm.DeserializeFormData(Instance);
+            foreach (var item in formData.Items())
+                variables[$"Data.{item.Key}"] = item.Value;
                 
-                var instancedata = Instance.GetValues(true);
-                foreach (var item in instancedata)
-                    variables[$"Form.{item.Key}"] = item.Value;
+            var instancedata = Instance.GetValues(true);
+            foreach (var item in instancedata)
+                variables[$"Form.{item.Key}"] = item.Value;
                 
-                var entitydata = Entity.GetValues(true);
-                foreach (var item in entitydata)
-                    variables[$"{typeof(TEntity).EntityName().Split('.').Last()}.{item.Key}"] = item.Value;
+            var entitydata = Entity.GetValues(true);
+            foreach (var item in entitydata)
+                variables[$"{typeof(TEntity).EntityName().Split('.').Last()}.{item.Key}"] = item.Value;
                 
-                var expression = new CoreExpression(Instance.Form.DescriptionExpression);
-                var result = expression.Evaluate(variables)?.ToString();
-                Instance.Description = !String.IsNullOrWhiteSpace(result) 
-                    ? result 
-                    : Instance.Form.Description;
-            }
+            var exp = new CoreExpression(expression);
+            return exp.Evaluate(variables)?.ToString();
+        }
+
+        private void DoUpdate()
+        {
+
+            var result = EvaluateExpression(Instance.Form.DescriptionExpression);
+            Instance.Description = !String.IsNullOrWhiteSpace(result) 
+                ? result 
+                : Instance.Form.Description;
             
             BeforeModelSaved?.Invoke(this);
 

+ 41 - 0
InABox.Core/DigitalForms/DigitalFormReportDataModel.cs

@@ -119,5 +119,46 @@ namespace InABox.Core
 
             Load(typeof(CoreTable), FormDataTable!, requiredtables, "Form_Data");
         }
+        
+        public string? EvaluateExpression(string expression)
+        {
+            if (string.IsNullOrWhiteSpace(expression))
+                return null;
+            
+            Dictionary<string, object?> variables = new Dictionary<string, object?>();
+
+            var formrow = FormDataTable.Rows.FirstOrDefault();
+            foreach (var column in FormDataTable.Columns)
+                variables[$"Data.{column.ColumnName}"] = formrow?[column.ColumnName];
+
+            var instancetable = GetTable<T>();
+            var instancerow = instancetable.Rows.FirstOrDefault();
+            foreach (var column in instancetable.Columns)
+                variables[$"Form.{column.ColumnName}"] = instancerow?[column.ColumnName];
+                
+            var inter = typeof(T)
+                .GetInterfaces()
+                .FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IDigitalFormInstance<>));
+            if (inter != null)
+            {
+                var link = inter.GenericTypeArguments[0];
+                var entityLinkDef = link.GetSuperclassDefinition(typeof(EntityLink<>));
+                if (entityLinkDef != null)
+                {
+                    var entityType = entityLinkDef.GenericTypeArguments[0];
+                    var parenttablename = $"{DataModel.TableName(typeof(T))}_{DataModel.TableName(entityType)}";
+                    var entitytable = GetDataModelTable(parenttablename);
+                    if (entitytable != null)
+                    {
+                        foreach (var column in entitytable.Table.Columns)
+                            variables[$"{entityType.EntityName().Split('.').Last()}.{column.ColumnName}"] =
+                                instancerow?[column.ColumnName];
+                    }
+                }
+            }
+            
+            var exp = new CoreExpression(expression);
+            return exp.Evaluate(variables)?.ToString();
+        }
     }
 }

+ 4 - 0
InABox.Core/DigitalForms/Forms/DigitalForm.cs

@@ -82,6 +82,10 @@ namespace InABox.Core
         [EditorSequence(8)]
         [ExpressionEditor(null, ToolTip = "Evaluates to a string which becomes the description of the form when saved.")]
         public string DescriptionExpression { get; set; } = "";
+        
+        [EditorSequence(9)]
+        [ExpressionEditor(null, ToolTip = "Evaluates to a string which becomes the filename of the form when saved.")]
+        public string ExportExpression { get; set; } = "";
 
         [NullEditor]
         public string Report { get; set; }

+ 1 - 1
inabox.wpf/DigitalForms/DigitalFormGrid.cs

@@ -378,7 +378,7 @@ namespace InABox.DynamicGrid
         // 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)
+            if(editor is ExpressionEditor exp && ( new Column<DigitalForm>(x => x.DescriptionExpression).IsEqualTo(column.ColumnName) || new Column<DigitalForm>(x => x.ExportExpression).IsEqualTo(column.ColumnName)))
             {
                 exp.OnGetVariables += () =>
                 {