PageRenderTime 2514ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/source/Griffin.MvcContrib.Admin/Areas/Griffin/Controllers/LocalizeTypesController.cs

https://github.com/jgauffin/griffin.mvccontrib
C# | 283 lines | 248 code | 34 blank | 1 comment | 34 complexity | b878eb2fff48a294547d26b356066c28 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.Serialization.Json;
  8. using System.Web;
  9. using System.Web.Mvc;
  10. using Griffin.MvcContrib.Areas.Griffin.Models;
  11. using Griffin.MvcContrib.Areas.Griffin.Models.LocalizeTypes;
  12. using Griffin.MvcContrib.Localization;
  13. using Griffin.MvcContrib.Localization.Types;
  14. using TypePrompt = Griffin.MvcContrib.Localization.Types.TypePrompt;
  15. namespace Griffin.MvcContrib.Areas.Griffin.Controllers
  16. {
  17. [GriffinAuthorize(GriffinAdminRoles.TranslatorName)]
  18. [Localized]
  19. public class LocalizeTypesController : Controller
  20. {
  21. private readonly ITypePromptImporter _importer;
  22. private readonly ILocalizedTypesRepository _repository;
  23. public LocalizeTypesController(ILocalizedTypesRepository repository)
  24. {
  25. _repository = repository;
  26. // it's optional, since it depends on the implementation
  27. _importer = DependencyResolver.Current.GetService<ITypePromptImporter>();
  28. AddValidationPromptsIfMissing();
  29. }
  30. protected override void OnResultExecuting(ResultExecutingContext filterContext)
  31. {
  32. ViewBag.Importer = _importer != null;
  33. base.OnResultExecuting(filterContext);
  34. }
  35. [HttpPost]
  36. public ActionResult CreateLanguage(string lang)
  37. {
  38. try
  39. {
  40. _repository.CreateLanguage(new CultureInfo(lang), DefaultUICulture.Value);
  41. return RedirectToAction("Index", new { lang });
  42. }
  43. catch (Exception err)
  44. {
  45. ModelState.AddModelError("", err.Message);
  46. var allPrompts = _repository.GetPrompts(CultureInfo.CurrentUICulture, DefaultUICulture.Value,
  47. new SearchFilter());
  48. var model = new IndexModel
  49. {
  50. Cultures = _repository.GetAvailableLanguages(),
  51. Prompts = allPrompts.Select(p => new Models.LocalizeTypes.TypePrompt(p))
  52. };
  53. return View("Index", model);
  54. }
  55. }
  56. public ActionResult MakeCommon(string id)
  57. {
  58. var key = new TypePromptKey(id);
  59. var prompt = _repository.GetPrompt(CultureInfo.CurrentUICulture, key);
  60. prompt.TypeFullName = typeof(CommonPrompts).FullName;
  61. _repository.Save(CultureInfo.CurrentUICulture, typeof(CommonPrompts).FullName, prompt.TextName,
  62. prompt.TranslatedText);
  63. _repository.Delete(CultureInfo.CurrentUICulture, key);
  64. return RedirectToAction("Index");
  65. }
  66. public ActionResult Delete(string id)
  67. {
  68. var key = new TypePromptKey(id);
  69. _repository.Delete(CultureInfo.CurrentUICulture, key);
  70. return RedirectToAction("Index");
  71. }
  72. public ActionResult Import()
  73. {
  74. return View();
  75. }
  76. [HttpPost]
  77. public ActionResult Import(HttpPostedFileBase dataFile)
  78. {
  79. var serializer = new DataContractJsonSerializer(typeof(TypePrompt[]));
  80. var deserialized = serializer.ReadObject(dataFile.InputStream);
  81. var prompts = (IEnumerable<TypePrompt>)deserialized;
  82. _importer.Import(prompts);
  83. return View("Imported", prompts.Count());
  84. }
  85. public ActionResult Export()
  86. {
  87. return View();
  88. }
  89. [HttpPost]
  90. public ActionResult Export(bool commons, string filter, bool allLanguages)
  91. {
  92. var allPrompts = GetPromptsForExport(filter, commons, allLanguages);
  93. Response.AddHeader("Content-Disposition", string.Format("attachment;filename=type-prompts-{0}.json", DateTime.Now.ToString("yyyyMMdd-HHmm")));
  94. var serializer = new DataContractJsonSerializer(typeof(List<Localization.Types.TypePrompt>));
  95. var ms = new MemoryStream();
  96. serializer.WriteObject(ms, allPrompts);
  97. ms.Position = 0;
  98. return File(ms, "application/json");
  99. }
  100. public ActionResult ExportPreview(bool commons, string filter, bool allLanguages)
  101. {
  102. var allPrompts = GetPromptsForExport(filter, commons, allLanguages);
  103. var model = allPrompts.Select(x => new Models.LocalizeTypes.TypePrompt(x));
  104. return PartialView("_ExportPreview", model);
  105. }
  106. private List<TypePrompt> GetPromptsForExport(string filter, bool includeCommon, bool allLanguages)
  107. {
  108. var cultures = allLanguages
  109. ? _repository.GetAvailableLanguages()
  110. : new[] { CultureInfo.CurrentUICulture };
  111. var allPrompts = new List<TypePrompt>();
  112. foreach (var cultureInfo in cultures)
  113. {
  114. var sf = new SearchFilter { Path = filter };
  115. var prompts = _repository.GetPrompts(cultureInfo, cultureInfo, sf);
  116. foreach (var prompt in prompts)
  117. {
  118. if (!allPrompts.Any(x => x.LocaleId == prompt.LocaleId && x.Key == prompt.Key))
  119. allPrompts.Add(prompt);
  120. }
  121. if (includeCommon)
  122. {
  123. sf.Path = typeof(CommonPrompts).Namespace + ".CommonPrompts";
  124. prompts = _repository.GetPrompts(cultureInfo, cultureInfo, sf);
  125. foreach (var prompt in prompts)
  126. {
  127. if (!allPrompts.Any(x => x.LocaleId == prompt.LocaleId && x.Key == prompt.Key))
  128. allPrompts.Add(prompt);
  129. }
  130. }
  131. }
  132. return allPrompts.Where(x => !string.IsNullOrEmpty(x.TranslatedText)).ToList();
  133. }
  134. bool OnlyNotTranslated
  135. {
  136. get
  137. {
  138. var value = Session["OnlyNotTranslated"];
  139. if (value == null)
  140. return false;
  141. return (bool)value;
  142. }
  143. set { Session["OnlyNotTranslated"] = value; }
  144. }
  145. string TableFilter
  146. {
  147. get
  148. {
  149. var value = Session["TableFilter"];
  150. if (value == null)
  151. return null;
  152. return (string)value;
  153. }
  154. set { Session["TableFilter"] = value; }
  155. }
  156. public ActionResult Index(FilterModel filter)
  157. {
  158. var cookie = Request.Cookies["ShowMetadata"];
  159. var showMetadata = cookie != null && cookie.Value == "1";
  160. var languages = _repository.GetAvailableLanguages();
  161. if (Request.HttpMethod == "POST" && filter != null)
  162. {
  163. TableFilter = filter.TableFilter;
  164. OnlyNotTranslated = filter.OnlyNotTranslated;
  165. }
  166. var sf = new SearchFilter();
  167. if (TableFilter != null)
  168. sf.Path = TableFilter;
  169. if (OnlyNotTranslated)
  170. sf.OnlyNotTranslated = true;
  171. var prompts =
  172. _repository.GetPrompts(CultureInfo.CurrentUICulture, DefaultUICulture.Value, sf).Select(
  173. p => new Models.LocalizeTypes.TypePrompt(p)).OrderBy(p => p.TypeName).
  174. ToList();
  175. if (!showMetadata)
  176. prompts = prompts.Where(p => p.TextName == null || !p.TextName.Contains("_")).ToList();
  177. var model = new IndexModel
  178. {
  179. Prompts = prompts,
  180. Cultures = languages,
  181. ShowMetadata = showMetadata,
  182. OnlyNotTranslated = OnlyNotTranslated,
  183. TableFilter = TableFilter
  184. };
  185. return View(model);
  186. }
  187. private void AddValidationPromptsIfMissing()
  188. {
  189. if (!DefaultUICulture.IsActive)
  190. return;
  191. var prompt = _repository.GetPrompt(DefaultUICulture.Value,
  192. new TypePromptKey(typeof(StringLengthAttribute).FullName, "class"));
  193. if (prompt == null)
  194. {
  195. var provider = new ValidationAttributesStringProvider();
  196. foreach (var typePrompt in provider.GetPrompts(DefaultUICulture.Value))
  197. {
  198. if (!string.IsNullOrEmpty(typePrompt.TranslatedText))
  199. _repository.Save(DefaultUICulture.Value, typePrompt.TypeFullName, typePrompt.TextName,
  200. typePrompt.TranslatedText);
  201. }
  202. }
  203. }
  204. public ActionResult Edit(string id)
  205. {
  206. var model = CreateModel(id);
  207. return View(model);
  208. }
  209. private EditModel CreateModel(string id)
  210. {
  211. var key = new TypePromptKey(id);
  212. var prompt = _repository.GetPrompt(CultureInfo.CurrentUICulture, key);
  213. var defaultLang = _repository.GetPrompt(DefaultUICulture.Value, key);
  214. var model = new EditModel
  215. {
  216. DefaultText = defaultLang != null ? defaultLang.TranslatedText : "",
  217. LocaleId = prompt.LocaleId,
  218. Path =
  219. string.Format("{0} / {1} / {2}", CultureInfo.CurrentUICulture.DisplayName,
  220. prompt.TypeName, prompt.TextName),
  221. Text = prompt.TranslatedText,
  222. TextKey = prompt.Key.ToString()
  223. };
  224. return model;
  225. }
  226. [HttpPost]
  227. public ActionResult Edit(TranslateModel inmodel)
  228. {
  229. var model = CreateModel(inmodel.TextKey);
  230. model.Text = inmodel.Text;
  231. if (!ModelState.IsValid)
  232. {
  233. return View(model);
  234. }
  235. if (!ModelState.IsValid)
  236. return View(model);
  237. try
  238. {
  239. _repository.Update(CultureInfo.CurrentUICulture, new TypePromptKey(model.TextKey), model.Text);
  240. return RedirectToAction("Index");
  241. }
  242. catch (Exception err)
  243. {
  244. ModelState.AddModelError("", err.Message);
  245. return View(model);
  246. }
  247. }
  248. }
  249. }