MiscController.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using DevExpress.Utils;
  2. using DevExpress.Web;
  3. using DevExpress.Web.ASPxThemes;
  4. using DevExpress.Web.Mvc;
  5. using DevExpress.XtraPrinting;
  6. using GreenTree.Nachtragsmanagement.Core.Domain.Deviation;
  7. using GreenTree.Nachtragsmanagement.Core.Domain.User;
  8. using GreenTree.Nachtragsmanagement.Services.Appendix;
  9. using GreenTree.Nachtragsmanagement.Services.Deviation;
  10. using GreenTree.Nachtragsmanagement.Services.Misc;
  11. using GreenTree.Nachtragsmanagement.Services.User;
  12. using GreenTree.Nachtragsmanagement.Web.Framework.Authorization;
  13. using GreenTree.Nachtragsmanagement.Web.Models.Global;
  14. using GreenTree.Nachtragsmanagement.Web.Models.Misc;
  15. using Newtonsoft.Json;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Web;
  21. using System.Web.Mvc;
  22. using System.Web.UI;
  23. using System.Web.UI.WebControls;
  24. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  25. {
  26. public class MiscController : Controller
  27. {
  28. private readonly IMiscService _miscService;
  29. private readonly IUserService _userService;
  30. private readonly INotificationService _notificationService;
  31. public MiscController(
  32. IMiscService miscService,
  33. IUserService userService,
  34. INotificationService notificationService)
  35. {
  36. _miscService = miscService;
  37. _userService = userService;
  38. _notificationService = notificationService;
  39. ViewData["AllUsers"] = _userService.GetAllUsers();
  40. ViewData["AllUsersWithRole"] =
  41. _userService.GetAllUsers()
  42. .Select(u => new
  43. {
  44. Id = u.Id,
  45. Description = String.Format("{0} - {1}", u.Lastname,
  46. String.Join(", ", u.Roles
  47. .Select(r => r.Description)))
  48. })
  49. .ToList();
  50. ViewData["AllNotificationPlugins"] =
  51. _notificationService.GetNotificationPlugins();
  52. }
  53. #region MailNotifications
  54. /// <summary>
  55. /// Basic mailNotification view function
  56. /// </summary>
  57. [FunctionAuthorize(true, "Misc-MailNotifications")]
  58. public ActionResult ViewMailNotifications()
  59. {
  60. var mailNotifications = _miscService.GetAllMailNotifications();
  61. var mailNotificationModels = mailNotifications
  62. .Select(u => MailNotificationDataModel.FromMailNotification(u, false, _notificationService))
  63. .ToList();
  64. return View("~/Views/Misc/MailNotifications.cshtml", mailNotificationModels);
  65. }
  66. /// <summary>
  67. /// Get JSON data of specific mailNotification
  68. /// </summary>
  69. /// <param name="id">MailNotification id.</param>
  70. public ActionResult GetMailNotification(int id = -1)
  71. {
  72. var mailNotification = _miscService.GetMailNotificationById(id);
  73. if (mailNotification == null)
  74. return new JsonResult
  75. {
  76. Data = "notFound",
  77. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  78. };
  79. var mailNotificationModel = MailNotificationDataModel.FromMailNotification(mailNotification, false, _notificationService);
  80. return new JsonResult
  81. {
  82. Data = JsonConvert.SerializeObject(mailNotificationModel),
  83. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  84. };
  85. }
  86. /// <summary>
  87. /// Callback result for mailNotification grid
  88. /// </summary>
  89. /// <param name="scrollHeight">The height of the grid scrollable component.</param>
  90. public ActionResult PartialMailNotifications(int scrollHeight = -1)
  91. {
  92. var mailNotifications = _miscService.GetAllMailNotifications();
  93. var mailNotificationModels = mailNotifications
  94. .Select(u => MailNotificationDataModel.FromMailNotification(u, false, _notificationService))
  95. .ToList();
  96. ViewData["ScrollHeight"] = scrollHeight;
  97. return PartialView("~/Views/Misc/_MailNotificationGridPartial.cshtml", mailNotificationModels);
  98. }
  99. /// <summary>
  100. /// Callback result for mailNotification job combobox
  101. /// </summary>
  102. /// <param name="pluginSystemName">The system name of the corresponding notification plugin.</param>
  103. public ActionResult PartialNotificationPluginJobs(string pluginSystemName)
  104. {
  105. var notificationPlugin = _notificationService.GetNotificationPlugin(pluginSystemName);
  106. if (notificationPlugin == null)
  107. return PartialView("~/Views/Misc/_MailNotificationPluginJobsPartial.cshtml", null);
  108. var mailNotificationModel = new MailNotificationDataModel
  109. {
  110. NotificationPlugin = notificationPlugin
  111. };
  112. return PartialView("~/Views/Misc/_MailNotificationPluginJobsPartial.cshtml", mailNotificationModel);
  113. }
  114. /// <summary>
  115. /// Export result for mailNotification grid
  116. /// </summary>
  117. [HttpPost]
  118. public ActionResult ExportPartialMailNotifications(GridViewExportFormat exportformat)
  119. {
  120. if (exportformat == null || String.IsNullOrEmpty(exportformat.Format))
  121. return new EmptyResult();
  122. var mailNotifications = _miscService.GetAllMailNotifications();
  123. var mailNotificationModels = mailNotifications
  124. .Select(u => MailNotificationDataModel.FromMailNotification(u, false, _notificationService))
  125. .ToList();
  126. var viewContext = new ViewContext();
  127. var viewPage = new ViewPage();
  128. var htmlHelper = new HtmlHelper(viewContext, viewPage);
  129. var gridViewSettings = Extensions.GridViewSettingsHelper.MailNotificationGridViewSettings(htmlHelper);
  130. switch (exportformat.Format.ToLower())
  131. {
  132. case "xlsx":
  133. return GridViewExtension.ExportToXlsx(gridViewSettings, mailNotificationModels);
  134. case "xls":
  135. return GridViewExtension.ExportToXls(gridViewSettings, mailNotificationModels);
  136. case "pdf":
  137. return GridViewExtension.ExportToPdf(gridViewSettings, mailNotificationModels);
  138. default:
  139. return new EmptyResult();
  140. }
  141. }
  142. /// <summary>
  143. /// Partial edit for editing of existing or for new mailNotification
  144. /// </summary>
  145. /// <param name="id">Id for existing mailNotification, otherweise -1.</param>
  146. public ActionResult EditMailNotification(int id = -1)
  147. {
  148. var mailNotification = _miscService.GetMailNotificationById(id);
  149. var mailNotificationModel = MailNotificationDataModel.FromMailNotification(mailNotification, true, _notificationService);
  150. return PartialView("~/Views/Misc/_MailNotificationEditPartial.cshtml", mailNotificationModel);
  151. }
  152. /// <summary>
  153. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  154. /// </summary>
  155. /// <param name="mailNotificationModel">MailNotification model to be saved.</param>
  156. [HttpPost, ValidateInput(false)]
  157. public ActionResult EditMailNotification(MailNotificationDataModel mailNotificationModel)
  158. {
  159. if (!ModelState.IsValid)
  160. {
  161. foreach (var role in mailNotificationModel.UserValues)
  162. mailNotificationModel.UserDescriptions.Add(
  163. ((IList<User>)ViewData["AllUsers"])
  164. .First(r => r.Id == role).Lastname);
  165. return PartialView("~/Views/Misc/_MailNotificationEditPartial.cshtml", mailNotificationModel);
  166. }
  167. var selectedUsers = _userService.GetUsersByIds(mailNotificationModel.UserValues.ToArray());
  168. if (mailNotificationModel.Id == -1)
  169. {
  170. var mailNotification = mailNotificationModel.ToMailNotification();
  171. mailNotification.SetUsers(selectedUsers);
  172. _miscService.InsertMailNotification(mailNotification);
  173. }
  174. else
  175. {
  176. var mailNotification = _miscService.GetMailNotificationById(mailNotificationModel.Id);
  177. mailNotification.CronExpression = mailNotificationModel.CronExpression;
  178. mailNotification.NotificationPluginSystemName = mailNotificationModel.NotificationPluginSystemName;
  179. mailNotification.NotificationJobSystemName = mailNotificationModel.NotificationJobSystemName;
  180. mailNotification.SetUsers(selectedUsers);
  181. _miscService.UpdateMailNotification(mailNotification);
  182. }
  183. return new JsonResult
  184. {
  185. Data = "success"
  186. };
  187. }
  188. /// <summary>
  189. /// Simple JSON result for deleting a specific mailNotification
  190. /// </summary>
  191. /// <param name="id">MailNotification id.</param>
  192. [HttpPost]
  193. public ActionResult DeleteMailNotification(int id)
  194. {
  195. var mailNotification = _miscService.GetMailNotificationById(id);
  196. if (mailNotification != null)
  197. _miscService.DeleteMailNotification(mailNotification);
  198. return new JsonResult
  199. {
  200. Data = "success"
  201. };
  202. }
  203. #endregion
  204. }
  205. }