PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/umbraco/plugins/uSiteBuilderAdmin/ExportCode.ascx.cs

https://bitbucket.org/Mulliman/usitebuilder-back-office-extension-forked-from-kelvindigital
C# | 378 lines | 322 code | 54 blank | 2 comment | 26 complexity | ef00875ca602ebd4e607f5ddfdced685 MD5 | raw file
  1. namespace USiteBuilderBackOffice.Umbraco4.Plugins.uSiteBuilderAdmin
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Web;
  9. using System.Web.UI.WebControls;
  10. using ICSharpCode.SharpZipLib.Core;
  11. using ICSharpCode.SharpZipLib.Zip;
  12. using Vega.USiteBuilder;
  13. using umbraco.cms.businesslogic.datatype;
  14. using umbraco.cms.businesslogic.template;
  15. using umbraco.cms.businesslogic.web;
  16. public partial class ExportCode : System.Web.UI.UserControl
  17. {
  18. private const string code = @"
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. using System.Web;
  23. using Vega.USiteBuilder;
  24. {7}
  25. {{
  26. [DocumentType(Name=""{0}"", IconUrl=""{1}"",
  27. Alias=""{8}"",
  28. AllowedTemplates= new string[] {{{2}}},
  29. AllowedChildNodeTypes = new Type[] {{{3}}})]
  30. public partial class {4} : {5}
  31. {{
  32. public {4} (int nodeId) : base(nodeId){{}}
  33. public {4} () {{}}
  34. {6}
  35. }}
  36. }}";
  37. private string property = @"
  38. [DocumentTypeProperty(UmbracoPropertyType.{5}, {6} Name = ""{0}"", Description = ""{1}"", Tab = ""{2}"", Mandatory = {3} )]
  39. public {7} {4} {{ get; set; }}
  40. ";
  41. private const string dataTypeCode = @"
  42. using System;
  43. using System.Collections.Generic;
  44. using System.Linq;
  45. using System.Web;
  46. using umbraco.cms.businesslogic.datatype;
  47. using Vega.USiteBuilder;
  48. {6}
  49. {{
  50. [DataType(Name = ""{0}"", UniqueId = ""{1}"", RenderControlGuid = ""{2}"", DatabaseDataType = {3})]
  51. public partial class {4} : DataTypeBase
  52. {{
  53. public override DataTypePrevalue[] Prevalues
  54. {{
  55. get {{ return new DataTypePrevalue[] {{ {5} }}; }}
  56. }}
  57. }}
  58. }}";
  59. private const string templateCode = @"
  60. using System.Web.UI;
  61. using Vega.USiteBuilder;
  62. {0}
  63. {{
  64. public partial class {1} : TemplateBase{2}
  65. {{
  66. }}
  67. }}
  68. ";
  69. protected override void OnLoad(EventArgs e)
  70. {
  71. base.OnLoad(e);
  72. DataTypesNamespaceTextBox.Enabled = ExportDataTypesCheckBox.Checked;
  73. DocTypesNamespaceTextBox.Enabled = ExportDocTypesCheckBox.Checked;
  74. TemplatesNamespaceTextBox.Enabled = ExportTemplatesCheckBox.Checked;
  75. }
  76. protected void Preview_Click(object sender, EventArgs e)
  77. {
  78. StringBuilder sb = new StringBuilder();
  79. if (ExportTemplatesCheckBox.Checked)
  80. {
  81. Template.GetAllAsList().ForEach(template => sb.AppendLine(ExportTemplate(template)));
  82. }
  83. if (ExportDataTypesCheckBox.Checked)
  84. {
  85. DataTypeDefinition.GetAll().ToList().ForEach(dataType => sb.AppendLine(ExportDataType(dataType)));
  86. }
  87. if (ExportDocTypesCheckBox.Checked)
  88. {
  89. DocumentType.GetAllAsList().ForEach(docType => sb.AppendLine(ExportDocType(docType.Id)));
  90. }
  91. CodeLiteral.Text = Server.HtmlEncode(sb.ToString());
  92. ActivateTabPlaceHolder.Visible = true;
  93. }
  94. protected void Save_Click(object sender, EventArgs e)
  95. {
  96. Response.ContentType = "application/octet-stream";
  97. Response.AppendHeader("content-disposition", "attachment; filename=\"Exported uSiteBuilder Classes.zip\"");
  98. Response.CacheControl = "Private";
  99. Response.Cache.SetExpires(DateTime.Now.AddMinutes(3));
  100. var zipStream = new ZipOutputStream(Response.OutputStream);
  101. zipStream.SetLevel(3);
  102. if (ExportDocTypesCheckBox.Checked)
  103. {
  104. DocumentType.GetAllAsList().ForEach(docType =>
  105. {
  106. string docTypeCode = ExportDocType(docType.Id);
  107. var memoryStream = new MemoryStream();
  108. using (TextWriter tw = new StreamWriter(memoryStream))
  109. {
  110. tw.Write(docTypeCode);
  111. tw.Flush();
  112. ZipEntry newEntry =
  113. new ZipEntry("DocumentTypes/" + CleanName(docType.Alias) +
  114. ".cs");
  115. newEntry.DateTime = DateTime.Now;
  116. zipStream.PutNextEntry(newEntry);
  117. memoryStream.Position = 0;
  118. StreamUtils.Copy(memoryStream, zipStream, new byte[4096]);
  119. zipStream.CloseEntry();
  120. }
  121. });
  122. }
  123. if (ExportDataTypesCheckBox.Checked)
  124. {
  125. DataTypeDefinition.GetAll().ToList().ForEach(dataType =>
  126. {
  127. string dataTypeCode = ExportDataType(dataType);
  128. var memoryStream = new MemoryStream();
  129. using (
  130. TextWriter tw = new StreamWriter(memoryStream))
  131. {
  132. tw.Write(dataTypeCode);
  133. tw.Flush();
  134. ZipEntry newEntry =
  135. new ZipEntry("DataTypes/" +
  136. CleanName(dataType.Text) +
  137. ".cs");
  138. newEntry.DateTime = DateTime.Now;
  139. zipStream.PutNextEntry(newEntry);
  140. memoryStream.Position = 0;
  141. StreamUtils.Copy(memoryStream, zipStream,
  142. new byte[4096]);
  143. zipStream.CloseEntry();
  144. }
  145. });
  146. }
  147. if (ExportTemplatesCheckBox.Checked)
  148. {
  149. Template.GetAllAsList().ForEach(template =>
  150. {
  151. string exportedTemplate = ExportTemplate(template);
  152. var memoryStream = new MemoryStream();
  153. using (TextWriter tw = new StreamWriter(memoryStream))
  154. {
  155. tw.Write(exportedTemplate);
  156. tw.Flush();
  157. ZipEntry newEntry =
  158. new ZipEntry("Templates/" + CleanName(template.Alias) +
  159. ".master.cs");
  160. newEntry.DateTime = DateTime.Now;
  161. zipStream.PutNextEntry(newEntry);
  162. memoryStream.Position = 0;
  163. StreamUtils.Copy(memoryStream, zipStream, new byte[4096]);
  164. zipStream.CloseEntry();
  165. }
  166. var memoryStream2 = new MemoryStream();
  167. using (TextWriter tw = new StreamWriter(memoryStream2))
  168. {
  169. tw.Write("<%@ Master Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"" + CleanName(template.Alias) + ".master.cs\" Inherits=\"" + TemplatesNamespaceTextBox.Text + "." + CleanName(template.Alias) + "\" %>");
  170. string design = template.Design;
  171. int startPos = design.IndexOf(">");
  172. tw.Write(design.Substring(startPos + 1));
  173. tw.Flush();
  174. ZipEntry newEntry =
  175. new ZipEntry("Templates/" + CleanName(template.Alias) +
  176. ".master");
  177. newEntry.DateTime = DateTime.Now;
  178. zipStream.PutNextEntry(newEntry);
  179. memoryStream2.Position = 0;
  180. StreamUtils.Copy(memoryStream2, zipStream, new byte[4096]);
  181. zipStream.CloseEntry();
  182. }
  183. });
  184. }
  185. zipStream.IsStreamOwner = false;
  186. zipStream.Close();
  187. Response.End();
  188. }
  189. // these are private readonlys as const can't be Guids
  190. private readonly Guid DATATYPE_YESNO_GUID = new Guid("38b352c1-e9f8-4fd8-9324-9a2eab06d97a");
  191. private readonly Guid DATATYPE_TINYMCE_GUID = new Guid("5e9b75ae-face-41c8-b47e-5f4b0fd82f83");
  192. private readonly Guid DATATYPE_DATETIMEPICKER_GUID = new Guid("b6fb1622-afa5-4bbf-a3cc-d9672a442222");
  193. private readonly Guid DATATYPE_DATEPICKER_GUID = new Guid("23e93522-3200-44e2-9f29-e61a6fcbb79a");
  194. private string GetPropertyType(Guid datatype)
  195. {
  196. if (datatype == DATATYPE_YESNO_GUID)
  197. {
  198. return "bool";
  199. }
  200. if (datatype == DATATYPE_TINYMCE_GUID)
  201. {
  202. return "HtmlString";
  203. }
  204. if (datatype == DATATYPE_DATETIMEPICKER_GUID)
  205. {
  206. return "DateTime";
  207. }
  208. if (datatype == DATATYPE_DATEPICKER_GUID)
  209. {
  210. return "DateTime";
  211. }
  212. return "string";
  213. }
  214. private string CleanName(string name)
  215. {
  216. //Compliant with item 2.4.2 of the C# specification
  217. System.Text.RegularExpressions.Regex regex =
  218. new System.Text.RegularExpressions.Regex(
  219. @"[^\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Nl}\p{Mn}\p{Mc}\p{Cf}\p{Pc}\p{Lm}]");
  220. string ret = regex.Replace(name, ""); //The identifier must start with a character
  221. ret = ret.Substring(0, 1).ToUpper() + ret.Substring(1, ret.Length - 1);
  222. if (!char.IsLetter(ret, 0))
  223. {
  224. ret = string.Concat("_", ret);
  225. }
  226. return ret;
  227. }
  228. private string ExportTemplate(Template template)
  229. {
  230. string docTypeClass = "";
  231. List<DocumentType> documentTypes = DocumentType.GetAllAsList();
  232. if (documentTypes.Count(docType => docType.allowedTemplates.Any(tmpl => tmpl.Alias == template.Alias)) == 1)
  233. {
  234. var defaultDocumentType =
  235. documentTypes.Single(docType => docType.allowedTemplates.Any(tmpl => tmpl.Alias == template.Alias));
  236. docTypeClass = string.IsNullOrEmpty(DocTypesNamespaceTextBox.Text)
  237. ? string.Format("<{0}>", CleanName(defaultDocumentType.Alias))
  238. : string.Format("<{0}.{1}>", DocTypesNamespaceTextBox.Text, CleanName(defaultDocumentType.Alias));
  239. }
  240. return string.Format(templateCode,
  241. !string.IsNullOrEmpty(TemplatesNamespaceTextBox.Text)
  242. ? "namespace " + TemplatesNamespaceTextBox.Text
  243. : "",
  244. CleanName(template.Alias.Replace(" ", "")),
  245. docTypeClass);
  246. }
  247. private string ExportDataType(DataTypeDefinition dataType)
  248. {
  249. if (dataType.DataType == null)
  250. {
  251. return "";
  252. }
  253. var defaultData = dataType.DataType.Data as DefaultData;
  254. string dbType = "DBTypes.Nvarchar";
  255. if (defaultData != null)
  256. {
  257. dbType = "DBTypes." + defaultData.DatabaseType;
  258. }
  259. string preValues = "";
  260. var settingsStorage = new DataEditorSettingsStorage();
  261. var existingSettings = settingsStorage.GetSettings(dataType.Id);
  262. if (existingSettings.Any())
  263. {
  264. preValues = string.Join("," + Environment.NewLine,
  265. existingSettings.Select(
  266. setting =>
  267. string.Format("new DataTypePrevalue(\"{0}\", \"{1}\")", setting.Key,
  268. setting.Value.Replace("\"", "\\\""))));
  269. }
  270. return string.Format(dataTypeCode, dataType.Text.Replace("\"", "\\\""), dataType.UniqueId.ToString(),
  271. dataType.DataType.Id.ToString(), dbType, CleanName(dataType.Text), preValues,
  272. !string.IsNullOrEmpty(DataTypesNamespaceTextBox.Text)
  273. ? "namespace " + DataTypesNamespaceTextBox.Text
  274. : "");
  275. }
  276. private string ExportDocType(int id)
  277. {
  278. DocumentType docType = new DocumentType(id);
  279. string templates = String.Join(", ",
  280. docType.allowedTemplates.Select(template => "\"" + template.Alias + "\""));
  281. string childNodes = String.Join(", ",
  282. docType.AllowedChildContentTypeIDs.Select(
  283. contentTypeId => "typeof(" + CleanName(new DocumentType(contentTypeId).Alias) + ")"));
  284. string properties = String.Join(string.Empty,
  285. docType.getVirtualTabs.SelectMany(
  286. tab =>
  287. tab.GetPropertyTypes(id, false).Select(
  288. prop =>
  289. String.Format(property, prop.Name.Replace("\"", "\\\""), prop.Description.Replace(Environment.NewLine, "").Replace("\"", "\\\""),
  290. (prop.TabId == 0
  291. ? "Properties"
  292. : umbraco.cms.businesslogic.ContentType.Tab.
  293. GetTab(prop.TabId).Caption),
  294. prop.Mandatory.ToString().ToLower(), CleanName(prop.Alias),
  295. Enum.IsDefined(typeof(UmbracoPropertyType),
  296. prop.DataTypeDefinition.Id)
  297. ? ((UmbracoPropertyType)
  298. prop.DataTypeDefinition.Id).ToString()
  299. : "Other",
  300. Enum.IsDefined(typeof(UmbracoPropertyType),
  301. prop.DataTypeDefinition.Id)
  302. ? ""
  303. : "OtherTypeName=\"" +
  304. prop.DataTypeDefinition.Text.Replace("\"", "\\\"") + "\", ",
  305. GetPropertyType(umbraco.cms.businesslogic.ContentType.GetDataType(docType.Alias, prop.Alias))))));
  306. return string.Format(code,
  307. docType.Text.Replace("\"", "\\\""),
  308. docType.IconUrl,
  309. templates,
  310. childNodes,
  311. CleanName(docType.Alias),
  312. (docType.MasterContentType == 0
  313. ? "DocumentTypeBase"
  314. : CleanName(new DocumentType(docType.MasterContentType).Alias)),
  315. properties,
  316. !string.IsNullOrEmpty(DocTypesNamespaceTextBox.Text)
  317. ? "namespace " + DocTypesNamespaceTextBox.Text
  318. : "",
  319. docType.Alias.Replace("\"", "\\\""));
  320. }
  321. }
  322. }