MiscController.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. using DevExpress.Utils;
  2. using DevExpress.Web;
  3. using DevExpress.Web.ASPxThemes;
  4. using DevExpress.Web.Mvc;
  5. using DevExpress.XtraPrinting;
  6. using DevExpress.XtraScheduler;
  7. using GreenTree.Nachtragsmanagement.Core.Authentication;
  8. using GreenTree.Nachtragsmanagement.Core.Domain.Deviation;
  9. using GreenTree.Nachtragsmanagement.Core.Domain.User;
  10. using GreenTree.Nachtragsmanagement.Services.Appendix;
  11. using GreenTree.Nachtragsmanagement.Services.Configuration;
  12. using GreenTree.Nachtragsmanagement.Services.Deviation;
  13. using GreenTree.Nachtragsmanagement.Services.Logging;
  14. using GreenTree.Nachtragsmanagement.Services.Misc;
  15. using GreenTree.Nachtragsmanagement.Services.Scheduling;
  16. using GreenTree.Nachtragsmanagement.Services.User;
  17. using GreenTree.Nachtragsmanagement.Web.Extensions;
  18. using GreenTree.Nachtragsmanagement.Web.Framework.Authorization;
  19. using GreenTree.Nachtragsmanagement.Web.Models.Config;
  20. using GreenTree.Nachtragsmanagement.Web.Models.Global;
  21. using GreenTree.Nachtragsmanagement.Web.Models.Misc;
  22. using Newtonsoft.Json;
  23. using System;
  24. using System.Collections.Generic;
  25. using System.IO;
  26. using System.Linq;
  27. using System.Net.Mime;
  28. using System.Web;
  29. using System.Web.Mvc;
  30. using System.Web.UI;
  31. using System.Web.UI.WebControls;
  32. using static GreenTree.Nachtragsmanagement.Web.Extensions.MVCxGridViewGeneratorHelper;
  33. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  34. {
  35. public class MiscController : Controller
  36. {
  37. private readonly IMiscService _miscService;
  38. private readonly IUserService _userService;
  39. private readonly INotificationService _notificationService;
  40. private readonly INotificationScheduler _notificationScheduler;
  41. private readonly IConfigurationService _configurationService;
  42. private readonly IUserHelper _userHelper;
  43. private readonly ILogger _logger;
  44. public MiscController(
  45. IMiscService miscService,
  46. IUserService userService,
  47. INotificationService notificationService,
  48. INotificationScheduler notificationScheduler,
  49. IConfigurationService configurationService,
  50. IUserHelper userHelper,
  51. ILogger logger)
  52. {
  53. _miscService = miscService;
  54. _userService = userService;
  55. _notificationService = notificationService;
  56. _notificationScheduler = notificationScheduler;
  57. _configurationService = configurationService;
  58. _userHelper = userHelper;
  59. _logger = logger;
  60. ViewData["AllUsers"] = _userService.GetAllUsers();
  61. ViewData["AllUsersWithRole"] =
  62. _userService.GetAllUsers()
  63. .Select(u => new
  64. {
  65. Id = u.Id,
  66. Description = String.Format("{0} - {1}", u.Lastname,
  67. String.Join(", ", u.Roles
  68. .Select(r => r.Description)))
  69. })
  70. .ToList();
  71. ViewData["AllNotificationPlugins"] =
  72. _notificationService.GetNotificationPlugins();
  73. }
  74. #region MailNotifications
  75. /// <summary>
  76. /// Basic mailNotification view function
  77. /// </summary>
  78. [FunctionAuthorize(true, "Misc-MailNotifications")]
  79. public ActionResult ViewMailNotifications()
  80. {
  81. var mailNotifications = _miscService.GetAllMailNotifications();
  82. var mailNotificationModels = mailNotifications
  83. .Select(u => MailNotificationDataModel.FromMailNotification(
  84. u, false, _notificationService, _notificationScheduler))
  85. .ToList();
  86. return View("~/Views/Misc/MailNotifications.cshtml", mailNotificationModels);
  87. }
  88. /// <summary>
  89. /// Get JSON data of specific mailNotification
  90. /// </summary>
  91. /// <param name="id">MailNotification id.</param>
  92. public ActionResult GetMailNotification(int id = -1)
  93. {
  94. var mailNotification = _miscService.GetMailNotificationById(id);
  95. if (mailNotification == null)
  96. return new JsonResult
  97. {
  98. Data = "notFound",
  99. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  100. };
  101. var mailNotificationModel = MailNotificationDataModel.FromMailNotification(
  102. mailNotification, false, _notificationService, _notificationScheduler);
  103. return new JsonResult
  104. {
  105. Data = JsonConvert.SerializeObject(mailNotificationModel),
  106. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  107. };
  108. }
  109. /// <summary>
  110. /// Callback result for mailNotification grid
  111. /// </summary>
  112. /// <param name="scrollHeight">The height of the grid scrollable component.</param>
  113. public ActionResult PartialMailNotifications(int scrollHeight = -1)
  114. {
  115. var mailNotifications = _miscService.GetAllMailNotifications();
  116. var mailNotificationModels = mailNotifications
  117. .Select(u => MailNotificationDataModel.FromMailNotification(
  118. u, false, _notificationService, _notificationScheduler))
  119. .ToList();
  120. ViewData["ScrollHeight"] = scrollHeight;
  121. return PartialView("~/Views/Misc/_MailNotificationGridPartial.cshtml", mailNotificationModels);
  122. }
  123. /// <summary>
  124. /// Callback result for mailNotification job combobox
  125. /// </summary>
  126. /// <param name="pluginSystemName">The system name of the corresponding notification plugin.</param>
  127. public ActionResult PartialNotificationPluginJobs(string pluginSystemName)
  128. {
  129. var notificationPlugin = _notificationService.GetNotificationPlugin(pluginSystemName);
  130. if (notificationPlugin == null)
  131. return PartialView("~/Views/Misc/_MailNotificationPluginJobsPartial.cshtml", null);
  132. var mailNotificationModel = new MailNotificationDataModel
  133. {
  134. NotificationPlugin = notificationPlugin
  135. };
  136. return PartialView("~/Views/Misc/_MailNotificationPluginJobsPartial.cshtml", mailNotificationModel);
  137. }
  138. /// <summary>
  139. /// Partial edit for editing of existing or for new mailNotification
  140. /// </summary>
  141. /// <param name="id">Id for existing mailNotification, otherweise -1.</param>
  142. public ActionResult EditMailNotification(int id = -1)
  143. {
  144. var mailNotification = _miscService.GetMailNotificationById(id);
  145. var mailNotificationModel = MailNotificationDataModel.FromMailNotification(
  146. mailNotification, true, _notificationService, _notificationScheduler);
  147. return PartialView("~/Views/Misc/_MailNotificationEditPartial.cshtml", mailNotificationModel);
  148. }
  149. /// <summary>
  150. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  151. /// </summary>
  152. /// <param name="mailNotificationModel">MailNotification model to be saved.</param>
  153. [HttpPost, ValidateInput(false)]
  154. public ActionResult EditMailNotification(MailNotificationDataModel mailNotificationModel)
  155. {
  156. try
  157. {
  158. if (!ModelState.IsValid)
  159. {
  160. foreach (var role in mailNotificationModel.UserValues)
  161. mailNotificationModel.UserDescriptions.Add(
  162. ((IList<User>)ViewData["AllUsers"])
  163. .First(r => r.Id == role).Lastname);
  164. var notificationPlugin = _notificationService.GetNotificationPlugin(mailNotificationModel.NotificationPluginSystemName);
  165. if (notificationPlugin != null)
  166. mailNotificationModel.NotificationPlugin = notificationPlugin;
  167. return PartialView("~/Views/Misc/_MailNotificationEditPartial.cshtml", mailNotificationModel);
  168. }
  169. if (mailNotificationModel.CronExpression.Split(' ').Length == 5)
  170. mailNotificationModel.CronExpression = mailNotificationModel.CronExpression + " *";
  171. var selectedUsers = _userService.GetUsersByIds(mailNotificationModel.UserValues.ToArray());
  172. if (mailNotificationModel.Id == -1)
  173. {
  174. var mailNotification = mailNotificationModel.ToMailNotification();
  175. mailNotification.SetUsers(selectedUsers);
  176. _miscService.InsertMailNotification(mailNotification);
  177. _logger.Entity(mailNotification, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  178. }
  179. else
  180. {
  181. var mailNotification = _miscService.GetMailNotificationById(mailNotificationModel.Id);
  182. mailNotification.CronExpression = mailNotificationModel.CronExpression;
  183. mailNotification.NotificationPluginSystemName = mailNotificationModel.NotificationPluginSystemName;
  184. mailNotification.NotificationJobSystemName = mailNotificationModel.NotificationJobSystemName;
  185. mailNotification.SetUsers(selectedUsers);
  186. _miscService.UpdateMailNotification(mailNotification);
  187. _logger.Entity(mailNotification, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  188. }
  189. _notificationScheduler.Start();
  190. return new JsonResult
  191. {
  192. Data = "success"
  193. };
  194. }
  195. catch (Exception ex)
  196. {
  197. _logger.Error("Fehler bei Speicherung einer Mail-Benachrichtigung.", ex, _userHelper.FromCookies());
  198. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  199. }
  200. }
  201. /// <summary>
  202. /// Simple JSON result for deleting a specific mailNotification
  203. /// </summary>
  204. /// <param name="id">MailNotification id.</param>
  205. [HttpPost]
  206. public ActionResult DeleteMailNotification(int id)
  207. {
  208. try
  209. {
  210. var mailNotification = _miscService.GetMailNotificationById(id);
  211. if (mailNotification != null)
  212. _miscService.DeleteMailNotification(mailNotification);
  213. _logger.Entity(mailNotification, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
  214. return new JsonResult
  215. {
  216. Data = "success"
  217. };
  218. }
  219. catch (Exception ex)
  220. {
  221. _logger.Error("Fehler bei Löschung einer Mail-Benachrichtigung.", ex, _userHelper.FromCookies());
  222. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  223. }
  224. }
  225. /// <summary>
  226. /// Processes the specific mailNotification
  227. /// </summary>
  228. /// <param name="id">MailNotification id.</param>
  229. [HttpPost]
  230. public ActionResult ProcessMailNotification(int id)
  231. {
  232. try
  233. {
  234. var mailNotification = _miscService.GetMailNotificationById(id);
  235. if (mailNotification != null)
  236. {
  237. var notificationPlugin = _notificationService.GetNotificationPlugin(mailNotification.NotificationPluginSystemName);
  238. notificationPlugin.ProcessNotifications(new[] { mailNotification });
  239. }
  240. return new JsonResult
  241. {
  242. Data = "success"
  243. };
  244. }
  245. catch (Exception ex)
  246. {
  247. _logger.Error("Fehler bei Ausführung einer Mail-Benachrichtigung.", ex, _userHelper.FromCookies());
  248. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  249. }
  250. }
  251. #endregion
  252. #region Logs
  253. /// <summary>
  254. /// Basic log view function
  255. /// </summary>
  256. [FunctionAuthorize(true, "Misc-Logs")]
  257. public ActionResult ViewLogs()
  258. {
  259. var logs = _logger.GetAllLogs();
  260. var logModels = logs
  261. .Select(u => LogDataModel.FromLog(u, false))
  262. .ToList();
  263. return View("~/Views/Misc/Logs.cshtml", logModels);
  264. }
  265. /// <summary>
  266. /// Get JSON data of specific log
  267. /// </summary>
  268. /// <param name="id">Log id.</param>
  269. public ActionResult GetLog(int id = -1)
  270. {
  271. var log = _logger.GetLogById(id);
  272. if (log == null)
  273. return new JsonResult
  274. {
  275. Data = "notFound",
  276. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  277. };
  278. var logModel = LogDataModel.FromLog(log, false);
  279. return new JsonResult
  280. {
  281. Data = JsonConvert.SerializeObject(logModel),
  282. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  283. };
  284. }
  285. /// <summary>
  286. /// Callback result for log grid
  287. /// </summary>
  288. /// <param name="scrollHeight">The height of the grid scrollable component.</param>
  289. public ActionResult PartialLogs(int scrollHeight = -1)
  290. {
  291. var logs = _logger.GetAllLogs();
  292. var logModels = logs
  293. .Select(u => LogDataModel.FromLog(u, false))
  294. .ToList();
  295. ViewData["ScrollHeight"] = scrollHeight;
  296. return PartialView("~/Views/Misc/_LogGridPartial.cshtml", logModels);
  297. }
  298. /// <summary>
  299. /// Export result for misc grid
  300. /// </summary>
  301. [HttpPost]
  302. public ActionResult ExportPartialLogs(string displayMode, string exportformat)
  303. {
  304. if (String.IsNullOrEmpty(displayMode))
  305. return new EmptyResult();
  306. var currentUser = _userHelper.FromCookies();
  307. var logs = _logger.GetAllLogs();
  308. var logModels = logs
  309. .Select(u => LogDataModel.FromLog(u, false))
  310. .ToList();
  311. var viewContext = new ViewContext();
  312. var viewPage = new ViewPage();
  313. var htmlHelper = new System.Web.Mvc.HtmlHelper(viewContext, viewPage);
  314. MVCxGridViewState gridViewState = (MVCxGridViewState)Session["LogGridViewState"];
  315. var settings = GridViewSettingsHelper.LogGridViewSettings(htmlHelper);
  316. if (gridViewState != null)
  317. {
  318. var generator = new MVCReportGeneratonHelper();
  319. generator.CustomizeColumnsCollection += new CustomizeColumnsCollectionEventHandler(logGenerator_CustomizeColumnsCollection);
  320. var report = generator.GenerateMVCReport(gridViewState, logModels, "Logliste");
  321. if (displayMode == "popup")
  322. {
  323. return PartialView("~/Views/Shared/_PrintPopupPartial.cshtml",
  324. new PrintGridModel(report, "devGridViewLog",
  325. new { Controller = "Misc", Action = "ExportPartialLogs",
  326. displayMode = "callback", exportformat = String.Empty },
  327. new { Controller = "Misc", Action = "ExportPartialLogs",
  328. displayMode = "export", exportformat = String.Empty }));
  329. }
  330. else if (displayMode == "callback")
  331. {
  332. return PartialView("~/Views/Shared/_PrintDocumentViewerPartial.cshtml",
  333. new PrintGridModel(report, "devGridViewLog",
  334. new { Controller = "Misc", Action = "ExportPartialLogs",
  335. displayMode = "callback", exportformat = String.Empty },
  336. new { Controller = "Misc", Action = "ExportPartialLogs",
  337. displayMode = "export", exportformat = String.Empty }));
  338. }
  339. else if (displayMode == "export")
  340. {
  341. switch (exportformat.ToLower())
  342. {
  343. case "xlsx":
  344. return GridViewExtension.ExportToXlsx(settings, logModels);
  345. case "xls":
  346. return GridViewExtension.ExportToXls(settings, logModels);
  347. case "pdf":
  348. report.Name = "Logliste";
  349. return DocumentViewerExtension.ExportTo(report);
  350. }
  351. }
  352. return new EmptyResult();
  353. }
  354. else
  355. return new EmptyResult();
  356. }
  357. /// <summary>
  358. /// Customize created columns
  359. /// </summary>
  360. private void logGenerator_CustomizeColumnsCollection(object source, ColumnsCreationEventArgs e)
  361. {
  362. foreach (var column in e.ColumnsInfo)
  363. {
  364. if (column.ColumnCaption == "#") { column.IsVisible = false; }
  365. if (column.FieldName == "LogLevelDescription") { column.ColumnWidth = 60; }
  366. if (column.FieldName == "ShortMessage") { column.ColumnWidth = 300; }
  367. if (column.FieldName == "FullMessage") { column.IsVisible = false; column.IsDetail = true; }
  368. if (column.FieldName == "IpAddress") { column.ColumnWidth = 70; }
  369. if (column.FieldName == "UserDescription") { column.ColumnWidth = 70; }
  370. if (column.FieldName == "EntityType") { column.ColumnWidth = 60; }
  371. if (column.FieldName == "EntityId") { column.ColumnWidth = 50; }
  372. if (column.FieldName == "CreatedOnUtc") { column.ColumnWidth = 70; }
  373. }
  374. }
  375. /// <summary>
  376. /// Partial edit for editing of existing or for new log
  377. /// </summary>
  378. /// <param name="id">Id for existing log, otherweise -1.</param>
  379. public ActionResult ViewLog(int id = -1)
  380. {
  381. var log = _logger.GetLogById(id);
  382. var logModel = LogDataModel.FromLog(log, true);
  383. return PartialView("~/Views/Misc/_LogViewPartial.cshtml", logModel);
  384. }
  385. /// <summary>
  386. /// Simple JSON result for deleting a specific log
  387. /// </summary>
  388. /// <param name="id">Log id.</param>
  389. [HttpPost]
  390. public ActionResult DeleteLog(int id)
  391. {
  392. try
  393. {
  394. var log = _logger.GetLogById(id);
  395. if (log != null)
  396. _logger.DeleteLog(log);
  397. return new JsonResult
  398. {
  399. Data = "success"
  400. };
  401. }
  402. catch (Exception ex)
  403. {
  404. _logger.Error("Fehler bei Löschung eines Logs.", ex, _userHelper.FromCookies());
  405. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  406. }
  407. }
  408. #endregion
  409. #region ConfigItems
  410. /// <summary>
  411. /// Basic configItem view function
  412. /// </summary>
  413. [FunctionAuthorize(true, "Misc-ConfigItems")]
  414. public ActionResult ViewConfigItems()
  415. {
  416. var configItems = _configurationService.GetAllConfigItems();
  417. var configItemModels = configItems
  418. .Select(u => ConfigItemDataModel.FromConfigItem(u, false))
  419. .ToList();
  420. return View("~/Views/Config/View.cshtml", configItemModels);
  421. }
  422. /// <summary>
  423. /// Get JSON data of specific configItem
  424. /// </summary>
  425. /// <param name="id">ConfigItem id.</param>
  426. public ActionResult GetConfigItem(int id = -1)
  427. {
  428. var configItem = _configurationService.GetConfigItemById(id);
  429. if (configItem == null)
  430. return new JsonResult
  431. {
  432. Data = "notFound",
  433. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  434. };
  435. var configItemModel = ConfigItemDataModel.FromConfigItem(configItem, false);
  436. return new JsonResult
  437. {
  438. Data = JsonConvert.SerializeObject(configItemModel),
  439. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  440. };
  441. }
  442. /// <summary>
  443. /// Callback result for configItem grid
  444. /// </summary>
  445. /// <param name="scrollHeight">The height of the grid scrollable component.</param>
  446. public ActionResult PartialConfigItems(int scrollHeight = -1)
  447. {
  448. var configItems = _configurationService.GetAllConfigItems();
  449. var configItemModels = configItems
  450. .Select(u => ConfigItemDataModel.FromConfigItem(u, false))
  451. .ToList();
  452. ViewData["ScrollHeight"] = scrollHeight;
  453. return PartialView("~/Views/Config/_ConfigItemGridPartial.cshtml", configItemModels);
  454. }
  455. /// <summary>
  456. /// Partial edit for config item value
  457. /// </summary>
  458. /// <param name="typeFullName">Value type.</param>
  459. public ActionResult GetValueTypePartialEdit(string typeFullName)
  460. {
  461. var model = new ConfigItemDataModel
  462. {
  463. TypeFullName = typeFullName
  464. };
  465. return PartialView("~/Views/Config/_ConfigItemValueEditPartial.cshtml", model);
  466. }
  467. /// <summary>
  468. /// Partial edit for editing of existing or for new configItem
  469. /// </summary>
  470. /// <param name="id">Id for existing configItem, otherweise -1.</param>
  471. public ActionResult EditConfigItem(int id = -1)
  472. {
  473. var configItem = _configurationService.GetConfigItemById(id);
  474. var configItemModel = ConfigItemDataModel.FromConfigItem(configItem, true);
  475. return PartialView("~/Views/Config/_ConfigItemEditPartial.cshtml", configItemModel);
  476. }
  477. /// <summary>
  478. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  479. /// </summary>
  480. /// <param name="configItemModel">ConfigItem model to be saved.</param>
  481. [HttpPost, ValidateInput(false)]
  482. public ActionResult EditConfigItem(ConfigItemDataModel configItemModel)
  483. {
  484. try
  485. {
  486. if (!ModelState.IsValid)
  487. return PartialView("~/Views/Config/_ConfigItemEditPartial.cshtml", configItemModel);
  488. if (configItemModel.Id == -1)
  489. {
  490. var configItem = configItemModel.ToConfigItem();
  491. _configurationService.InsertConfigItem(configItem);
  492. _logger.Entity(configItem, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  493. }
  494. else
  495. {
  496. var configItem = _configurationService.GetConfigItemById(configItemModel.Id);
  497. configItem.Name = configItemModel.Name;
  498. configItem.TypeFullName = configItemModel.TypeFullName;
  499. configItem.Value = configItemModel.Value;
  500. configItem.Description = configItemModel.Description;
  501. _configurationService.UpdateConfigItem(configItem);
  502. _logger.Entity(configItem, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  503. }
  504. _notificationScheduler.Start();
  505. return new JsonResult
  506. {
  507. Data = "success"
  508. };
  509. }
  510. catch (Exception ex)
  511. {
  512. _logger.Error("Fehler bei Speicherung einer Einstellung.", ex, _userHelper.FromCookies());
  513. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  514. }
  515. }
  516. /// <summary>
  517. /// Simple JSON result for deleting a specific configItem
  518. /// </summary>
  519. /// <param name="id">ConfigItem id.</param>
  520. [HttpPost]
  521. public ActionResult DeleteConfigItem(int id)
  522. {
  523. try
  524. {
  525. var configItem = _configurationService.GetConfigItemById(id);
  526. if (configItem != null)
  527. _configurationService.DeleteConfigItem(configItem);
  528. _logger.Entity(configItem, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
  529. return new JsonResult
  530. {
  531. Data = "success"
  532. };
  533. }
  534. catch (Exception ex)
  535. {
  536. _logger.Error("Fehler bei Löschung einer Einstellung.", ex, _userHelper.FromCookies());
  537. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  538. }
  539. }
  540. #endregion
  541. #region HelpPages
  542. /// <summary>
  543. /// Default fileSaveSettings for the image uploader of the htmlEditor
  544. /// </summary>
  545. private static HtmlEditorFileSaveSettings fileSaveSettings;
  546. /// <summary>
  547. /// Default fileSaveSettings for the image uploader of the htmlEditor
  548. /// </summary>
  549. public static HtmlEditorFileSaveSettings FileSaveSettings
  550. {
  551. get
  552. {
  553. if (fileSaveSettings == null)
  554. {
  555. fileSaveSettings = new HtmlEditorFileSaveSettings();
  556. fileSaveSettings.FileSystemSettings.UploadFolder = "~/Content/Images/HelpPages/";
  557. }
  558. return fileSaveSettings;
  559. }
  560. }
  561. /// <summary>
  562. /// Default validationSettings for the image uploader of the htmlEditor
  563. /// </summary>
  564. public static readonly UploadControlValidationSettings ImageUploadValidationSettings = new UploadControlValidationSettings
  565. {
  566. AllowedFileExtensions = new string[] { ".jpg", ".jpeg", ".jpe", ".gif", ".png" },
  567. MaxFileSize = 4000000
  568. };
  569. /// <summary>
  570. /// Creates the helpPage tree recursively
  571. /// </summary>
  572. /// <param name="models">Full helpPage model list.</param>
  573. /// <param name="nodesCollection">Parent node collection.</param>
  574. /// <param name="parentID">Parent node id.</param>
  575. public static void CreateHelpPageTree(IEnumerable<HelpPageDataModel> models, MVCxTreeViewNodeCollection nodesCollection,
  576. int? parentID)
  577. {
  578. var children = models
  579. .Where(p => p.ParentId == parentID);
  580. foreach (var model in children)
  581. {
  582. var node = nodesCollection.Add(model.Title, model.Id.ToString());
  583. node.Expanded = true;
  584. CreateHelpPageTree(models, node.Nodes, model.Id);
  585. }
  586. }
  587. /// <summary>
  588. /// Basic helpPage view function
  589. /// </summary>
  590. /// <param name="id">Id of initial editing helpPage.</param>
  591. [FunctionAuthorize(true, "Misc-HelpPages")]
  592. public ActionResult ViewHelpPages(int id = -1)
  593. {
  594. var helpPages = _miscService.GetAllHelpPagesWithoutContent();
  595. var helpPageModels = helpPages
  596. .Select(u => HelpPageDataModel.FromHelpPage(u, false))
  597. .OrderBy(u => u.DisplayIndex)
  598. .ToList();
  599. var initialHelpPageModelExists = helpPageModels
  600. .Any(p => p.Id == id);
  601. if (initialHelpPageModelExists)
  602. ViewData["InitialEditHelpPage"] = id;
  603. ViewData["IsHelpPageSelectionTree"] = false;
  604. return View("~/Views/Misc/HelpPages.cshtml", helpPageModels);
  605. }
  606. /// <summary>
  607. /// Callback result for editing of help page
  608. /// </summary>
  609. /// <param name="helpPageModel">HelpPage model to be edited.</param>
  610. public ActionResult PartialHtmlEditor(HelpPageDataModel helpPageModel)
  611. {
  612. return PartialView("~/Views/Misc/_HelpPageHtmlEditPartial.cshtml", helpPageModel);
  613. }
  614. /// <summary>
  615. /// Callback result for processing the upload of an image for a helpPage
  616. /// </summary>
  617. public ActionResult UploadProcessingImageUpload()
  618. {
  619. FileSavingEventHandler onFileSaving = (s, e) => {
  620. using (var fileContent = e.UploadedFile.FileContent)
  621. {
  622. using (var image = System.Drawing.Image.FromStream(fileContent))
  623. {
  624. var memoryStream = new MemoryStream();
  625. image.Save(memoryStream, image.RawFormat);
  626. e.OutputStream = memoryStream;
  627. e.FileName = e.UploadedFile.FileName;
  628. }
  629. }
  630. };
  631. HtmlEditorExtension.SaveUploadedFile(FileSaveSettings, ImageUploadValidationSettings, onFileSaving);
  632. return null;
  633. }
  634. /// <summary>
  635. /// Get JSON data of specific helpPage
  636. /// </summary>
  637. /// <param name="id">HelpPage id.</param>
  638. public ActionResult GetHelpPage(int id = -1)
  639. {
  640. var helpPage = _miscService.GetHelpPageById(id);
  641. if (helpPage == null)
  642. return new JsonResult
  643. {
  644. Data = "notFound",
  645. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  646. };
  647. var helpPageModel = HelpPageDataModel.FromHelpPage(helpPage, false);
  648. return new JsonResult
  649. {
  650. Data = JsonConvert.SerializeObject(helpPageModel),
  651. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  652. };
  653. }
  654. /// <summary>
  655. /// Partial edit for editing of existing or for new helpPage
  656. /// </summary>
  657. /// <param name="id">Id for existing helpPage, otherweise -1.</param>
  658. public ActionResult ViewHelpPage(int id = -1)
  659. {
  660. var helpPage = _miscService.GetHelpPageById(id);
  661. var helpPageModel = HelpPageDataModel.FromHelpPage(helpPage, true);
  662. if (helpPage != null)
  663. return PartialView("~/Views/Misc/_HelpPageViewPartial.cshtml", helpPageModel);
  664. return new EmptyResult();
  665. }
  666. /// <summary>
  667. /// Partial edit for editing of existing or for new helpPage
  668. /// </summary>
  669. /// <param name="id">Id for existing helpPage, otherweise -1.</param>
  670. public ActionResult EditHelpPage(int id = -1)
  671. {
  672. var helpPages = _miscService.GetAllHelpPages();
  673. var helpPageModels = helpPages
  674. .Select(u => HelpPageDataModel.FromHelpPage(u, false))
  675. .ToList();
  676. var helpPage = _miscService.GetHelpPageById(id);
  677. var helpPageModel = HelpPageDataModel.FromHelpPage(helpPage, true);
  678. ViewData["AllHelpPages"] = helpPageModels;
  679. ViewData["IsHelpPageSelectionTree"] = true;
  680. return PartialView("~/Views/Misc/_HelpPageEditPartial.cshtml", helpPageModel);
  681. }
  682. /// <summary>
  683. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  684. /// </summary>
  685. /// <param name="helpPageModel">HelpPage model to be saved.</param>
  686. [HttpPost, ValidateInput(false)]
  687. public ActionResult EditHelpPage(HelpPageDataModel helpPageModel)
  688. {
  689. try
  690. {
  691. if (!ModelState.IsValid)
  692. return PartialView("~/Views/Misc/_HelpPageEditPartial.cshtml", helpPageModel);
  693. if (helpPageModel.Id == -1)
  694. {
  695. var helpPage = helpPageModel.ToHelpPage();
  696. _miscService.InsertHelpPage(helpPage);
  697. _logger.Entity(helpPage, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  698. return new JsonResult
  699. {
  700. Data = String.Format("success-{0}", helpPage.Id)
  701. };
  702. }
  703. else
  704. {
  705. var helpPage = _miscService.GetHelpPageById(helpPageModel.Id);
  706. helpPage.Title = helpPageModel.Title;
  707. helpPage.Content = helpPageModel.Content;
  708. helpPage.ParentId = helpPageModel.ParentId;
  709. helpPage.DisplayIndex = helpPageModel.DisplayIndex;
  710. _miscService.UpdateHelpPage(helpPage);
  711. _logger.Entity(helpPage, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  712. return new JsonResult
  713. {
  714. Data = String.Format("success-{0}", helpPage.Id)
  715. };
  716. }
  717. }
  718. catch (Exception ex)
  719. {
  720. _logger.Error("Fehler bei Speicherung einer Hilfe-Seite.", ex, _userHelper.FromCookies());
  721. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  722. }
  723. }
  724. /// <summary>
  725. /// Simple JSON result for deleting a specific helpPage
  726. /// </summary>
  727. /// <param name="id">HelpPage id.</param>
  728. [HttpPost]
  729. public ActionResult DeleteHelpPage(int id)
  730. {
  731. try
  732. {
  733. var helpPage = _miscService.GetHelpPageById(id);
  734. if (helpPage != null)
  735. _miscService.DeleteHelpPage(helpPage);
  736. return new JsonResult
  737. {
  738. Data = "success"
  739. };
  740. }
  741. catch (Exception ex)
  742. {
  743. _logger.Error("Fehler bei Löschung einer Hilfe-Seite.", ex, _userHelper.FromCookies());
  744. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  745. }
  746. }
  747. #endregion
  748. }
  749. }