/src/Orchard.Web/Modules/Orchard.Email/Forms/EmailForm.cs

https://github.com/siyamandayubi/Orchardcollaboration · C# · 123 lines · 105 code · 18 blank · 0 comment · 7 complexity · 075f29fed3baa5fd12cd377c759d339f MD5 · raw file

  1. using System;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Web.Mvc;
  5. using Orchard.DisplayManagement;
  6. using Orchard.Environment.Extensions;
  7. using Orchard.Environment.Features;
  8. using Orchard.Forms.Services;
  9. using Orchard.Localization;
  10. namespace Orchard.Email.Forms {
  11. [OrchardFeature("Orchard.Email.Workflows")]
  12. public class EmailForm : Component, IFormProvider {
  13. private readonly IFeatureManager _featureManager;
  14. protected dynamic New { get; set; }
  15. public EmailForm(IShapeFactory shapeFactory,
  16. IFeatureManager featureManager) {
  17. _featureManager = featureManager;
  18. New = shapeFactory;
  19. }
  20. public void Describe(DescribeContext context) {
  21. Func<IShapeFactory, dynamic> formFactory =
  22. shape => {
  23. var jobsQueueEnabled = _featureManager.GetEnabledFeatures().Any(x => x.Id == "Orchard.JobsQueue");
  24. var form = New.Form(
  25. Id: "EmailActivity",
  26. _Type: New.FieldSet(
  27. Title: T("Send to"),
  28. _Recipients: New.Textbox(
  29. Id: "recipients",
  30. Name: "Recipients",
  31. Title: T("Email Addresses"),
  32. Description: T("Specify a comma-separated list of recipient email addresses. To include a display name, use the following format: John Doe &lt;john.doe@outlook.com&gt;"),
  33. Classes: new[] {"large", "text", "tokenized"}),
  34. _Bcc: New.TextBox(
  35. Id: "bcc",
  36. Name: "Bcc",
  37. Title: T("Bcc"),
  38. Description: T("Specify a comma-separated list of email addresses for a blind carbon copy"),
  39. Classes: new[]{"large","text","tokenized"}),
  40. _CC: New.TextBox(
  41. Id: "cc",
  42. Name: "CC",
  43. Title: T("CC"),
  44. Description: T("Specify a comma-separated list of email addresses for a carbon copy"),
  45. Classes: new[] { "large", "text", "tokenized" }),
  46. _ReplyTo: New.Textbox(
  47. Id: "reply-to",
  48. Name: "ReplyTo",
  49. Title: T("Reply To Address"),
  50. Description: T("If necessary, specify an email address for replies."),
  51. Classes: new [] {"large", "text", "tokenized"}),
  52. _Subject: New.Textbox(
  53. Id: "Subject", Name: "Subject",
  54. Title: T("Subject"),
  55. Description: T("The subject of the email message."),
  56. Classes: new[] {"large", "text", "tokenized"}),
  57. _Message: New.Textarea(
  58. Id: "Body", Name: "Body",
  59. Title: T("Body"),
  60. Description: T("The body of the email message."),
  61. Classes: new[] {"tokenized"})
  62. ));
  63. if (jobsQueueEnabled) {
  64. form._Type._Queued(New.Checkbox(
  65. Id: "Queued", Name: "Queued",
  66. Title: T("Queued"),
  67. Checked: false, Value: "true",
  68. Description: T("Check send it as a queued job.")));
  69. form._Type._Priority(New.SelectList(
  70. Id: "priority",
  71. Name: "Priority",
  72. Title: T("Priority"),
  73. Description: ("The priority of this message.")
  74. ));
  75. form._Type._Priority.Add(new SelectListItem { Value = "-50", Text = T("Low").Text });
  76. form._Type._Priority.Add(new SelectListItem { Value = "0", Text = T("Normal").Text });
  77. form._Type._Priority.Add(new SelectListItem { Value = "50", Text = T("High").Text });
  78. }
  79. return form;
  80. };
  81. context.Form("EmailActivity", formFactory);
  82. }
  83. }
  84. public class EmailFormValidator : IFormEventHandler {
  85. public Localizer T { get; set; }
  86. public void Building(BuildingContext context) {}
  87. public void Built(BuildingContext context) {}
  88. public void Validated(ValidatingContext context) { }
  89. public void Validating(ValidatingContext context) {
  90. if (context.FormName != "EmailActivity") return;
  91. var recipients = context.ValueProvider.GetValue("Recipients").AttemptedValue;
  92. var subject = context.ValueProvider.GetValue("Subject").AttemptedValue;
  93. var body = context.ValueProvider.GetValue("Body").AttemptedValue;
  94. if (String.IsNullOrWhiteSpace(recipients)) {
  95. context.ModelState.AddModelError("Recipients", T("You must specify at least one recipient.").Text);
  96. }
  97. if (String.IsNullOrWhiteSpace(subject)) {
  98. context.ModelState.AddModelError("Subject", T("You must provide a Subject.").Text);
  99. }
  100. if (String.IsNullOrWhiteSpace(body)) {
  101. context.ModelState.AddModelError("Body", T("You must provide a Body.").Text);
  102. }
  103. }
  104. }
  105. }