Przeglądaj źródła

PRS Classes and Desktop - Email Template Manager (INCOMPLETE)

Nick-PRSDigital@bitbucket.org 2 lat temu
rodzic
commit
3051d610de

+ 4 - 0
prs.classes/Entities/Supplier/Supplier.cs

@@ -61,6 +61,9 @@ namespace Comal.Classes
         [TextBoxEditor]
         public string Name { get; set; }
 
+        [TextBoxEditor]
+        public string Email { get; set; }
+
         public Address Delivery { get; set; }
 
         public Address Postal { get; set; }
@@ -91,6 +94,7 @@ namespace Comal.Classes
             base.Init();
             Delivery = new Address();
             Postal = new Address();
+            Email = "";
         }
     }
 }

+ 1 - 1
prs.desktop/Configuration/CustomModuleGrid.cs

@@ -5,7 +5,7 @@ using InABox.Clients;
 using InABox.Core;
 using InABox.DynamicGrid;
 using InABox.WPF;
-using ScriptEditor = InABox.DynamicGrid.ScriptEditor;
+using ScriptEditor = InABox.DynamicGrid.ScriptEditorWindow;
 
 namespace PRSDesktop.Configuration
 {

+ 1 - 1
prs.desktop/Configuration/DatabaseScriptGrid.cs

@@ -5,7 +5,7 @@ using InABox.Clients;
 using InABox.Core;
 using InABox.DynamicGrid;
 using InABox.WPF;
-using ScriptEditor = InABox.DynamicGrid.ScriptEditor;
+using ScriptEditor = InABox.DynamicGrid.ScriptEditorWindow;
 
 namespace PRSDesktop
 {

+ 16 - 0
prs.desktop/EmailTemplateManagerWindow.cs

@@ -0,0 +1,16 @@
+using InABox.Core;
+using InABox.Wpf;
+
+namespace PRSDesktop
+{
+    public class EmailTemplateManagerWindow : ThemableWindow
+    {
+        public EmailTemplateManagerWindow(DataModel model)
+        {
+            var grid = new DataModelTemplateGrid();
+            grid.Model = model;
+            grid.Refresh(true, true);
+            Content = grid;
+        }
+    }
+}

+ 30 - 14
prs.desktop/Grids/DataModelTemplateGrid.cs

@@ -6,7 +6,8 @@ using InABox.Clients;
 using InABox.Core;
 using InABox.DynamicGrid;
 using InABox.WPF;
-using ScriptEditor = InABox.DynamicGrid.ScriptEditor;
+using static InABox.Core.DataModelTemplate;
+using ScriptEditorWindow = InABox.DynamicGrid.ScriptEditorWindow;
 
 namespace PRSDesktop
 {
@@ -16,21 +17,35 @@ namespace PRSDesktop
 
         public DataModelTemplateGrid()
         {
-            ActionColumns.Add(new DynamicImageColumn(EditImage, EditAction));
             HiddenColumns.Add(x => x.Template);
-        }
 
-        public DataModel Model { get; set; }
+            OnCustomiseEditor += DataModelTemplateGrid_OnCustomiseEditor;
+        }
 
-        private BitmapImage EditImage(CoreRow row)
+        private void DataModelTemplateGrid_OnCustomiseEditor(IDynamicEditorForm sender, DataModelTemplate[]? items, DynamicGridColumn column, BaseEditor editor)
         {
-            return _edit;
+            if (editor == null)
+                return;
+
+            if (column.ColumnName.Equals("To") || 
+                column.ColumnName.Equals("CC") ||
+                column.ColumnName.Equals("BCC") ||
+                column.ColumnName.Equals("Subject") ||
+                column.ColumnName.Equals("AttachmentName")
+                )
+                (editor as ExpressionEditor).OnGetVariables += EmailTemplateManagerWindow_OnGetVariables;
+
+            //else if (column.ColumnName.Equals("Template") && items.Count() == 1)
+            //    (editor as ScriptEditor).OnScriptEditorCalled += () =>
+            //    {
+            //        EditTemplate(items);
+            //    };
         }
 
-        private bool EditAction(CoreRow row)
+        private void EditTemplate(DataModelTemplate[] items)
         {
-            var template = row.ToObject<DataModelTemplate>();
-            var editor = new ScriptEditor(template.Template, SyntaxLanguage.HTML);
+            var template = items[0];
+            var editor = new ScriptEditorWindow(template.Template, SyntaxLanguage.HTML);
 
             var snippets = new Dictionary<string, string[]>();
             var tables = Model.AsDictionary;
@@ -48,19 +63,20 @@ namespace PRSDesktop
                     cursnippets.Add("{{ " + variable + "." + prop.Name + " }}");
                 snippets[name] = cursnippets.ToArray();
             }
-
-
             editor.Snippets = snippets;
             if (editor.ShowDialog() == true)
             {
                 template.Template = editor.Script;
-                new Client<DataModelTemplate>().Save(template, "Updated by User");
-                return true;
             }
+        }
 
-            return false;
+        private DataModel EmailTemplateManagerWindow_OnGetVariables()
+        {
+            return Model;
         }
 
+        public DataModel Model { get; set; }
+
         protected override void Reload(Filters<DataModelTemplate> criteria, Columns<DataModelTemplate> columns, ref SortOrder<DataModelTemplate> sort,
             Action<CoreTable, Exception> action)
         {

+ 65 - 3
prs.desktop/MainWindow.xaml.cs

@@ -69,6 +69,7 @@ using ValidationResult = InABox.Clients.ValidationResult;
 using System.Diagnostics.CodeAnalysis;
 using System.Runtime.InteropServices;
 using FastReport.Export.Email;
+using InABox.Wpf;
 
 namespace PRSDesktop
 {
@@ -491,8 +492,11 @@ namespace PRSDesktop
             AddSetupSeparator(tab);
             AddSetupAction(tab, "Custom Modules", ManageModules, PRSDesktop.Resources.script).SetSecurityToken<CanCustomiseModules>();
             AddSetupAction(tab, "Reports", ManageReports, PRSDesktop.Resources.printer).SetSecurityToken<CanDesignReports>();
+            AddSetupAction(tab, "Email Templates", ManageEmailTemplates, PRSDesktop.Resources.email).SetSecurityToken<CanDesignReports>();
         }
 
+
+
         private void ConfigureMainScreen()
         {
 
@@ -1791,10 +1795,51 @@ namespace PRSDesktop
         #region Email
 
         private void DoEmailReport(DataModel model, byte[] data)
+        {           
+            CheckTemplates(model, data);           
+        }
+
+        private void CheckTemplates(DataModel model, byte[] data)
         {
             string attachmentName = DetermineName(model);
 
-            EmailUtils.CreateEMLFile(attachmentName, data, App.EmployeeEmail, "Emailing report for " + attachmentName);
+            //this section is meant to be for parsing templates into emails - not working yet!
+
+            //var templates = new Client<DataModelTemplate>().Query(new Filter<DataModelTemplate>(x => x.Model).IsEqualTo(model.Name));
+            //if (templates.Rows.Any())
+            //{
+            //    var context = new ContextMenu();
+            //    foreach (var row in templates.Rows)
+            //    {
+            //        var menu = new MenuItem
+            //        {
+            //            Header = row.Get<DataModelTemplate, string>(x => x.Name),
+            //            Tag = row
+            //        };
+            //        menu.Click += (o, args) =>
+            //        {
+            //            var chosenrow = ((MenuItem)o).Tag as CoreRow;                       
+            //            try
+            //            {
+            //                var data = chosenrow.Get<DataModelTemplate, string>(x => x.Template);
+            //                var to = chosenrow.Get<DataModelTemplate, string>(x => x.To);
+
+            //                var body = DataModelUtils.ParseTemplate(model, data);
+
+            //            }
+            //            catch (Exception err)
+            //            {
+            //                MessageBox.Show("Unable to Parse Template!\n\n" + err.Message);
+            //            }
+            //        };
+            //        context.Items.Add(menu);
+            //    }
+            //    context.IsOpen = true;
+            //}
+            //else
+            //{              
+                EmailUtils.CreateEMLFile(attachmentName, data, App.EmployeeEmail, "Emailing report for " + attachmentName);
+            //}                
         }
 
         private string DetermineName(DataModel model)
@@ -1814,11 +1859,11 @@ namespace PRSDesktop
                 else if (table.Rows.Count > 1)
                 {
                     foreach (CoreRow row in table.Rows)
-                    {                     
+                    {
                         title = title + row.Get<PurchaseOrder, string>(x => x.PONumber) + ", ";
                     }
                     title = title.Substring(0, title.Length - 2);
-                }                
+                }
             }
 
             return title;
@@ -3764,6 +3809,23 @@ namespace PRSDesktop
             ReloadReports(section, model);
         }
 
+        private void ManageEmailTemplates()
+        {
+            if (CurrentTab is null || CurrentPanel is null)
+                return;
+
+            var section = CurrentPanel.SectionName;
+            var model = CurrentPanel.DataModel(Selection.None);
+            if (model == null)
+            {
+                MessageBox.Show("No DataModel for " + CurrentTab.Header);
+                return;
+            }
+
+            var window = new EmailTemplateManagerWindow(model);
+            window.ShowDialog();
+        }
+
         private void ManageReportsMenu_Click(object sender, RoutedEventArgs e)
         {
             ManageReports();