NotificationPanel.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media.Imaging;
  9. using Comal.Classes;
  10. using InABox.Clients;
  11. using InABox.Core;
  12. using InABox.DynamicGrid;
  13. using InABox.WPF;
  14. using InABox.Wpf;
  15. using System.ComponentModel;
  16. using sun.net.www;
  17. using Syncfusion.Windows.Controls.RichTextBoxAdv;
  18. using static ICSharpCode.AvalonEdit.Document.TextDocumentWeakEventManager;
  19. using SelectionChangedEventArgs = System.Windows.Controls.SelectionChangedEventArgs;
  20. namespace PRSDesktop;
  21. /// <summary>
  22. /// Interaction logic for NotificationPanel.xaml
  23. /// </summary>
  24. public partial class NotificationPanel : UserControl, IPanel<Notification>
  25. {
  26. private readonly Button Archive;
  27. private readonly Button AttachToJob;
  28. private readonly Button CreateDelivery;
  29. private readonly Button CreateRequi;
  30. private readonly Button CreateSetout;
  31. private readonly Button CreateTask;
  32. private readonly List<Tuple<string, BitmapImage>> folders = new()
  33. {
  34. new("Inbox", PRSDesktop.Resources.download.AsBitmapImage()),
  35. new("Sent", PRSDesktop.Resources.upload.AsBitmapImage()),
  36. new("Archive", PRSDesktop.Resources.box.AsBitmapImage())
  37. };
  38. private readonly Button Forward;
  39. private readonly Employee me = new Client<Employee>().Load(new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid))
  40. .FirstOrDefault();
  41. private readonly DynamicDataGrid<Notification> Notifications;
  42. private readonly Button Reopen;
  43. //Button WriteNew = null;
  44. private readonly Button Reply;
  45. private readonly Button ViewEntity;
  46. private readonly Button ViewForm;
  47. private readonly Button ViewJob;
  48. public NotificationPanel()
  49. {
  50. InitializeComponent();
  51. Notifications = new DynamicDataGrid<Notification>();
  52. Notifications.HiddenColumns.Add(x => x.Sender.ID);
  53. Notifications.HiddenColumns.Add(x => x.Employee.ID);
  54. Notifications.HiddenColumns.Add(x => x.Job.ID);
  55. Notifications.HiddenColumns.Add(x => x.EntityType);
  56. Notifications.HiddenColumns.Add(x => x.EntityID);
  57. //Notifications.HiddenColumns.Add(x => x.Kanban.ID);
  58. //Notifications.HiddenColumns.Add(x => x.Setout.ID);
  59. //Notifications.HiddenColumns.Add(x => x.Requisition.ID);
  60. //Notifications.HiddenColumns.Add(x => x.Delivery.ID);
  61. //Notifications.HiddenColumns.Add(x => x.LeaveRequestLink.ID);
  62. Notifications.HiddenColumns.Add(x => x.Closed);
  63. Notifications.Reconfigure(options =>
  64. {
  65. options.RecordCount = true;
  66. options.FilterRows = true;
  67. options.SelectColumns = true;
  68. options.MultiSelect = true;
  69. });
  70. Notifications.SetValue(Grid.RowProperty, 0);
  71. Notifications.SetValue(Grid.ColumnProperty, 1);
  72. Notifications.Margin = new Thickness(0);
  73. Notifications.OnReload += Notifications_OnReload;
  74. Notifications.OnSelectItem += Notifications_OnSelectItem;
  75. Notifications.OnValidate += Notifications_OnValidate;
  76. Notifications.OnCreateItem += Notifications_OnCreateItem;
  77. //WriteNew = CreateButton("Write New", null, WriteNewClick);
  78. //WriteNew.Visibility = Visibility.Visible;
  79. //WriteNew.Margin = new Thickness(WriteNew.Margin.Left, WriteNew.Margin.Top, 10.0F, WriteNew.Margin.Bottom);
  80. Reply = CreateButton("Reply", null, ReplyClick);
  81. Forward = CreateButton("Forward", null, ForwardClick);
  82. ViewForm = CreateButton("View Form", null, ViewFormClick);
  83. ViewEntity = CreateButton("View Item", null, ViewEntityClick);
  84. AttachToJob = CreateButton("Attach To Job", null, AttachToJobClick);
  85. ViewJob = CreateButton("View Job", null, ViewJobClick);
  86. CreateTask = CreateButton("Create Task", null, CreateTaskClick);
  87. CreateSetout = CreateButton("Create Setout", null, CreateSetoutClick);
  88. CreateRequi = CreateButton("Create Requi", null, CreateRequiClick);
  89. CreateDelivery = CreateButton("Create Delivery", null, CreateDeliveryClick);
  90. Archive = CreateButton("Archive", null, ArchiveClick);
  91. Archive.Margin = new Thickness(10.0F, Archive.Margin.Top, Archive.Margin.Right, Archive.Margin.Bottom);
  92. Reopen = CreateButton("Reopen", null, ReopenClick);
  93. Reopen.Margin = new Thickness(10.0F, Reopen.Margin.Top, Reopen.Margin.Right, Reopen.Margin.Bottom);
  94. Layout.Children.Add(Notifications);
  95. Folders.ItemsSource = folders;
  96. }
  97. private void Notifications_OnCreateItem(object sender, object item)
  98. {
  99. if (item is not Notification notification)
  100. return;
  101. notification.Sender.ID = me.ID;
  102. }
  103. private void Notifications_OnValidate(object sender, Notification[] items, List<string> errors)
  104. {
  105. foreach(var item in items)
  106. {
  107. if(!item.Sender.IsValid())
  108. {
  109. errors.Add("[Sender] may not be blank!");
  110. }
  111. if(!item.Employee.IsValid())
  112. {
  113. errors.Add("[Employee] may not be blank!");
  114. }
  115. }
  116. }
  117. public bool IsReady { get; set; }
  118. public event DataModelUpdateEvent? OnUpdateDataModel;
  119. private Button CreateButton(string caption, BitmapImage? umage, DynamicGridButtonClickEvent action)
  120. {
  121. var result = Notifications.AddButton(caption, umage, action);
  122. result.Width = 100.0F;
  123. result.Visibility = Visibility.Collapsed;
  124. return result;
  125. }
  126. private void Notifications_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  127. {
  128. var singlerow = e.Rows?.Length == 1;
  129. //WriteNew.Visibility = (Folders.SelectedIndex == 0) ? Visibility.Visible : Visibility.Collapsed;
  130. var row = e.Rows?.FirstOrDefault();
  131. if (row != null)
  132. {
  133. var hasjob = row.IsEntityLinkValid<Notification, JobLink>(x => x.Job);
  134. var entitytype = row.Get<Notification, string>(x => x.EntityType);
  135. NotificationUtils.MakeVisible(Reply, 0, 5, singlerow, Folders.SelectedIndex == 0 || Folders.SelectedIndex == 2);
  136. NotificationUtils.MakeVisible(Forward, 5, 10, singlerow);
  137. NotificationUtils.MakeVisible(ViewForm, 0, 5, singlerow, NotificationUtils.IsDigitalForm(entitytype));
  138. NotificationUtils.MakeVisible(ViewEntity, 0, 10, singlerow, !string.IsNullOrWhiteSpace(entitytype));
  139. ViewEntity.Content = string.Format("View {0}", NotificationUtils.GetEntityType(entitytype)?.GetCaption());
  140. NotificationUtils.MakeVisible(AttachToJob, 0, 10, singlerow, !hasjob);
  141. NotificationUtils.MakeVisible(ViewJob, 0, 10, singlerow, hasjob, !string.Equals(entitytype, typeof(Job).EntityName()));
  142. var create = NotificationUtils.MakeVisible(CreateTask, 0, 5, singlerow, !string.Equals(entitytype, typeof(Kanban).EntityName()));
  143. create = NotificationUtils.MakeVisible(CreateSetout, 0, 5, singlerow, hasjob, !string.Equals(entitytype, typeof(Setout).EntityName())) ||
  144. create;
  145. create =
  146. NotificationUtils.MakeVisible(CreateRequi, 0, 5, singlerow, hasjob, !string.Equals(entitytype, typeof(Requisition).EntityName())) ||
  147. create;
  148. create = NotificationUtils.MakeVisible(CreateDelivery, 0, 5, singlerow, !string.Equals(entitytype, typeof(Delivery).EntityName())) ||
  149. create;
  150. NotificationUtils.MakeVisible(Archive, create ? 10 : 0, 5, Folders.SelectedIndex == 0);
  151. NotificationUtils.MakeVisible(Reopen, create ? 10 : 0, 5, Folders.SelectedIndex == 2);
  152. new Client<Notification>().Query(
  153. new Filter<Notification>(x => x.ID).IsEqualTo(row.Get<Notification, Guid>(x => x.ID)),
  154. Columns.None<Notification>().Add(x => x.Description),
  155. null,
  156. CoreRange.All,
  157. (table, error) =>
  158. {
  159. var drow = table?.Rows.FirstOrDefault();
  160. var desc = drow?.Get<Notification, string>(x => x.Description)?.Replace("background:NoColor;", "") ?? "";
  161. var ms = new MemoryStream(Encoding.ASCII.GetBytes(desc));
  162. Dispatcher.Invoke(() =>
  163. {
  164. Editor.Load(ms, FormatType.Html);
  165. //cursor.Dispose();
  166. });
  167. }
  168. );
  169. }
  170. else
  171. {
  172. NotificationUtils.MakeVisible(Reply, 0, 0, false);
  173. NotificationUtils.MakeVisible(Forward, 0, 0, false);
  174. NotificationUtils.MakeVisible(ViewForm, 0, 0, false);
  175. NotificationUtils.MakeVisible(ViewEntity, 0, 0, false);
  176. NotificationUtils.MakeVisible(AttachToJob, 0, 0, false);
  177. NotificationUtils.MakeVisible(ViewJob, 0, 0, false);
  178. NotificationUtils.MakeVisible(CreateTask, 0, 0, false);
  179. NotificationUtils.MakeVisible(CreateSetout, 0, 0, false);
  180. NotificationUtils.MakeVisible(CreateRequi, 0, 0, false);
  181. NotificationUtils.MakeVisible(CreateDelivery, 0, 0, false);
  182. NotificationUtils.MakeVisible(Archive, 0, 0, false);
  183. NotificationUtils.MakeVisible(Reopen, 0, 0, false);
  184. var ms = new MemoryStream(Encoding.ASCII.GetBytes(""));
  185. Editor.Load(ms, FormatType.Html);
  186. }
  187. }
  188. public void AddNotification(Notification notification)
  189. {
  190. Dispatcher.Invoke(() =>
  191. {
  192. if (IsInbox)
  193. {
  194. //Notifications.AddRow(notification);
  195. Notifications.Refresh(false, true);
  196. }
  197. });
  198. }
  199. private bool IsInbox => Folders.SelectedIndex == 0;
  200. private bool IsOutbox => Folders.SelectedIndex == 1;
  201. private bool IsArchive => Folders.SelectedIndex == 2;
  202. private void Notifications_OnReload(object sender, Filters<Notification> criteria, Columns<Notification> columns,
  203. ref SortOrder<Notification>? sortby)
  204. {
  205. var filter = new Filter<Notification>(x => x.ID).IsEqualTo(Guid.Empty);
  206. if (me != null)
  207. {
  208. if (IsInbox)
  209. filter = new Filter<Notification>(x => x.Employee.ID).IsEqualTo(me.ID).And(x => x.Closed).IsEqualTo(DateTime.MinValue);
  210. else if (IsOutbox)
  211. filter = new Filter<Notification>(x => x.Sender.ID).IsEqualTo(me.ID);
  212. else if (IsArchive)
  213. filter = new Filter<Notification>(x => x.Employee.ID).IsEqualTo(me.ID).And(x => x.Closed).IsNotEqualTo(DateTime.MinValue);
  214. }
  215. criteria.Add(filter);
  216. sortby = new SortOrder<Notification>(x => x.Created, SortDirection.Descending);
  217. }
  218. private void Folders_SelectionChanged(object sender, SelectionChangedEventArgs e)
  219. {
  220. if (IsReady)
  221. using (new WaitCursor())
  222. {
  223. Notifications.ColumnsTag = folders[Folders.SelectedIndex].Item1;
  224. Notifications.Refresh(true, true);
  225. }
  226. }
  227. #region IPanel<T> stuff
  228. public void CreateToolbarButtons(IPanelHost host)
  229. {
  230. // host.CreatePanelAction(new PanelAction() { Caption = "Send Notification", Image = PRSDesktop.Resources.email, OnExecute = SendNotificationClick });
  231. host.CreateSetupAction(
  232. Security.IsAllowed<CanManageEvents>() ? "Events" : "Event Subscriptions",
  233. Security.IsAllowed<CanManageEvents>() ? PRSDesktop.Resources.edit : PRSDesktop.Resources.email, action =>
  234. {
  235. var list = new MasterList(typeof(Event));
  236. list.ShowDialog();
  237. });
  238. }
  239. public void Setup()
  240. {
  241. Folders.SelectedIndex = 0;
  242. Notifications.ColumnsTag = folders[Folders.SelectedIndex].Item1;
  243. Notifications.Refresh(true, false);
  244. }
  245. public void Shutdown(CancelEventArgs? cancel)
  246. {
  247. }
  248. public void Refresh()
  249. {
  250. Notifications.Refresh(false, true);
  251. }
  252. public string SectionName => "Notifications";
  253. public DataModel DataModel(Selection selection)
  254. {
  255. var ids = Notifications.ExtractValues(c => c.ID, selection).ToArray();
  256. return new BaseDataModel<Notification>(new Filter<Notification>(x => x.ID).InList(ids));
  257. }
  258. public Dictionary<string, object[]> Selected()
  259. {
  260. return new Dictionary<string, object[]>();
  261. }
  262. #endregion
  263. #region Actions
  264. private Guid[] SelectedIDs => Notifications.ExtractValues(x => x.ID, Selection.Selected).ToArray();
  265. private bool ViewFormClick(Button sender, CoreRow[] rows)
  266. {
  267. return NotificationUtils.ViewForm(SelectedIDs);
  268. }
  269. private bool ViewEntityClick(Button sender, CoreRow[] rows)
  270. {
  271. return NotificationUtils.ViewEntity(SelectedIDs);
  272. }
  273. private bool WriteNewClick(Button sender, CoreRow[] rows)
  274. {
  275. var form = new NotificationForm();
  276. return form.ShowDialog() == true;
  277. }
  278. private bool ReplyClick(Button sender, CoreRow[] rows)
  279. {
  280. return NotificationUtils.ReplyOrForward(SelectedIDs, "RE:", true, Folders.SelectedIndex == 1);
  281. }
  282. private bool ForwardClick(Button sender, CoreRow[] rows)
  283. {
  284. return NotificationUtils.ReplyOrForward(SelectedIDs, "FW:", false, Folders.SelectedIndex == 1);
  285. }
  286. private bool CreateTaskClick(Button sender, CoreRow[] rows)
  287. {
  288. return NotificationUtils.CreateTask(SelectedIDs, me.ID);
  289. }
  290. private bool AttachToJobClick(Button sender, CoreRow[] rows)
  291. {
  292. return NotificationUtils.AttachToJob(SelectedIDs);
  293. }
  294. private bool ViewJobClick(Button sender, CoreRow[] rows)
  295. {
  296. return NotificationUtils.ViewJob(SelectedIDs);
  297. }
  298. private bool CreateSetoutClick(Button sender, CoreRow[] rows)
  299. {
  300. return NotificationUtils.CreateSetout(SelectedIDs);
  301. }
  302. private bool CreateRequiClick(Button sender, CoreRow[] rows)
  303. {
  304. return NotificationUtils.CreateRequi(SelectedIDs, me.ID);
  305. }
  306. private bool CreateDeliveryClick(Button sender, CoreRow[] rows)
  307. {
  308. return NotificationUtils.CreateDelivery(SelectedIDs);
  309. }
  310. private bool ArchiveClick(Button sender, CoreRow[] rows)
  311. {
  312. return NotificationUtils.Archive(SelectedIDs);
  313. }
  314. private bool ReopenClick(Button sender, CoreRow[] rows)
  315. {
  316. return NotificationUtils.Reopen(SelectedIDs);
  317. }
  318. public void Heartbeat(TimeSpan time)
  319. {
  320. }
  321. #endregion
  322. }