123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.Core.Postable;
- using InABox.DynamicGrid;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- namespace PRSDesktop
- {
- public static class PostUtils
- {
- private static readonly Inflector.Inflector inflector = new(new CultureInfo("en"));
- public static void PostEntities<T>(IDataModel<T> model, Action refresh, Action? configurePost = null)
- where T : Entity, IPostable, IRemotable, IPersistent, new()
- {
- try
- {
- if (PosterUtils.Process(model))
- {
- MessageBox.Show("Processing successful!");
- refresh();
- }
- else
- {
- MessageBox.Show("Processing failed.");
- refresh();
- }
- }
- catch (EmptyPostException)
- {
- MessageBox.Show($"Please select at least one {typeof(T).Name}.");
- }
- catch (RepostedException)
- {
- MessageBox.Show("At least one of the items you selected has already been processed. Processing cancelled.");
- }
- catch (PostCancelledException)
- {
- MessageBox.Show("Processing cancelled.");
- }
- catch (MissingSettingsException)
- {
- if (configurePost is not null && Security.CanConfigurePost<T>())
- {
- if (MessageBox.Show($"Processing has not been configured for {inflector.Pluralize(typeof(T).Name)}. Would you like to configure this now?",
- "Configure Processing?", MessageBoxButton.YesNoCancel) == MessageBoxResult.Yes)
- {
- configurePost();
- }
- else
- {
- MessageBox.Show("Processing cancelled.");
- }
- }
- else
- {
- MessageBox.Show($"Processing has not been configured for {inflector.Pluralize(typeof(T).Name)}!");
- }
- }
- catch (Exception e)
- {
- MessageBox.Show($"Processing failed: {e.Message}");
- refresh();
- }
- }
- public static void CreateToolbarButtons<T>(IPanelHost host, Func<IDataModel<T>> model, Action refresh, Action? configurePost = null)
- where T : Entity, IPostable, IRemotable, IPersistent, new()
- {
- var postSettings = PosterUtils.LoadPostableSettings<T>();
- if (Security.CanPost<T>() && !postSettings.PosterType.IsNullOrWhiteSpace())
- {
- Bitmap? image = null;
- if (postSettings.Thumbnail.ID != Guid.Empty)
- {
- var icon = new Client<Document>()
- .Load(new Filter<Document>(x => x.ID).IsEqualTo(postSettings.Thumbnail.ID)).FirstOrDefault();
- if (icon is not null)
- {
- image = new ImageConverter().ConvertFrom(icon.Data) as Bitmap;
- }
- }
- host.CreatePanelAction(new PanelAction
- {
- Caption = postSettings.ButtonName.NotWhiteSpaceOr($"Process {inflector.Pluralize(typeof(T).Name)}"),
- Image = image ?? PRSDesktop.Resources.edit,
- OnExecute = action =>
- {
- PostEntities(
- model(),
- refresh,
- configurePost);
- }
- });
- }
- if (configurePost is not null && Security.CanConfigurePost<T>())
- {
- host.CreateSetupAction(new PanelAction
- {
- Caption = $"Configure {typeof(T).Name} Processing",
- OnExecute = action =>
- {
- configurePost();
- }
- });
- }
- }
- public static void ConfigurePost<T>()
- where T : Entity, IPostable, IRemotable, IPersistent, new()
- {
- var postSettings = PosterUtils.LoadPostableSettings<T>();
- var grid = (DynamicGridUtils.CreateDynamicGrid(typeof(DynamicGrid<>), typeof(PostableSettings)) as DynamicGrid<PostableSettings>)!;
- if (grid.EditItems(new PostableSettings[] { postSettings }))
- {
- PosterUtils.SavePostableSettings<T>(postSettings);
- }
- }
- public static void CreateToolbarButtons<T>(IPanelHost host, Func<IDataModel<T>> model, Action refresh, bool allowConfig)
- where T : Entity, IPostable, IRemotable, IPersistent, new()
- {
- CreateToolbarButtons(host, model, refresh, allowConfig ? ConfigurePost<T> : null);
- }
- }
- }
|