using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using Comal.Classes; using InABox.Clients; using InABox.Configuration; using InABox.Core; using InABox.DynamicGrid; using InABox.Reports; using InABox.Core.Reports; using InABox.Scripting; using InABox.Wpf.Reports; using InABox.WPF; using PRSDesktop.Configuration; using Syncfusion.Windows.Tools; using Syncfusion.Windows.Tools.Controls; namespace PRSDesktop { /// /// Interaction logic for SecondaryWindow.xaml /// public partial class SecondaryWindow : RibbonWindow, IPanelHost { private readonly Guid _id = Guid.Empty; private readonly string _modulesection = ""; private readonly IBasePanel _panel; private readonly string _type = ""; private bool bLoaded; public SecondaryWindow(Guid id, string type, string modulesection, double left, double top, double width, double height) { _id = id; _modulesection = modulesection; _type = type; InitializeComponent(); _ribbon.BackStageButton.Visibility = Visibility.Collapsed; Left = left; Top = top; Width = width; Height = height; _panel = Activator.CreateInstance(Type.GetType(type)) as IBasePanel; ContentBorder.Child = _panel as UIElement; Title = string.Format("{0} - PRS Desktop (Release {1})", modulesection, CoreUtils.GetVersion()); (_ribbon.Items[0] as RibbonTab).Caption = modulesection; } private void Window_Loaded(object sender, RoutedEventArgs e) { _panel.Setup(); _panel.Refresh(); var model = _panel.DataModel(Selection.None); var section = _panel.SectionName; ReloadModules(section, model); ReloadReports(section, model); bLoaded = true; } private void RefreshMenu_Click(object sender, RoutedEventArgs e) { _panel.Refresh(); } private void Wiki_Click(object sender, RoutedEventArgs e) { Process.Start("https://prsdigital.com.au/wiki/index.php/" + _type.Replace(" ", "_").Replace("/", "")); } #region Save / Load Window Location private void SaveSettings() { if (App.IsClosing) return; var tuple = new Tuple( _panel.GetType().EntityName(), _modulesection, Left, Top, Width, Height ); App.DatabaseSettings.SecondaryWindows[_id] = tuple; new LocalConfiguration(App.Profile).Save(App.DatabaseSettings); } private void Window_Closing(object sender, CancelEventArgs e) { if (App.IsClosing) return; App.DatabaseSettings.SecondaryWindows.Remove(_id); new LocalConfiguration(App.Profile).Save(App.DatabaseSettings); } private void Window_LocationChanged(object sender, EventArgs e) { if (bLoaded) SaveSettings(); } private void Window_SizeChanged(object sender, SizeChangedEventArgs e) { if (bLoaded) SaveSettings(); } #endregion #region Report Buttons private void ReloadReports(string section, DataModel model) { Print.Visibility = Security.IsAllowed() ? Visibility.Visible : Visibility.Collapsed; Print.LauncherButton.Tag = model; Print.IsLauncherButtonVisible = Security.IsAllowed(); Print.Items.Clear(); var worker = new BackgroundWorker(); IProgress> progress = new Progress>(report => CreateReportButton(report)); worker.DoWork += (o, e) => { var client = new Client(); var templates = client.Query( new Filter(x => x.DataModel).IsEqualTo(model.Name) .And(x => x.Section).IsEqualTo(section) .And(x => x.Visible).IsEqualTo(true), new Columns(x => x.ID, x => x.Name), new SortOrder(x => x.Name) ); foreach (var row in templates.Rows) progress.Report( new Tuple( row.Get(x => x.Name), row.Get(x => x.ID) ) ); }; worker.RunWorkerAsync(); } public void CreateReportButton(Tuple report) { Print.Visibility = Visibility.Visible; var button = new RibbonButton { Label = report.Item1, LargeIcon = PRSDesktop.Resources.printer.AsBitmapImage(), Tag = report.Item2, SizeForm = SizeForm.Large, MinWidth = 60 }; button.Click += ReportMenu_Checked; Print.Items.Add(button); } private void ReportMenu_Checked(object sender, RoutedEventArgs e) { var item = (RibbonButton)sender; var id = (Guid)item.Tag; var template = new Client().Load(new Filter(x => x.ID).IsEqualTo(id)).FirstOrDefault(); var selection = Selection.None; if (template.SelectedRecords && template.AllRecords) selection = RecordSelectionDialog.Execute(); else if (template.SelectedRecords) selection = Selection.Selected; else if (template.AllRecords) selection = Selection.All; else MessageBox.Show("Report must have either [Selected Records] or [All Records] checked to display!"); if (selection != Selection.None) ReportUtils.PreviewReport(template, _panel.DataModel(selection), false, Security.IsAllowed()); } private void ManageReportsClick(object sender, RoutedEventArgs e) { var model = _panel.DataModel(Selection.None); var section = _panel.SectionName; var form = new ReportManager { DataModel = model, Section = section }; form.ShowDialog(); ReloadReports(section, model); } #endregion #region Modules public void CreatePanelAction(PanelAction action) { var button = new RibbonButton { Label = action.Caption, LargeIcon = action.Image.AsBitmapImage(), SizeForm = SizeForm.Large, MinWidth = 60, Tag = action }; button.Click += PaneActionClick; Actions.Items.Add(button); } public void CreateSetupAction(PanelAction action) => CreatePanelAction(action); private void PaneActionClick(object sender, RoutedEventArgs e) { var button = (Control)sender; var action = (PanelAction)button.Tag; action.Execute(); } private void ReloadModules(string section, DataModel model) { if (!ClientFactory.IsSupported()) return; Actions.IsLauncherButtonVisible = Security.IsAllowed(); while (Actions.Items.Count > 2) Actions.Items.RemoveAt(Actions.Items.Count - 1); _panel.CreateToolbarButtons(this); new Client().Query( new Filter(x => x.Section).IsEqualTo(section) .And(x => x.DataModel).IsEqualTo(model.Name) .And(x => x.Visible).IsEqualTo(true), new Columns(x => x.ID) .Add(x => x.Name) .Add(x => x.Script) .Add(x => x.Thumbnail.ID), new SortOrder(x => x.Name), (modules, e1) => { if (modules != null) { var ids = modules.ExtractValues(x => x.Thumbnail.ID).ToArray(); new Client().Query( new Filter(x => x.ID).InList(ids), new Columns(x => x.ID) .Add(x => x.Data), null, (documents, e2) => { Dispatcher.Invoke(() => { CreateModules(modules, documents); CheckModuleSeparator(); }); } ); } else { Dispatcher.Invoke(() => { CheckModuleSeparator(); }); } } ); } private void CheckModuleSeparator() { Separator.Visibility = Actions.Items.Count > 2 ? Visibility.Visible : Visibility.Collapsed; } private void CreateModules(CoreTable modules, CoreTable documents) { foreach (var module in modules.Rows) { var bmp = PRSDesktop.Resources.edit; var document = documents.Rows.FirstOrDefault(r => r.Get(c => c.ID) == module.Get(c => c.Thumbnail.ID)); if (document != null) bmp = new ImageConverter().ConvertFrom(document.Get(c => c.Data)) as Bitmap; var button = new RibbonButton { Label = module.Get(c => c.Name), LargeIcon = bmp.AsBitmapImage(), SizeForm = SizeForm.Large, MinWidth = 60, Tag = module.Get(c => c.Script) }; button.Click += Module_Click; Actions.Items.Add(button); } } private void Module_Click(object sender, RoutedEventArgs e) { var item = (RibbonButton)sender; var code = (string)item.Tag; if (!string.IsNullOrWhiteSpace(code)) try { var script = new ScriptDocument(code); if (script.Compile()) { script.SetValue("Data", _panel.Selected()); var result = script.Execute(); if (result) _panel.Refresh(); } else { MessageBox.Show("Unable to Compile Script!"); } } catch (Exception err) { MessageBox.Show(CoreUtils.FormatException(err)); } else MessageBox.Show("Unable to load " + item.Label); } private void ManageModulesClick(object sender, RoutedEventArgs e) { var model = _panel.DataModel(Selection.None); var section = _panel.SectionName; var manager = new CustomModuleManager { DataModel = model, Section = section }; manager.ShowDialog(); ReloadModules(section, model); } #endregion } }