NotificationPanel.xaml.cs 13 KB

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