EventGrid.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.DynamicGrid;
  5. using InABox.WPF;
  6. using PRS.Shared.Events;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. namespace PRS.Shared;
  18. public class EventGrid : DynamicDataGrid<Event>
  19. {
  20. private readonly BitmapImage _email = InABox.Wpf.Resources.email.AsBitmapImage();
  21. private readonly BitmapImage _notification = InABox.Wpf.Resources.notification.AsBitmapImage();
  22. private readonly BitmapImage _edit = InABox.Wpf.Resources.pencil.AsBitmapImage();
  23. private Button? EnableButton = null;
  24. private Dictionary<Guid, EventSubscriberType> _subscribedSet = new();
  25. public Guid EmployeeID { get; set; }
  26. protected override void Init()
  27. {
  28. base.Init();
  29. HiddenColumns.Add(x => x.Data);
  30. HiddenColumns.Add(x => x.Enabled);
  31. HiddenColumns.Add(x => x.Visible);
  32. ActionColumns.Add(new DynamicMenuColumn(BuildMenu, getImage: Menu_GetImage) { ToolTip = Subscribed_ToolTip });
  33. if (Security.IsAllowed<CanManageEvents>())
  34. {
  35. ActionColumns.Add(new DynamicImageColumn(_edit, Edit_Click));
  36. EnableButton = AddButton("Disable", null, Enable_Click);
  37. EnableButton.Visibility = Visibility.Collapsed;
  38. }
  39. }
  40. protected override void SelectItems(CoreRow[]? rows)
  41. {
  42. base.SelectItems(rows);
  43. if(EnableButton is not null)
  44. {
  45. if(rows is not null && rows.Length > 0)
  46. {
  47. EnableButton.Visibility = Visibility.Visible;
  48. EnableButton.Content = rows.Any(x => !x.Get<Event, bool>(x => x.Enabled))
  49. ? "Enable" : "Disable";
  50. }
  51. else
  52. {
  53. EnableButton.Visibility = Visibility.Collapsed;
  54. }
  55. }
  56. }
  57. private bool Enable_Click(Button button, CoreRow[] rows)
  58. {
  59. var items = LoadItems(rows);
  60. if(items.Any(x => !x.Enabled))
  61. {
  62. foreach(var item in items)
  63. {
  64. item.Enabled = true;
  65. }
  66. Client.Save(items, "Event enabled.");
  67. return true;
  68. }
  69. else
  70. {
  71. foreach(var item in items)
  72. {
  73. item.Enabled = false;
  74. }
  75. Client.Save(items, "Event disabled.");
  76. return true;
  77. }
  78. }
  79. #region UIComponent
  80. private class UIComponent : DynamicGridGridUIComponent<Event>
  81. {
  82. protected override Brush? GetCellBackground(CoreRow row, DynamicColumnBase column)
  83. {
  84. if(row.Get<Event, bool>(x => x.Enabled))
  85. {
  86. return null;
  87. }
  88. else
  89. {
  90. return Colors.Gainsboro.ToBrush(0.5);
  91. }
  92. }
  93. }
  94. protected override IDynamicGridUIComponent CreateUIComponent()
  95. {
  96. return new UIComponent { Parent = this };
  97. }
  98. #endregion
  99. protected override void DoReconfigure(DynamicGridOptions options)
  100. {
  101. base.DoReconfigure(options);
  102. if (Security.IsAllowed<CanManageEvents>())
  103. {
  104. options.AddRows = true;
  105. options.EditRows = true;
  106. options.DeleteRows = true;
  107. }
  108. else
  109. {
  110. options.AddRows = false;
  111. options.EditRows = false;
  112. options.DeleteRows = false;
  113. }
  114. }
  115. #region Action Columns
  116. private FrameworkElement? Subscribed_ToolTip(DynamicActionColumn column, CoreRow? row)
  117. {
  118. if(row is null)
  119. {
  120. return column.TextToolTip("Are you subscribed to this event?");
  121. }
  122. else
  123. {
  124. return _subscribedSet.TryGetValue(row.Get<Event, Guid>(x => x.ID), out var type)
  125. ? (type == EventSubscriberType.Notification
  126. ? column.TextToolTip("You are subscribed to this event by notification.")
  127. : column.TextToolTip("You are subscribed to this event by email."))
  128. : null;
  129. }
  130. }
  131. private void BuildMenu(DynamicMenuColumn column, CoreRow? row)
  132. {
  133. if (row is null) return;
  134. var menu = column.GetMenu();
  135. var eventID = row.Get<Event, Guid>(x => x.ID);
  136. if (_subscribedSet.TryGetValue(eventID, out var type))
  137. {
  138. menu.AddItem("Unsubscribe", null, row, Unsubscribe);
  139. if(type == EventSubscriberType.Notification)
  140. {
  141. menu.AddItem("Subscribe by Email", null, (row, EventSubscriberType.Email), ChangeSubscription);
  142. }
  143. else
  144. {
  145. menu.AddItem("Subscribe by Notification", null, (row, EventSubscriberType.Notification), ChangeSubscription);
  146. }
  147. }
  148. else
  149. {
  150. menu.AddItem("Subscribe by Notification", null, (row, EventSubscriberType.Notification), Subscribe);
  151. menu.AddItem("Subscribe by Email", null, (row, EventSubscriberType.Email), Subscribe);
  152. }
  153. }
  154. private BitmapImage? Menu_GetImage(CoreRow? row)
  155. {
  156. if (row is null) return _notification;
  157. return _subscribedSet.TryGetValue(row.Get<Event, Guid>(x => x.ID), out var type)
  158. ? (type == EventSubscriberType.Notification ? _notification : _email)
  159. : null;
  160. }
  161. private void ChangeSubscription((CoreRow row, EventSubscriberType Notification) tuple)
  162. {
  163. Unsubscribe(tuple.row, false);
  164. Subscribe(tuple);
  165. }
  166. private void Subscribe((CoreRow row, EventSubscriberType type) tuple)
  167. {
  168. var ev = tuple.row.ToObject<Event>();
  169. var subscriber = new EventSubscriber();
  170. subscriber.Event.CopyFrom(ev);
  171. subscriber.Employee.ID = EmployeeID;
  172. subscriber.SubscriberType = tuple.type;
  173. Client.Save(subscriber, "");
  174. _subscribedSet.Add(ev.ID, subscriber.SubscriberType);
  175. Refresh(false, true);
  176. }
  177. private void Unsubscribe(CoreRow row)
  178. {
  179. Unsubscribe(row, true);
  180. }
  181. private void Unsubscribe(CoreRow row, bool refresh)
  182. {
  183. var eventID = row.Get<Event, Guid>(x => x.ID);
  184. var subscriber = Client.Query(
  185. new Filter<EventSubscriber>(x => x.Employee.ID).IsEqualTo(EmployeeID)
  186. .And(x => x.Event.ID).IsEqualTo(eventID),
  187. Columns.None<EventSubscriber>().Add(x => x.ID))
  188. .ToObjects<EventSubscriber>().FirstOrDefault();
  189. if(subscriber is not null)
  190. {
  191. Client.Delete(subscriber, "");
  192. }
  193. _subscribedSet.Remove(eventID);
  194. if (refresh)
  195. {
  196. Refresh(false, true);
  197. }
  198. }
  199. private bool Edit_Click(CoreRow? row)
  200. {
  201. if (row is null) return false;
  202. var ev = row.ToObject<Event>();
  203. IEventData? data = null;
  204. if(ev.Data is not null && ev.Data.Length != 0)
  205. {
  206. data = EventUtils.Deserialize(ev.Data);
  207. }
  208. var type = ev.EventType;
  209. if(EventEditor.Run(ref type, ref data))
  210. {
  211. ev.EventType = type;
  212. if(data is not null)
  213. {
  214. ev.Data = EventUtils.Serialize(data);
  215. }
  216. else
  217. {
  218. ev.Data = "";
  219. }
  220. SaveItem(ev);
  221. return true;
  222. }
  223. else
  224. {
  225. return false;
  226. }
  227. }
  228. #endregion
  229. private readonly Column<Event> EventTypeColumn = new(x => x.EventType);
  230. private readonly Column<Event> NotificationExpressionColumn = new(x => x.NotificationExpression);
  231. private IEventData? EventData;
  232. protected override void BeforeLoad(IDynamicEditorForm form, Event[] items)
  233. {
  234. base.BeforeLoad(form, items);
  235. form.ReadOnly = !Security.IsAllowed<CanManageEvents>();
  236. var ev = items.First();
  237. IEventData? data = null;
  238. if(ev.Data is not null && ev.Data.Length != 0)
  239. {
  240. data = EventUtils.Deserialize(ev.Data);
  241. }
  242. EventData = data;
  243. }
  244. protected override void CustomiseEditor(IDynamicEditorForm form, Event[] items, DynamicGridColumn column, BaseEditor editor)
  245. {
  246. base.CustomiseEditor(form, items, column, editor);
  247. if(EventTypeColumn.IsEqualTo(column.ColumnName) && editor is EnumLookupEditor enumEditor)
  248. {
  249. var ev = items.First();
  250. var editButton = new EditorButton(ev, "Edit Event", 100, (e, i) => EditEvent_Click(form, e, i), false);
  251. enumEditor.Buttons = [editButton];
  252. }
  253. else if(NotificationExpressionColumn.IsEqualTo(column.ColumnName) && editor is ExpressionEditor exprEditor)
  254. {
  255. exprEditor.OnGetVariables += ExprEditor_OnGetVariables;
  256. }
  257. }
  258. private IEnumerable<string> ExprEditor_OnGetVariables()
  259. {
  260. return EventData?.Event.DataModelDefinition().GetVariables().Select(x => x.Name) ?? [];
  261. }
  262. private void EditEvent_Click(IDynamicEditorForm form, object editor, object? item)
  263. {
  264. if (item is not Event ev) return;
  265. var type = ev.EventType;
  266. var data = EventData;
  267. if(EventEditor.Run(ref type, ref data))
  268. {
  269. EventData = data;
  270. ev.EventType = type;
  271. form.SetEditorValue(nameof(Event.EventType), type);
  272. if(data is not null)
  273. {
  274. ev.Data = EventUtils.Serialize(data);
  275. }
  276. else
  277. {
  278. ev.Data = "";
  279. }
  280. }
  281. else
  282. {
  283. // We have to 'cancel' by just re-deserialising.
  284. if(ev.Data is not null && ev.Data.Length != 0)
  285. {
  286. EventData = EventUtils.Deserialize(ev.Data);
  287. }
  288. }
  289. }
  290. protected override void Reload(Filters<Event> criteria, Columns<Event> columns, ref SortOrder<Event>? sort, CancellationToken token, Action<CoreTable?, Exception?> action)
  291. {
  292. if (!Security.IsAllowed<CanManageEvents>())
  293. {
  294. criteria.Add(new Filter<Event>(x => x.Visible).IsEqualTo(true));
  295. }
  296. base.Reload(criteria, columns, ref sort, token, (data, error) =>
  297. {
  298. if(data is not null)
  299. {
  300. var ids = data.ExtractValues<Event, Guid>(x => x.ID);
  301. _subscribedSet = Client.Query(
  302. new Filter<EventSubscriber>(x => x.Employee.ID).IsEqualTo(EmployeeID)
  303. .And(x => x.Event.ID).InList(ids),
  304. Columns.None<EventSubscriber>().Add(x => x.Event.ID).Add(x => x.SubscriberType))
  305. .ToObjects<EventSubscriber>().ToDictionary(x => x.Event.ID, x => x.SubscriberType);
  306. }
  307. action(data, error);
  308. });
  309. }
  310. }