ReportHelper.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using MediaBrowser.Controller.Entities;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace MediaBrowser.Api.Reports
  8. {
  9. public class ReportHelper
  10. {
  11. /// <summary> Gets java script localized string. </summary>
  12. /// <param name="phrase"> The phrase. </param>
  13. /// <returns> The java script localized string. </returns>
  14. public static string GetJavaScriptLocalizedString(string phrase)
  15. {
  16. var dictionary = BaseItem.LocalizationManager.GetJavaScriptLocalizationDictionary(BaseItem.ConfigurationManager.Configuration.UICulture);
  17. string value;
  18. if (dictionary.TryGetValue(phrase, out value))
  19. {
  20. return value;
  21. }
  22. return phrase;
  23. }
  24. /// <summary> Gets server localized string. </summary>
  25. /// <param name="phrase"> The phrase. </param>
  26. /// <returns> The server localized string. </returns>
  27. public static string GetServerLocalizedString(string phrase)
  28. {
  29. return BaseItem.LocalizationManager.GetLocalizedString(phrase, BaseItem.ConfigurationManager.Configuration.UICulture);
  30. }
  31. /// <summary> Gets row type. </summary>
  32. /// <param name="rowType"> The type. </param>
  33. /// <returns> The row type. </returns>
  34. public static ReportViewType GetRowType(string rowType)
  35. {
  36. if (string.IsNullOrEmpty(rowType))
  37. return ReportViewType.BaseItem;
  38. ReportViewType rType;
  39. if (!Enum.TryParse<ReportViewType>(rowType, out rType))
  40. return ReportViewType.BaseItem;
  41. return rType;
  42. }
  43. /// <summary> Gets header metadata type. </summary>
  44. /// <param name="header"> The header. </param>
  45. /// <returns> The header metadata type. </returns>
  46. public static HeaderMetadata GetHeaderMetadataType(string header)
  47. {
  48. if (string.IsNullOrEmpty(header))
  49. return HeaderMetadata.None;
  50. HeaderMetadata rType;
  51. if (!Enum.TryParse<HeaderMetadata>(header, out rType))
  52. return HeaderMetadata.None;
  53. return rType;
  54. }
  55. /// <summary> Convert field to string. </summary>
  56. /// <typeparam name="T"> Generic type parameter. </typeparam>
  57. /// <param name="value"> The value. </param>
  58. /// <param name="fieldType"> Type of the field. </param>
  59. /// <returns> The field converted to string. </returns>
  60. public static string ConvertToString<T>(T value, ReportFieldType fieldType)
  61. {
  62. if (value == null)
  63. return "";
  64. switch (fieldType)
  65. {
  66. case ReportFieldType.String:
  67. return value.ToString();
  68. case ReportFieldType.Boolean:
  69. return value.ToString();
  70. case ReportFieldType.Date:
  71. return string.Format("{0:d}", value);
  72. case ReportFieldType.Time:
  73. return string.Format("{0:t}", value);
  74. case ReportFieldType.DateTime:
  75. return string.Format("{0:d}", value);
  76. case ReportFieldType.Minutes:
  77. return string.Format("{0}mn", value);
  78. case ReportFieldType.Int:
  79. return string.Format("", value);
  80. default:
  81. if (value is Guid)
  82. return string.Format("{0:N}", value);
  83. return value.ToString();
  84. }
  85. }
  86. }
  87. }