EventGrid.cs 9.8 KB

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