PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Json/JsonCustomFilterList.cs

#
C# | 83 lines | 64 code | 6 blank | 13 comment | 5 complexity | 7343b0ac4a2e97ff5e416d59a288253f MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace BlogEngine.Core.Json
  2. {
  3. using System;
  4. using System.Data;
  5. using System.Collections.Generic;
  6. using Web.Extensions;
  7. ///<summary>
  8. /// Handle json-friendly list of custom filters
  9. ///</summary>
  10. public class JsonCustomFilterList
  11. {
  12. static protected ExtensionSettings CustomFilters;
  13. /// <summary>
  14. /// Get list of custom filters
  15. /// </summary>
  16. /// <returns>List of filters</returns>
  17. public static List<JsonCustomFilter> GetCustomFilters()
  18. {
  19. var filterList = new List<JsonCustomFilter>();
  20. try
  21. {
  22. CustomFilters = ExtensionManager.GetSettings("MetaExtension", "BeCustomFilters");
  23. DataTable dt = CustomFilters.GetDataTable();
  24. foreach (DataRow row in dt.Rows)
  25. {
  26. var f = new JsonCustomFilter
  27. {
  28. Name = row["Name"].ToString(),
  29. FullName = row["FullName"].ToString(),
  30. Checked = int.Parse(row["Checked"].ToString()),
  31. Spam = int.Parse(row["Cought"].ToString()),
  32. Mistakes = int.Parse(row["Reported"].ToString())
  33. };
  34. var ext = ExtensionManager.GetExtension(f.Name);
  35. f.Enabled = ext == null ? true : ext.Enabled;
  36. filterList.Add(f);
  37. }
  38. }
  39. catch (Exception ex)
  40. {
  41. Utils.Log("JsonCustomFilterList.GetCustomFilters(): " + ex.Message);
  42. }
  43. return filterList;
  44. }
  45. /// <summary>
  46. /// Reset counters for custom filter
  47. /// </summary>
  48. /// <param name="filterName">Filter name</param>
  49. /// <returns>Json response</returns>
  50. public static JsonResponse ResetCounters(string filterName)
  51. {
  52. try
  53. {
  54. if (!string.IsNullOrEmpty(filterName))
  55. {
  56. // reset statistics for this filter
  57. for (int i = 0; i < CustomFilters.Parameters[0].Values.Count; i++)
  58. {
  59. if (CustomFilters.Parameters[1].Values[i] == filterName)
  60. {
  61. CustomFilters.Parameters[2].Values[i] = "0";
  62. CustomFilters.Parameters[3].Values[i] = "0";
  63. CustomFilters.Parameters[4].Values[i] = "0";
  64. }
  65. }
  66. ExtensionManager.SaveSettings("MetaExtension", CustomFilters);
  67. }
  68. return new JsonResponse() { Success = true, Message = string.Format("Counters for {0} reset", filterName) };
  69. }
  70. catch (Exception ex)
  71. {
  72. Utils.Log(string.Format("JsonCustomFilterList.ResetCounters: {0}", ex.Message));
  73. return new JsonResponse() { Message = "Error resetting counters" };
  74. }
  75. }
  76. }
  77. }